github.com/go-graphite/carbonapi@v0.17.0/expr/functions/aggregateSeriesLists/function_test.go (about)

     1  package aggregateSeriesLists
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/go-graphite/carbonapi/expr/interfaces"
     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  
    15  var (
    16  	md      []interfaces.FunctionMetadata = New("")
    17  	now                                   = time.Now().Unix()
    18  	shipped                               = []*types.MetricData{
    19  		types.MakeMetricData("mining.other.shipped", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 1, now),
    20  		types.MakeMetricData("mining.diamond.shipped", []float64{0, -1, -1, 2, 3, -5, -8, 13, 21, -34, -55, 89, 144, -233, -377}, 1, now),
    21  		types.MakeMetricData("mining.graphite.shipped", []float64{math.NaN(), 2.3, math.NaN(), -4.5, math.NaN(), 6.7, math.NaN(), -8.9, math.NaN(), 10.111, math.NaN(), -12.13, math.NaN(), 14.15, math.NaN(), -16.17, math.NaN(), 18.19, math.NaN(), -20.21}, 1, now),
    22  		types.MakeMetricData("mining.carbon.shipped", []float64{3.141, math.NaN(), 2.718, 6.022, 6.674, math.NaN(), 6.626, 1.602, 2.067, 9.274, math.NaN(), 5.555}, 1, now),
    23  	}
    24  	extracted = []*types.MetricData{
    25  		types.MakeMetricData("mining.other.extracted", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 1, now),
    26  		types.MakeMetricData("mining.diamond.extracted", []float64{0, 1, -1, 2, -3, 5, -8, 13, -21, 34, -55, 89, -144, 233, -377}, 1, now),
    27  		types.MakeMetricData("mining.graphite.extracted", []float64{math.NaN(), 9, 8, math.NaN(), 6, math.NaN(), 4, 3, 2, math.NaN(), 0, -1, math.NaN(), -3, -4, -5, math.NaN(), math.NaN(), -8, -9, -10}, 1, now),
    28  		types.MakeMetricData("mining.carbon.extracted", []float64{7.22, math.NaN(), 2.718, math.NaN(), 2.54, -1.234, -6.16, -13.37, math.NaN(), -7.77, 0.128, 8.912}, 1, now),
    29  	}
    30  )
    31  
    32  func init() {
    33  	for _, m := range md {
    34  		metadata.RegisterFunction(m.Name, m.F)
    35  	}
    36  }
    37  
    38  func TestFunction(t *testing.T) {
    39  	tests := []th.EvalTestItem{
    40  		{
    41  			"aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\")",
    42  			map[parser.MetricRequest][]*types.MetricData{
    43  				{Metric: "mining.*.shipped", From: 0, Until: 1}:   shipped,
    44  				{Metric: "mining.*.extracted", From: 0, Until: 1}: extracted,
    45  			},
    46  			[]*types.MetricData{
    47  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\")", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 1, now),
    48  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\")", []float64{0.0, 0.0, -1.0, 2.0, 0.0, 0.0, -8.0, 13.0, 0.0, 0.0, -55.0, 89.0, 0.0, 0.0, -377.0}, 1, now),
    49  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\")", []float64{math.NaN(), 5.65, 8.0, -4.5, 6.0, 6.7, 4.0, -2.95, 2.0, 10.111, 0.0, -6.565, math.NaN(), 5.575, -4.0, -10.585, math.NaN(), 18.19, -8.0, -14.605, -10.0}, 1, now),
    50  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\")", []float64{5.1805, math.NaN(), 2.718, 6.022, 4.607, -1.234, 0.2330000000000001, -5.8839999999999995, 2.067, 0.7519999999999998, 0.128, 7.2335}, 1, now),
    51  			},
    52  		},
    53  		{
    54  			"aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"sum\")",
    55  			map[parser.MetricRequest][]*types.MetricData{
    56  				{Metric: "mining.*.shipped", From: 0, Until: 1}:   shipped,
    57  				{Metric: "mining.*.extracted", From: 0, Until: 1}: extracted,
    58  			},
    59  			[]*types.MetricData{
    60  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"sum\")", []float64{2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40}, 1, now),
    61  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"sum\")", []float64{0, 0, -2, 4, 0, 0, -16, 26, 0, 0, -110, 178, 0, 0, -754}, 1, now),
    62  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"sum\")", []float64{math.NaN(), 11.3, 8, -4.5, 6, 6.7, 4, -5.9, 2, 10.111, 0, -13.13, math.NaN(), 11.15, -4, -21.17, math.NaN(), 18.19, -8, -29.21, -10}, 1, now),
    63  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"sum\")", []float64{10.361, math.NaN(), 5.436, 6.022, 9.214, -1.234, 0.4660000000000002, -11.767999999999999, 2.067, 1.5039999999999996, 0.128, 14.467}, 1, now),
    64  			},
    65  		},
    66  		{
    67  			"aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"diff\")",
    68  			map[parser.MetricRequest][]*types.MetricData{
    69  				{Metric: "mining.*.shipped", From: 0, Until: 1}:   shipped,
    70  				{Metric: "mining.*.extracted", From: 0, Until: 1}: extracted,
    71  			},
    72  			[]*types.MetricData{
    73  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"diff\")", []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 1, now),
    74  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"diff\")", []float64{0, -2, 0, 0, 6, -10, 0, 0, 42, -68, 0, 0, 288, -466, 0}, 1, now),
    75  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"diff\")", []float64{math.NaN(), -6.7, 8, -4.5, 6, 6.7, 4, -11.9, 2, 10.111, 0, -11.13, math.NaN(), 17.15, -4, -11.170000000000002, math.NaN(), 18.19, -8, -11.21, -10}, 1, now),
    76  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"diff\")", []float64{-4.079, math.NaN(), 0.0, 6.022, 4.134, -1.234, 12.786000000000001, 14.972, 2.067, 17.043999999999997, 0.128, -3.357000000000001}, 1, now),
    77  			},
    78  		},
    79  		{
    80  			"aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"multiply\")",
    81  			map[parser.MetricRequest][]*types.MetricData{
    82  				{Metric: "mining.*.shipped", From: 0, Until: 1}:   shipped,
    83  				{Metric: "mining.*.extracted", From: 0, Until: 1}: extracted,
    84  			},
    85  			[]*types.MetricData{
    86  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"multiply\")", []float64{1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400}, 1, now),
    87  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"multiply\")", []float64{0, -1, 1, 4, -9, -25, 64, 169, -441, -1156, 3025, 7921, -20736, -54289, 142129}, 1, now),
    88  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"multiply\")", []float64{math.NaN(), 20.7, math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), -26.7, math.NaN(), math.NaN(), math.NaN(), 12.13, math.NaN(), -42.45, math.NaN(), 80.85, math.NaN(), math.NaN(), math.NaN(), 181.89, math.NaN()}, 1, now),
    89  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"multiply\")", []float64{22.67802, math.NaN(), 7.387524, math.NaN(), 16.95196, math.NaN(), -40.81616, -21.41874, math.NaN(), -72.05897999999999, math.NaN(), 49.50616}, 1, now),
    90  			},
    91  		},
    92  		{
    93  			"aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"max\")",
    94  			map[parser.MetricRequest][]*types.MetricData{
    95  				{Metric: "mining.*.shipped", From: 0, Until: 1}:   shipped,
    96  				{Metric: "mining.*.extracted", From: 0, Until: 1}: extracted,
    97  			},
    98  			[]*types.MetricData{
    99  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"max\")", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 1, now),
   100  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"max\")", []float64{0, 1, -1, 2, 3, 5, -8, 13, 21, 34, -55, 89, 144, 233, -377}, 1, now),
   101  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"max\")", []float64{math.NaN(), 9, 8, -4.5, 6, 6.7, 4, 3, 2, 10.111, 0, -1, math.NaN(), 14.15, -4, -5, math.NaN(), 18.19, -8, -9, -10}, 1, now),
   102  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"max\")", []float64{7.22, math.NaN(), 2.718, 6.022, 6.674, -1.234, 6.626, 1.602, 2.067, 9.274, 0.128, 8.912}, 1, now),
   103  			},
   104  		},
   105  		{
   106  			"aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"min\")",
   107  			map[parser.MetricRequest][]*types.MetricData{
   108  				{Metric: "mining.*.shipped", From: 0, Until: 1}:   shipped,
   109  				{Metric: "mining.*.extracted", From: 0, Until: 1}: extracted,
   110  			},
   111  			[]*types.MetricData{
   112  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"min\")", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 1, now),
   113  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"min\")", []float64{0, -1, -1, 2, -3, -5, -8, 13, -21, -34, -55, 89, -144, -233, -377}, 1, now),
   114  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"min\")", []float64{math.NaN(), 2.3, 8, -4.5, 6, 6.7, 4, -8.9, 2, 10.111, 0, -12.13, math.NaN(), -3, -4, -16.17, math.NaN(), 18.19, -8, -20.21, -10}, 1, now),
   115  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"min\")", []float64{3.141, math.NaN(), 2.718, 6.022, 2.54, -1.234, -6.16, -13.37, 2.067, -7.77, 0.128, 5.555}, 1, now),
   116  			},
   117  		},
   118  		{
   119  			"aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\", 0.6)", // Test with xFilesFactor
   120  			map[parser.MetricRequest][]*types.MetricData{
   121  				{Metric: "mining.*.shipped", From: 0, Until: 1}:   shipped,
   122  				{Metric: "mining.*.extracted", From: 0, Until: 1}: extracted,
   123  			},
   124  			[]*types.MetricData{
   125  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\", 0.6)", []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}, 1, now),
   126  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\", 0.6)", []float64{0.0, 0.0, -1.0, 2.0, 0.0, 0.0, -8.0, 13.0, 0.0, 0.0, -55.0, 89.0, 0.0, 0.0, -377.0}, 1, now),
   127  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\", 0.6)", []float64{math.NaN(), 5.65, math.NaN(), math.NaN(), math.NaN(), math.NaN(), math.NaN(), -2.95, math.NaN(), math.NaN(), math.NaN(), -6.565, math.NaN(), 5.575, math.NaN(), -10.585, math.NaN(), math.NaN(), math.NaN(), -14.605, math.NaN()}, 1, now),
   128  				types.MakeMetricData("aggregateSeriesLists(mining.*.shipped, mining.*.extracted,\"avg\", 0.6)", []float64{5.1805, math.NaN(), 2.718, math.NaN(), 4.607, math.NaN(), 0.2330000000000001, -5.8839999999999995, math.NaN(), 0.7519999999999998, math.NaN(), 7.2335}, 1, now),
   129  			},
   130  		},
   131  	}
   132  
   133  	for _, test := range tests {
   134  		t.Run(test.Target, func(t *testing.T) {
   135  			eval := th.EvaluatorFromFunc(md[0].F)
   136  			th.TestEvalExpr(t, eval, &test)
   137  		})
   138  	}
   139  }