base.go 2.8 KB

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