reminder: uses UpdatedAt to track state changes.

This commit is contained in:
bergquist
2018-10-02 11:19:09 +02:00
parent 9e09b2b969
commit 7f1d7cefc0
7 changed files with 21 additions and 25 deletions
+2 -2
View File
@@ -68,7 +68,7 @@ func (n *notificationService) sendAndMarkAsComplete(evalContext *EvalContext, no
if err != nil {
n.log.Error("failed to send notification", "id", not.GetNotifierId())
} else {
notifierState.state.SentAt = time.Now().UTC().Unix()
notifierState.state.UpdatedAt = time.Now().UTC().Unix()
}
if evalContext.IsTestRun {
@@ -185,7 +185,7 @@ func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []
err = bus.DispatchCtx(evalContext.Ctx, query)
if err != nil {
n.log.Error("Could not get notification state.", "notifier", notification.Id)
n.log.Error("Could not get notification state.", "notifier", notification.Id, "error", err)
continue
}
+2 -2
View File
@@ -53,8 +53,8 @@ func defaultShouldNotify(context *alerting.EvalContext, sendReminder bool, frequ
if context.PrevAlertState == context.Rule.State && sendReminder {
// Do not notify if interval has not elapsed
lastNotify := time.Unix(notificationState.SentAt, 0)
if notificationState.SentAt != 0 && lastNotify.Add(frequency).After(time.Now()) {
lastNotify := time.Unix(notificationState.UpdatedAt, 0)
if notificationState.UpdatedAt != 0 && lastNotify.Add(frequency).After(time.Now()) {
return false
}
+3 -3
View File
@@ -84,7 +84,7 @@ func TestShouldSendAlertNotification(t *testing.T) {
prevState: m.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
state: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
expect: true,
},
@@ -104,7 +104,7 @@ func TestShouldSendAlertNotification(t *testing.T) {
prevState: m.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
state: &m.AlertNotificationState{SentAt: tnow.Add(-time.Minute).Unix()},
state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-time.Minute).Unix()},
expect: false,
},
@@ -114,7 +114,7 @@ func TestShouldSendAlertNotification(t *testing.T) {
prevState: m.AlertStateAlerting,
frequency: time.Minute * 10,
sendReminder: true,
state: &m.AlertNotificationState{SentAt: tnow.Add(-11 * time.Minute).Unix()},
state: &m.AlertNotificationState{UpdatedAt: tnow.Add(-11 * time.Minute).Unix()},
expect: true,
},
+1
View File
@@ -102,6 +102,7 @@ func NewRuleFromDBAlert(ruleDef *m.Alert) (*Rule, error) {
model.State = ruleDef.State
model.NoDataState = m.NoDataOption(ruleDef.Settings.Get("noDataState").MustString("no_data"))
model.ExecutionErrorState = m.ExecutionErrorOption(ruleDef.Settings.Get("executionErrorState").MustString("alerting"))
model.StateChanges = ruleDef.StateChanges
for _, v := range ruleDef.Settings.Get("notifications").MustArray() {
jsonModel := simplejson.NewFromAny(v)