forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rule.go
58 lines (43 loc) · 1.06 KB
/
test_rule.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package alerting
import (
"context"
"fmt"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/simplejson"
m "github.com/grafana/grafana/pkg/models"
)
type AlertTestCommand struct {
Dashboard *simplejson.Json
PanelId int64
OrgId int64
Result *EvalContext
}
func init() {
bus.AddHandler("alerting", handleAlertTestCommand)
}
func handleAlertTestCommand(cmd *AlertTestCommand) error {
dash := m.NewDashboardFromJson(cmd.Dashboard)
extractor := NewDashAlertExtractor(dash, cmd.OrgId)
alerts, err := extractor.GetAlerts()
if err != nil {
return err
}
for _, alert := range alerts {
if alert.PanelId == cmd.PanelId {
rule, err := NewRuleFromDBAlert(alert)
if err != nil {
return err
}
cmd.Result = testAlertRule(rule)
return nil
}
}
return fmt.Errorf("Could not find alert with panel id %d", cmd.PanelId)
}
func testAlertRule(rule *Rule) *EvalContext {
handler := NewEvalHandler()
context := NewEvalContext(context.TODO(), rule)
context.IsTestRun = true
handler.Eval(context)
return context
}