mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
rename GetNotificationStateQuery to GetOrCreateNotificationStateQuery
This commit is contained in:
@@ -106,7 +106,7 @@ type SetAlertNotificationStateToCompleteCommand struct {
|
|||||||
State *AlertNotificationState
|
State *AlertNotificationState
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetNotificationStateQuery struct {
|
type GetOrCreateNotificationStateQuery struct {
|
||||||
OrgId int64
|
OrgId int64
|
||||||
AlertId int64
|
AlertId int64
|
||||||
NotifierId int64
|
NotifierId int64
|
||||||
|
|||||||
@@ -177,7 +177,7 @@ func (n *notificationService) getNeededNotifiers(orgId int64, notificationIds []
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
query := &m.GetNotificationStateQuery{
|
query := &m.GetOrCreateNotificationStateQuery{
|
||||||
NotifierId: notification.Id,
|
NotifierId: notification.Id,
|
||||||
AlertId: evalContext.Rule.Id,
|
AlertId: evalContext.Rule.Id,
|
||||||
OrgId: evalContext.Rule.OrgId,
|
OrgId: evalContext.Rule.OrgId,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func init() {
|
|||||||
bus.AddHandler("sql", DeleteAlertNotification)
|
bus.AddHandler("sql", DeleteAlertNotification)
|
||||||
bus.AddHandler("sql", GetAlertNotificationsToSend)
|
bus.AddHandler("sql", GetAlertNotificationsToSend)
|
||||||
bus.AddHandler("sql", GetAllAlertNotifications)
|
bus.AddHandler("sql", GetAllAlertNotifications)
|
||||||
bus.AddHandlerCtx("sql", GetAlertNotificationState)
|
bus.AddHandlerCtx("sql", GetOrCreateAlertNotificationState)
|
||||||
bus.AddHandlerCtx("sql", SetAlertNotificationStateToCompleteCommand)
|
bus.AddHandlerCtx("sql", SetAlertNotificationStateToCompleteCommand)
|
||||||
bus.AddHandlerCtx("sql", SetAlertNotificationStateToPendingCommand)
|
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 {
|
return withDbSession(ctx, func(sess *DBSession) error {
|
||||||
nj := &m.AlertNotificationState{}
|
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.
|
return sess.
|
||||||
Where("alert_notification_state.org_id = ?", cmd.OrgId).
|
Where("alert_notification_state.org_id = ?", cmd.OrgId).
|
||||||
Where("alert_notification_state.alert_id = ?", cmd.AlertId).
|
Where("alert_notification_state.alert_id = ?", cmd.AlertId).
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
|||||||
timeNow = func() time.Time { return now }
|
timeNow = func() time.Time { return now }
|
||||||
|
|
||||||
Convey("Get no existing state should create a new state", func() {
|
Convey("Get no existing state should create a new state", func() {
|
||||||
query := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
query := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||||
err := GetAlertNotificationState(context.Background(), query)
|
err := GetOrCreateAlertNotificationState(context.Background(), query)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(query.Result, ShouldNotBeNil)
|
So(query.Result, ShouldNotBeNil)
|
||||||
So(query.Result.State, ShouldEqual, "unknown")
|
So(query.Result.State, ShouldEqual, "unknown")
|
||||||
@@ -32,8 +32,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
|||||||
So(query.Result.UpdatedAt, ShouldEqual, now.Unix())
|
So(query.Result.UpdatedAt, ShouldEqual, now.Unix())
|
||||||
|
|
||||||
Convey("Get existing state should not create a new state", func() {
|
Convey("Get existing state should not create a new state", func() {
|
||||||
query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||||
err := GetAlertNotificationState(context.Background(), query2)
|
err := GetOrCreateAlertNotificationState(context.Background(), query2)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(query2.Result, ShouldNotBeNil)
|
So(query2.Result, ShouldNotBeNil)
|
||||||
So(query2.Result.Id, ShouldEqual, query.Result.Id)
|
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.Version, ShouldEqual, 1)
|
||||||
So(cmd.State.State, ShouldEqual, models.AlertNotificationStatePending)
|
So(cmd.State.State, ShouldEqual, models.AlertNotificationStatePending)
|
||||||
|
|
||||||
query2 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
query2 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||||
err = GetAlertNotificationState(context.Background(), query2)
|
err = GetOrCreateAlertNotificationState(context.Background(), query2)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(query2.Result.Version, ShouldEqual, 1)
|
So(query2.Result.Version, ShouldEqual, 1)
|
||||||
So(query2.Result.State, ShouldEqual, models.AlertNotificationStatePending)
|
So(query2.Result.State, ShouldEqual, models.AlertNotificationStatePending)
|
||||||
@@ -65,8 +65,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
|||||||
err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
|
err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||||
err = GetAlertNotificationState(context.Background(), query3)
|
err = GetOrCreateAlertNotificationState(context.Background(), query3)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(query3.Result.Version, ShouldEqual, 2)
|
So(query3.Result.Version, ShouldEqual, 2)
|
||||||
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
|
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
|
||||||
@@ -82,8 +82,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
|
|||||||
err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
|
err := SetAlertNotificationStateToCompleteCommand(context.Background(), &cmd)
|
||||||
So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
|
So(err, ShouldEqual, models.ErrAlertNotificationStateVersionConflict)
|
||||||
|
|
||||||
query3 := &models.GetNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
query3 := &models.GetOrCreateNotificationStateQuery{AlertId: alertID, OrgId: orgID, NotifierId: notifierID}
|
||||||
err = GetAlertNotificationState(context.Background(), query3)
|
err = GetOrCreateAlertNotificationState(context.Background(), query3)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(query3.Result.Version, ShouldEqual, 1001)
|
So(query3.Result.Version, ShouldEqual, 1001)
|
||||||
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
|
So(query3.Result.State, ShouldEqual, models.AlertNotificationStateCompleted)
|
||||||
|
|||||||
Reference in New Issue
Block a user