alerting.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(":id")
  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. })
  56. }
  57. dashboardsQuery := models.GetDashboardsQuery{
  58. DashboardIds: dashboardIds,
  59. }
  60. if err := bus.Dispatch(&dashboardsQuery); err != nil {
  61. return ApiError(500, "List alerts failed", err)
  62. }
  63. //TODO: should be possible to speed this up with lookup table
  64. for _, alert := range alertDTOs {
  65. for _, dash := range *dashboardsQuery.Result {
  66. if alert.DashboardId == dash.Id {
  67. alert.DashbboardUri = "db/" + dash.Slug
  68. }
  69. }
  70. }
  71. return Json(200, alertDTOs)
  72. }
  73. // GET /api/alerts/:id
  74. func GetAlert(c *middleware.Context) Response {
  75. id := c.ParamsInt64(":id")
  76. query := models.GetAlertByIdQuery{Id: id}
  77. if err := bus.Dispatch(&query); err != nil {
  78. return ApiError(500, "List alerts failed", err)
  79. }
  80. return Json(200, &query.Result)
  81. }
  82. // PUT /api/alerts/state/:id
  83. func PutAlertState(c *middleware.Context, cmd models.UpdateAlertStateCommand) Response {
  84. alertId := c.ParamsInt64(":alertId")
  85. if alertId != cmd.AlertId {
  86. return ApiError(401, "Bad Request", nil)
  87. }
  88. query := models.GetAlertByIdQuery{Id: alertId}
  89. if err := bus.Dispatch(&query); err != nil {
  90. return ApiError(500, "Failed to get alertstate", err)
  91. }
  92. if query.Result.OrgId != 0 && query.Result.OrgId != c.OrgId {
  93. return ApiError(500, "Alert not found", nil)
  94. }
  95. if err := bus.Dispatch(&cmd); err != nil {
  96. return ApiError(500, "Failed to set new state", err)
  97. }
  98. return Json(200, cmd.Result)
  99. }