github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_agg_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package funcs 7 8 import ( 9 "testing" 10 "time" 11 12 "github.com/GuanceCloud/cliutils/pipeline/ptinput" 13 "github.com/GuanceCloud/cliutils/pipeline/ptinput/plmap" 14 "github.com/stretchr/testify/assert" 15 16 "github.com/GuanceCloud/cliutils/point" 17 ) 18 19 func TestAgg(t *testing.T) { 20 cases := []struct { 21 name, pl string 22 in []string 23 fail bool 24 out map[point.Category]map[string]map[string]any 25 }{ 26 { 27 name: "", 28 pl: ` 29 set_tag("t0", "t1111") 30 set_tag("t1", "t1111") 31 set_tag("t2", "t2__") 32 f0 = _ 33 cast(f0, "int") 34 agg_create("abc", on_interval="5s",on_count=0, const_tags={"a":"b"}) 35 agg_create("def") 36 37 agg_metric("abc", "f1", "avg", ["t2","t0"], "f0") 38 agg_metric("def", "f1", "avg", ["t1", "t2"], "f0") 39 40 agg_create("abc", on_interval="5s",on_count=0, const_tags={"a":"b"}, 41 category="logging") 42 agg_create("def", category="logging") 43 44 agg_metric("abc", "f1", "avg", ["t2","t0"], "f0", category="logging") 45 agg_metric("def", "f1", "avg", ["t1", "t2"], "f0", "logging") 46 `, 47 in: []string{`1`, `2`}, 48 out: map[point.Category]map[string]map[string]any{ 49 point.Metric: { 50 "abc": { 51 "f1": float64(1.5), 52 "t0": "t1111", 53 }, 54 "def": { 55 "f1": float64(1.5), 56 "t1": "t1111", 57 }, 58 }, 59 point.Logging: { 60 "abc": { 61 "f1": float64(1.5), 62 "t0": "t1111", 63 }, 64 "def": { 65 "f1": float64(1.5), 66 "t1": "t1111", 67 }, 68 }, 69 }, 70 }, 71 } 72 73 for idx, tc := range cases { 74 t.Run(tc.name, func(t *testing.T) { 75 runner, err := NewTestingRunner(tc.pl) 76 if err != nil { 77 if tc.fail { 78 t.Logf("[%d]expect error: %s", idx, err) 79 } else { 80 t.Errorf("[%d] failed: %s", idx, err) 81 } 82 return 83 } 84 85 ptsLi := map[point.Category]map[string][]*point.Point{} 86 fn := func(cat point.Category, n string, d any) error { 87 catBuk, ok := ptsLi[cat] 88 if !ok { 89 catBuk = map[string][]*point.Point{} 90 ptsLi[cat] = catBuk 91 } 92 catBuk[n] = append(catBuk[n], d.([]*point.Point)...) 93 return nil 94 } 95 96 buks := plmap.NewAggBuks(fn, nil) 97 for _, tcIn := range tc.in { 98 pt := ptinput.NewPlPoint( 99 point.Logging, "test", nil, map[string]any{"message": tcIn}, time.Now()) 100 pt.SetAggBuckets(buks) 101 errR := runScript(runner, pt) 102 if errR != nil { 103 t.Fatal(*errR) 104 } 105 } 106 107 buks.StopAllBukScanner() 108 109 for cat, catBuk := range tc.out { 110 for bukName, kv := range catBuk { 111 for k, v := range kv { 112 if len(ptsLi[cat][bukName]) == 0 { 113 t.Fatal("no data") 114 } 115 pt := ptsLi[cat][bukName][0] 116 f := pt.InfluxFields() 117 tags := pt.MapTags() 118 if _, ok := f[k]; ok { 119 assert.Equal(t, v, f[k]) 120 } else if _, ok := tags[k]; ok { 121 assert.Equal(t, v, tags[k]) 122 } else { 123 t.Error(k) 124 } 125 } 126 } 127 } 128 }) 129 } 130 }