test_notification.go 1.6 KB

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