1package moving
2
3import (
4	"math"
5	"testing"
6	"time"
7
8	"github.com/go-graphite/carbonapi/expr/helper"
9	"github.com/go-graphite/carbonapi/expr/metadata"
10	"github.com/go-graphite/carbonapi/expr/types"
11	"github.com/go-graphite/carbonapi/pkg/parser"
12	th "github.com/go-graphite/carbonapi/tests"
13)
14
15func init() {
16	md := New("")
17	evaluator := th.EvaluatorFromFunc(md[0].F)
18	metadata.SetEvaluator(evaluator)
19	helper.SetEvaluator(evaluator)
20	for _, m := range md {
21		metadata.RegisterFunction(m.Name, m.F)
22	}
23}
24
25func TestMoving(t *testing.T) {
26	now32 := int64(time.Now().Unix())
27
28	tests := []th.EvalTestItem{
29		{
30			"movingAverage(metric1,4)",
31			map[parser.MetricRequest][]*types.MetricData{
32				{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 1, 1, 1, 2, 2, 2, 4, 6, 4, 6, 8}, 1, now32)},
33			},
34			[]*types.MetricData{types.MakeMetricData("movingAverage(metric1,4)", []float64{math.NaN(), math.NaN(), math.NaN(), math.NaN(), 1, 1.25, 1.5, 1.75, 2.5, 3.5, 4, 5}, 1, 0)}, // StartTime = from
35		},
36		{
37			"movingSum(metric1,2)",
38			map[parser.MetricRequest][]*types.MetricData{
39				{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 2, 3, 4, 5, 6}, 1, now32)},
40			},
41			[]*types.MetricData{types.MakeMetricData("movingSum(metric1,2)", []float64{math.NaN(), math.NaN(), 3, 5, 7, 9}, 1, 0)}, // StartTime = from
42		},
43		{
44			"movingMin(metric1,2)",
45			map[parser.MetricRequest][]*types.MetricData{
46				{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 2, 3, 2, 1, 0}, 1, now32)},
47			},
48			[]*types.MetricData{types.MakeMetricData("movingMin(metric1,2)", []float64{math.NaN(), math.NaN(), 1, 2, 2, 1}, 1, 0)}, // StartTime = from
49		},
50		{
51			"movingMax(metric1,2)",
52			map[parser.MetricRequest][]*types.MetricData{
53				{"metric1", 0, 1}: {types.MakeMetricData("metric1", []float64{1, 2, 3, 2, 1, 0}, 1, now32)},
54			},
55			[]*types.MetricData{types.MakeMetricData("movingMax(metric1,2)", []float64{math.NaN(), math.NaN(), 2, 3, 3, 2}, 1, 0)}, // StartTime = from
56		},
57	}
58
59	for _, tt := range tests {
60		testName := tt.Target
61		t.Run(testName, func(t *testing.T) {
62			th.TestEvalExpr(t, &tt)
63		})
64	}
65
66}
67