diff --git a/pkg/services/alerting/notifiers/base.go b/pkg/services/alerting/notifiers/base.go index 9fe940f70dd..fbade2eccac 100644 --- a/pkg/services/alerting/notifiers/base.go +++ b/pkg/services/alerting/notifiers/base.go @@ -45,16 +45,17 @@ func NewNotifierBase(model *models.AlertNotification) NotifierBase { } } -func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequency time.Duration, notificationState *models.AlertNotificationState) bool { +// ShouldNotify checks this evaluation should send an alert notification +func (n *NotifierBase) ShouldNotify(ctx context.Context, context *alerting.EvalContext, notiferState *models.AlertNotificationState) bool { // Only notify on state change. - if context.PrevAlertState == context.Rule.State && !sendReminder { + if context.PrevAlertState == context.Rule.State && !n.SendReminder { return false } - if context.PrevAlertState == context.Rule.State && sendReminder { + if context.PrevAlertState == context.Rule.State && n.SendReminder { // Do not notify if interval has not elapsed - lastNotify := time.Unix(notificationState.UpdatedAt, 0) - if notificationState.UpdatedAt != 0 && lastNotify.Add(frequency).After(time.Now()) { + lastNotify := time.Unix(notiferState.UpdatedAt, 0) + if notiferState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) { return false } @@ -75,8 +76,8 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ } // Do not notifu if state pending and it have been updated last minute - if notificationState.State == models.AlertNotificationStatePending { - lastUpdated := time.Unix(notificationState.UpdatedAt, 0) + if notiferState.State == models.AlertNotificationStatePending { + lastUpdated := time.Unix(notiferState.UpdatedAt, 0) if lastUpdated.Add(1 * time.Minute).After(time.Now()) { return false } @@ -85,11 +86,6 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ return true } -// ShouldNotify checks this evaluation should send an alert notification -func (n *NotifierBase) ShouldNotify(ctx context.Context, c *alerting.EvalContext, notiferState *models.AlertNotificationState) bool { - return defaultShouldNotify(c, n.SendReminder, n.Frequency, notiferState) -} - func (n *NotifierBase) GetType() string { return n.Type } diff --git a/pkg/services/alerting/notifiers/base_test.go b/pkg/services/alerting/notifiers/base_test.go index 778be41a93f..5e46d3ad72e 100644 --- a/pkg/services/alerting/notifiers/base_test.go +++ b/pkg/services/alerting/notifiers/base_test.go @@ -142,7 +142,9 @@ func TestShouldSendAlertNotification(t *testing.T) { }) evalContext.Rule.State = tc.newState - if defaultShouldNotify(evalContext, tc.sendReminder, tc.frequency, tc.state) != tc.expect { + nb := &NotifierBase{SendReminder: tc.sendReminder, Frequency: tc.frequency} + + if nb.ShouldNotify(evalContext.Ctx, evalContext, tc.state) != tc.expect { t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect) } }