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

Added firingEvalution to Rule test

utkarshcmu 9 лет назад
Родитель
Сommit
690868c837

+ 2 - 1
pkg/api/alerting.go

@@ -119,7 +119,8 @@ func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
 	res := backendCmd.Result
 	res := backendCmd.Result
 
 
 	dtoRes := &dtos.AlertTestResult{
 	dtoRes := &dtos.AlertTestResult{
-		Firing: res.Firing,
+		Firing:     res.Firing,
+		FiringEval: res.FiringEval,
 	}
 	}
 
 
 	if res.Error != nil {
 	if res.Error != nil {

+ 1 - 0
pkg/api/dtos/alerting.go

@@ -36,6 +36,7 @@ type AlertTestCommand struct {
 
 
 type AlertTestResult struct {
 type AlertTestResult struct {
 	Firing      bool                  `json:"firing"`
 	Firing      bool                  `json:"firing"`
+	FiringEval  string                `json:"firingEvaluation"`
 	TimeMs      string                `json:"timeMs"`
 	TimeMs      string                `json:"timeMs"`
 	Error       string                `json:"error,omitempty"`
 	Error       string                `json:"error,omitempty"`
 	EvalMatches []*EvalMatch          `json:"matches,omitempty"`
 	EvalMatches []*EvalMatch          `json:"matches,omitempty"`

+ 1 - 0
pkg/services/alerting/eval_context.go

@@ -18,6 +18,7 @@ type EvalContext struct {
 	Logs            []*ResultLogEntry
 	Logs            []*ResultLogEntry
 	Error           error
 	Error           error
 	Description     string
 	Description     string
+	FiringEval      string
 	StartTime       time.Time
 	StartTime       time.Time
 	EndTime         time.Time
 	EndTime         time.Time
 	Rule            *Rule
 	Rule            *Rule

+ 13 - 1
pkg/services/alerting/eval_handler.go

@@ -1,6 +1,7 @@
 package alerting
 package alerting
 
 
 import (
 import (
+	"strconv"
 	"time"
 	"time"
 
 
 	"github.com/grafana/grafana/pkg/log"
 	"github.com/grafana/grafana/pkg/log"
@@ -21,7 +22,9 @@ func NewEvalHandler() *DefaultEvalHandler {
 
 
 func (e *DefaultEvalHandler) Eval(context *EvalContext) {
 func (e *DefaultEvalHandler) Eval(context *EvalContext) {
 	firing := true
 	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)
 		cr, err := condition.Eval(context)
 		if err != nil {
 		if err != nil {
 			context.Error = err
 			context.Error = err
@@ -33,15 +36,24 @@ func (e *DefaultEvalHandler) Eval(context *EvalContext) {
 		}
 		}
 
 
 		// calculating Firing based on operator
 		// calculating Firing based on operator
+		operator := "AND"
 		if cr.Operator == "or" {
 		if cr.Operator == "or" {
 			firing = firing || cr.Firing
 			firing = firing || cr.Firing
+			operator = "OR"
 		} else {
 		} else {
 			firing = firing && cr.Firing
 			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.EvalMatches = append(context.EvalMatches, cr.EvalMatches...)
 	}
 	}
 
 
+	context.FiringEval = firingEval + " = " + strconv.FormatBool(firing)
 	context.Firing = firing
 	context.Firing = firing
 	context.EndTime = time.Now()
 	context.EndTime = time.Now()
 	elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond
 	elapsedTime := context.EndTime.Sub(context.StartTime) / time.Millisecond