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

writer_test.go

Blame
  • writer_test.go 3.68 KiB
    package search_test
    
    import (
    	"fmt"
    	"math/rand"
    	"sort"
    	"testing"
    	"time"
    
    	"gitlab.com/uafrica/go-utils/logger"
    	"gitlab.com/uafrica/go-utils/search"
    )
    
    func TestLocalWriter(t *testing.T) {
    	test(t, search.Config{
    		Addresses: []string{"https://localhost:9200"},
    	})
    }
    
    func TestDevWriter(t *testing.T) {
    	test(t, search.Config{
    		Addresses: []string{"https://search-uafrica-v3-api-logs-fefgiypvmb3sg5wqohgsbqnzvq.af-south-1.es.amazonaws.com/"}, //from AWS Console OpenSearch Service > Domains > uafrica-v3-api-logs > General Information: Domain Endpoints
    		Username:  "uafrica",
    		Password:  "Aiz}a4ee",
    	})
    }
    
    func test(t *testing.T, c search.Config) {
    	logger.SetGlobalFormat(logger.NewConsole())
    	logger.SetGlobalLevel(logger.LevelDebug)
    	a, err := search.New(c)
    	if err != nil {
    		t.Fatalf("failed to create writer: %+v", err)
    	}
    
    	indexName := "go-utils-audit-test"
    	ts, err := a.TimeSeries(indexName, testStruct{})
    	if err != nil {
    		t.Fatalf("failed to create time series: %+v", err)
    	}
    
    	//write N records
    	methods := []string{"GET", "POST", "GET", "PATCH", "GET", "GET", "DELETE", "GET", "GET"} //more gets than others
    	paths := []string{"/users", "/orders", "/accounts", "/shipment", "/rates", "/accounts", "/shipment", "/rates", "/accounts", "/shipment", "/rates", "/accounts", "/shipment", "/rates"}
    	N := 100
    	testTime := time.Now().Add(-time.Hour * time.Duration(N))
    	for i := 0; i < N; i++ {
    		testTime = testTime.Add(time.Duration(float64(rand.Intn(100)) / 60.0 * float64(time.Hour)))
    		method := methods[i%len(methods)]
    		path := paths[i%len(paths)]
    		if err := ts.Write(
    			testTime,
    			testTime.Add(-time.Duration(float64(time.Second)*(float64(rand.Intn(100))/100.0+0.1))),
    			testStruct{
    				TimeSeriesHeader: search.TimeSeriesHeader{},
    				Test1:            fmt.Sprintf("%d", i+1),          //1,2,3,...
    				Test2:            fmt.Sprintf("ACC_%05d", 93+i%7), //ACC_00093..ACC00100
    				Test3:            i%3 + 8,                         //8,9, or 10
    				HTTP: httpData{
    					Method: method,
    					Path:   path,
    				},
    				HTTPMethod: method,
    				HTTPPath:   path,
    			}); err != nil {
    			t.Fatalf("failed to add doc: %+v", err)
    		}
    	}
    
    	docs, totalCount, err := ts.Search(10)