test_notification.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package alerting
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/log"
  6. m "github.com/grafana/grafana/pkg/models"
  7. )
  8. type NotificationTestCommand struct {
  9. State m.AlertStateType
  10. Name string
  11. Type string
  12. Settings *simplejson.Json
  13. }
  14. func init() {
  15. bus.AddHandler("alerting", handleNotificationTestCommand)
  16. }
  17. func handleNotificationTestCommand(cmd *NotificationTestCommand) error {
  18. notifier := NewRootNotifier()
  19. model := &m.AlertNotification{
  20. Name: cmd.Name,
  21. Type: cmd.Type,
  22. Settings: cmd.Settings,
  23. }
  24. notifiers, err := notifier.createNotifierFor(model)
  25. if err != nil {
  26. log.Error2("Failed to create notifier", "error", err.Error())
  27. return err
  28. }
  29. notifier.sendNotifications([]Notifier{notifiers}, createTestEvalContext())
  30. return nil
  31. }
  32. func createTestEvalContext() *EvalContext {
  33. testRule := &Rule{
  34. DashboardId: 1,
  35. PanelId: 1,
  36. Name: "Test notification",
  37. Message: "Someone is testing the alert notification within grafana.",
  38. State: m.AlertStateAlerting,
  39. }
  40. ctx := NewEvalContext(testRule)
  41. ctx.ImagePublicUrl = "http://grafana.org/assets/img/blog/mixed_styles.png"
  42. ctx.IsTestRun = true
  43. ctx.Firing = true
  44. ctx.Error = nil
  45. ctx.EvalMatches = evalMatchesBasedOnState()
  46. return ctx
  47. }
  48. func evalMatchesBasedOnState() []*EvalMatch {
  49. matches := make([]*EvalMatch, 0)
  50. matches = append(matches, &EvalMatch{
  51. Metric: "High value",
  52. Value: 100,
  53. })
  54. matches = append(matches, &EvalMatch{
  55. Metric: "Higher Value",
  56. Value: 200,
  57. })
  58. return matches
  59. }