grafana/pkg/services/alerting/test_notification.go

77 lines
1.7 KiB
Go
Raw Normal View History

package alerting
import (
"context"
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/components/null"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
2016-09-13 08:09:55 -05:00
m "github.com/grafana/grafana/pkg/models"
)
type NotificationTestCommand struct {
2016-09-13 08:09:55 -05:00
State m.AlertStateType
Name string
Type string
Settings *simplejson.Json
}
func init() {
bus.AddHandler("alerting", handleNotificationTestCommand)
}
func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
notifier := NewRootNotifier()
2016-09-13 08:09:55 -05:00
model := &m.AlertNotification{
Name: cmd.Name,
Type: cmd.Type,
Settings: cmd.Settings,
}
notifiers, err := notifier.createNotifierFor(model)
if err != nil {
log.Error2("Failed to create notifier", "error", err.Error())
return err
}
return notifier.sendNotifications(createTestEvalContext(), []Notifier{notifiers})
}
2016-09-13 13:14:18 -05:00
func createTestEvalContext() *EvalContext {
testRule := &Rule{
DashboardId: 1,
PanelId: 1,
Name: "Test notification",
Message: "Someone is testing the alert notification within grafana.",
2016-09-13 13:14:18 -05:00
State: m.AlertStateAlerting,
}
ctx := NewEvalContext(context.TODO(), testRule)
ctx.ImagePublicUrl = "http://grafana.org/assets/img/blog/mixed_styles.png"
ctx.IsTestRun = true
2016-09-13 13:14:18 -05:00
ctx.Firing = true
ctx.Error = nil
2016-09-13 13:14:18 -05:00
ctx.EvalMatches = evalMatchesBasedOnState()
return ctx
}
2016-09-13 13:14:18 -05:00
func evalMatchesBasedOnState() []*EvalMatch {
matches := make([]*EvalMatch, 0)
matches = append(matches, &EvalMatch{
Metric: "High value",
Value: null.FloatFrom(100),
})
matches = append(matches, &EvalMatch{
Metric: "Higher Value",
Value: null.FloatFrom(200),
})
return matches
}