Просмотр исходного кода

feat(alerting): implement more simple reducers

bergquist 9 лет назад
Родитель
Сommit
95c1a4a936

+ 23 - 0
pkg/services/alerting/conditions/reducer.go

@@ -19,6 +19,29 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) float64 {
 			value += point[0]
 		}
 		value = value / float64(len(series.Points))
+	case "sum":
+		for _, point := range series.Points {
+			value += point[0]
+		}
+	case "min":
+		for i, point := range series.Points {
+			if i == 0 {
+				value = point[0]
+			}
+
+			if value > point[0] {
+				value = point[0]
+			}
+		}
+	case "max":
+		for _, point := range series.Points {
+			if value < point[0] {
+				value = point[0]
+			}
+		}
+	case "mean":
+		meanPosition := int64(len(series.Points) / 2)
+		value = series.Points[meanPosition][0]
 	}
 
 	return value

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

@@ -8,11 +8,32 @@ import (
 )
 
 func TestSimpleReducer(t *testing.T) {
-	Convey("Test simple reducer", t, func() {
-		Convey("can calculate avg of time serie", func() {
+	Convey("Test simple reducer by calculating", t, func() {
+		Convey("avg", func() {
 			result := testReducer("avg", 1, 2, 3)
 			So(result, ShouldEqual, float64(2))
 		})
+
+		Convey("sum", func() {
+			result := testReducer("sum", 1, 2, 3)
+			So(result, ShouldEqual, float64(6))
+		})
+
+		Convey("min", func() {
+			result := testReducer("min", 3, 2, 1)
+			So(result, ShouldEqual, float64(1))
+		})
+
+		Convey("max", func() {
+			result := testReducer("max", 1, 2, 3)
+			So(result, ShouldEqual, float64(3))
+		})
+
+		Convey("mean odd numbers", func() {
+			result := testReducer("mean", 1, 2, 3000)
+			So(result, ShouldEqual, float64(2))
+		})
+
 	})
 }