From 2b1f45b760d2ea763ca8e0e363c5f8d17f8960b4 Mon Sep 17 00:00:00 2001 From: Jason Wilder Date: Thu, 14 May 2015 15:20:22 -0600 Subject: [PATCH] Prevent division by 0 for derivative --- influxql/engine.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/influxql/engine.go b/influxql/engine.go index a6f32447a96..69e4e6fe610 100644 --- a/influxql/engine.go +++ b/influxql/engine.go @@ -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 @@ -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 {