Skip to content

Commit

Permalink
Prevent division by 0 for derivative
Browse files Browse the repository at this point in the history
  • Loading branch information
jwilder committed May 14, 2015
1 parent 8f8c9df commit 2b1f45b
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions influxql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,13 @@ func (m *MapReduceJob) processRawQueryDerivative(lastValueFromPreviousChunk *raw

// Calculate the derivative of successive points by dividing the difference
// of each value by the elapsed time normalized to the interval
var value interface{}
diff := v.Values.(float64) - lastValueFromPreviousChunk.Values.(float64)
elapsed := v.Time - lastValueFromPreviousChunk.Time
value = diff / (float64(elapsed) / float64(m.derivativeInterval()))

value := 0.0
if elapsed > 0 {
value = diff / (float64(elapsed) / float64(m.derivativeInterval()))
}

lastValueFromPreviousChunk = v

Expand Down Expand Up @@ -464,10 +467,12 @@ func (m *MapReduceJob) processDerivative(results [][]interface{}) [][]interface{
continue
}

var value interface{}
elapsed := cur[0].(time.Time).Sub(prev[0].(time.Time))
diff := cur[1].(float64) - prev[1].(float64)
value = float64(diff) / (float64(elapsed) / float64(m.derivativeInterval()))
value := 0.0
if elapsed > 0 {
value = float64(diff) / (float64(elapsed) / float64(m.derivativeInterval()))
}

// Drop negative values for non-negative derivatives
if isNonNegative && diff < 0 {
Expand Down

0 comments on commit 2b1f45b

Please sign in to comment.