Fix percent_diff calculation when points are nulls

This commit is contained in:
Maxim Neverov 2019-02-14 16:31:15 +01:00
parent 4f3b9c6c37
commit 3c2f6094b2
2 changed files with 23 additions and 8 deletions

View File

@ -131,14 +131,17 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
break break
} }
} }
// get the oldest point
points = points[0:i] if i >= 1 {
for i := 0; i < len(points); i++ { // get the oldest point
if points[i][0].Valid { points = points[0:i]
allNull = false for i := 0; i < len(points); i++ {
val := (first - points[i][0].Float64) / points[i][0].Float64 * 100 if points[i][0].Valid {
value = math.Abs(val) allNull = false
break val := (first - points[i][0].Float64) / points[i][0].Float64 * 100
value = math.Abs(val)
break
}
} }
} }
case "count_non_null": case "count_non_null":

View File

@ -157,6 +157,18 @@ func TestSimpleReducer(t *testing.T) {
result := testReducer("percent_diff", 30, 40, 40) result := testReducer("percent_diff", 30, 40, 40)
So(result, ShouldEqual, float64(33.33333333333333)) So(result, ShouldEqual, float64(33.33333333333333))
}) })
Convey("percent_diff with only nulls", func() {
reducer := NewSimpleReducer("percent_diff")
series := &tsdb.TimeSeries{
Name: "test time serie",
}
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 1))
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), 2))
So(reducer.Reduce(series).Valid, ShouldEqual, false)
})
}) })
} }