alert_notification.go 3.7 KB

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