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

Merge pull request #6499 from utkarshcmu/last

Added last function for alerting conditions
Carl Bergquist 9 лет назад
Родитель
Сommit
a7be3b91fd

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

@@ -62,6 +62,15 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float {
 	case "count":
 		value = float64(len(series.Points))
 		allNull = false
+	case "last":
+		points := series.Points
+		for i := len(points) - 1; i >= 0; i-- {
+			if points[i][0].Valid {
+				value = points[i][0].Float64
+				allNull = false
+				break
+			}
+		}
 	}
 
 	if allNull {

+ 6 - 0
pkg/services/alerting/conditions/reducer_test.go

@@ -35,6 +35,12 @@ func TestSimpleReducer(t *testing.T) {
 			result := testReducer("count", 1, 2, 3000)
 			So(result, ShouldEqual, float64(3))
 		})
+
+		Convey("last", func() {
+			result := testReducer("last", 1, 2, 3000)
+			So(result, ShouldEqual, float64(3000))
+		})
+
 	})
 }
 

+ 1 - 0
public/app/features/alerting/alert_def.ts

@@ -34,6 +34,7 @@ var reducerTypes = [
   {text: 'max()', value: 'max'},
   {text: 'sum()' , value: 'sum'},
   {text: 'count()', value: 'count'},
+  {text: 'last()', value: 'last'},
 ];
 
 var noDataModes = [