alert_notification.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package sqlstore
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strconv"
  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.GetAlertNotificationQuery) error {
  28. return getAlertNotifications(query, x.NewSession())
  29. }
  30. func getAlertNotifications(query *m.GetAlertNotificationQuery, 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, strconv.Itoa(int(query.Id)))
  52. }
  53. if len(query.Ids) > 0 {
  54. sql.WriteString(` AND (`)
  55. for i, id := range query.Ids {
  56. if i != 0 {
  57. sql.WriteString(` OR`)
  58. }
  59. sql.WriteString(` alert_notification.id = ?`)
  60. params = append(params, id)
  61. }
  62. sql.WriteString(`)`)
  63. }
  64. var result []*m.AlertNotification
  65. if err := sess.Sql(sql.String(), params...).Find(&result); err != nil {
  66. return err
  67. }
  68. query.Result = result
  69. return nil
  70. }
  71. func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
  72. return inTransaction(func(sess *xorm.Session) error {
  73. existingQuery := &m.GetAlertNotificationQuery{OrgID: cmd.OrgID, Name: cmd.Name}
  74. err := getAlertNotifications(existingQuery, sess)
  75. if err != nil {
  76. return err
  77. }
  78. if len(existingQuery.Result) > 0 {
  79. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  80. }
  81. alertNotification := &m.AlertNotification{
  82. OrgId: cmd.OrgID,
  83. Name: cmd.Name,
  84. Type: cmd.Type,
  85. Created: time.Now(),
  86. Settings: cmd.Settings,
  87. Updated: time.Now(),
  88. }
  89. _, err = sess.Insert(alertNotification)
  90. if err != nil {
  91. return err
  92. }
  93. //alertNotification.Id = int(id)
  94. cmd.Result = alertNotification
  95. return nil
  96. })
  97. }
  98. func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
  99. return inTransaction(func(sess *xorm.Session) (err error) {
  100. current := &m.AlertNotification{}
  101. _, err = sess.Id(cmd.Id).Get(current)
  102. if err != nil {
  103. return err
  104. }
  105. alertNotification := &m.AlertNotification{}
  106. alertNotification.Id = cmd.Id
  107. alertNotification.OrgId = cmd.OrgID
  108. alertNotification.Name = cmd.Name
  109. alertNotification.Type = cmd.Type
  110. alertNotification.Settings = cmd.Settings
  111. alertNotification.Updated = time.Now()
  112. alertNotification.Created = current.Created
  113. var affected int64
  114. //affected, err = sess.Id(alertNotification.Id).Cols("name", "type", "settings", "updated").Update(alertNotification)
  115. affected, err = sess.Id(alertNotification.Id).Update(alertNotification)
  116. if err != nil {
  117. return err
  118. }
  119. if affected == 0 {
  120. return fmt.Errorf("Could not find alert notification")
  121. }
  122. cmd.Result = alertNotification
  123. return nil
  124. })
  125. }