Prevent division by 0 for derivative

pull/2569/head
Jason Wilder 2015-05-14 15:20:22 -06:00
parent 34007a8be9
commit 61712d82fb
1 changed files with 9 additions and 4 deletions

View File

@ -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 {