Skip to content

Commit

Permalink
Validate previous value in derivative calc as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Dec 4, 2015
1 parent 4b7cc67 commit daaaacb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [#4872](https://github.com/influxdb/influxdb/pull/4872): Add option to disable logging for meta service.

### Bugfixes
- [#4666](https://github.com/influxdb/influxdb/issues/4666): Fix panic in derivative with invalid values.
- [#4858](https://github.com/influxdb/influxdb/pull/4858): Validate nested aggregations in queries. Thanks @viru
- [#4921](https://github.com/influxdb/influxdb/pull/4921): Error responses should be JSON-formatted. Thanks @pires
- [#4974](https://github.com/influxdb/influxdb/issues/4974) Fix Data Race in TSDB when setting measurement field name
Expand Down
16 changes: 12 additions & 4 deletions tsdb/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,15 +698,23 @@ func ProcessAggregateDerivative(results [][]interface{}, isNonNegative bool, int

// Check the value's type to ensure it's an numeric, if not, return a nil result. We only check the first value
// because derivatives cannot be combined with other aggregates currently.
validType := false
curValidType := false
switch cur[1].(type) {
case int64:
validType = true
curValidType = true
case float64:
validType = true
curValidType = true
}

if !validType {
prevValidType := false
switch prev[1].(type) {
case int64:
prevValidType = true
case float64:
prevValidType = true
}

if !curValidType || !prevValidType {
derivatives = append(derivatives, []interface{}{
cur[0], nil,
})
Expand Down
34 changes: 34 additions & 0 deletions tsdb/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,40 @@ func TestProcessAggregateDerivative_Bool(t *testing.T) {
}
}

func TestProcessAggregateDerivative_FirstInvalid(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), 1.0},
[]interface{}{time.Unix(0, 0).Add(72 * time.Hour), 1.0},
}) {
t.Fatalf("unexpected results: %s", spew.Sdump(results))
}
}

func TestProcessAggregateDerivative_RandomInvalid(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), 1.0},
[]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))
}
}

func TestRawQueryDerivative_Process_Empty(t *testing.T) {
p := tsdb.RawQueryDerivativeProcessor{
IsNonNegative: false,
Expand Down

0 comments on commit daaaacb

Please sign in to comment.