| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package notifiers
- import (
- "context"
- "time"
- "github.com/grafana/grafana/pkg/bus"
- "github.com/grafana/grafana/pkg/log"
- "github.com/grafana/grafana/pkg/models"
- "github.com/grafana/grafana/pkg/services/alerting"
- )
- type NotifierBase struct {
- Name string
- Type string
- Id int64
- IsDeault bool
- UploadImage bool
- SendReminder bool
- Frequency time.Duration
- log log.Logger
- }
- func NewNotifierBase(model *models.AlertNotification) NotifierBase {
- uploadImage := true
- value, exist := model.Settings.CheckGet("uploadImage")
- if exist {
- uploadImage = value.MustBool()
- }
- return NotifierBase{
- Id: model.Id,
- Name: model.Name,
- IsDeault: model.IsDefault,
- Type: model.Type,
- UploadImage: uploadImage,
- SendReminder: model.SendReminder,
- Frequency: model.Frequency,
- log: log.New("alerting.notifier." + model.Name),
- }
- }
- func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequency time.Duration, lastNotify time.Time) bool {
- // Only notify on state change.
- if context.PrevAlertState == context.Rule.State && !sendReminder {
- return false
- }
- // Do not notify if interval has not elapsed
- if sendReminder && !lastNotify.IsZero() && lastNotify.Add(frequency).After(time.Now()) {
- return false
- }
- // Do not notify if alert state if OK or pending even on repeated notify
- if sendReminder && (context.Rule.State == models.AlertStateOK || context.Rule.State == models.AlertStatePending) {
- return false
- }
- // Do not notify when we become OK for the first time.
- if (context.PrevAlertState == models.AlertStatePending) && (context.Rule.State == models.AlertStateOK) {
- return false
- }
- return true
- }
- // ShouldNotify checks this evaluation should send an alert notification
- func (n *NotifierBase) ShouldNotify(ctx context.Context, c *alerting.EvalContext) bool {
- cmd := &models.GetLatestNotificationQuery{
- OrgId: c.Rule.OrgId,
- AlertId: c.Rule.Id,
- NotifierId: n.Id,
- }
- err := bus.DispatchCtx(ctx, cmd)
- if err == models.ErrJournalingNotFound {
- return true
- }
- if err != nil {
- n.log.Error("Could not determine last time alert notifier fired", "Alert name", c.Rule.Name, "Error", err)
- return false
- }
- if !cmd.Result.Success {
- return true
- }
- return defaultShouldNotify(c, n.SendReminder, n.Frequency, time.Unix(cmd.Result.SentAt, 0))
- }
- func (n *NotifierBase) GetType() string {
- return n.Type
- }
- func (n *NotifierBase) NeedsImage() bool {
- return n.UploadImage
- }
- func (n *NotifierBase) GetNotifierId() int64 {
- return n.Id
- }
- func (n *NotifierBase) GetIsDefault() bool {
- return n.IsDeault
- }
- func (n *NotifierBase) GetSendReminder() bool {
- return n.SendReminder
- }
- func (n *NotifierBase) GetFrequency() time.Duration {
- return n.Frequency
- }
|