mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Added firingEvalution to Rule test
This commit is contained in:
parent
f46c4c88ca
commit
690868c837
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user