From 9e48d5446534fef8353ed412cd656c2d3f4e2704 Mon Sep 17 00:00:00 2001 From: coral Date: Fri, 29 Sep 2017 17:20:02 +0800 Subject: [PATCH 1/2] add diff and pdiff for conditions --- pkg/services/alerting/conditions/reducer.go | 47 +++++++++++++++++++ .../alerting/conditions/reducer_test.go | 11 +++++ public/app/features/alerting/alert_def.ts | 2 + 3 files changed, 60 insertions(+) diff --git a/pkg/services/alerting/conditions/reducer.go b/pkg/services/alerting/conditions/reducer.go index 0f396e2ffc3..81078b601e4 100644 --- a/pkg/services/alerting/conditions/reducer.go +++ b/pkg/services/alerting/conditions/reducer.go @@ -94,6 +94,53 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { value = (values[(length/2)-1] + values[length/2]) / 2 } } + case "diff": + var ( + points = series.Points + first float64 + i int + ) + // get the newest point + for i = len(points) - 1; i >= 0; i-- { + if points[i][0].Valid { + allNull = false + first = points[i][0].Float64 + break + } + } + // get other points + points = points[0:i] + for i := len(points) - 1; i >= 0; i-- { + if points[i][0].Valid { + allNull = false + value = first - points[i][0].Float64 + break + } + } + case "pdiff": + var ( + points = series.Points + first float64 + i int + ) + // get the newest point + for i = len(points) - 1; i >= 0; i-- { + if points[i][0].Valid { + allNull = false + first = points[i][0].Float64 + break + } + } + // get other points + points = points[0:i] + for i := len(points) - 1; i >= 0; i-- { + if points[i][0].Valid { + allNull = false + val := (first - points[i][0].Float64) / points[i][0].Float64 * 100 + value = math.Abs(val) + break + } + } } if allNull { diff --git a/pkg/services/alerting/conditions/reducer_test.go b/pkg/services/alerting/conditions/reducer_test.go index ff6105a06ec..686a5b85478 100644 --- a/pkg/services/alerting/conditions/reducer_test.go +++ b/pkg/services/alerting/conditions/reducer_test.go @@ -80,6 +80,17 @@ func TestSimpleReducer(t *testing.T) { So(reducer.Reduce(series).Float64, ShouldEqual, float64(3)) }) + + Convey("diff", func() { + result := testReducer("diff", 30, 40) + So(result, ShouldEqual, float64(10)) + }) + + Convey("pdiff", func() { + result := testReducer("pdiff", 30, 40) + So(result, ShouldEqual, float64(33.33333333333333)) + }) + }) } diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index 48cd4b056d9..cf6585bb6c6 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/public/app/features/alerting/alert_def.ts @@ -49,6 +49,8 @@ var reducerTypes = [ {text: 'count()', value: 'count'}, {text: 'last()', value: 'last'}, {text: 'median()', value: 'median'}, + {text: 'diff()', value: 'diff'}, + {text: 'pdiff()', value: 'pdiff'}, ]; var noDataModes = [ From 68ed4d45fa11d29527b5dfa15e7cc374df8a7ca7 Mon Sep 17 00:00:00 2001 From: coral Date: Fri, 29 Sep 2017 18:14:17 +0800 Subject: [PATCH 2/2] change pdiff to percent_diff for conditions --- pkg/services/alerting/conditions/reducer.go | 2 +- pkg/services/alerting/conditions/reducer_test.go | 4 ++-- public/app/features/alerting/alert_def.ts | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/services/alerting/conditions/reducer.go b/pkg/services/alerting/conditions/reducer.go index 81078b601e4..6eaa21b958e 100644 --- a/pkg/services/alerting/conditions/reducer.go +++ b/pkg/services/alerting/conditions/reducer.go @@ -117,7 +117,7 @@ func (s *SimpleReducer) Reduce(series *tsdb.TimeSeries) null.Float { break } } - case "pdiff": + case "percent_diff": var ( points = series.Points first float64 diff --git a/pkg/services/alerting/conditions/reducer_test.go b/pkg/services/alerting/conditions/reducer_test.go index 686a5b85478..f0147e9021a 100644 --- a/pkg/services/alerting/conditions/reducer_test.go +++ b/pkg/services/alerting/conditions/reducer_test.go @@ -86,8 +86,8 @@ func TestSimpleReducer(t *testing.T) { So(result, ShouldEqual, float64(10)) }) - Convey("pdiff", func() { - result := testReducer("pdiff", 30, 40) + Convey("percent_diff", func() { + result := testReducer("percent_diff", 30, 40) So(result, ShouldEqual, float64(33.33333333333333)) }) diff --git a/public/app/features/alerting/alert_def.ts b/public/app/features/alerting/alert_def.ts index cf6585bb6c6..51cbbd9691f 100644 --- a/public/app/features/alerting/alert_def.ts +++ b/public/app/features/alerting/alert_def.ts @@ -50,7 +50,7 @@ var reducerTypes = [ {text: 'last()', value: 'last'}, {text: 'median()', value: 'median'}, {text: 'diff()', value: 'diff'}, - {text: 'pdiff()', value: 'pdiff'}, + {text: 'percent_diff()', value: 'percent_diff'}, ]; var noDataModes = [