base.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, lastNotify time.Time) bool {
  38. // Only notify on state change.
  39. if context.PrevAlertState == context.Rule.State && !sendReminder {
  40. return false
  41. }
  42. // Do not notify if interval has not elapsed
  43. if sendReminder && !lastNotify.IsZero() && lastNotify.Add(frequency).After(time.Now()) {
  44. return false
  45. }
  46. // Do not notify if alert state if OK or pending even on repeated notify
  47. if sendReminder && (context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending) {
  48. return false
  49. }
  50. // Do not notify when we become OK for the first time.
  51. if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) {
  52. return false
  53. }
  54. return true
  55. }
  56. // ShouldNotify checks this evaluation should send an alert notification
  57. func (n *NotifierBase) ShouldNotify(ctx context.Context, c *alerting.EvalContext) bool {
  58. cmd := &models.GetLatestNotificationQuery{
  59. OrgId: c.Rule.OrgId,
  60. AlertId: c.Rule.Id,
  61. NotifierId: n.Id,
  62. }
  63. err := bus.DispatchCtx(ctx, cmd)
  64. if err == models.ErrJournalingNotFound {
  65. return true
  66. }
  67. if err != nil {
  68. n.log.Error("Could not determine last time alert notifier fired", "Alert name", c.Rule.Name, "Error", err)
  69. return false
  70. }
  71. if !cmd.Result.Success {
  72. return true
  73. }
  74. return defaultShouldNotify(c, n.SendReminder, n.Frequency, time.Unix(cmd.Result.SentAt, 0))
  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. }