base.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package notifiers
  2. import (
  3. "context"
  4. "time"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/log"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. )
  10. type NotifierBase struct {
  11. Name string
  12. Type string
  13. Id int64
  14. IsDeault bool
  15. UploadImage bool
  16. SendReminder bool
  17. Frequency time.Duration
  18. log log.Logger
  19. }
  20. func NewNotifierBase(model *models.AlertNotification) NotifierBase {
  21. uploadImage := true
  22. value, exist := model.Settings.CheckGet("uploadImage")
  23. if exist {
  24. uploadImage = value.MustBool()
  25. }
  26. return NotifierBase{
  27. Id: model.Id,
  28. Name: model.Name,
  29. IsDeault: model.IsDefault,
  30. Type: model.Type,
  31. UploadImage: uploadImage,
  32. SendReminder: model.SendReminder,
  33. Frequency: model.Frequency,
  34. log: log.New("alerting.notifier." + model.Name),
  35. }
  36. }
  37. func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequency time.Duration, notificationState *models.AlertNotificationState) bool {
  38. // Only notify on state change.
  39. if context.PrevAlertState == context.Rule.State && !sendReminder {
  40. return false
  41. }
  42. // Do not notify if interval has not elapsed
  43. lastNotify := time.Unix(notificationState.SentAt, 0)
  44. if sendReminder && !lastNotify.IsZero() && lastNotify.Add(frequency).After(time.Now()) {
  45. return false
  46. }
  47. // Do not notify if alert state if OK or pending even on repeated notify
  48. if sendReminder && (context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending) {
  49. return false
  50. }
  51. // Do not notify when we become OK for the first time.
  52. if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) {
  53. return false
  54. }
  55. return true
  56. }
  57. // ShouldNotify checks this evaluation should send an alert notification
  58. func (n *NotifierBase) ShouldNotify(ctx context.Context, c *alerting.EvalContext, notiferState *models.AlertNotificationState) bool {
  59. cmd := &models.GetNotificationStateQuery{
  60. OrgId: c.Rule.OrgId,
  61. AlertId: c.Rule.Id,
  62. NotifierId: n.Id,
  63. }
  64. err := bus.DispatchCtx(ctx, cmd)
  65. if err != nil {
  66. n.log.Error("Could not determine last time alert notifier fired", "Alert name", c.Rule.Name, "Error", err)
  67. return false
  68. }
  69. return defaultShouldNotify(c, n.SendReminder, n.Frequency, cmd.Result)
  70. }
  71. func (n *NotifierBase) GetType() string {
  72. return n.Type
  73. }
  74. func (n *NotifierBase) NeedsImage() bool {
  75. return n.UploadImage
  76. }
  77. func (n *NotifierBase) GetNotifierId() int64 {
  78. return n.Id
  79. }
  80. func (n *NotifierBase) GetIsDefault() bool {
  81. return n.IsDeault
  82. }
  83. func (n *NotifierBase) GetSendReminder() bool {
  84. return n.SendReminder
  85. }
  86. func (n *NotifierBase) GetFrequency() time.Duration {
  87. return n.Frequency
  88. }