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"
m "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
}
func DeleteAlertNotification ( cmd * m . 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
}
2018-12-14 03:53:50 -06:00
func DeleteAlertNotificationWithUid ( cmd * m . DeleteAlertNotificationWithUidCommand ) error {
existingNotification := & m . 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 {
deleteCommand := & m . DeleteAlertNotificationCommand {
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
}
2016-08-01 07:34:58 -05:00
func GetAlertNotifications ( query * m . GetAlertNotificationsQuery ) error {
2017-05-23 03:56:23 -05:00
return getAlertNotificationInternal ( query , newSession ( ) )
2016-06-14 01:33:50 -05:00
}
2018-12-14 03:53:50 -06:00
func GetAlertNotificationsWithUid ( query * m . GetAlertNotificationsWithUidQuery ) error {
return getAlertNotificationWithUidInternal ( query , newSession ( ) )
}
2016-09-06 01:42:35 -05:00
func GetAllAlertNotifications ( query * m . GetAllAlertNotificationsQuery ) error {
results := make ( [ ] * m . AlertNotification , 0 )
if err := x . Where ( "org_id = ?" , query . OrgId ) . Find ( & results ) ; err != nil {
return err
}
query . Result = results
return nil
}
2018-12-14 03:53:50 -06:00
func GetAlertNotificationsWithUidToSend ( query * m . 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 ,
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 ( ` ) ` )
results := make ( [ ] * m . 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
}
2017-05-23 03:56:23 -05:00
func getAlertNotificationInternal ( query * m . 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 ,
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
}
2016-07-22 09:45:17 -05:00
results := make ( [ ] * m . 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
}
2018-12-14 03:53:50 -06:00
func getAlertNotificationWithUidInternal ( query * m . GetAlertNotificationsWithUidQuery , sess * DBSession ) error {
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 )
results := make ( [ ] * m . AlertNotification , 0 )
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
}
2016-06-14 01:33:50 -05:00
func CreateAlertNotificationCommand ( cmd * m . 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 == "" {
if uid , uidGenerationErr := generateNewAlertNotificationUid ( sess , cmd . OrgId ) ; uidGenerationErr != nil {
return uidGenerationErr
} else {
cmd . Uid = uid
}
}
existingQuery := & m . GetAlertNotificationsWithUidQuery { OrgId : cmd . OrgId , Uid : cmd . Uid }
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
sameNameQuery := & m . GetAlertNotificationsQuery { OrgId : cmd . OrgId , Name : cmd . Name }
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 == "" {
return m . ErrNotificationFrequencyNotFound
}
frequency , err = time . ParseDuration ( cmd . Frequency )
if err != nil {
return err
}
2018-05-20 11:12:10 -05:00
}
2016-06-14 01:33:50 -05:00
alertNotification := & m . 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 ,
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 ( )
2018-12-14 03:53:50 -06:00
exists , err := sess . Where ( "org_id=? AND uid=?" , orgId , uid ) . Get ( & m . AlertNotification { } )
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
2018-12-14 03:53:50 -06:00
return "" , m . ErrAlertNotificationFailedGenerateUniqueUid
}
2016-06-13 09:39:00 -05:00
func UpdateAlertNotification ( cmd * m . UpdateAlertNotificationCommand ) error {
2017-05-23 03:56:23 -05:00
return inTransaction ( func ( sess * DBSession ) ( err error ) {
2016-07-22 09:45:17 -05:00
current := m . 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
sameNameQuery := & m . 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
2016-07-22 09:45:17 -05:00
current . Updated = time . Now ( )
current . Settings = cmd . Settings
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 == "" {
return m . ErrNotificationFrequencyNotFound
}
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
2018-12-14 03:53:50 -06:00
func UpdateAlertNotificationWithUid ( cmd * m . UpdateAlertNotificationWithUidCommand ) error {
getAlertNotificationWithUidQuery := & m . GetAlertNotificationsWithUidQuery { OrgId : cmd . OrgId , Uid : cmd . Uid }
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
}
2018-12-14 03:53:50 -06:00
updateNotification := & m . UpdateAlertNotificationCommand {
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 ,
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
}
return nil
}
2018-09-27 04:33:13 -05:00
func SetAlertNotificationStateToCompleteCommand ( ctx context . Context , cmd * m . 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
2018-09-28 07:12:26 -05:00
var current m . AlertNotificationState
2018-10-02 06:57:41 -05:00
sess . ID ( cmd . Id ) . Get ( & current )
2018-09-28 07:12:26 -05:00
2018-10-02 06:57:41 -05:00
newVersion := cmd . Version + 1
2018-09-28 07:12:26 -05:00
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 = ? `
2018-10-02 06:57:41 -05:00
_ , err := sess . Exec ( sql , m . AlertNotificationStateCompleted , newVersion , timeNow ( ) . Unix ( ) , cmd . Id )
2018-09-28 07:12:26 -05:00
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
} )
}
func SetAlertNotificationStateToPendingCommand ( ctx context . Context , cmd * m . 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 ,
2018-10-02 06:57:41 -05:00
m . AlertNotificationStatePending ,
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 {
return m . ErrAlertNotificationStateVersionConflict
}
2018-10-02 06:57:41 -05:00
cmd . ResultVersion = newVersion
2018-09-27 04:14:44 -05:00
return nil
} )
}
2018-10-02 04:23:40 -05:00
func GetOrCreateAlertNotificationState ( ctx context . Context , cmd * m . GetOrCreateNotificationStateQuery ) error {
2018-10-02 08:07:44 -05:00
return inTransactionCtx ( ctx , func ( sess * DBSession ) error {
2018-09-26 10:26:02 -05:00
nj := & m . 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
}
2018-09-28 04:17:03 -05:00
notificationState := & m . AlertNotificationState {
OrgId : cmd . OrgId ,
AlertId : cmd . AlertId ,
NotifierId : cmd . NotifierId ,
2018-10-02 04:19:09 -05:00
State : m . 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
2018-10-02 04:23:40 -05:00
func getAlertNotificationState ( sess * DBSession , cmd * m . GetOrCreateNotificationStateQuery , nj * m . 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 )
}