base.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // get last successfully sent notification
  43. // lastNotify := time.Time{}
  44. // for _, j := range journals {
  45. // if j.Success {
  46. // lastNotify = time.Unix(j.SentAt, 0)
  47. // break
  48. // }
  49. // }
  50. // Do not notify if interval has not elapsed
  51. lastNotify := time.Unix(notificationState.SentAt, 0)
  52. if sendReminder && !lastNotify.IsZero() && lastNotify.Add(frequency).After(time.Now()) {
  53. return false
  54. }
  55. // Do not notify if alert state if OK or pending even on repeated notify
  56. if sendReminder && (context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending) {
  57. return false
  58. }
  59. // Do not notify when we become OK for the first time.
  60. if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) {
  61. return false
  62. }
  63. return true
  64. }
  65. // ShouldNotify checks this evaluation should send an alert notification
  66. func (n *NotifierBase) ShouldNotify(ctx context.Context, c *alerting.EvalContext) bool {
  67. cmd := &models.GetNotificationStateQuery{
  68. OrgId: c.Rule.OrgId,
  69. AlertId: c.Rule.Id,
  70. NotifierId: n.Id,
  71. }
  72. err := bus.DispatchCtx(ctx, cmd)
  73. if err != nil {
  74. n.log.Error("Could not determine last time alert notifier fired", "Alert name", c.Rule.Name, "Error", err)
  75. return false
  76. }
  77. return defaultShouldNotify(c, n.SendReminder, n.Frequency, cmd.Result)
  78. }
  79. func (n *NotifierBase) GetType() string {
  80. return n.Type
  81. }
  82. func (n *NotifierBase) NeedsImage() bool {
  83. return n.UploadImage
  84. }
  85. func (n *NotifierBase) GetNotifierId() int64 {
  86. return n.Id
  87. }
  88. func (n *NotifierBase) GetIsDefault() bool {
  89. return n.IsDeault
  90. }
  91. func (n *NotifierBase) GetSendReminder() bool {
  92. return n.SendReminder
  93. }
  94. func (n *NotifierBase) GetFrequency() time.Duration {
  95. return n.Frequency
  96. }