From 341d8af637d6801ca277d50239e6c54d10e93ee6 Mon Sep 17 00:00:00 2001 From: bergquist Date: Tue, 2 Oct 2018 11:23:40 +0200 Subject: [PATCH] rename GetNotificationStateQuery to GetOrCreateNotificationStateQuery --- pkg/models/alert_notifications.go | 2 +- pkg/services/alerting/notifier.go | 2 +- pkg/services/sqlstore/alert_notification.go | 6 +++--- .../sqlstore/alert_notification_test.go | 20 +++++++++---------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/pkg/models/alert_notifications.go b/pkg/models/alert_notifications.go index c44f3a35e0c..d908200b36e 100644 --- a/pkg/models/alert_notifications.go +++ b/pkg/models/alert_notifications.go @@ -106,7 +106,7 @@ type SetAlertNotificationStateToCompleteCommand struct { State *AlertNotificationState } -type GetNotificationStateQuery struct { +type GetOrCreateNotificationStateQuery struct { OrgId int64 AlertId int64 NotifierId int64 diff --git a/pkg/services/alerting/notifier.go b/pkg/services/alerting/notifier.go index cd31c9f64af..4f866a58ab7 100644 --- a/pkg/services/alerting/notifier.go +++ b/pkg/services/alerting/notifier.go @@ -177,7 +177,7 @@ func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds [] continue } - query := &m.GetNotificationStateQuery{ + query := &m.GetOrCreateNotificationStateQuery{ NotifierId: notification.Id, AlertId: evalContext.Rule.Id, OrgId: evalContext.Rule.OrgId, diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index e55c783c63a..33d19570a3c 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -19,7 +19,7 @@ func init() { bus.AddHandler("sql", DeleteAlertNotification) bus.AddHandler("sql", GetAlertNotificationsToSend) bus.AddHandler("sql", GetAllAlertNotifications) - bus.AddHandlerCtx("sql", GetAlertNotificationState) + bus.AddHandlerCtx("sql", GetOrCreateAlertNotificationState) bus.AddHandlerCtx("sql", SetAlertNotificationStateToCompleteCommand) bus.AddHandlerCtx("sql", SetAlertNotificationStateToPendingCommand) } @@ -304,7 +304,7 @@ func SetAlertNotificationStateToPendingCommand(ctx context.Context, cmd *m.SetAl }) } -func GetAlertNotificationState(ctx context.Context, cmd *m.GetNotificationStateQuery) error { +func GetOrCreateAlertNotificationState(ctx context.Context, cmd *m.GetOrCreateNotificationStateQuery) error { return withDbSession(ctx, func(sess *DBSession) error { nj := &m.AlertNotificationState{} @@ -352,7 +352,7 @@ func GetAlertNotificationState(ctx context.Context, cmd *m.GetNotificationStateQ }) } -func getAlertNotificationState(sess *DBSession, cmd *m.GetNotificationStateQuery, nj *m.AlertNotificationState) (bool, error) { +func getAlertNotificationState(sess *DBSession, cmd *m.GetOrCreateNotificationStateQuery, nj *m.AlertNotificationState) (bool, error) { return sess. Where("alert_notification_state.org_id = ?", cmd.OrgId). Where("alert_notification_state.alert_id = ?", cmd.AlertId). diff --git a/pkg/services/sqlstore/alert_notification_test.go b/pkg/services/sqlstore/alert_notification_test.go index 9bcf2c18f4d..559a1379f91 100644 --- a/pkg/services/sqlstore/alert_notification_test.go +++ b/pkg/services/sqlstore/alert_notification_test.go @@ -23,8 +23,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) { timeNow = func() time.Time { return now } Convey("Get no existing state should create a new state", func() { - query := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} - err := GetAlertNotificationState(context.Background(), query) + query := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} + err := GetOrCreateAlertNotificationState(context.Background(), query) So(err, ShouldBeNil) So(query.Result, ShouldNotBeNil) So(query.Result.State, ShouldEqual, "unknown") @@ -32,8 +32,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) { So(query.Result.UpdatedAt, ShouldEqual, now.Unix()) Convey("Get existing state should not create a new state", func() { - query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} - err := GetAlertNotificationState(context.Background(), query2) + query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} + err := GetOrCreateAlertNotificationState(context.Background(), query2) So(err, ShouldBeNil) So(query2.Result, ShouldNotBeNil) So(query2.Result.Id, ShouldEqual, query.Result.Id) @@ -50,8 +50,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) { So(cmd.State.Version, ShouldEqual, 1) So(cmd.State.State, ShouldEqual, models.AlertNotificationStatePending) - query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} - err = GetAlertNotificationState(context.Background(), query2) + query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} + err = GetOrCreateAlertNotificationState(context.Background(), query2) So(err, ShouldBeNil) So(query2.Result.Version, ShouldEqual, 1) So(query2.Result.State, ShouldEqual, models.AlertNotificationStatePending) @@ -65,8 +65,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) { err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd) So(err, ShouldBeNil) - query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} - err = GetAlertNotificationState(context.Background(), query3) + query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} + err = GetOrCreateAlertNotificationState(context.Background(), query3) So(err, ShouldBeNil) So(query3.Result.Version, ShouldEqual, 2) So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted) @@ -82,8 +82,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) { err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd) So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict) - query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} - err = GetAlertNotificationState(context.Background(), query3) + query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID} + err = GetOrCreateAlertNotificationState(context.Background(), query3) So(err, ShouldBeNil) So(query3.Result.Version, ShouldEqual, 1001) So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)