alert_notification.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package sqlstore
  2. import (
  3. "bytes"
  4. "fmt"
  5. "time"
  6. "github.com/go-xorm/xorm"
  7. "github.com/grafana/grafana/pkg/bus"
  8. m "github.com/grafana/grafana/pkg/models"
  9. )
  10. func init() {
  11. bus.AddHandler("sql", AlertNotificationQuery)
  12. bus.AddHandler("sql", CreateAlertNotificationCommand)
  13. bus.AddHandler("sql", UpdateAlertNotification)
  14. bus.AddHandler("sql", DeleteAlertNotification)
  15. }
  16. func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
  17. return inTransaction(func(sess *xorm.Session) error {
  18. sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
  19. _, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
  20. if err != nil {
  21. return err
  22. }
  23. return nil
  24. })
  25. }
  26. func AlertNotificationQuery(query *m.GetAlertNotificationsQuery) error {
  27. return getAlertNotifications(query, x.NewSession())
  28. }
  29. func getAlertNotifications(query *m.GetAlertNotificationsQuery, sess *xorm.Session) error {
  30. var sql bytes.Buffer
  31. params := make([]interface{}, 0)
  32. sql.WriteString(`SELECT
  33. alert_notification.id,
  34. alert_notification.org_id,
  35. alert_notification.name,
  36. alert_notification.type,
  37. alert_notification.created,
  38. alert_notification.updated,
  39. alert_notification.settings
  40. FROM alert_notification
  41. `)
  42. sql.WriteString(` WHERE alert_notification.org_id = ?`)
  43. params = append(params, query.OrgId)
  44. if query.Name != "" {
  45. sql.WriteString(` AND alert_notification.name = ?`)
  46. params = append(params, query.Name)
  47. }
  48. if query.Id != 0 {
  49. sql.WriteString(` AND alert_notification.id = ?`)
  50. params = append(params, query.Id)
  51. }
  52. if len(query.Ids) > 0 {
  53. sql.WriteString(` AND alert_notification.id IN (?)`)
  54. params = append(params, query.Ids)
  55. }
  56. results := make([]*m.AlertNotification, 0)
  57. if err := sess.Sql(sql.String(), params...).Find(&results); err != nil {
  58. return err
  59. }
  60. query.Result = results
  61. return nil
  62. }
  63. func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
  64. return inTransaction(func(sess *xorm.Session) error {
  65. existingQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
  66. err := getAlertNotifications(existingQuery, sess)
  67. if err != nil {
  68. return err
  69. }
  70. if len(existingQuery.Result) > 0 {
  71. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  72. }
  73. alertNotification := &m.AlertNotification{
  74. OrgId: cmd.OrgId,
  75. Name: cmd.Name,
  76. Type: cmd.Type,
  77. Settings: cmd.Settings,
  78. Created: time.Now(),
  79. Updated: time.Now(),
  80. }
  81. if _, err = sess.Insert(alertNotification); err != nil {
  82. return err
  83. }
  84. cmd.Result = alertNotification
  85. return nil
  86. })
  87. }
  88. func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
  89. return inTransaction(func(sess *xorm.Session) (err error) {
  90. current := m.AlertNotification{}
  91. if _, err = sess.Id(cmd.Id).Get(&current); err != nil {
  92. return err
  93. }
  94. // check if name exists
  95. sameNameQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
  96. if err := getAlertNotifications(sameNameQuery, sess); err != nil {
  97. return err
  98. }
  99. if len(sameNameQuery.Result) > 0 && sameNameQuery.Result[0].Id != current.Id {
  100. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  101. }
  102. current.Updated = time.Now()
  103. current.Settings = cmd.Settings
  104. current.Name = cmd.Name
  105. current.Type = cmd.Type
  106. if affected, err := sess.Id(cmd.Id).Update(current); err != nil {
  107. return err
  108. } else if affected == 0 {
  109. return fmt.Errorf("Could not find alert notification")
  110. }
  111. cmd.Result = &current
  112. return nil
  113. })
  114. }