Commit 9289cba6 by bergquist

merges defaultShouldNotify and ShouldNotify

parent 67e5f625
...@@ -45,16 +45,17 @@ func NewNotifierBase(model *models.AlertNotification) NotifierBase { ...@@ -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. // Only notify on state change.
if context.PrevAlertState == context.Rule.State && !sendReminder { if context.PrevAlertState == context.Rule.State && !n.SendReminder {
return false 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 // Do not notify if interval has not elapsed
lastNotify := time.Unix(notificationState.UpdatedAt, 0) lastNotify := time.Unix(notiferState.UpdatedAt, 0)
if notificationState.UpdatedAt != 0 && lastNotify.Add(frequency).After(time.Now()) { if notiferState.UpdatedAt != 0 && lastNotify.Add(n.Frequency).After(time.Now()) {
return false return false
} }
...@@ -75,8 +76,8 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ ...@@ -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 // Do not notifu if state pending and it have been updated last minute
if notificationState.State == models.AlertNotificationStatePending { if notiferState.State == models.AlertNotificationStatePending {
lastUpdated := time.Unix(notificationState.UpdatedAt, 0) lastUpdated := time.Unix(notiferState.UpdatedAt, 0)
if lastUpdated.Add(1 * time.Minute).After(time.Now()) { if lastUpdated.Add(1 * time.Minute).After(time.Now()) {
return false return false
} }
...@@ -85,11 +86,6 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ ...@@ -85,11 +86,6 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ
return true 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 { func (n *NotifierBase) GetType() string {
return n.Type return n.Type
} }
......
...@@ -142,7 +142,9 @@ func TestShouldSendAlertNotification(t *testing.T) { ...@@ -142,7 +142,9 @@ func TestShouldSendAlertNotification(t *testing.T) {
}) })
evalContext.Rule.State = tc.newState 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) t.Errorf("failed test %s.\n expected \n%+v \nto return: %v", tc.name, tc, tc.expect)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment