alerting.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package api
  2. import (
  3. "fmt"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. "github.com/grafana/grafana/pkg/bus"
  6. "github.com/grafana/grafana/pkg/middleware"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/alerting"
  9. )
  10. func ValidateOrgAlert(c *middleware.Context) {
  11. id := c.ParamsInt64(":alertId")
  12. query := models.GetAlertByIdQuery{Id: id}
  13. if err := bus.Dispatch(&query); err != nil {
  14. c.JsonApiErr(404, "Alert not found", nil)
  15. return
  16. }
  17. if c.OrgId != query.Result.OrgId {
  18. c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
  19. return
  20. }
  21. }
  22. // GET /api/alerts/rules/
  23. func GetAlerts(c *middleware.Context) Response {
  24. query := models.GetAlertsQuery{
  25. OrgId: c.OrgId,
  26. State: c.QueryStrings("state"),
  27. DashboardId: c.QueryInt64("dashboardId"),
  28. PanelId: c.QueryInt64("panelId"),
  29. }
  30. if err := bus.Dispatch(&query); err != nil {
  31. return ApiError(500, "List alerts failed", err)
  32. }
  33. dashboardIds := make([]int64, 0)
  34. alertDTOs := make([]*dtos.AlertRuleDTO, 0)
  35. for _, alert := range query.Result {
  36. dashboardIds = append(dashboardIds, alert.DashboardId)
  37. alertDTOs = append(alertDTOs, &dtos.AlertRuleDTO{
  38. Id: alert.Id,
  39. DashboardId: alert.DashboardId,
  40. PanelId: alert.PanelId,
  41. Name: alert.Name,
  42. Description: alert.Description,
  43. State: alert.State,
  44. })
  45. }
  46. dashboardsQuery := models.GetDashboardsQuery{
  47. DashboardIds: dashboardIds,
  48. }
  49. if len(alertDTOs) > 0 {
  50. if err := bus.Dispatch(&dashboardsQuery); err != nil {
  51. return ApiError(500, "List alerts failed", err)
  52. }
  53. }
  54. //TODO: should be possible to speed this up with lookup table
  55. for _, alert := range alertDTOs {
  56. for _, dash := range dashboardsQuery.Result {
  57. if alert.DashboardId == dash.Id {
  58. alert.DashbboardUri = "db/" + dash.Slug
  59. }
  60. }
  61. }
  62. return Json(200, alertDTOs)
  63. }
  64. // POST /api/alerts/test
  65. func AlertTest(c *middleware.Context, dto dtos.AlertTestCommand) Response {
  66. backendCmd := alerting.AlertTestCommand{
  67. OrgId: c.OrgId,
  68. Dashboard: dto.Dashboard,
  69. PanelId: dto.PanelId,
  70. }
  71. if err := bus.Dispatch(&backendCmd); err != nil {
  72. return ApiError(500, "Failed to test rule", err)
  73. }
  74. res := backendCmd.Result
  75. dtoRes := &dtos.AlertTestResult{
  76. Triggered: res.Triggered,
  77. }
  78. if res.Error != nil {
  79. dtoRes.Error = res.Error.Error()
  80. }
  81. dtoRes.Timing = fmt.Sprintf("%1.3fs", res.GetDurationSeconds())
  82. return Json(200, dtoRes)
  83. }
  84. // GET /api/alerts/:id
  85. func GetAlert(c *middleware.Context) Response {
  86. id := c.ParamsInt64(":alertId")
  87. query := models.GetAlertByIdQuery{Id: id}
  88. if err := bus.Dispatch(&query); err != nil {
  89. return ApiError(500, "List alerts failed", err)
  90. }
  91. return Json(200, &query.Result)
  92. }
  93. // DEL /api/alerts/:id
  94. func DelAlert(c *middleware.Context) Response {
  95. alertId := c.ParamsInt64(":alertId")
  96. if alertId == 0 {
  97. return ApiError(401, "Failed to parse alertid", nil)
  98. }
  99. cmd := models.DeleteAlertCommand{AlertId: alertId}
  100. if err := bus.Dispatch(&cmd); err != nil {
  101. return ApiError(500, "Failed to delete alert", err)
  102. }
  103. var resp = map[string]interface{}{"alertId": alertId}
  104. return Json(200, resp)
  105. }
  106. // GET /api/alerts/events/:id
  107. func GetAlertStates(c *middleware.Context) Response {
  108. alertId := c.ParamsInt64(":alertId")
  109. query := models.GetAlertsStateQuery{
  110. AlertId: alertId,
  111. }
  112. if err := bus.Dispatch(&query); err != nil {
  113. return ApiError(500, "Failed get alert state log", err)
  114. }
  115. return Json(200, query.Result)
  116. }
  117. // PUT /api/alerts/events/:id
  118. func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
  119. cmd.AlertId = c.ParamsInt64(":alertId")
  120. cmd.OrgId = c.OrgId
  121. query := models.GetAlertByIdQuery{Id: cmd.AlertId}
  122. if err := bus.Dispatch(&query); err != nil {
  123. return ApiError(500, "Failed to get alertstate", err)
  124. }
  125. if query.Result.OrgId != 0 && query.Result.OrgId != c.OrgId {
  126. return ApiError(500, "Alert not found", nil)
  127. }
  128. if err := bus.Dispatch(&cmd); err != nil {
  129. return ApiError(500, "Failed to set new state", err)
  130. }
  131. return Json(200, cmd.Result)
  132. }
  133. func GetAlertNotifications(c *middleware.Context) Response {
  134. query := &models.GetAlertNotificationQuery{
  135. OrgID: c.OrgId,
  136. }
  137. if err := bus.Dispatch(query); err != nil {
  138. return ApiError(500, "Failed to get alert notifications", err)
  139. }
  140. var result []dtos.AlertNotificationDTO
  141. for _, notification := range query.Result {
  142. result = append(result, dtos.AlertNotificationDTO{
  143. Id: notification.Id,
  144. Name: notification.Name,
  145. Type: notification.Type,
  146. Created: notification.Created,
  147. Updated: notification.Updated,
  148. })
  149. }
  150. return Json(200, result)
  151. }
  152. func GetAlertNotificationById(c *middleware.Context) Response {
  153. query := &models.GetAlertNotificationQuery{
  154. OrgID: c.OrgId,
  155. Id: c.ParamsInt64("notificationId"),
  156. }
  157. if err := bus.Dispatch(query); err != nil {
  158. return ApiError(500, "Failed to get alert notifications", err)
  159. }
  160. return Json(200, query.Result[0])
  161. }
  162. func CreateAlertNotification(c *middleware.Context, cmd models.CreateAlertNotificationCommand) Response {
  163. cmd.OrgID = c.OrgId
  164. if err := bus.Dispatch(&cmd); err != nil {
  165. return ApiError(500, "Failed to create alert notification", err)
  166. }
  167. return Json(200, cmd.Result)
  168. }
  169. func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotificationCommand) Response {
  170. cmd.OrgID = c.OrgId
  171. if err := bus.Dispatch(&cmd); err != nil {
  172. return ApiError(500, "Failed to update alert notification", err)
  173. }
  174. return Json(200, cmd.Result)
  175. }
  176. func DeleteAlertNotification(c *middleware.Context) Response {
  177. cmd := models.DeleteAlertNotificationCommand{
  178. OrgId: c.OrgId,
  179. Id: c.ParamsInt64("notificationId"),
  180. }
  181. if err := bus.Dispatch(&cmd); err != nil {
  182. return ApiError(500, "Failed to delete alert notification", err)
  183. }
  184. return Json(200, map[string]interface{}{"notificationId": cmd.Id})
  185. }