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