github.com/go-graphite/carbonapi@v0.17.0/expr/functions/holtWintersConfidenceArea/function_test.go (about) 1 //go:build cairo 2 // +build cairo 3 4 package holtWintersConfidenceArea 5 6 import ( 7 "testing" 8 9 "github.com/go-graphite/carbonapi/expr/holtwinters" 10 "github.com/go-graphite/carbonapi/expr/interfaces" 11 12 "github.com/go-graphite/carbonapi/expr/metadata" 13 "github.com/go-graphite/carbonapi/expr/types" 14 "github.com/go-graphite/carbonapi/pkg/parser" 15 th "github.com/go-graphite/carbonapi/tests" 16 ) 17 18 var ( 19 md []interfaces.FunctionMetadata = New("") 20 ) 21 22 func init() { 23 for _, m := range md { 24 metadata.RegisterFunction(m.Name, m.F) 25 } 26 } 27 28 func TestHoltWintersConfidenceArea(t *testing.T) { 29 var startTime int64 = 2678400 30 var step int64 = 600 31 var points int64 = 10 32 33 tests := []th.EvalTestItemWithRange{ 34 { 35 Target: "holtWintersConfidenceArea(metric1)", 36 M: map[parser.MetricRequest][]*types.MetricData{ 37 {Metric: "metric1", From: startTime - holtwinters.DefaultBootstrapInterval, Until: startTime + step*points}: {types.MakeMetricData("metric1", generateHwRange(0, ((holtwinters.DefaultBootstrapInterval/step)+points)*step, step), step, startTime-holtwinters.DefaultBootstrapInterval)}, 38 }, 39 Want: []*types.MetricData{ 40 types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{0.2841206166091448, 1.0581027098774411, 0.3338172102994683, 0.5116859493263242, -0.18199175514936972, 0.2366173792019426, -1.2941554508809152, -0.513426806531049, -0.7970905542723132, 0.09868900726536012}, step, startTime).SetTag("holtWintersConfidenceArea", "1"), 41 types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{8.424944558327624, 9.409422251880809, 10.607070189221787, 10.288439865038768, 9.491556863132963, 9.474595784593738, 8.572310478053845, 8.897670449095346, 8.941566968508148, 9.409728797779282}, step, startTime).SetTag("holtWintersConfidenceArea", "1"), 42 }, 43 From: startTime, 44 Until: startTime + step*points, 45 }, 46 { 47 Target: "holtWintersConfidenceArea(metric1,4,'6d')", 48 M: map[parser.MetricRequest][]*types.MetricData{ 49 {Metric: "metric1", From: startTime - 6*holtwinters.SecondsPerDay, Until: startTime + step*points}: {types.MakeMetricData("metric1", generateHwRange(0, ((6*holtwinters.SecondsPerDay/step)+points)*step, step), step, startTime-6*holtwinters.SecondsPerDay)}, 50 }, 51 Want: []*types.MetricData{ 52 types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{-0.6535362793382391, -0.26554972418633316, -1.1060549683277792, -0.5788026852576289, -1.4594446935142829, -0.6933311085203409, -1.6566119269969288, -1.251651025511391, -1.7938581852717226, -1.1791817117029604}, step, startTime).SetTag("holtWintersConfidenceArea", "1"), 53 types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{8.166528156512886, 8.759008839563066, 9.250962452510654, 9.994110161265208, 10.511931730022393, 11.34313475259535, 12.639554646464758, 11.972601342482212, 10.920216551100442, 10.618692557967133}, step, startTime).SetTag("holtWintersConfidenceArea", "1"), 54 }, 55 From: startTime, 56 Until: startTime + step*points, 57 }, 58 { 59 Target: "holtWintersConfidenceArea(metric1,4,'1d','2d')", 60 M: map[parser.MetricRequest][]*types.MetricData{ 61 {Metric: "metric1", From: startTime - holtwinters.SecondsPerDay, Until: startTime + step*points}: {types.MakeMetricData("metric1", generateHwRange(0, ((holtwinters.SecondsPerDay/step)+points)*step, step), step, startTime-holtwinters.SecondsPerDay)}, 62 }, 63 Want: []*types.MetricData{ 64 types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{4.106587168490873, 3.8357974803355406, 3.564589629688576, 3.421354957735917, 3.393696278743315, 3.470415673952413, 3.2748850646377368, 3.3539750816574316, 3.5243322056965765, 3.7771201010598134}, step, startTime).SetTag("holtWintersConfidenceArea", "1"), 65 types.MakeMetricData("holtWintersConfidenceArea(metric1)", []float64{4.24870339314537, 4.501056063000946, 4.956252698437961, 5.466294981886822, 6.0258698337471355, 6.630178145979606, 7.6413984841547204, 6.492608523867341, 5.556775146625346, 4.813280235806231}, step, startTime).SetTag("holtWintersConfidenceArea", "1"), 66 }, 67 From: startTime, 68 Until: startTime + step*points, 69 }, 70 } 71 72 for _, tt := range tests { 73 testName := tt.Target 74 t.Run(testName, func(t *testing.T) { 75 eval := th.EvaluatorFromFunc(md[0].F) 76 th.TestEvalExprWithRange(t, eval, &tt) 77 }) 78 } 79 } 80 81 func generateHwRange(x, y, jump int64) []float64 { 82 var valuesList []float64 83 for x < y { 84 val := float64((x / jump) % 10) 85 valuesList = append(valuesList, val) 86 x += jump 87 } 88 return valuesList 89 }