Ejemplo n.º 1
0
func TestProcessAggregateDerivative_SingleRow(t *testing.T) {
	results := tsdb.ProcessAggregateDerivative([][]interface{}{
		[]interface{}{time.Unix(0, 0), 1.0},
	}, false, 24*time.Hour)

	if !reflect.DeepEqual(results, [][]interface{}{
		[]interface{}{time.Unix(0, 0), 0.0},
	}) {
		t.Fatalf("unexpected results: %s", spew.Sdump(results))
	}
}
Ejemplo n.º 2
0
func TestProcessAggregateDerivative_Negative_NonNegative(t *testing.T) {
	results := tsdb.ProcessAggregateDerivative([][]interface{}{
		[]interface{}{time.Unix(0, 0), 1.0},
		[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), 2.0},
		[]interface{}{time.Unix(0, 0).Add(48 * time.Hour), 0.0},
		[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), 4.0},
	}, true, 24*time.Hour)

	if !reflect.DeepEqual(results, [][]interface{}{
		[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), 1.0},
		[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), 4.0},
	}) {
		t.Fatalf("unexpected results: %s", spew.Sdump(results))
	}
}
Ejemplo n.º 3
0
func TestProcessAggregateDerivative_String(t *testing.T) {
	results := tsdb.ProcessAggregateDerivative([][]interface{}{
		[]interface{}{time.Unix(0, 0), "1.0"},
		[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), "2.0"},
		[]interface{}{time.Unix(0, 0).Add(48 * time.Hour), "3.0"},
		[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), "4.0"},
	}, false, 24*time.Hour)

	if !reflect.DeepEqual(results, [][]interface{}{
		[]interface{}{time.Unix(0, 0).Add(24 * time.Hour), nil},
		[]interface{}{time.Unix(0, 0).Add(48 * time.Hour), nil},
		[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), nil},
	}) {
		t.Fatalf("unexpected results: %s", spew.Sdump(results))
	}
}
Ejemplo n.º 4
0
// TestProccessAggregateDerivative tests the RawQueryDerivativeProcessor transformation function on the engine.
// The is called for a query with a GROUP BY.
func TestProcessAggregateDerivative(t *testing.T) {
	tests := []struct {
		name     string
		fn       string
		interval time.Duration
		in       [][]interface{}
		exp      [][]interface{}
	}{
		{
			name:     "empty input",
			fn:       "derivative",
			interval: 24 * time.Hour,
			in:       [][]interface{}{},
			exp:      [][]interface{}{},
		},

		{
			name:     "single row returns 0.0",
			fn:       "derivative",
			interval: 24 * time.Hour,
			in: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 1.0,
				},
			},
			exp: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 0.0,
				},
			},
		},
		{
			name:     "basic derivative",
			fn:       "derivative",
			interval: 24 * time.Hour,
			in: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 1.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 3.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), 5.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 9.0,
				},
			},
			exp: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 2.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), 2.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 4.0,
				},
			},
		},
		{
			name:     "12h interval",
			fn:       "derivative",
			interval: 12 * time.Hour,
			in: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 1.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 2.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), 3.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 4.0,
				},
			},
			exp: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 0.5,
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), 0.5,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 0.5,
				},
			},
		},
		{
			name:     "negative derivatives",
			fn:       "derivative",
			interval: 24 * time.Hour,
			in: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 1.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 2.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), 0.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 4.0,
				},
			},
			exp: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 1.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), -2.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 4.0,
				},
			},
		},
		{
			name:     "negative derivatives",
			fn:       "non_negative_derivative",
			interval: 24 * time.Hour,
			in: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 1.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 2.0,
				},
				// Show resultes in negative derivative
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), 0.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 4.0,
				},
			},
			exp: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 1.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 4.0,
				},
			},
		},
		{
			name:     "integer derivatives",
			fn:       "derivative",
			interval: 24 * time.Hour,
			in: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 1.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), int64(3),
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), int64(5),
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), int64(9),
				},
			},
			exp: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), 2.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), 2.0,
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), 4.0,
				},
			},
		},
		{
			name:     "string derivatives",
			fn:       "derivative",
			interval: 24 * time.Hour,
			in: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), "1.0",
				},
				[]interface{}{
					time.Unix(0, 0).Add(24 * time.Hour), "2.0",
				},
				[]interface{}{
					time.Unix(0, 0).Add(48 * time.Hour), "3.0",
				},
				[]interface{}{
					time.Unix(0, 0).Add(72 * time.Hour), "4.0",
				},
			},
			exp: [][]interface{}{
				[]interface{}{
					time.Unix(0, 0), 0.0,
				},
			},
		},
	}

	for _, test := range tests {
		got := tsdb.ProcessAggregateDerivative(test.in, test.fn == "non_negative_derivative", test.interval)

		if len(got) != len(test.exp) {
			t.Fatalf("ProcessAggregateDerivative(%s) - %s\nlen mismatch: got %d, exp %d", test.fn, test.name, len(got), len(test.exp))
		}

		for i := 0; i < len(test.exp); i++ {
			if test.exp[i][0] != got[i][0] || test.exp[i][1] != got[i][1] {
				t.Fatalf("ProcessAggregateDerivative - %s results mismatch:\ngot %v\nexp %v", test.name, got, test.exp)
			}
		}
	}
}
Ejemplo n.º 5
0
func TestProcessAggregateDerivative_Empty(t *testing.T) {
	results := tsdb.ProcessAggregateDerivative([][]interface{}{}, false, 24*time.Hour)
	if !reflect.DeepEqual(results, [][]interface{}{}) {
		t.Fatalf("unexpected results: %s", spew.Sdump(results))
	}
}