示例#1
0
func TestRawQueryDerivative_Process_Empty(t *testing.T) {
	p := tsdb.RawQueryDerivativeProcessor{
		IsNonNegative:      false,
		DerivativeInterval: 24 * time.Hour,
	}

	results := p.Process([]*tsdb.MapperValue{})
	if !reflect.DeepEqual(results, []*tsdb.MapperValue{}) {
		t.Fatalf("unexpected results: %s", spew.Sdump(results))
	}
}
示例#2
0
func TestRawQueryDerivative_Process_Negative_NonNegative(t *testing.T) {
	p := tsdb.RawQueryDerivativeProcessor{
		IsNonNegative:      true,
		DerivativeInterval: 24 * time.Hour,
	}

	results := p.Process([]*tsdb.MapperValue{
		{Time: time.Unix(0, 0).Unix(), Value: 1.0},
		{Time: time.Unix(0, 0).Add(24 * time.Hour).UnixNano(), Value: 2.0},
		{Time: time.Unix(0, 0).Add(48 * time.Hour).UnixNano(), Value: 0.0},
		{Time: time.Unix(0, 0).Add(72 * time.Hour).UnixNano(), Value: 4.0},
	})
	if !reflect.DeepEqual(results, []*tsdb.MapperValue{
		{Time: time.Unix(0, 0).Add(24 * time.Hour).UnixNano(), Value: 1.0},
		{Time: time.Unix(0, 0).Add(72 * time.Hour).UnixNano(), Value: 4.0},
	}) {
		t.Fatalf("unexpected results: %s", spew.Sdump(results))
	}
}
示例#3
0
// TestProcessRawQueryDerivative tests the RawQueryDerivativeProcessor transformation function on the engine.
// The is called for a queries that do not have a group by.
func TestProcessRawQueryDerivative(t *testing.T) {
	tests := []struct {
		name     string
		fn       string
		interval time.Duration
		in       []*tsdb.MapperValue
		exp      []*tsdb.MapperValue
	}{
		{
			name:     "empty input",
			fn:       "derivative",
			interval: 24 * time.Hour,
			in:       []*tsdb.MapperValue{},
			exp:      []*tsdb.MapperValue{},
		},

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

	for _, test := range tests {
		p := tsdb.RawQueryDerivativeProcessor{
			IsNonNegative:      test.fn == "non_negative_derivative",
			DerivativeInterval: test.interval,
		}
		got := p.Process(test.in)

		if len(got) != len(test.exp) {
			t.Fatalf("RawQueryDerivativeProcessor(%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].Time != got[i].Time || math.Abs((test.exp[i].Value.(float64)-got[i].Value.(float64))) > 0.0000001 {
				t.Fatalf("RawQueryDerivativeProcessor - %s results mismatch:\ngot %v\nexp %v", test.name, got, test.exp)
			}
		}
	}
}