From 690868c837ef93ab7d25f82446fcf02b75680cf0 Mon Sep 17 00:00:00 2001 From: utkarshcmu Date: Thu, 17 Nov 2016 01:28:17 -0800 Subject: [PATCH] Added firingEvalution to Rule test --- pkg/api/alerting.go | 3 ++- pkg/api/dtos/alerting.go | 1 + pkg/services/alerting/eval_context.go | 1 + pkg/services/alerting/eval_handler.go | 14 +++++++++++++- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index ab9f4b7c80b..5ba5ea277bf 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -119,7 +119,8 @@ func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response { res := backendCmd.Result dtoRes := &dtos.AlertTestResult{ - Firing: res.Firing, + Firing: res.Firing, + FiringEval: res.FiringEval, } if res.Error != nil { diff --git a/pkg/api/dtos/alerting.go b/pkg/api/dtos/alerting.go index bf4d7f4353e..fb3130ce636 100644 --- a/pkg/api/dtos/alerting.go +++ b/pkg/api/dtos/alerting.go @@ -36,6 +36,7 @@ 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"` diff --git a/pkg/services/alerting/eval_context.go b/pkg/services/alerting/eval_context.go index 711416970c5..a19312c0768 100644 --- a/pkg/services/alerting/eval_context.go +++ b/pkg/services/alerting/eval_context.go @@ -18,6 +18,7 @@ type EvalContext struct { Logs []*ResultLogEntry Error error Description string + FiringEval 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 7079eac5f38..e0ccea24ade 100644 --- a/pkg/services/alerting/eval_handler.go +++ b/pkg/services/alerting/eval_handler.go @@ -1,6 +1,7 @@ package alerting import ( + "strconv" "time" "github.com/grafana/grafana/pkg/log" @@ -21,7 +22,9 @@ func NewEvalHandler() *DefaultEvalHandler { func (e *DefaultEvalHandler) Eval(context *EvalContext) { firing := true - for _, condition := range context.Rule.Conditions { + firingEval := "" + for i := 0; i < len(context.Rule.Conditions); i++ { + condition := context.Rule.Conditions[i] cr, err := condition.Eval(context) if err != nil { context.Error = err @@ -33,15 +36,24 @@ 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) + "]" + } else { + firingEval = strconv.FormatBool(firing) + } + context.EvalMatches = append(context.EvalMatches, cr.EvalMatches...) } + context.FiringEval = firingEval + " = " + strconv.FormatBool(firing) context.Firing = firing context.EndTime = time.Now() elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond