github.com/go-graphite/carbonapi@v0.17.0/expr/functions/hitcount/function_test.go (about) 1 package hitcount 2 3 import ( 4 "math" 5 "testing" 6 7 "github.com/go-graphite/carbonapi/expr/interfaces" 8 "github.com/go-graphite/carbonapi/expr/metadata" 9 "github.com/go-graphite/carbonapi/expr/types" 10 "github.com/go-graphite/carbonapi/pkg/parser" 11 th "github.com/go-graphite/carbonapi/tests" 12 ) 13 14 var ( 15 md []interfaces.FunctionMetadata = New("") 16 ) 17 18 func init() { 19 for _, m := range md { 20 metadata.RegisterFunction(m.Name, m.F) 21 } 22 } 23 func TestHitcountEmptyData(t *testing.T) { 24 tests := []th.EvalTestItem{ 25 { 26 "hitcount(foo.bar, '1min')", 27 map[parser.MetricRequest][]*types.MetricData{ 28 {Metric: "foo.bar", From: 0, Until: 1}: {}, 29 }, 30 []*types.MetricData{}, 31 }, 32 } 33 34 for _, tt := range tests { 35 testName := tt.Target 36 t.Run(testName, func(t *testing.T) { 37 eval := th.EvaluatorFromFunc(md[0].F) 38 th.TestEvalExpr(t, eval, &tt) 39 }) 40 } 41 } 42 43 func TestHitcount(t *testing.T) { 44 _, tenFiftyNine, tenThirty := th.InitTestSummarize() 45 now32 := tenThirty 46 47 tests := []th.SummarizeEvalTestItem{ 48 { 49 Target: "hitcount(metric1,\"30s\")", 50 M: map[parser.MetricRequest][]*types.MetricData{ 51 {Metric: "metric1", From: now32, Until: now32 + 31*5}: {types.MakeMetricData("metric1", []float64{ 52 1, 1, 1, 1, 1, 2, 53 2, 2, 2, 2, 3, 3, 54 3, 3, 3, 4, 4, 4, 55 4, 4, 5, 5, 5, 5, 56 math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), 57 5}, 5, now32)}, 58 }, 59 Want: []float64{5, 40, 75, 110, 120, 25}, 60 From: now32, 61 Until: now32 + 31*5, 62 Name: "hitcount(metric1,'30s')", 63 Step: 30, 64 Start: 1410344975, 65 Stop: now32 + 31*5, 66 }, 67 { 68 Target: "hitcount(metric1,\"1h\")", 69 M: map[parser.MetricRequest][]*types.MetricData{ 70 {Metric: "metric1", From: tenFiftyNine, Until: tenFiftyNine + 25*5}: {types.MakeMetricData("metric1", []float64{ 71 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 72 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 73 5}, 5, tenFiftyNine)}, 74 }, 75 Want: []float64{375}, 76 From: tenFiftyNine, 77 Until: tenFiftyNine + 25*5, 78 Name: "hitcount(metric1,'1h')", 79 Step: 3600, 80 Start: 1410343265, 81 Stop: tenFiftyNine + 25*5, 82 }, 83 { 84 Target: "hitcount(metric1,\"1h\",true)", 85 M: map[parser.MetricRequest][]*types.MetricData{ 86 {Metric: "metric1", From: 1410343200, Until: 1410350340}: {types.MakeMetricData("metric1", []float64{ 87 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 88 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 89 5}, 5, tenFiftyNine)}, 90 }, 91 Want: []float64{375}, 92 From: 1410343200, 93 Until: 1410350340, 94 Name: "hitcount(metric1,'1h',true)", 95 Step: 3600, 96 Start: tenFiftyNine, 97 Stop: tenFiftyNine + (((tenFiftyNine + 25*5) - tenFiftyNine) / 3600) + 3600, // The end time is adjusted because of alignToInterval being set to true 98 }, 99 { 100 Target: "hitcount(metric1,\"1h\",alignToInterval=true)", 101 M: map[parser.MetricRequest][]*types.MetricData{ 102 {Metric: "metric1", From: 1410343200, Until: 1410350340}: {types.MakeMetricData("metric1", []float64{ 103 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 104 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 105 5}, 5, tenFiftyNine)}, 106 }, 107 Want: []float64{375}, 108 From: 1410343200, 109 Until: 1410350340, 110 Name: "hitcount(metric1,'1h',true)", 111 Step: 3600, 112 Start: tenFiftyNine, 113 Stop: tenFiftyNine + (((tenFiftyNine + 25*5) - tenFiftyNine) / 3600) + 3600, // The end time is adjusted because of alignToInterval being set to true 114 }, 115 { 116 Target: "hitcount(metric1,\"15s\")", // Test having a smaller interval than the data's step 117 M: map[parser.MetricRequest][]*types.MetricData{ 118 {Metric: "metric1", From: now32, Until: now32 + 5*30}: {types.MakeMetricData("metric1", []float64{ 119 11, 7, 19, 32, 23}, 30, now32)}, 120 }, 121 Want: []float64{165, 165, 105, 105, 285, 285, 480, 480, 345, 345}, 122 From: now32, 123 Until: now32 + 5*30, 124 Name: "hitcount(metric1,'15s')", 125 Step: 15, 126 Start: now32, 127 Stop: now32 + 5*30, 128 }, 129 } 130 131 for _, tt := range tests { 132 testName := tt.Target 133 t.Run(testName, func(t *testing.T) { 134 eval := th.EvaluatorFromFunc(md[0].F) 135 th.TestSummarizeEvalExpr(t, eval, &tt) 136 }) 137 } 138 139 }