mirror of
https://github.com/grafana/grafana.git
synced 2024-11-30 04:34:23 -06:00
c38f6ff182
* tech(routines): move the async logic from notification to alerting notifier
* tech(notification): reduce code dupe
* fix(notification): dont touch the response unless its an error
* feat(alerting): make alerting exeuction async but flow sync
* tech(alerting): remove commented code
* tech(alerting): remove unused code
* tech(alerting): fix typo
* tech(alerting): implement Context on EvalContext
* tech(alerting): wait for all alerts to return
* feat(alerting): dont allow alert responses to cancel
* Revert "feat(alerting): dont allow alert responses to cancel"
This reverts commit 324b006c96
.
* feat(alerting): give alerts some time to finish before closing down
59 lines
1.1 KiB
Go
59 lines
1.1 KiB
Go
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
|
|
}
|