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