2016-06-13 16:39:00 +02:00
package sqlstore
import (
"bytes"
2018-06-15 15:30:17 +02:00
"context"
2018-09-28 11:17:03 +02:00
"errors"
2016-06-14 08:33:50 +02:00
"fmt"
2016-07-26 12:29:52 +02:00
"strings"
2016-06-14 08:33:50 +02:00
"time"
2016-06-13 16:39:00 +02:00
"github.com/grafana/grafana/pkg/bus"
2020-02-29 13:35:15 +01:00
"github.com/grafana/grafana/pkg/models"
2018-12-14 11:53:50 +02:00
"github.com/grafana/grafana/pkg/util"
2016-06-13 16:39:00 +02:00
)
func init ( ) {
2016-08-01 14:34:58 +02:00
bus . AddHandler ( "sql" , GetAlertNotifications )
2016-06-14 08:33:50 +02:00
bus . AddHandler ( "sql" , CreateAlertNotificationCommand )
bus . AddHandler ( "sql" , UpdateAlertNotification )
2016-06-16 15:21:44 +02:00
bus . AddHandler ( "sql" , DeleteAlertNotification )
2016-09-06 08:42:35 +02:00
bus . AddHandler ( "sql" , GetAllAlertNotifications )
2018-10-02 11:23:40 +02:00
bus . AddHandlerCtx ( "sql" , GetOrCreateAlertNotificationState )
2018-09-27 11:33:13 +02:00
bus . AddHandlerCtx ( "sql" , SetAlertNotificationStateToCompleteCommand )
bus . AddHandlerCtx ( "sql" , SetAlertNotificationStateToPendingCommand )
2018-12-14 11:53:50 +02:00
bus . AddHandler ( "sql" , GetAlertNotificationsWithUid )
bus . AddHandler ( "sql" , UpdateAlertNotificationWithUid )
bus . AddHandler ( "sql" , DeleteAlertNotificationWithUid )
bus . AddHandler ( "sql" , GetAlertNotificationsWithUidToSend )
2016-06-16 15:21:44 +02:00
}
2020-02-29 13:35:15 +01:00
func DeleteAlertNotification ( cmd * models . DeleteAlertNotificationCommand ) error {
2017-05-23 10:56:23 +02:00
return inTransaction ( func ( sess * DBSession ) error {
2016-06-16 15:21:44 +02:00
sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
2018-09-30 21:57:15 +02:00
if _ , err := sess . Exec ( sql , cmd . OrgId , cmd . Id ) ; err != nil {
return err
}
if _ , err := sess . Exec ( "DELETE FROM alert_notification_state WHERE alert_notification_state.org_id = ? AND alert_notification_state.notifier_id = ?" , cmd . OrgId , cmd . Id ) ; err != nil {
return err
}
return nil
2016-06-16 15:21:44 +02:00
} )
2016-06-13 16:39:00 +02:00
}
2020-02-29 13:35:15 +01:00
func DeleteAlertNotificationWithUid ( cmd * models . DeleteAlertNotificationWithUidCommand ) error {
existingNotification := & models . GetAlertNotificationsWithUidQuery { OrgId : cmd . OrgId , Uid : cmd . Uid }
2018-12-20 12:45:18 +02:00
if err := getAlertNotificationWithUidInternal ( existingNotification , newSession ( ) ) ; err != nil {
return err
2018-12-14 11:53:50 +02:00
}
if existingNotification . Result != nil {
2020-02-29 13:35:15 +01:00
deleteCommand := & models . DeleteAlertNotificationCommand {
2018-12-14 11:53:50 +02:00
Id : existingNotification . Result . Id ,
OrgId : existingNotification . Result . OrgId ,
}
2018-12-20 12:45:18 +02:00
if err := bus . Dispatch ( deleteCommand ) ; err != nil {
return err
2018-12-14 11:53:50 +02:00
}
}
return nil
}
2020-02-29 13:35:15 +01:00
func GetAlertNotifications ( query * models . GetAlertNotificationsQuery ) error {
2017-05-23 10:56:23 +02:00
return getAlertNotificationInternal ( query , newSession ( ) )
2016-06-14 08:33:50 +02:00
}
2020-03-18 15:00:56 +02:00
func ( ss * SqlStore ) addAlertNotificationUidByIdHandler ( ) {
bus . AddHandler ( "sql" , ss . GetAlertNotificationUidWithId )
}
func ( ss * SqlStore ) GetAlertNotificationUidWithId ( query * models . GetAlertNotificationUidQuery ) error {
cacheKey := newAlertNotificationUidCacheKey ( query . OrgId , query . Id )
if cached , found := ss . CacheService . Get ( cacheKey ) ; found {
query . Result = cached . ( string )
return nil
}
err := getAlertNotificationUidInternal ( query , newSession ( ) )
if err != nil {
return err
}
ss . CacheService . Set ( cacheKey , query . Result , - 1 ) //Infinite, never changes
return nil
}
func newAlertNotificationUidCacheKey ( orgID , notificationId int64 ) string {
return fmt . Sprintf ( "notification-uid-by-org-%d-and-id-%d" , orgID , notificationId )
}
2020-02-29 13:35:15 +01:00
func GetAlertNotificationsWithUid ( query * models . GetAlertNotificationsWithUidQuery ) error {
2018-12-14 11:53:50 +02:00
return getAlertNotificationWithUidInternal ( query , newSession ( ) )
}
2020-02-29 13:35:15 +01:00
func GetAllAlertNotifications ( query * models . GetAllAlertNotificationsQuery ) error {
results := make ( [ ] * models . AlertNotification , 0 )
2016-09-06 08:42:35 +02:00
if err := x . Where ( "org_id = ?" , query . OrgId ) . Find ( & results ) ; err != nil {
return err
}
query . Result = results
return nil
}
2020-02-29 13:35:15 +01:00
func GetAlertNotificationsWithUidToSend ( query * models . GetAlertNotificationsWithUidToSendQuery ) error {
2016-09-06 08:42:35 +02:00
var sql bytes . Buffer
params := make ( [ ] interface { } , 0 )
2019-01-29 21:17:56 +01:00
sql . WriteString ( ` SELECT
2016-09-06 08:42:35 +02:00
alert_notification . id ,
2018-12-14 11:53:50 +02:00
alert_notification . uid ,
2016-09-06 08:42:35 +02:00
alert_notification . org_id ,
alert_notification . name ,
alert_notification . type ,
alert_notification . created ,
alert_notification . updated ,
alert_notification . settings ,
2018-05-20 12:12:10 -04:00
alert_notification . is_default ,
2018-10-17 10:41:18 +02:00
alert_notification . disable_resolve_message ,
2018-06-05 10:27:29 +02:00
alert_notification . send_reminder ,
2018-05-20 12:12:10 -04:00
alert_notification . frequency
2016-09-06 08:42:35 +02:00
FROM alert_notification
` )
sql . WriteString ( ` WHERE alert_notification.org_id = ? ` )
params = append ( params , query . OrgId )
2016-09-23 08:07:14 +02:00
sql . WriteString ( ` AND ((alert_notification.is_default = ?) ` )
params = append ( params , dialect . BooleanStr ( true ) )
2018-12-14 11:53:50 +02:00
if len ( query . Uids ) > 0 {
sql . WriteString ( ` OR alert_notification.uid IN (? ` + strings . Repeat ( ",?" , len ( query . Uids ) - 1 ) + ")" )
for _ , v := range query . Uids {
2016-09-06 08:42:35 +02:00
params = append ( params , v )
}
}
sql . WriteString ( ` ) ` )
2020-02-29 13:35:15 +01:00
results := make ( [ ] * models . AlertNotification , 0 )
2018-01-23 22:30:45 +01:00
if err := x . SQL ( sql . String ( ) , params ... ) . Find ( & results ) ; err != nil {
2016-09-06 08:42:35 +02:00
return err
}
query . Result = results
return nil
}
2020-03-18 15:00:56 +02:00
func getAlertNotificationUidInternal ( query * models . GetAlertNotificationUidQuery , sess * DBSession ) error {
var sql bytes . Buffer
params := make ( [ ] interface { } , 0 )
sql . WriteString ( ` SELECT
alert_notification . uid
FROM alert_notification
` )
sql . WriteString ( ` WHERE alert_notification.org_id = ? ` )
params = append ( params , query . OrgId )
sql . WriteString ( ` AND alert_notification.id = ? ` )
params = append ( params , query . Id )
results := make ( [ ] string , 0 )
if err := sess . SQL ( sql . String ( ) , params ... ) . Find ( & results ) ; err != nil {
return err
}
if len ( results ) == 0 {
return fmt . Errorf ( "Alert notification [ Id: %v, OrgId: %v ] not found" , query . Id , query . OrgId )
}
query . Result = results [ 0 ]
return nil
}
2020-02-29 13:35:15 +01:00
func getAlertNotificationInternal ( query * models . GetAlertNotificationsQuery , sess * DBSession ) error {
2016-06-13 16:39:00 +02:00
var sql bytes . Buffer
params := make ( [ ] interface { } , 0 )
sql . WriteString ( ` SELECT
2016-09-05 21:33:05 +02:00
alert_notification . id ,
2019-03-26 18:37:02 +07:00
alert_notification . uid ,
2016-09-05 21:33:05 +02:00
alert_notification . org_id ,
alert_notification . name ,
alert_notification . type ,
alert_notification . created ,
alert_notification . updated ,
alert_notification . settings ,
2018-05-20 12:12:10 -04:00
alert_notification . is_default ,
2018-10-17 10:41:18 +02:00
alert_notification . disable_resolve_message ,
2018-06-05 10:27:29 +02:00
alert_notification . send_reminder ,
2018-05-20 12:12:10 -04:00
alert_notification . frequency
2016-09-05 21:33:05 +02:00
FROM alert_notification
` )
2016-06-13 16:39:00 +02:00
sql . WriteString ( ` WHERE alert_notification.org_id = ? ` )
2016-07-22 16:45:17 +02:00
params = append ( params , query . OrgId )
2016-06-13 16:39:00 +02:00
2016-09-06 08:42:35 +02:00
if query . Name != "" || query . Id != 0 {
2016-09-05 21:33:05 +02:00
if query . Name != "" {
sql . WriteString ( ` AND alert_notification.name = ? ` )
params = append ( params , query . Name )
}
2016-06-13 16:39:00 +02:00
2016-09-05 21:33:05 +02:00
if query . Id != 0 {
sql . WriteString ( ` AND alert_notification.id = ? ` )
params = append ( params , query . Id )
}
2016-06-14 16:56:14 +02:00
}
2020-02-29 13:35:15 +01:00
results := make ( [ ] * models . AlertNotification , 0 )
2018-09-16 12:26:05 +02:00
if err := sess . SQL ( sql . String ( ) , params ... ) . Find ( & results ) ; err != nil {
2016-06-13 16:39:00 +02:00
return err
}
2016-09-06 08:42:35 +02:00
if len ( results ) == 0 {
query . Result = nil
} else {
query . Result = results [ 0 ]
}
2016-06-13 16:39:00 +02:00
return nil
}
2020-02-29 13:35:15 +01:00
func getAlertNotificationWithUidInternal ( query * models . GetAlertNotificationsWithUidQuery , sess * DBSession ) error {
2018-12-14 11:53:50 +02:00
var sql bytes . Buffer
params := make ( [ ] interface { } , 0 )
sql . WriteString ( ` SELECT
alert_notification . id ,
alert_notification . uid ,
alert_notification . org_id ,
alert_notification . name ,
alert_notification . type ,
alert_notification . created ,
alert_notification . updated ,
alert_notification . settings ,
alert_notification . is_default ,
alert_notification . disable_resolve_message ,
alert_notification . send_reminder ,
alert_notification . frequency
FROM alert_notification
` )
sql . WriteString ( ` WHERE alert_notification.org_id = ? AND alert_notification.uid = ? ` )
params = append ( params , query . OrgId , query . Uid )
2020-02-29 13:35:15 +01:00
results := make ( [ ] * models . AlertNotification , 0 )
2018-12-14 11:53:50 +02:00
if err := sess . SQL ( sql . String ( ) , params ... ) . Find ( & results ) ; err != nil {
return err
}
if len ( results ) == 0 {
query . Result = nil
} else {
query . Result = results [ 0 ]
}
return nil
}
2020-02-29 13:35:15 +01:00
func CreateAlertNotificationCommand ( cmd * models . CreateAlertNotificationCommand ) error {
2017-05-23 10:56:23 +02:00
return inTransaction ( func ( sess * DBSession ) error {
2018-12-14 11:53:50 +02:00
if cmd . Uid == "" {
2019-04-23 11:24:47 +03:00
uid , uidGenerationErr := generateNewAlertNotificationUid ( sess , cmd . OrgId )
if uidGenerationErr != nil {
2018-12-14 11:53:50 +02:00
return uidGenerationErr
}
2019-04-23 11:24:47 +03:00
cmd . Uid = uid
2018-12-14 11:53:50 +02:00
}
2020-02-29 13:35:15 +01:00
existingQuery := & models . GetAlertNotificationsWithUidQuery { OrgId : cmd . OrgId , Uid : cmd . Uid }
2018-12-14 11:53:50 +02:00
err := getAlertNotificationWithUidInternal ( existingQuery , sess )
2016-06-14 08:33:50 +02:00
if err != nil {
return err
}
2016-09-06 08:42:35 +02:00
if existingQuery . Result != nil {
2018-12-14 11:53:50 +02:00
return fmt . Errorf ( "Alert notification uid %s already exists" , cmd . Uid )
}
// check if name exists
2020-02-29 13:35:15 +01:00
sameNameQuery := & models . GetAlertNotificationsQuery { OrgId : cmd . OrgId , Name : cmd . Name }
2018-12-14 11:53:50 +02:00
if err := getAlertNotificationInternal ( sameNameQuery , sess ) ; err != nil {
return err
}
if sameNameQuery . Result != nil {
2016-06-14 08:33:50 +02:00
return fmt . Errorf ( "Alert notification name %s already exists" , cmd . Name )
}
2016-06-13 16:39:00 +02:00
2018-05-25 14:14:33 -04:00
var frequency time . Duration
2018-06-05 10:27:29 +02:00
if cmd . SendReminder {
2018-06-04 22:19:27 +02:00
if cmd . Frequency == "" {
2020-02-29 13:35:15 +01:00
return models . ErrNotificationFrequencyNotFound
2018-06-04 22:19:27 +02:00
}
frequency , err = time . ParseDuration ( cmd . Frequency )
if err != nil {
return err
}
2018-05-20 12:12:10 -04:00
}
2020-02-29 13:35:15 +01:00
alertNotification := & models . AlertNotification {
2018-12-14 11:53:50 +02:00
Uid : cmd . Uid ,
2018-10-17 10:41:18 +02:00
OrgId : cmd . OrgId ,
Name : cmd . Name ,
Type : cmd . Type ,
Settings : cmd . Settings ,
SendReminder : cmd . SendReminder ,
DisableResolveMessage : cmd . DisableResolveMessage ,
Frequency : frequency ,
Created : time . Now ( ) ,
Updated : time . Now ( ) ,
IsDefault : cmd . IsDefault ,
2016-06-14 08:33:50 +02:00
}
2016-06-13 16:39:00 +02:00
2018-06-05 10:27:29 +02:00
if _ , err = sess . MustCols ( "send_reminder" ) . Insert ( alertNotification ) ; err != nil {
2016-06-14 08:33:50 +02:00
return err
}
cmd . Result = alertNotification
return nil
2016-06-13 16:39:00 +02:00
} )
}
2018-12-14 11:53:50 +02:00
func generateNewAlertNotificationUid ( sess * DBSession , orgId int64 ) ( string , error ) {
for i := 0 ; i < 3 ; i ++ {
2019-01-29 21:17:56 +01:00
uid := util . GenerateShortUID ( )
2020-02-29 13:35:15 +01:00
exists , err := sess . Where ( "org_id=? AND uid=?" , orgId , uid ) . Get ( & models . AlertNotification { } )
2018-12-14 11:53:50 +02:00
if err != nil {
return "" , err
}
2019-01-28 22:03:16 +01:00
2018-12-14 11:53:50 +02:00
if ! exists {
return uid , nil
}
}
2019-01-28 22:03:16 +01:00
2020-02-29 13:35:15 +01:00
return "" , models . ErrAlertNotificationFailedGenerateUniqueUid
2018-12-14 11:53:50 +02:00
}
2020-02-29 13:35:15 +01:00
func UpdateAlertNotification ( cmd * models . UpdateAlertNotificationCommand ) error {
2017-05-23 10:56:23 +02:00
return inTransaction ( func ( sess * DBSession ) ( err error ) {
2020-02-29 13:35:15 +01:00
current := models . AlertNotification { }
2016-06-13 16:39:00 +02:00
2018-01-23 22:30:45 +01:00
if _ , err = sess . ID ( cmd . Id ) . Get ( & current ) ; err != nil {
2016-06-14 08:33:50 +02:00
return err
}
2016-07-22 16:45:17 +02:00
// check if name exists
2020-02-29 13:35:15 +01:00
sameNameQuery := & models . GetAlertNotificationsQuery { OrgId : cmd . OrgId , Name : cmd . Name }
2016-09-06 08:42:35 +02:00
if err := getAlertNotificationInternal ( sameNameQuery , sess ) ; err != nil {
2016-07-22 16:45:17 +02:00
return err
2016-06-20 16:19:15 +02:00
}
2016-09-06 08:42:35 +02:00
if sameNameQuery . Result != nil && sameNameQuery . Result . Id != current . Id {
2016-07-22 16:45:17 +02:00
return fmt . Errorf ( "Alert notification name %s already exists" , cmd . Name )
}
2016-06-14 08:33:50 +02:00
2016-07-22 16:45:17 +02:00
current . Updated = time . Now ( )
current . Settings = cmd . Settings
current . Name = cmd . Name
current . Type = cmd . Type
2016-09-05 21:33:05 +02:00
current . IsDefault = cmd . IsDefault
2018-06-05 10:27:29 +02:00
current . SendReminder = cmd . SendReminder
2018-10-17 10:41:18 +02:00
current . DisableResolveMessage = cmd . DisableResolveMessage
2018-05-20 12:12:10 -04:00
2019-03-29 15:42:38 +07:00
if cmd . Uid != "" {
current . Uid = cmd . Uid
}
2018-06-05 10:27:29 +02:00
if current . SendReminder {
2018-06-04 22:19:27 +02:00
if cmd . Frequency == "" {
2020-02-29 13:35:15 +01:00
return models . ErrNotificationFrequencyNotFound
2018-06-04 22:19:27 +02:00
}
2018-05-20 16:08:42 -04:00
2018-06-04 22:19:27 +02:00
frequency , err := time . ParseDuration ( cmd . Frequency )
if err != nil {
return err
}
current . Frequency = frequency
2018-05-20 12:12:10 -04:00
}
2016-09-05 21:33:05 +02:00
2018-10-17 10:41:18 +02:00
sess . UseBool ( "is_default" , "send_reminder" , "disable_resolve_message" )
2016-06-14 08:33:50 +02:00
2018-01-23 22:30:45 +01:00
if affected , err := sess . ID ( cmd . Id ) . Update ( current ) ; err != nil {
2016-06-14 08:33:50 +02:00
return err
2016-07-22 16:45:17 +02:00
} else if affected == 0 {
2018-06-04 22:19:27 +02:00
return fmt . Errorf ( "Could not update alert notification" )
2016-06-14 16:56:14 +02:00
}
2016-07-22 16:45:17 +02:00
cmd . Result = & current
2016-06-14 08:33:50 +02:00
return nil
} )
}
2018-05-19 16:21:00 -04:00
2020-02-29 13:35:15 +01:00
func UpdateAlertNotificationWithUid ( cmd * models . UpdateAlertNotificationWithUidCommand ) error {
getAlertNotificationWithUidQuery := & models . GetAlertNotificationsWithUidQuery { OrgId : cmd . OrgId , Uid : cmd . Uid }
2018-12-14 11:53:50 +02:00
2018-12-20 12:45:18 +02:00
if err := getAlertNotificationWithUidInternal ( getAlertNotificationWithUidQuery , newSession ( ) ) ; err != nil {
return err
2018-12-14 11:53:50 +02:00
}
current := getAlertNotificationWithUidQuery . Result
if current == nil {
return fmt . Errorf ( "Cannot update, alert notification uid %s doesn't exist" , cmd . Uid )
}
2019-03-29 15:42:38 +07:00
if cmd . NewUid == "" {
cmd . NewUid = cmd . Uid
}
2020-02-29 13:35:15 +01:00
updateNotification := & models . UpdateAlertNotificationCommand {
2018-12-14 11:53:50 +02:00
Id : current . Id ,
2019-03-29 15:42:38 +07:00
Uid : cmd . NewUid ,
2018-12-14 11:53:50 +02:00
Name : cmd . Name ,
Type : cmd . Type ,
SendReminder : cmd . SendReminder ,
DisableResolveMessage : cmd . DisableResolveMessage ,
Frequency : cmd . Frequency ,
IsDefault : cmd . IsDefault ,
Settings : cmd . Settings ,
OrgId : cmd . OrgId ,
}
2018-12-20 12:45:18 +02:00
if err := bus . Dispatch ( updateNotification ) ; err != nil {
return err
2018-12-14 11:53:50 +02:00
}
2019-04-04 17:52:40 +02:00
cmd . Result = updateNotification . Result
2018-12-14 11:53:50 +02:00
return nil
}
2020-02-29 13:35:15 +01:00
func SetAlertNotificationStateToCompleteCommand ( ctx context . Context , cmd * models . SetAlertNotificationStateToCompleteCommand ) error {
2018-10-02 14:53:39 +02:00
return inTransactionCtx ( ctx , func ( sess * DBSession ) error {
2018-10-02 13:57:41 +02:00
version := cmd . Version
2020-02-29 13:35:15 +01:00
var current models . AlertNotificationState
2019-10-22 14:08:18 +02:00
if _ , err := sess . ID ( cmd . Id ) . Get ( & current ) ; err != nil {
return err
}
2018-09-28 14:12:26 +02:00
2018-10-02 13:57:41 +02:00
newVersion := cmd . Version + 1
2018-09-27 12:07:43 +02:00
sql := ` UPDATE alert_notification_state SET
2018-09-28 11:17:03 +02:00
state = ? ,
2018-09-28 15:11:03 +02:00
version = ? ,
2018-09-30 21:52:50 +02:00
updated_at = ?
2018-09-27 11:33:13 +02:00
WHERE
id = ? `
2020-02-29 13:35:15 +01:00
_ , err := sess . Exec ( sql , models . AlertNotificationStateCompleted , newVersion , timeNow ( ) . Unix ( ) , cmd . Id )
2018-09-27 11:33:13 +02:00
if err != nil {
return err
}
2018-09-28 14:12:26 +02:00
if current . Version != version {
2018-10-02 15:07:44 +02:00
sqlog . Error ( "notification state out of sync. the notification is marked as complete but has been modified between set as pending and completion." , "notifierId" , current . NotifierId )
2018-09-27 11:33:13 +02:00
}
return nil
} )
}
2020-02-29 13:35:15 +01:00
func SetAlertNotificationStateToPendingCommand ( ctx context . Context , cmd * models . SetAlertNotificationStateToPendingCommand ) error {
2018-09-27 11:14:44 +02:00
return withDbSession ( ctx , func ( sess * DBSession ) error {
2018-10-02 13:57:41 +02:00
newVersion := cmd . Version + 1
2018-09-27 12:07:43 +02:00
sql := ` UPDATE alert_notification_state SET
2018-09-28 11:17:03 +02:00
state = ? ,
2018-09-30 21:52:50 +02:00
version = ? ,
2018-10-01 14:13:03 +02:00
updated_at = ? ,
alert_rule_state_updated_version = ?
2018-09-27 11:14:44 +02:00
WHERE
id = ? AND
2018-10-01 14:13:03 +02:00
( version = ? OR alert_rule_state_updated_version < ? ) `
2018-09-27 11:14:44 +02:00
2018-10-01 14:13:03 +02:00
res , err := sess . Exec ( sql ,
2020-02-29 13:35:15 +01:00
models . AlertNotificationStatePending ,
2018-10-02 13:57:41 +02:00
newVersion ,
2018-10-01 14:13:03 +02:00
timeNow ( ) . Unix ( ) ,
cmd . AlertRuleStateUpdatedVersion ,
2018-10-02 13:57:41 +02:00
cmd . Id ,
cmd . Version ,
2018-10-01 14:13:03 +02:00
cmd . AlertRuleStateUpdatedVersion )
2018-09-28 14:12:26 +02:00
2018-09-27 11:14:44 +02:00
if err != nil {
return err
}
affected , _ := res . RowsAffected ( )
if affected == 0 {
2020-02-29 13:35:15 +01:00
return models . ErrAlertNotificationStateVersionConflict
2018-09-27 11:14:44 +02:00
}
2018-10-02 13:57:41 +02:00
cmd . ResultVersion = newVersion
2018-09-27 11:14:44 +02:00
return nil
} )
}
2020-02-29 13:35:15 +01:00
func GetOrCreateAlertNotificationState ( ctx context . Context , cmd * models . GetOrCreateNotificationStateQuery ) error {
2018-10-02 15:07:44 +02:00
return inTransactionCtx ( ctx , func ( sess * DBSession ) error {
2020-02-29 13:35:15 +01:00
nj := & models . AlertNotificationState { }
2018-06-29 15:15:31 +02:00
2018-09-28 10:48:08 +02:00
exist , err := getAlertNotificationState ( sess , cmd , nj )
2018-06-16 11:27:04 +02:00
2018-09-27 14:32:54 +02:00
// if exists, return it, otherwise create it with default values
2018-05-19 16:21:00 -04:00
if err != nil {
return err
}
2018-09-28 10:48:08 +02:00
if exist {
cmd . Result = nj
return nil
}
2020-02-29 13:35:15 +01:00
notificationState := & models . AlertNotificationState {
2018-09-28 11:17:03 +02:00
OrgId : cmd . OrgId ,
AlertId : cmd . AlertId ,
NotifierId : cmd . NotifierId ,
2020-02-29 13:35:15 +01:00
State : models . AlertNotificationStateUnknown ,
2018-09-30 21:52:50 +02:00
UpdatedAt : timeNow ( ) . Unix ( ) ,
2018-09-28 11:17:03 +02:00
}
2018-09-27 14:32:54 +02:00
2018-09-28 11:17:03 +02:00
if _ , err := sess . Insert ( notificationState ) ; err != nil {
if dialect . IsUniqueConstraintViolation ( err ) {
exist , err = getAlertNotificationState ( sess , cmd , nj )
2018-09-27 14:32:54 +02:00
2018-09-28 11:17:03 +02:00
if err != nil {
return err
}
2018-09-28 10:48:08 +02:00
2018-09-28 11:17:03 +02:00
if ! exist {
return errors . New ( "Should not happen" )
2018-09-27 14:32:54 +02:00
}
2018-09-28 11:17:03 +02:00
cmd . Result = nj
return nil
2018-09-28 10:48:08 +02:00
}
2018-09-28 11:17:03 +02:00
return err
2018-09-27 11:14:44 +02:00
}
2018-09-28 11:17:03 +02:00
cmd . Result = notificationState
2018-05-19 16:21:00 -04:00
return nil
} )
}
2018-09-28 10:48:08 +02:00
2020-02-29 13:35:15 +01:00
func getAlertNotificationState ( sess * DBSession , cmd * models . GetOrCreateNotificationStateQuery , nj * models . AlertNotificationState ) ( bool , error ) {
2018-10-02 11:19:09 +02:00
return sess .
2018-09-28 10:48:08 +02:00
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 )
}