alerting.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/bus"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. "github.com/grafana/grafana/pkg/models"
  7. )
  8. func ValidateOrgAlert(c *middleware.Context) {
  9. id := c.ParamsInt64(":alertId")
  10. query := models.GetAlertByIdQuery{Id: id}
  11. if err := bus.Dispatch(&query); err != nil {
  12. c.JsonApiErr(404, "Alert not found", nil)
  13. return
  14. }
  15. if c.OrgId != query.Result.OrgId {
  16. c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
  17. return
  18. }
  19. }
  20. // GET /api/alerting/changes
  21. func GetAlertChanges(c *middleware.Context) Response {
  22. query := models.GetAlertChangesQuery{
  23. OrgId: c.OrgId,
  24. }
  25. limit := c.QueryInt64("limit")
  26. if limit == 0 {
  27. limit = 50
  28. }
  29. query.Limit = limit
  30. query.SinceId = c.QueryInt64("sinceId")
  31. if err := bus.Dispatch(&query); err != nil {
  32. return ApiError(500, "List alerts failed", err)
  33. }
  34. return Json(200, query.Result)
  35. }
  36. // GET /api/alerts/rules/
  37. func GetAlerts(c *middleware.Context) Response {
  38. query := models.GetAlertsQuery{
  39. OrgId: c.OrgId,
  40. State: c.QueryStrings("state"),
  41. DashboardId: c.QueryInt64("dashboardId"),
  42. PanelId: c.QueryInt64("panelId"),
  43. }
  44. if err := bus.Dispatch(&query); err != nil {
  45. return ApiError(500, "List alerts failed", err)
  46. }
  47. dashboardIds := make([]int64, 0)
  48. alertDTOs := make([]*dtos.AlertRuleDTO, 0)
  49. for _, alert := range query.Result {
  50. dashboardIds = append(dashboardIds, alert.DashboardId)
  51. alertDTOs = append(alertDTOs, &dtos.AlertRuleDTO{
  52. Id: alert.Id,
  53. DashboardId: alert.DashboardId,
  54. PanelId: alert.PanelId,
  55. Name: alert.Name,
  56. Description: alert.Description,
  57. State: alert.State,
  58. })
  59. }
  60. dashboardsQuery := models.GetDashboardsQuery{
  61. DashboardIds: dashboardIds,
  62. }
  63. if len(alertDTOs) > 0 {
  64. if err := bus.Dispatch(&dashboardsQuery); err != nil {
  65. return ApiError(500, "List alerts failed", err)
  66. }
  67. }
  68. //TODO: should be possible to speed this up with lookup table
  69. for _, alert := range alertDTOs {
  70. for _, dash := range *dashboardsQuery.Result {
  71. if alert.DashboardId == dash.Id {
  72. alert.DashbboardUri = "db/" + dash.Slug
  73. }
  74. }
  75. }
  76. return Json(200, alertDTOs)
  77. }
  78. // GET /api/alerts/:id
  79. func GetAlert(c *middleware.Context) Response {
  80. id := c.ParamsInt64(":alertId")
  81. query := models.GetAlertByIdQuery{Id: id}
  82. if err := bus.Dispatch(&query); err != nil {
  83. return ApiError(500, "List alerts failed", err)
  84. }
  85. return Json(200, &query.Result)
  86. }
  87. // DEL /api/alerts/:id
  88. func DelAlert(c *middleware.Context) Response {
  89. alertId := c.ParamsInt64(":alertId")
  90. if alertId == 0 {
  91. return ApiError(401, "Failed to parse alertid", nil)
  92. }
  93. cmd := models.DeleteAlertCommand{AlertId: alertId}
  94. if err := bus.Dispatch(&cmd); err != nil {
  95. return ApiError(500, "Failed to delete alert", err)
  96. }
  97. var resp = map[string]interface{}{"alertId": alertId}
  98. return Json(200, resp)
  99. }
  100. // GET /api/alerts/events/:id
  101. func GetAlertStates(c *middleware.Context) Response {
  102. alertId := c.ParamsInt64(":alertId")
  103. query := models.GetAlertsStateQuery{
  104. AlertId: alertId,
  105. }
  106. if err := bus.Dispatch(&query); err != nil {
  107. return ApiError(500, "Failed get alert state log", err)
  108. }
  109. return Json(200, query.Result)
  110. }
  111. // PUT /api/alerts/events/:id
  112. func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
  113. cmd.AlertId = c.ParamsInt64(":alertId")
  114. query := models.GetAlertByIdQuery{Id: cmd.AlertId}
  115. if err := bus.Dispatch(&query); err != nil {
  116. return ApiError(500, "Failed to get alertstate", err)
  117. }
  118. if query.Result.OrgId != 0 && query.Result.OrgId != c.OrgId {
  119. return ApiError(500, "Alert not found", nil)
  120. }
  121. if err := bus.Dispatch(&cmd); err != nil {
  122. return ApiError(500, "Failed to set new state", err)
  123. }
  124. return Json(200, cmd.Result)
  125. }
  126. func GetAlertNotifications(c *middleware.Context) Response {
  127. query := &models.GetAlertNotificationQuery{
  128. OrgID: c.OrgId,
  129. }
  130. if err := bus.Dispatch(query); err != nil {
  131. return ApiError(500, "Failed to get alert notifications", err)
  132. }
  133. return Json(200, query.Result)
  134. }
  135. func CreateAlertNotification(c *middleware.Context, cmd *models.CreateAlertNotificationCommand) Response {
  136. cmd.OrgID = c.OrgId
  137. if err := bus.Dispatch(cmd); err != nil {
  138. return ApiError(500, "Failed to create alert notification", err)
  139. }
  140. return Json(200, cmd.Result)
  141. }
  142. func UpdateAlertNotification(c *middleware.Context, cmd *models.UpdateAlertNotificationCommand) Response {
  143. cmd.OrgID = c.OrgId
  144. if err := bus.Dispatch(cmd); err != nil {
  145. return ApiError(500, "Failed to update alert notification", err)
  146. }
  147. return Json(200, cmd.Result)
  148. }