alerting.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/bus"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. "github.com/grafana/grafana/pkg/models"
  6. )
  7. func ValidateOrgAlert(c *middleware.Context) {
  8. id := c.ParamsInt64(":id")
  9. query := models.GetAlertById{Id: id}
  10. if err := bus.Dispatch(&query); err != nil {
  11. c.JsonApiErr(404, "Alert not found", nil)
  12. return
  13. }
  14. if c.OrgId != query.Result.OrgId {
  15. c.JsonApiErr(403, "You are not allowed to edit/view alert", nil)
  16. return
  17. }
  18. }
  19. // GET /api/alert_rule
  20. func GetAlertChanges(c *middleware.Context) Response {
  21. query := models.GetAlertChangesQuery{
  22. OrgId: c.OrgId,
  23. }
  24. if err := bus.Dispatch(&query); err != nil {
  25. return ApiError(500, "List alerts failed", err)
  26. }
  27. return Json(200, query.Result)
  28. }
  29. // GET /api/alert_rule
  30. func GetAlerts(c *middleware.Context) Response {
  31. query := models.GetAlertsQuery{
  32. OrgId: c.OrgId,
  33. }
  34. if err := bus.Dispatch(&query); err != nil {
  35. return ApiError(500, "List alerts failed", err)
  36. }
  37. return Json(200, query.Result)
  38. }
  39. // GET /api/alert_rule/:id
  40. func GetAlert(c *middleware.Context) Response {
  41. id := c.ParamsInt64(":id")
  42. query := models.GetAlertById{Id: id}
  43. if err := bus.Dispatch(&query); err != nil {
  44. return ApiError(500, "List alerts failed", err)
  45. }
  46. return Json(200, &query.Result)
  47. }