2016-07-20 16:13:36 +02:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
|
|
import (
|
2016-10-03 09:38:03 +02:00
|
|
|
"context"
|
2016-07-20 16:13:36 +02:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
|
)
|
|
|
|
|
|
2016-07-21 10:29:11 +02:00
|
|
|
type AlertTestCommand struct {
|
2016-07-20 16:13:36 +02:00
|
|
|
Dashboard *simplejson.Json
|
|
|
|
|
PanelId int64
|
|
|
|
|
OrgId int64
|
|
|
|
|
|
2016-07-27 16:29:28 +02:00
|
|
|
Result *EvalContext
|
2016-07-20 16:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2016-07-21 10:29:11 +02:00
|
|
|
bus.AddHandler("alerting", handleAlertTestCommand)
|
2016-07-20 16:13:36 +02:00
|
|
|
}
|
|
|
|
|
|
2016-07-21 10:29:11 +02:00
|
|
|
func handleAlertTestCommand(cmd *AlertTestCommand) error {
|
2016-07-20 16:13:36 +02:00
|
|
|
|
2016-07-21 10:29:11 +02:00
|
|
|
dash := m.NewDashboardFromJson(cmd.Dashboard)
|
2016-07-20 16:13:36 +02:00
|
|
|
|
2016-07-21 10:29:11 +02:00
|
|
|
extractor := NewDashAlertExtractor(dash, cmd.OrgId)
|
|
|
|
|
alerts, err := extractor.GetAlerts()
|
2016-07-20 16:13:36 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 10:29:11 +02:00
|
|
|
for _, alert := range alerts {
|
|
|
|
|
if alert.PanelId == cmd.PanelId {
|
2016-07-27 16:29:28 +02:00
|
|
|
rule, err := NewRuleFromDBAlert(alert)
|
2016-07-21 10:29:11 +02:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 13:09:12 +02:00
|
|
|
cmd.Result = testAlertRule(rule)
|
|
|
|
|
return nil
|
2016-07-20 16:13:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("Could not find alert with panel id %d", cmd.PanelId)
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 16:29:28 +02:00
|
|
|
func testAlertRule(rule *Rule) *EvalContext {
|
|
|
|
|
handler := NewEvalHandler()
|
2016-07-20 16:13:36 +02:00
|
|
|
|
2017-02-24 09:20:21 +01:00
|
|
|
context := NewEvalContext(context.Background(), rule)
|
2016-07-21 13:09:12 +02:00
|
|
|
context.IsTestRun = true
|
2016-07-20 16:13:36 +02:00
|
|
|
|
2016-07-27 16:29:28 +02:00
|
|
|
handler.Eval(context)
|
2018-03-21 20:48:29 +01:00
|
|
|
context.Rule.State = context.GetNewState()
|
2016-07-21 13:09:12 +02:00
|
|
|
|
|
|
|
|
return context
|
2016-07-20 16:13:36 +02:00
|
|
|
}
|