Select Git revision
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"},
}