Skip to content
Snippets Groups Projects
Select Git revision
  • d98ac562356f074dad24b2c58e5ae20a28f26821
  • dev default protected
  • prod protected
  • 1.0.58
  • 1.0.57
  • 1.0.52
  • 1.0.56
  • 1.0.51
  • 1.0.50
  • 1.0.33
  • 1.0.32
  • 1.0.31
  • 1.0.30
  • 1.0.29
  • 1.0.28
  • 1.0.27
  • 1.0.26
  • 1.0.25
  • 1.0.24
  • 1.0.23
  • 1.0.22
  • 1.0.21
  • 1.0.20
23 results

deployment.xml

Blame
  • writer_test.go 3.53 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)
    		}
    	}
    
    	oldList, err := a.DelOldTimeSeries(indexName, 2)