Skip to content
Snippets Groups Projects
Select Git revision
  • a9d36e4d27d038f7736b4a9a8f3bb644ef1819e0
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • 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
  • v1.278.0
31 results

time_series.go

Blame
  • time_series.go 11.67 KiB
    package search
    
    import (
    	"bytes"
    	"context"
    	"encoding/json"
    	"net/http"
    	"reflect"
    	"strings"
    	"time"
    
    	opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logger"
    	"gitlab.com/uafrica/go-utils/reflection"
    )
    
    //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 interface {
    	Write(StartTime time.Time, EndTime time.Time, data interface{}) error
    	Search(query Query, limit int64) (docs interface{}, totalCount int, err error)
    }
    
    type timeSeries struct {
    	w             *writer
    	name          string
    	dataType      reflect.Type
    	fields        []dataField
    	jsonIndexSpec []byte
    	createdDates  map[string]bool
    
    	searchResponseBodyType reflect.Type
    }
    
    type dataField struct {
    	name    string
    	index   []int
    	mapping MappingProperty
    }
    
    //purpose:
    //	create a time series to write e.g. api logs
    //parameters:
    //	name must be the openSearch index name prefix without the date, e.g. "uafrica-v3-api-logs"
    //		the actual indices in openSearch will be called "<indexName>-<ccyymmdd>" e.g. "uafrica-v3-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) TimeSeries(name string, tmpl interface{}) (TimeSeries, error) {
    	if !indexNameRegex.MatchString(name) {
    		return nil, 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 nil, errors.Errorf("%T is not a struct", tmpl)
    	}
    	if structType.NumField() < 1 || !structType.Field(0).Anonymous || structType.Field(0).Type != reflect.TypeOf(TimeSeriesHeader{}) {