base.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.SentAt, 0)
  47. if !lastNotify.IsZero() && 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. return true
  64. }
  65. // ShouldNotify checks this evaluation should send an alert notification
  66. func (n *NotifierBase) ShouldNotify(ctx context.Context, c *alerting.EvalContext, notiferState *models.AlertNotificationState) bool {
  67. return defaultShouldNotify(c, n.SendReminder, n.Frequency, notiferState)
  68. }
  69. func (n *NotifierBase) GetType() string {
  70. return n.Type
  71. }
  72. func (n *NotifierBase) NeedsImage() bool {
  73. return n.UploadImage
  74. }
  75. func (n *NotifierBase) GetNotifierId() int64 {
  76. return n.Id
  77. }
  78. func (n *NotifierBase) GetIsDefault() bool {
  79. return n.IsDeault
  80. }
  81. func (n *NotifierBase) GetSendReminder() bool {
  82. return n.SendReminder
  83. }
  84. func (n *NotifierBase) GetFrequency() time.Duration {
  85. return n.Frequency
  86. }