alert_notification.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. }
  16. func AlertNotificationQuery(query *m.GetAlertNotificationQuery) error {
  17. return getAlertNotifications(query, x.NewSession())
  18. }
  19. func getAlertNotifications(query *m.GetAlertNotificationQuery, sess *xorm.Session) error {
  20. var sql bytes.Buffer
  21. params := make([]interface{}, 0)
  22. sql.WriteString(`SELECT
  23. alert_notification.id,
  24. alert_notification.org_id,
  25. alert_notification.name,
  26. alert_notification.type,
  27. alert_notification.created,
  28. alert_notification.updated,
  29. alert_notification.settings
  30. FROM alert_notification
  31. `)
  32. sql.WriteString(` WHERE alert_notification.org_id = ?`)
  33. params = append(params, query.OrgID)
  34. if query.Name != "" {
  35. sql.WriteString(` AND alert_notification.name = ?`)
  36. params = append(params, query.Name)
  37. }
  38. if query.Id != 0 {
  39. sql.WriteString(` AND alert_notification.id = ?`)
  40. params = append(params, strconv.Itoa(int(query.Id)))
  41. }
  42. if len(query.Ids) > 0 {
  43. sql.WriteString(` AND (`)
  44. for i, id := range query.Ids {
  45. if i != 0 {
  46. sql.WriteString(` OR`)
  47. }
  48. sql.WriteString(` alert_notification.id = ?`)
  49. params = append(params, id)
  50. }
  51. sql.WriteString(`)`)
  52. }
  53. var result []*m.AlertNotification
  54. if err := sess.Sql(sql.String(), params...).Find(&result); err != nil {
  55. return err
  56. }
  57. query.Result = result
  58. return nil
  59. }
  60. func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
  61. return inTransaction(func(sess *xorm.Session) error {
  62. existingQuery := &m.GetAlertNotificationQuery{OrgID: cmd.OrgID, Name: cmd.Name}
  63. err := getAlertNotifications(existingQuery, sess)
  64. if err != nil {
  65. return err
  66. }
  67. if len(existingQuery.Result) > 0 {
  68. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  69. }
  70. alertNotification := &m.AlertNotification{
  71. OrgId: cmd.OrgID,
  72. Name: cmd.Name,
  73. Type: cmd.Type,
  74. Created: time.Now(),
  75. Settings: cmd.Settings,
  76. Updated: time.Now(),
  77. }
  78. _, err = sess.Insert(alertNotification)
  79. if err != nil {
  80. return err
  81. }
  82. //alertNotification.Id = int(id)
  83. cmd.Result = alertNotification
  84. return nil
  85. })
  86. }
  87. func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
  88. return inTransaction(func(sess *xorm.Session) (err error) {
  89. current := &m.AlertNotification{}
  90. _, err = sess.Id(cmd.Id).Get(current)
  91. if err != nil {
  92. return err
  93. }
  94. alertNotification := &m.AlertNotification{}
  95. alertNotification.Id = cmd.Id
  96. alertNotification.OrgId = cmd.OrgID
  97. alertNotification.Name = cmd.Name
  98. alertNotification.Type = cmd.Type
  99. alertNotification.Settings = cmd.Settings
  100. alertNotification.Updated = time.Now()
  101. alertNotification.Created = current.Created
  102. var affected int64
  103. //affected, err = sess.Id(alertNotification.Id).Cols("name", "type", "settings", "updated").Update(alertNotification)
  104. affected, err = sess.Id(alertNotification.Id).Update(alertNotification)
  105. if err != nil {
  106. return err
  107. }
  108. if affected == 0 {
  109. return fmt.Errorf("Could not find alert notification")
  110. }
  111. cmd.Result = alertNotification
  112. return nil
  113. })
  114. }