Commit 677117fb by Jesse Tane Committed by Torkel Ödegaard

fix diff and percent_diff (#12515)

* make diff and percent_diff tests more realistic

* fix diff and percent_diff

* include @marefr's additional tests
parent f53e1661
...@@ -108,9 +108,9 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { ...@@ -108,9 +108,9 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
break break
} }
} }
// get other points // get the oldest point
points = points[0:i] points = points[0:i]
for i := len(points) - 1; i >= 0; i-- { for i := 0; i < len(points); i++ {
if points[i][0].Valid { if points[i][0].Valid {
allNull = false allNull = false
value = first - points[i][0].Float64 value = first - points[i][0].Float64
...@@ -131,9 +131,9 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { ...@@ -131,9 +131,9 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
break break
} }
} }
// get other points // get the oldest point
points = points[0:i] points = points[0:i]
for i := len(points) - 1; i >= 0; i-- { for i := 0; i < len(points); i++ {
if points[i][0].Valid { if points[i][0].Valid {
allNull = false allNull = false
val := (first - points[i][0].Float64) / points[i][0].Float64 * 100 val := (first - points[i][0].Float64) / points[i][0].Float64 * 100
......
...@@ -110,16 +110,35 @@ func TestSimpleReducer(t *testing.T) { ...@@ -110,16 +110,35 @@ func TestSimpleReducer(t *testing.T) {
So(reducer.Reduce(series).Float64, ShouldEqual, float64(3)) So(reducer.Reduce(series).Float64, ShouldEqual, float64(3))
}) })
Convey("diff", func() { Convey("diff one point", func() {
result := testReducer("diff", 30)
So(result, ShouldEqual, float64(0))
})
Convey("diff two points", func() {
result := testReducer("diff", 30, 40) result := testReducer("diff", 30, 40)
So(result, ShouldEqual, float64(10)) So(result, ShouldEqual, float64(10))
}) })
Convey("percent_diff", func() { Convey("diff three points", func() {
result := testReducer("diff", 30, 40, 40)
So(result, ShouldEqual, float64(10))
})
Convey("percent_diff one point", func() {
result := testReducer("percent_diff", 40)
So(result, ShouldEqual, float64(0))
})
Convey("percent_diff two points", func() {
result := testReducer("percent_diff", 30, 40) result := testReducer("percent_diff", 30, 40)
So(result, ShouldEqual, float64(33.33333333333333)) So(result, ShouldEqual, float64(33.33333333333333))
}) })
Convey("percent_diff three points", func() {
result := testReducer("percent_diff", 30, 40, 40)
So(result, ShouldEqual, float64(33.33333333333333))
})
}) })
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment