base.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // ShouldNotify checks this evaluation should send an alert notification
  40. func (n *NotifierBase) ShouldNotify(ctx context.Context, context *alerting.EvalContext, notiferState *models.AlertNotificationState) bool {
  41. // Only notify on state change.
  42. if context.PrevAlertState == context.Rule.State && !n.SendReminder {
  43. return false
  44. }
  45. if context.PrevAlertState == context.Rule.State && n.SendReminder {
  46. // Do not notify if interval has not elapsed
  47. lastNotify := time.Unix(notiferState.UpdatedAt, 0)
  48. if notiferState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) {
  49. return false
  50. }
  51. // Do not notify if alert state is OK or pending even on repeated notify
  52. if context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending {
  53. return false
  54. }
  55. }
  56. // Do not notify when we become OK for the first time.
  57. if context.PrevAlertState == models.AlertStatePending && context.Rule.State == models.AlertStateOK {
  58. return false
  59. }
  60. // Do not notify when we OK -> Pending
  61. if context.PrevAlertState == models.AlertStateOK && context.Rule.State == models.AlertStatePending {
  62. return false
  63. }
  64. // Do not notifu if state pending and it have been updated last minute
  65. if notiferState.State == models.AlertNotificationStatePending {
  66. lastUpdated := time.Unix(notiferState.UpdatedAt, 0)
  67. if lastUpdated.Add(1 * time.Minute).After(time.Now()) {
  68. return false
  69. }
  70. }
  71. return true
  72. }
  73. func (n *NotifierBase) GetType() string {
  74. return n.Type
  75. }
  76. func (n *NotifierBase) NeedsImage() bool {
  77. return n.UploadImage
  78. }
  79. func (n *NotifierBase) GetNotifierId() int64 {
  80. return n.Id
  81. }
  82. func (n *NotifierBase) GetIsDefault() bool {
  83. return n.IsDeault
  84. }
  85. func (n *NotifierBase) GetSendReminder() bool {
  86. return n.SendReminder
  87. }
  88. func (n *NotifierBase) GetFrequency() time.Duration {
  89. return n.Frequency
  90. }