Skip to content
Snippets Groups Projects
Select Git revision
  • 11b32622914ca5417afcf88f3d7468bcfdd9ee08
  • main default protected
  • v1.298.0
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
22 results

time_series.go

Blame
  • time_series.go 14.90 KiB
    package opensearch
    
    import (
    	"bytes"
    	"context"
    	"encoding/json"
    	"io/ioutil"
    	"net/http"
    	"reflect"
    	"strings"
    	"time"
    
    	"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
    	"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
    )
    
    const TimeFormat = "2006-01-02T15:04:05Z07:00"
    const Timeout = "25s" // API Gateway times out after 30s, so return before that
    
    // TimeSeriesHeader embed this into your log struct
    type TimeSeriesHeader struct {
    	StartTime  time.Time `json:"@timestamp"`
    	EndTime    time.Time `json:"@end_time"`
    	DurationMs int64     `json:"@duration_ms"`
    }
    
    type TimeSeries struct {
    	w            *Writer
    	name         string
    	dataType     reflect.Type
    	settings     Settings
    	mappings     Mappings
    	jsonSettings []byte
    	jsonMappings []byte
    	createdDates map[string]bool
    }
    
    // NewTimeSeries purpose:
    //
    //	create a time series to write e.g. api api_logs
    //
    // parameters:
    //
    //	name must be the openSearch index name prefix without the date, e.g. "uafrica-v3-api-api_logs"
    //		the actual indices in openSearch will be called "<indexName>-<ccyymmdd>" e.g. "uafrica-v3-api-api_logs-20210102"
    //	tmpl must be your log data struct consisting of public fields as:
    //		Xxx string `json:"<name>" search:"keyword|text|long|date"`	(can later add more types)
    //		Xxx time.Time `json:"<name>"`								assumes type "date" for opensearch
    //		Xxx int `json:"<name>"`										assumes type "long" for opensearch, specify keyword if required
    func (w *Writer) NewTimeSeries(name string, tmpl interface{}) (TimeSeries, error) {
    	ts := TimeSeries{}
    
    	if !indexNameRegex.MatchString(name) {
    		return ts, errors.Errorf("invalid index_name:\"%s\"", name)
    	}
    
    	// if already created, just return
    	if existingTimeSeries, ok := w.timeSeriesByName[name]; ok {
    		return existingTimeSeries, nil
    	}
    
    	structType := reflect.TypeOf(tmpl)
    	if tmpl == nil || structType.Kind() != reflect.Struct {
    		return ts, errors.Errorf("%T is not a struct", tmpl)
    	}
    	if structType.NumField() < 1 || !structType.Field(0).Anonymous || structType.Field(0).Type != reflect.TypeOf(TimeSeriesHeader{}) {
    		return ts, errors.Errorf("%T does not start with anonymous TimeSeriesHeader", tmpl)
    	}