From 62e8a039a19a5b2b682a4e1bd5557f72a56612b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 17 Nov 2016 15:48:15 +0100 Subject: [PATCH] refactor(alerting): refactoring PR for OR conditions, #6579 --- pkg/api/alerting.go | 4 ++-- pkg/api/dtos/alerting.go | 12 ++++++------ pkg/services/alerting/conditions/query.go | 2 +- pkg/services/alerting/eval_context.go | 3 +-- pkg/services/alerting/eval_handler.go | 12 ++++++------ pkg/services/alerting/eval_handler_test.go | 16 ++++++++-------- .../features/alerting/partials/alert_tab.html | 10 +++++----- public/app/features/dashboard/row/row_model.ts | 3 +++ 8 files changed, 32 insertions(+), 30 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 5ba5ea277bf..6d55f03bea3 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -119,8 +119,8 @@ func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response { res := backendCmd.Result dtoRes := &dtos.AlertTestResult{ - Firing: res.Firing, - FiringEval: res.FiringEval, + Firing: res.Firing, + ConditionEvals: res.ConditionEvals, } if res.Error != nil { diff --git a/pkg/api/dtos/alerting.go b/pkg/api/dtos/alerting.go index fb3130ce636..c9ba30e2407 100644 --- a/pkg/api/dtos/alerting.go +++ b/pkg/api/dtos/alerting.go @@ -35,12 +35,12 @@ type AlertTestCommand struct { } type AlertTestResult struct { - Firing bool `json:"firing"` - FiringEval string `json:"firingEvaluation"` - TimeMs string `json:"timeMs"` - Error string `json:"error,omitempty"` - EvalMatches []*EvalMatch `json:"matches,omitempty"` - Logs []*AlertTestResultLog `json:"logs,omitempty"` + Firing bool `json:"firing"` + ConditionEvals string `json:"conditionEvals"` + TimeMs string `json:"timeMs"` + Error string `json:"error,omitempty"` + EvalMatches []*EvalMatch `json:"matches,omitempty"` + Logs []*AlertTestResultLog `json:"logs,omitempty"` } type AlertTestResultLog struct { diff --git a/pkg/services/alerting/conditions/query.go b/pkg/services/alerting/conditions/query.go index 394c1557490..e58dbf1d583 100644 --- a/pkg/services/alerting/conditions/query.go +++ b/pkg/services/alerting/conditions/query.go @@ -173,7 +173,7 @@ func NewQueryCondition(model *simplejson.Json, index int) (*QueryCondition, erro condition.Evaluator = evaluator operatorJson := model.Get("operator") - operator := operatorJson.Get("type").MustString() + operator := operatorJson.Get("type").MustString("and") condition.Operator = operator return &condition, nil diff --git a/pkg/services/alerting/eval_context.go b/pkg/services/alerting/eval_context.go index a19312c0768..2b252da8c4b 100644 --- a/pkg/services/alerting/eval_context.go +++ b/pkg/services/alerting/eval_context.go @@ -17,8 +17,7 @@ type EvalContext struct { EvalMatches []*EvalMatch Logs []*ResultLogEntry Error error - Description string - FiringEval string + ConditionEvals string StartTime time.Time EndTime time.Time Rule *Rule diff --git a/pkg/services/alerting/eval_handler.go b/pkg/services/alerting/eval_handler.go index e0ccea24ade..075d0833fdf 100644 --- a/pkg/services/alerting/eval_handler.go +++ b/pkg/services/alerting/eval_handler.go @@ -2,6 +2,7 @@ package alerting import ( "strconv" + "strings" "time" "github.com/grafana/grafana/pkg/log" @@ -22,7 +23,8 @@ func NewEvalHandler() *DefaultEvalHandler { func (e *DefaultEvalHandler) Eval(context *EvalContext) { firing := true - firingEval := "" + conditionEvals := "" + for i := 0; i < len(context.Rule.Conditions); i++ { condition := context.Rule.Conditions[i] cr, err := condition.Eval(context) @@ -36,24 +38,22 @@ func (e *DefaultEvalHandler) Eval(context *EvalContext) { } // calculating Firing based on operator - operator := "AND" if cr.Operator == "or" { firing = firing || cr.Firing - operator = "OR" } else { firing = firing && cr.Firing } if i > 0 { - firingEval = "[" + firingEval + " " + operator + " " + strconv.FormatBool(cr.Firing) + "]" + conditionEvals = "[" + conditionEvals + " " + strings.ToUpper(cr.Operator) + " " + strconv.FormatBool(cr.Firing) + "]" } else { - firingEval = strconv.FormatBool(firing) + conditionEvals = strconv.FormatBool(firing) } context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...) } - context.FiringEval = firingEval + " = " + strconv.FormatBool(firing) + context.ConditionEvals = conditionEvals + " = " + strconv.FormatBool(firing) context.Firing = firing context.EndTime = time.Now() elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond diff --git a/pkg/services/alerting/eval_handler_test.go b/pkg/services/alerting/eval_handler_test.go index 1bda0c7c609..03fa0142b10 100644 --- a/pkg/services/alerting/eval_handler_test.go +++ b/pkg/services/alerting/eval_handler_test.go @@ -30,7 +30,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, true) - So(context.FiringEval, ShouldEqual, "true = true") + So(context.ConditionEvals, ShouldEqual, "true = true") }) Convey("Show return false with not passing asdf", func() { @@ -43,7 +43,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, false) - So(context.FiringEval, ShouldEqual, "[true AND false] = false") + So(context.ConditionEvals, ShouldEqual, "[true AND false] = false") }) Convey("Show return true if any of the condition is passing with OR operator", func() { @@ -56,7 +56,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, true) - So(context.FiringEval, ShouldEqual, "[true OR false] = true") + So(context.ConditionEvals, ShouldEqual, "[true OR false] = true") }) Convey("Show return false if any of the condition is failing with AND operator", func() { @@ -69,7 +69,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, false) - So(context.FiringEval, ShouldEqual, "[true AND false] = false") + So(context.ConditionEvals, ShouldEqual, "[true AND false] = false") }) Convey("Show return true if one condition is failing with nested OR operator", func() { @@ -83,7 +83,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, true) - So(context.FiringEval, ShouldEqual, "[[true AND true] OR false] = true") + So(context.ConditionEvals, ShouldEqual, "[[true AND true] OR false] = true") }) Convey("Show return false if one condition is passing with nested OR operator", func() { @@ -97,7 +97,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, false) - So(context.FiringEval, ShouldEqual, "[[true AND false] OR false] = false") + So(context.ConditionEvals, ShouldEqual, "[[true AND false] OR false] = false") }) Convey("Show return false if a condition is failing with nested AND operator", func() { @@ -111,7 +111,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, false) - So(context.FiringEval, ShouldEqual, "[[true AND false] AND true] = false") + So(context.ConditionEvals, ShouldEqual, "[[true AND false] AND true] = false") }) Convey("Show return true if a condition is passing with nested OR operator", func() { @@ -125,7 +125,7 @@ func TestAlertingExecutor(t *testing.T) { handler.Eval(context) So(context.Firing, ShouldEqual, true) - So(context.FiringEval, ShouldEqual, "[[true OR false] OR true] = true") + So(context.ConditionEvals, ShouldEqual, "[[true OR false] OR true] = true") }) }) } diff --git a/public/app/features/alerting/partials/alert_tab.html b/public/app/features/alerting/partials/alert_tab.html index 2bc687aee19..f3fcf31fad6 100644 --- a/public/app/features/alerting/partials/alert_tab.html +++ b/public/app/features/alerting/partials/alert_tab.html @@ -38,23 +38,23 @@
Conditions
- + WHEN
- + OF
- +
- + - +