base.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Do not notify if interval has not elapsed
  45. lastNotify := time.Unix(notificationState.SentAt, 0)
  46. if sendReminder && !lastNotify.IsZero() && lastNotify.Add(frequency).After(time.Now()) {
  47. return false
  48. }
  49. // Do not notify if alert state if OK or pending even on repeated notify
  50. if sendReminder && (context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending) {
  51. return false
  52. }
  53. // Do not notify when we become OK for the first time.
  54. if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) {
  55. return false
  56. }
  57. return true
  58. }
  59. // ShouldNotify checks this evaluation should send an alert notification
  60. func (n *NotifierBase) ShouldNotify(ctx context.Context, c *alerting.EvalContext, notiferState *models.AlertNotificationState) bool {
  61. return defaultShouldNotify(c, n.SendReminder, n.Frequency, notiferState)
  62. }
  63. func (n *NotifierBase) GetType() string {
  64. return n.Type
  65. }
  66. func (n *NotifierBase) NeedsImage() bool {
  67. return n.UploadImage
  68. }
  69. func (n *NotifierBase) GetNotifierId() int64 {
  70. return n.Id
  71. }
  72. func (n *NotifierBase) GetIsDefault() bool {
  73. return n.IsDeault
  74. }
  75. func (n *NotifierBase) GetSendReminder() bool {
  76. return n.SendReminder
  77. }
  78. func (n *NotifierBase) GetFrequency() time.Duration {
  79. return n.Frequency
  80. }