Skip to content
Snippets Groups Projects
Select Git revision
  • f41e4cfe45dccb3bdd7b127eb7ecd081c8265e8e
  • 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 16.59 KiB
    package search
    
    import (
    	"bytes"
    	"context"
    	"encoding/json"
    	"io/ioutil"
    	"net/http"
    	"reflect"
    	"strings"
    	"time"
    
    	opensearchapi "github.com/opensearch-project/opensearch-go/opensearchapi"
    	"gitlab.com/uafrica/go-utils/errors"
    	"gitlab.com/uafrica/go-utils/logs"
    	"gitlab.com/uafrica/go-utils/reflection"
    )
    
    const TimeFormat = "2006-01-02T15:04:05Z07:00"
    
    //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 returns docs indexed on OpenSearch document ID which cat be used in Get(id)
    	// The docs value type is the same as that of tmpl specified when you created the TimeSeries(..., tmpl)
    	// So you can safely type assert e.g.
    	//		type myType struct {...}
    	//		ts := search.TimeSeries(..., myType{})
    	//		docs,totalCount,err := ts.Search(...)
    	//		if err == nil {
    	//			for id,docValue := range docs {
    	//				doc := docValue.(myType)
    	//				...
    	//			}
    	//		}
    	Search(query Query, searchQuery []map[string]string, limit int64, offset int64) (docs map[string]interface{}, totalCount int, err error)
    
    	// Get takes the id returned in Search()
    	// The id is uuid assigned by OpenSearch when documents are added with Write().
    	// The document value type is the same as that of tmpl specified when you created the TimeSeries(..., tmpl)
    	Get(id string) (interface{}, error)
    }
    
    type timeSeries struct {
    	w            *writer
    	name         string
    	dataType     reflect.Type
    	settings     Settings
    	mappings     Mappings
    	jsonSettings []byte
    	jsonMappings []byte
    	createdDates map[string]bool
    
    	searchResponseBodyType reflect.Type
    	getResponseBodyType    reflect.Type
    }
    
    // TimeSeries 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: