| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package sqlstore
- import (
- "bytes"
- "fmt"
- "strings"
- "time"
- "github.com/grafana/grafana/pkg/bus"
- m "github.com/grafana/grafana/pkg/models"
- )
- func init() {
- bus.AddHandler("sql", GetAlertNotifications)
- bus.AddHandler("sql", CreateAlertNotificationCommand)
- bus.AddHandler("sql", UpdateAlertNotification)
- bus.AddHandler("sql", DeleteAlertNotification)
- bus.AddHandler("sql", GetAlertNotificationsToSend)
- bus.AddHandler("sql", GetAllAlertNotifications)
- bus.AddHandler("sql", RecordNotificationJournal)
- bus.AddHandler("sql", GetLatestNotification)
- bus.AddHandler("sql", CleanNotificationJournal)
- }
- func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
- return inTransaction(func(sess *DBSession) error {
- sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
- _, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
- return err
- })
- }
- func GetAlertNotifications(query *m.GetAlertNotificationsQuery) error {
- return getAlertNotificationInternal(query, newSession())
- }
- 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
- }
- func GetAlertNotificationsToSend(query *m.GetAlertNotificationsToSendQuery) error {
- var sql bytes.Buffer
- params := make([]interface{}, 0)
- sql.WriteString(`SELECT
- alert_notification.id,
- 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.notify_once,
- alert_notification.frequency
- FROM alert_notification
- `)
- sql.WriteString(` WHERE alert_notification.org_id = ?`)
- params = append(params, query.OrgId)
- sql.WriteString(` AND ((alert_notification.is_default = ?)`)
- params = append(params, dialect.BooleanStr(true))
- if len(query.Ids) > 0 {
- sql.WriteString(` OR alert_notification.id IN (?` + strings.Repeat(",?", len(query.Ids)-1) + ")")
- for _, v := range query.Ids {
- params = append(params, v)
- }
- }
- sql.WriteString(`)`)
- results := make([]*m.AlertNotification, 0)
- if err := x.SQL(sql.String(), params...).Find(&results); err != nil {
- return err
- }
- query.Result = results
- return nil
- }
- func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *DBSession) error {
- var sql bytes.Buffer
- params := make([]interface{}, 0)
- sql.WriteString(`SELECT
- alert_notification.id,
- 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.notify_once,
- alert_notification.frequency
- FROM alert_notification
- `)
- sql.WriteString(` WHERE alert_notification.org_id = ?`)
- params = append(params, query.OrgId)
- if query.Name != "" || query.Id != 0 {
- if query.Name != "" {
- sql.WriteString(` AND alert_notification.name = ?`)
- params = append(params, query.Name)
- }
- if query.Id != 0 {
- sql.WriteString(` AND alert_notification.id = ?`)
- params = append(params, query.Id)
- }
- }
- 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
- }
- func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
- return inTransaction(func(sess *DBSession) error {
- existingQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
- err := getAlertNotificationInternal(existingQuery, sess)
- if err != nil {
- return err
- }
- if existingQuery.Result != nil {
- return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
- }
- if cmd.Frequency == "" {
- return fmt.Errorf("Alert notification frequency required")
- }
- frequency, err_convert := time.ParseDuration(cmd.Frequency)
- if err_convert != nil {
- return err
- }
- alertNotification := &m.AlertNotification{
- OrgId: cmd.OrgId,
- Name: cmd.Name,
- Type: cmd.Type,
- Settings: cmd.Settings,
- NotifyOnce: cmd.NotifyOnce,
- Frequency: frequency,
- Created: time.Now(),
- Updated: time.Now(),
- IsDefault: cmd.IsDefault,
- }
- if _, err = sess.MustCols("notify_once").Insert(alertNotification); err != nil {
- return err
- }
- cmd.Result = alertNotification
- return nil
- })
- }
- func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
- return inTransaction(func(sess *DBSession) (err error) {
- current := m.AlertNotification{}
- if _, err = sess.ID(cmd.Id).Get(¤t); err != nil {
- return err
- }
- // 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 && sameNameQuery.Result.Id != current.Id {
- return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
- }
- current.Updated = time.Now()
- current.Settings = cmd.Settings
- current.Name = cmd.Name
- current.Type = cmd.Type
- current.IsDefault = cmd.IsDefault
- current.NotifyOnce = cmd.NotifyOnce
- if cmd.Frequency == "" {
- return fmt.Errorf("Alert notification frequency required")
- }
- frequency, err_convert := time.ParseDuration(cmd.Frequency)
- if err_convert != nil {
- return err
- }
- current.Frequency = frequency
- sess.UseBool("is_default", "notify_once")
- if affected, err := sess.ID(cmd.Id).Update(current); err != nil {
- return err
- } else if affected == 0 {
- return fmt.Errorf("Could not find alert notification")
- }
- cmd.Result = ¤t
- return nil
- })
- }
- func RecordNotificationJournal(cmd *m.RecordNotificationJournalCommand) error {
- return inTransaction(func(sess *DBSession) error {
- journalEntry := &m.NotificationJournal{
- OrgId: cmd.OrgId,
- AlertId: cmd.AlertId,
- NotifierId: cmd.NotifierId,
- SentAt: cmd.SentAt,
- Success: cmd.Success,
- }
- if _, err := sess.Insert(journalEntry); err != nil {
- return err
- }
- return nil
- })
- }
- func GetLatestNotification(cmd *m.GetLatestNotificationQuery) error {
- return inTransaction(func(sess *DBSession) error {
- notificationJournal := &m.NotificationJournal{}
- _, err := sess.Desc("notification_journal.sent_at").Limit(1).Where("notification_journal.org_id = ? AND notification_journal.alert_id = ? AND notification_journal.notifier_id = ?", cmd.OrgId, cmd.AlertId, cmd.NotifierId).Get(notificationJournal)
- if err != nil {
- return err
- }
- cmd.Result = notificationJournal
- return nil
- })
- }
- func CleanNotificationJournal(cmd *m.CleanNotificationJournalCommand) error {
- return inTransaction(func(sess *DBSession) error {
- sql := "DELETE FROM notification_journal WHERE notification_journal.org_id = ? AND notification_journal.alert_id = ? AND notification_journal.notifier_id = ?"
- _, err := sess.Exec(sql, cmd.OrgId, cmd.AlertId, cmd.NotifierId)
- return err
- })
- }
|