From 74632b5e983281424d1fda0a9ca7eeda093a1df9 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Mon, 7 Nov 2016 17:28:30 -0800 Subject: [PATCH 1/2] Added last function for alerting conditions --- pkg/services/alerting/conditions/reducer.go | 7 +++++++ pkg/services/alerting/conditions/reducer_test.go | 6 ++++++ public/app/features/alerting/alert_def.ts | 1 + 3 files changed, 14 insertions(+) diff --git a/pkg/services/alerting/conditions/reducer.go b/pkg/services/alerting/conditions/reducer.go index a982fa63d33..5fbaeb18326 100644 --- a/pkg/services/alerting/conditions/reducer.go +++ b/pkg/services/alerting/conditions/reducer.go @@ -62,6 +62,13 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { case "count": value = float64(len(series.Points)) allNull = false + case "last": + for _, point := range series.Points { + if point[0].Valid { + value = point[0].Float64 + allNull = false + } + } } if allNull { diff --git a/pkg/services/alerting/conditions/reducer_test.go b/pkg/services/alerting/conditions/reducer_test.go index 198a52b746a..dfd229e1374 100644 --- a/pkg/services/alerting/conditions/reducer_test.go +++ b/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)) + }) + }) } diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index aaddb9e425f..8ee1981f3d8 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/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 = [ From 841fb74b39736e85987389389c10a550543cc0fb Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Mon, 7 Nov 2016 23:15:47 -0800 Subject: [PATCH 2/2] Reversed loop for last function --- pkg/services/alerting/conditions/reducer.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkg/services/alerting/conditions/reducer.go b/pkg/services/alerting/conditions/reducer.go index 5fbaeb18326..988a640591d 100644 --- a/pkg/services/alerting/conditions/reducer.go +++ b/pkg/services/alerting/conditions/reducer.go @@ -63,10 +63,12 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { value = float64(len(series.Points)) allNull = false case "last": - for _, point := range series.Points { - if point[0].Valid { - value = point[0].Float64 + points := series.Points + for i := len(points) - 1; i >= 0; i-- { + if points[i][0].Valid { + value = points[i][0].Float64 allNull = false + break } } }