Commit 690868c8 by utkarshcmu

Added firingEvalution to Rule test

parent f46c4c88
......@@ -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 {
......
......@@ -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"`
......
......@@ -18,6 +18,7 @@ type EvalContext struct {
Logs []*ResultLogEntry
Error error
Description string
FiringEval string
StartTime time.Time
EndTime time.Time
Rule *Rule
......
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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment