alert_notification.go 5.1 KB

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