base.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 != nil && 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. if err := bus.Dispatch(cmd); err != nil {
  63. n.log.Error("Could not determine last time alert notifier fired", "Alert name", c.Rule.Name, "Error", err)
  64. return false
  65. }
  66. return defaultShouldNotify(c, n.SendReminder, n.Frequency, &cmd.Result.SentAt)
  67. }
  68. func (n *NotifierBase) GetType() string {
  69. return n.Type
  70. }
  71. func (n *NotifierBase) NeedsImage() bool {
  72. return n.UploadImage
  73. }
  74. func (n *NotifierBase) GetNotifierId() int64 {
  75. return n.Id
  76. }
  77. func (n *NotifierBase) GetIsDefault() bool {
  78. return n.IsDeault
  79. }
  80. func (n *NotifierBase) GetSendReminder() bool {
  81. return n.SendReminder
  82. }
  83. func (n *NotifierBase) GetFrequency() time.Duration {
  84. return n.Frequency
  85. }