diff --git a/pkg/services/ngalert/state/state.go b/pkg/services/ngalert/state/state.go index 2bdf787d844..b8a084eba3b 100644 --- a/pkg/services/ngalert/state/state.go +++ b/pkg/services/ngalert/state/state.go @@ -174,12 +174,18 @@ func (a *State) resultNoData(alertRule *models.AlertRule, result eval.Result) { } func (a *State) NeedsSending(resendDelay time.Duration) bool { - if a.State == eval.Pending || a.State == eval.Normal && !a.Resolved { + switch a.State { + case eval.Pending: + // We do not send notifications for pending states return false + case eval.Normal: + // We should send a notification if the state is Normal because it was resolved + return a.Resolved + default: + // We should send, and re-send notifications, each time LastSentAt is <= LastEvaluationTime + resendDelay + nextSent := a.LastSentAt.Add(resendDelay) + return nextSent.Before(a.LastEvaluationTime) || nextSent.Equal(a.LastEvaluationTime) } - // if LastSentAt is before or equal to LastEvaluationTime + resendDelay, send again - nextSent := a.LastSentAt.Add(resendDelay) - return nextSent.Before(a.LastEvaluationTime) || nextSent.Equal(a.LastEvaluationTime) } func (a *State) Equals(b *State) bool { diff --git a/pkg/services/ngalert/state/state_test.go b/pkg/services/ngalert/state/state_test.go index 330e2c9ae4a..8c77b4de7bd 100644 --- a/pkg/services/ngalert/state/state_test.go +++ b/pkg/services/ngalert/state/state_test.go @@ -71,25 +71,14 @@ func TestNeedsSending(t *testing.T) { }, }, { - name: "state: normal + resolved sends after a minute", + name: "state: normal + resolved should send without waiting", resendDelay: 1 * time.Minute, expected: true, testState: &State{ State: eval.Normal, Resolved: true, LastEvaluationTime: evaluationTime, - LastSentAt: evaluationTime.Add(-1 * time.Minute), - }, - }, - { - name: "state: normal + resolved does _not_ send after 30 seconds (before one minute)", - resendDelay: 1 * time.Minute, - expected: false, - testState: &State{ - State: eval.Normal, - Resolved: true, - LastEvaluationTime: evaluationTime, - LastSentAt: evaluationTime.Add(-30 * time.Second), + LastSentAt: evaluationTime, }, }, {