alerting.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/alerts/changes
  21. func GetAlertChanges(c *middleware.Context) Response {
  22. query := models.GetAlertChangesQuery{
  23. OrgId: c.OrgId,
  24. }
  25. if err := bus.Dispatch(&query); err != nil {
  26. return ApiError(500, "List alerts failed", err)
  27. }
  28. return Json(200, query.Result)
  29. }
  30. // GET /api/alerts
  31. func GetAlerts(c *middleware.Context) Response {
  32. query := models.GetAlertsQuery{
  33. OrgId: c.OrgId,
  34. }
  35. if err := bus.Dispatch(&query); err != nil {
  36. return ApiError(500, "List alerts failed", err)
  37. }
  38. dashboardIds := make([]int64, 0)
  39. alertDTOs := make([]*dtos.AlertRuleDTO, 0)
  40. for _, alert := range query.Result {
  41. dashboardIds = append(dashboardIds, alert.DashboardId)
  42. alertDTOs = append(alertDTOs, &dtos.AlertRuleDTO{
  43. Id: alert.Id,
  44. DashboardId: alert.DashboardId,
  45. PanelId: alert.PanelId,
  46. Query: alert.Query,
  47. QueryRefId: alert.QueryRefId,
  48. WarnLevel: alert.WarnLevel,
  49. CritLevel: alert.CritLevel,
  50. Interval: alert.Interval,
  51. Title: alert.Title,
  52. Description: alert.Description,
  53. QueryRange: alert.QueryRange,
  54. Aggregator: alert.Aggregator,
  55. State: alert.State,
  56. })
  57. }
  58. dashboardsQuery := models.GetDashboardsQuery{
  59. DashboardIds: dashboardIds,
  60. }
  61. if err := bus.Dispatch(&dashboardsQuery); err != nil {
  62. return ApiError(500, "List alerts failed", err)
  63. }
  64. //TODO: should be possible to speed this up with lookup table
  65. for _, alert := range alertDTOs {
  66. for _, dash := range *dashboardsQuery.Result {
  67. if alert.DashboardId == dash.Id {
  68. alert.DashbboardUri = "db/" + dash.Slug
  69. }
  70. }
  71. }
  72. return Json(200, alertDTOs)
  73. }
  74. // GET /api/alerts/:id
  75. func GetAlert(c *middleware.Context) Response {
  76. id := c.ParamsInt64(":alertId")
  77. query := models.GetAlertByIdQuery{Id: id}
  78. if err := bus.Dispatch(&query); err != nil {
  79. return ApiError(500, "List alerts failed", err)
  80. }
  81. return Json(200, &query.Result)
  82. }
  83. // DEL /api/alerts/:id
  84. func DelAlert(c *middleware.Context) Response {
  85. alertId := c.ParamsInt64(":alertId")
  86. if alertId == 0 {
  87. return ApiError(401, "Failed to parse alertid", nil)
  88. }
  89. cmd := models.DeleteAlertCommand{AlertId: alertId}
  90. if err := bus.Dispatch(&cmd); err != nil {
  91. return ApiError(500, "Failed to delete alert", err)
  92. }
  93. var resp = map[string]interface{}{"alertId": alertId}
  94. return Json(200, resp)
  95. }
  96. // GET /api/alerts/state/:id
  97. func GetAlertState(c *middleware.Context) Response {
  98. alertId := c.ParamsInt64(":alertId")
  99. query := models.GetAlertsStateLogCommand{
  100. AlertId: alertId,
  101. }
  102. if err := bus.Dispatch(&query); err != nil {
  103. return ApiError(500, "Failed get alert state log", err)
  104. }
  105. return Json(200, query.Result)
  106. }
  107. // PUT /api/alerts/state/:id
  108. func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
  109. alertId := c.ParamsInt64(":alertId")
  110. if alertId != cmd.AlertId {
  111. return ApiError(401, "Bad Request", nil)
  112. }
  113. query := models.GetAlertByIdQuery{Id: alertId}
  114. if err := bus.Dispatch(&query); err != nil {
  115. return ApiError(500, "Failed to get alertstate", err)
  116. }
  117. if query.Result.OrgId != 0 && query.Result.OrgId != c.OrgId {
  118. return ApiError(500, "Alert not found", nil)
  119. }
  120. if err := bus.Dispatch(&cmd); err != nil {
  121. return ApiError(500, "Failed to set new state", err)
  122. }
  123. return Json(200, cmd.Result)
  124. }
  125. // GET /api/alerts-dashboard/:dashboardId
  126. func GetAlertsForDashboard(c *middleware.Context) Response {
  127. dashboardId := c.ParamsInt64(":dashboardId")
  128. query := &models.GetAlertsForDashboardQuery{
  129. DashboardId: dashboardId,
  130. }
  131. if err := bus.Dispatch(&query); err != nil {
  132. return ApiError(500, "Failed get alert ", err)
  133. }
  134. return Json(200, query.Result)
  135. }
  136. // GET /api/alerts-dashboard/:dashboardId/:panelId
  137. func GetAlertsForPanel(c *middleware.Context) Response {
  138. dashboardId := c.ParamsInt64(":dashboardId")
  139. panelId := c.ParamsInt64(":panelId")
  140. query := &models.GetAlertForPanelQuery{
  141. DashboardId: dashboardId,
  142. PanelId: panelId,
  143. }
  144. if err := bus.Dispatch(&query); err != nil {
  145. return ApiError(500, "Failed get alert ", err)
  146. }
  147. return Json(200, query.Result)
  148. }