Skip to content

Commit

Permalink
fixes influxdata#378 divide by zero in derivative code
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Mar 28, 2016
1 parent e1a8daf commit 5248a58
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ format a TICKscript file according to a common standard.

### Bugfixes

- [#378](https://github.com/influxdata/kapacitor/issues/378): Fix issue where derivative would divide by zero.


## v0.11.0 [2016-03-22]

Expand Down
4 changes: 4 additions & 0 deletions derivative.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ func (d *DerivativeNode) derivative(prev, curr models.Fields, prevTime, currTime
}

elapsed := float64(currTime.Sub(prevTime))
if elapsed == 0 {
d.logger.Printf("E! cannot perform derivative elapsed time was 0")
return 0, false
}
diff := f1 - f0
// Drop negative values for non-negative derivatives
if d.d.NonNegativeFlag && diff < 0 {
Expand Down
36 changes: 36 additions & 0 deletions integrations/data/TestStream_DerivativeZeroElapsed.srpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
dbname
rpname
packets value=1000 0000000001
dbname
rpname
packets value=1001 0000000002
dbname
rpname
packets value=1002 0000000002
dbname
rpname
packets value=1003 0000000004
dbname
rpname
packets value=1004 0000000005
dbname
rpname
packets value=1006 0000000006
dbname
rpname
packets value=1007 0000000007
dbname
rpname
packets value=1007 0000000008
dbname
rpname
packets value=1008 0000000009
dbname
rpname
packets value=1009 0000000010
dbname
rpname
packets value=1010 0000000011
dbname
rpname
packets value=1011 0000000012
29 changes: 29 additions & 0 deletions integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ stream
testStreamerWithOutput(t, "TestStream_Derivative", script, 15*time.Second, er, nil, false)
}

func TestStream_DerivativeZeroElapsed(t *testing.T) {

var script = `
stream
|from().measurement('packets')
|derivative('value')
|window()
.period(10s)
.every(10s)
|count('value')
|httpOut('TestStream_DerivativeZeroElapsed')
`
er := kapacitor.Result{
Series: imodels.Rows{
{
Name: "packets",
Tags: nil,
Columns: []string{"time", "count"},
Values: [][]interface{}{[]interface{}{
time.Date(1971, 1, 1, 0, 0, 10, 0, time.UTC),
9.0,
}},
},
},
}

testStreamerWithOutput(t, "TestStream_DerivativeZeroElapsed", script, 15*time.Second, er, nil, false)
}

func TestStream_DerivativeUnit(t *testing.T) {

var script = `
Expand Down

0 comments on commit 5248a58

Please sign in to comment.