wip: impl so that get alertstate also creates it if it does not exist

This commit is contained in:
Leonard Gram
2018-09-28 10:48:08 +02:00
parent 15ce474639
commit 1a75aa54de
5 changed files with 97 additions and 86 deletions
+26 -15
View File
@@ -302,17 +302,20 @@ func GetAlertNotificationState(ctx context.Context, cmd *m.GetNotificationStateQ
return withDbSession(ctx, func(sess *DBSession) error {
nj := &m.AlertNotificationState{}
exist, err := sess.Desc("alert_notification_state.sent_at").
Where("alert_notification_state.org_id = ?", cmd.OrgId).
Where("alert_notification_state.alert_id = ?", cmd.AlertId).
Where("alert_notification_state.notifier_id = ?", cmd.NotifierId).
Get(nj)
exist, err := getAlertNotificationState(sess, cmd, nj)
// if exists, return it, otherwise create it with default values
if err != nil {
return err
}
if exist {
cmd.Result = nj
return nil
}
// normally flow ends here
if !exist {
notificationState := &m.AlertNotificationState{
OrgId: cmd.OrgId,
@@ -323,30 +326,38 @@ func GetAlertNotificationState(ctx context.Context, cmd *m.GetNotificationStateQ
_, err := sess.Insert(notificationState)
if err == nil {
return nil
}
uniqenessIndexFailureCodes := []string{
"UNIQUE constraint failed",
"pq: duplicate key value violates unique constraint",
"Error 1062: Duplicate entry ",
}
var alreadyExists bool
for _, code := range uniqenessIndexFailureCodes {
if strings.HasPrefix(err.Error(), code) {
alreadyExists = true
exist, err = getAlertNotificationState(sess, cmd, nj)
if exist && err == nil {
cmd.Result = nj
return nil
}
}
}
return err
return m.ErrAlertNotificationStateNotFound
if err != nil {
return err
}
}
cmd.Result = nj
return nil
})
}
func getAlertNotificationState(sess *DBSession, cmd *m.GetNotificationStateQuery, nj *m.AlertNotificationState) (bool, error) {
exist, err := sess.Desc("alert_notification_state.sent_at").
Where("alert_notification_state.org_id = ?", cmd.OrgId).
Where("alert_notification_state.alert_id = ?", cmd.AlertId).
Where("alert_notification_state.notifier_id = ?", cmd.NotifierId).
Get(nj)
return exist, err
}