Przeglądaj źródła

fix diff and percent_diff (#12515)

* make diff and percent_diff tests more realistic

* fix diff and percent_diff

* include @marefr's additional tests
Jesse Tane 7 lat temu
rodzic
commit
677117fb03

+ 4 - 4
pkg/services/alerting/conditions/reducer.go

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

+ 21 - 2
pkg/services/alerting/conditions/reducer_test.go

@@ -110,16 +110,35 @@ func TestSimpleReducer(t *testing.T) {
 			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)
 			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)
 			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))
+		})
 	})
 }