alert_notification.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 = ?)`)
  57. params = append(params, dialect.BooleanStr(true))
  58. if len(query.Ids) > 0 {
  59. sql.WriteString(` OR alert_notification.id IN (?` + strings.Repeat(",?", len(query.Ids)-1) + ")")
  60. for _, v := range query.Ids {
  61. params = append(params, v)
  62. }
  63. }
  64. sql.WriteString(`)`)
  65. results := make([]*m.AlertNotification, 0)
  66. if err := x.Sql(sql.String(), params...).Find(&results); err != nil {
  67. return err
  68. }
  69. query.Result = results
  70. return nil
  71. }
  72. func getAlertNotificationInternal(query *m.GetAlertNotificationsQuery, sess *xorm.Session) error {
  73. var sql bytes.Buffer
  74. params := make([]interface{}, 0)
  75. sql.WriteString(`SELECT
  76. alert_notification.id,
  77. alert_notification.org_id,
  78. alert_notification.name,
  79. alert_notification.type,
  80. alert_notification.created,
  81. alert_notification.updated,
  82. alert_notification.settings,
  83. alert_notification.is_default
  84. FROM alert_notification
  85. `)
  86. sql.WriteString(` WHERE alert_notification.org_id = ?`)
  87. params = append(params, query.OrgId)
  88. if query.Name != "" || query.Id != 0 {
  89. if query.Name != "" {
  90. sql.WriteString(` AND alert_notification.name = ?`)
  91. params = append(params, query.Name)
  92. }
  93. if query.Id != 0 {
  94. sql.WriteString(` AND alert_notification.id = ?`)
  95. params = append(params, query.Id)
  96. }
  97. }
  98. results := make([]*m.AlertNotification, 0)
  99. if err := sess.Sql(sql.String(), params...).Find(&results); err != nil {
  100. return err
  101. }
  102. if len(results) == 0 {
  103. query.Result = nil
  104. } else {
  105. query.Result = results[0]
  106. }
  107. return nil
  108. }
  109. func CreateAlertNotificationCommand(cmd *m.CreateAlertNotificationCommand) error {
  110. return inTransaction(func(sess *xorm.Session) error {
  111. existingQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
  112. err := getAlertNotificationInternal(existingQuery, sess)
  113. if err != nil {
  114. return err
  115. }
  116. if existingQuery.Result != nil {
  117. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  118. }
  119. alertNotification := &m.AlertNotification{
  120. OrgId: cmd.OrgId,
  121. Name: cmd.Name,
  122. Type: cmd.Type,
  123. Settings: cmd.Settings,
  124. Created: time.Now(),
  125. Updated: time.Now(),
  126. IsDefault: cmd.IsDefault,
  127. }
  128. if _, err = sess.Insert(alertNotification); err != nil {
  129. return err
  130. }
  131. cmd.Result = alertNotification
  132. return nil
  133. })
  134. }
  135. func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
  136. return inTransaction(func(sess *xorm.Session) (err error) {
  137. current := m.AlertNotification{}
  138. if _, err = sess.Id(cmd.Id).Get(&current); err != nil {
  139. return err
  140. }
  141. // check if name exists
  142. sameNameQuery := &m.GetAlertNotificationsQuery{OrgId: cmd.OrgId, Name: cmd.Name}
  143. if err := getAlertNotificationInternal(sameNameQuery, sess); err != nil {
  144. return err
  145. }
  146. if sameNameQuery.Result != nil && sameNameQuery.Result.Id != current.Id {
  147. return fmt.Errorf("Alert notification name %s already exists", cmd.Name)
  148. }
  149. current.Updated = time.Now()
  150. current.Settings = cmd.Settings
  151. current.Name = cmd.Name
  152. current.Type = cmd.Type
  153. current.IsDefault = cmd.IsDefault
  154. sess.UseBool("is_default")
  155. if affected, err := sess.Id(cmd.Id).Update(current); err != nil {
  156. return err
  157. } else if affected == 0 {
  158. return fmt.Errorf("Could not find alert notification")
  159. }
  160. cmd.Result = &current
  161. return nil
  162. })
  163. }