base.go 2.9 KB

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