alert_notification.go 2.8 KB

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