Skip to content
Snippets Groups Projects
Select Git revision
  • 7cb1b0e2a8ee89a5d13b192306855cd30cdfa700
  • 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

go.mod

Blame
  • This project manages its dependencies using Go Modules. Learn more
    time_series.go 5.44 KiB
    package search
    
    import (
    	"encoding/json"
    	"net/http"
    	"reflect"
    	"strings"
    	"time"
    
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logger"
    )
    
    type TimeSeries interface {
    	Write(StartTime time.Time, EndTime time.Time, data interface{}) error
    }
    
    type timeSeries struct {
    	w         *writer
    	indexName string
    	dataType  reflect.Type
    	fields    []dataField
    }
    
    type dataField struct {
    	name    string
    	index   []int
    	mapping MappingProperty
    }
    
    //create a time series to write e.g. api logs
    //the 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(indexName string, tmpl interface{}) (TimeSeries, error) {
    	if !indexNameRegex.MatchString(indexName) {
    		return nil, errors.Errorf("invalid index_name:\"%s\"", indexName)
    	}
    	structType := reflect.TypeOf(tmpl)
    	if tmpl == nil || structType.Kind() != reflect.Struct {
    		return nil, errors.Errorf("%T is not a struct", tmpl)
    	}
    
    	ts := &timeSeries{
    		w:         w,
    		indexName: indexName,
    		dataType:  structType,
    		fields:    []dataField{},
    	}
    
    	//define the OpenSearch index mapping
    	indexSpec := Index{
    		Settings: Settings{
    			Index: &SettingsIndex{
    				NumberOfShards:   4,
    				NumberOfReplicas: 0,
    			},
    		},
    		Mappings: Mappings{
    			Properties: map[string]MappingProperty{},
    		},
    	}
    	for i := 0; i < structType.NumField(); i++ {
    		structField := structType.Field(i)
    		dataField := dataField{
    			name:    structField.Name,
    			index:   structField.Index,
    			mapping: MappingProperty{Type: "text"},
    		}