alert_notification.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. alert_notification.always_execute
  42. FROM alert_notification
  43. `)
  44. sql.WriteString(` WHERE alert_notification.org_id = ?`)
  45. params = append(params, query.OrgID)
  46. if query.Name != "" {
  47. sql.WriteString(` AND alert_notification.name = ?`)
  48. params = append(params, query.Name)
  49. }
  50. if query.Id != 0 {
  51. sql.WriteString(` AND alert_notification.id = ?`)
  52. params = append(params, strconv.Itoa(int(query.Id)))
  53. }
  54. if len(query.Ids) > 0 {
  55. sql.WriteString(` AND (`)
  56. for i, id := range query.Ids {
  57. if i != 0 {
  58. sql.WriteString(` OR`)
  59. }
  60. sql.WriteString(` alert_notification.id = ?`)
  61. params = append(params, id)
  62. }
  63. sql.WriteString(`)`)
  64. }
  65. var searches []*m.AlertNotification
  66. if err := sess.Sql(sql.String(), params...).Find(&searches); err != nil {
  67. return err
  68. }
  69. var result []*m.AlertNotification
  70. var def []*m.AlertNotification
  71. if query.IncludeAlwaysExecute {
  72. if err := sess.Where("org_id = ? AND always_execute = 1", query.OrgID).Find(&def); err != nil {
  73. return err
  74. }
  75. result = append(result, def...)
  76. }
  77. for _, s := range searches {
  78. canAppend := true
  79. for _, d := range result {
  80. if d.Id == s.Id {
  81. canAppend = false
  82. break
  83. }
  84. }
  85. if canAppend {
  86. result = append(result, s)
  87. }
  88. }
  89. query.Result = result
  90. return nil
  91. }
  92. func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
  93. return inTransaction(func(sess *xorm.Session) error {
  94. existingQuery := &m.GetAlertNotificationQuery{OrgID: cmd.OrgID, Name: cmd.Name, IncludeAlwaysExecute: false}
  95. err := getAlertNotifications(existingQuery, sess)
  96. if err != nil {
  97. return err
  98. }
  99. if len(existingQuery.Result) > 0 {
  100. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  101. }
  102. alertNotification := &m.AlertNotification{
  103. OrgId: cmd.OrgID,
  104. Name: cmd.Name,
  105. Type: cmd.Type,
  106. Created: time.Now(),
  107. Settings: cmd.Settings,
  108. Updated: time.Now(),
  109. AlwaysExecute: cmd.AlwaysExecute,
  110. }
  111. _, err = sess.Insert(alertNotification)
  112. if err != nil {
  113. return err
  114. }
  115. cmd.Result = alertNotification
  116. return nil
  117. })
  118. }
  119. func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
  120. return inTransaction(func(sess *xorm.Session) (err error) {
  121. current := &m.AlertNotification{}
  122. _, err = sess.Id(cmd.Id).Get(current)
  123. if err != nil {
  124. return err
  125. }
  126. alertNotification := &m.AlertNotification{
  127. Id: cmd.Id,
  128. OrgId: cmd.OrgID,
  129. Name: cmd.Name,
  130. Type: cmd.Type,
  131. Settings: cmd.Settings,
  132. Updated: time.Now(),
  133. Created: current.Created,
  134. AlwaysExecute: cmd.AlwaysExecute,
  135. }
  136. sess.UseBool("always_execute")
  137. var affected int64
  138. affected, err = sess.Id(alertNotification.Id).Update(alertNotification)
  139. if err != nil {
  140. return err
  141. }
  142. if affected == 0 {
  143. return fmt.Errorf("Could not find alert notification")
  144. }
  145. cmd.Result = alertNotification
  146. return nil
  147. })
  148. }