base.go 2.7 KB

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