alert_notification.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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", GetAlertNotifications)
  13. bus.AddHandler("sql", CreateAlertNotificationCommand)
  14. bus.AddHandler("sql", UpdateAlertNotification)
  15. bus.AddHandler("sql", DeleteAlertNotification)
  16. bus.AddHandler("sql", GetAlertNotificationsToSend)
  17. bus.AddHandler("sql", GetAllAlertNotifications)
  18. }
  19. func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
  20. return inTransaction(func(sess *xorm.Session) error {
  21. sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?"
  22. _, err := sess.Exec(sql, cmd.OrgId, cmd.Id)
  23. if err != nil {
  24. return err
  25. }
  26. return nil
  27. })
  28. }
  29. func GetAlertNotifications(query *m.GetAlertNotificationsQuery) error {
  30. return getAlertNotificationInternal(query, x.NewSession())
  31. }
  32. func GetAllAlertNotifications(query *m.GetAllAlertNotificationsQuery) error {
  33. results := make([]*m.AlertNotification, 0)
  34. if err := x.Where("org_id = ?", query.OrgId).Find(&results); err != nil {
  35. return err
  36. }
  37. query.Result = results
  38. return nil
  39. }
  40. func GetAlertNotificationsToSend(query *m.GetAlertNotificationsToSendQuery) error {
  41. var sql bytes.Buffer
  42. params := make([]interface{}, 0)
  43. sql.WriteString(`SELECT
  44. alert_notification.id,
  45. alert_notification.org_id,
  46. alert_notification.name,
  47. alert_notification.type,
  48. alert_notification.created,
  49. alert_notification.updated,
  50. alert_notification.settings,
  51. alert_notification.is_default
  52. FROM alert_notification
  53. `)
  54. sql.WriteString(` WHERE alert_notification.org_id = ?`)
  55. params = append(params, query.OrgId)
  56. sql.WriteString(` AND ((alert_notification.is_default = 1)`)
  57. if len(query.Ids) > 0 {
  58. sql.WriteString(` OR alert_notification.id IN (?` + strings.Repeat(",?", len(query.Ids)-1) + ")")
  59. for _, v := range query.Ids {
  60. params = append(params, v)
  61. }
  62. }
  63. sql.WriteString(`)`)
  64. results := make([]*m.AlertNotification, 0)
  65. if err := x.Sql(sql.String(), params...).Find(&results); err != nil {
  66. return err
  67. }
  68. query.Result = results
  69. return nil
  70. }
  71. func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *xorm.Session) error {
  72. var sql bytes.Buffer
  73. params := make([]interface{}, 0)
  74. sql.WriteString(`SELECT
  75. alert_notification.id,
  76. alert_notification.org_id,
  77. alert_notification.name,
  78. alert_notification.type,
  79. alert_notification.created,
  80. alert_notification.updated,
  81. alert_notification.settings,
  82. alert_notification.is_default
  83. FROM alert_notification
  84. `)
  85. sql.WriteString(` WHERE alert_notification.org_id = ?`)
  86. params = append(params, query.OrgId)
  87. if query.Name != "" || query.Id != 0 {
  88. if query.Name != "" {
  89. sql.WriteString(` AND alert_notification.name = ?`)
  90. params = append(params, query.Name)
  91. }
  92. if query.Id != 0 {
  93. sql.WriteString(` AND alert_notification.id = ?`)
  94. params = append(params, query.Id)
  95. }
  96. }
  97. results := make([]*m.AlertNotification, 0)
  98. if err := sess.Sql(sql.String(), params...).Find(&results); err != nil {
  99. return err
  100. }
  101. if len(results) == 0 {
  102. query.Result = nil
  103. } else {
  104. query.Result = results[0]
  105. }
  106. return nil
  107. }
  108. func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
  109. return inTransaction(func(sess *xorm.Session) error {
  110. existingQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
  111. err := getAlertNotificationInternal(existingQuery, sess)
  112. if err != nil {
  113. return err
  114. }
  115. if existingQuery.Result != nil {
  116. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  117. }
  118. alertNotification := &m.AlertNotification{
  119. OrgId: cmd.OrgId,
  120. Name: cmd.Name,
  121. Type: cmd.Type,
  122. Settings: cmd.Settings,
  123. Created: time.Now(),
  124. Updated: time.Now(),
  125. IsDefault: cmd.IsDefault,
  126. }
  127. if _, err = sess.Insert(alertNotification); err != nil {
  128. return err
  129. }
  130. cmd.Result = alertNotification
  131. return nil
  132. })
  133. }
  134. func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
  135. return inTransaction(func(sess *xorm.Session) (err error) {
  136. current := m.AlertNotification{}
  137. if _, err = sess.Id(cmd.Id).Get(&current); err != nil {
  138. return err
  139. }
  140. // check if name exists
  141. sameNameQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
  142. if err := getAlertNotificationInternal(sameNameQuery, sess); err != nil {
  143. return err
  144. }
  145. if sameNameQuery.Result != nil && sameNameQuery.Result.Id != current.Id {
  146. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  147. }
  148. current.Updated = time.Now()
  149. current.Settings = cmd.Settings
  150. current.Name = cmd.Name
  151. current.Type = cmd.Type
  152. current.IsDefault = cmd.IsDefault
  153. sess.UseBool("is_default")
  154. if affected, err := sess.Id(cmd.Id).Update(current); err != nil {
  155. return err
  156. } else if affected == 0 {
  157. return fmt.Errorf("Could not find alert notification")
  158. }
  159. cmd.Result = &current
  160. return nil
  161. })
  162. }