base.go 2.8 KB

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