annotations.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/middleware"
  5. "github.com/grafana/grafana/pkg/services/annotations"
  6. )
  7. func GetAnnotations(c *middleware.Context) Response {
  8. query := &annotations.ItemQuery{
  9. From: c.QueryInt64("from") / 1000,
  10. To: c.QueryInt64("to") / 1000,
  11. Type: annotations.ItemType(c.Query("type")),
  12. OrgId: c.OrgId,
  13. AlertId: c.QueryInt64("alertId"),
  14. DashboardId: c.QueryInt64("dashboardId"),
  15. PanelId: c.QueryInt64("panelId"),
  16. Limit: c.QueryInt64("limit"),
  17. NewState: c.QueryStrings("newState"),
  18. }
  19. repo := annotations.GetRepository()
  20. items, err := repo.Find(query)
  21. if err != nil {
  22. return ApiError(500, "Failed to get annotations", err)
  23. }
  24. result := make([]dtos.Annotation, 0)
  25. for _, item := range items {
  26. result = append(result, dtos.Annotation{
  27. AlertId: item.AlertId,
  28. Time: item.Epoch * 1000,
  29. Data: item.Data,
  30. NewState: item.NewState,
  31. PrevState: item.PrevState,
  32. Text: item.Text,
  33. Metric: item.Metric,
  34. Title: item.Title,
  35. })
  36. }
  37. return Json(200, result)
  38. }
  39. func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response {
  40. repo := annotations.GetRepository()
  41. err := repo.Delete(&annotations.DeleteParams{
  42. AlertId: cmd.PanelId,
  43. DashboardId: cmd.DashboardId,
  44. PanelId: cmd.PanelId,
  45. })
  46. if err != nil {
  47. return ApiError(500, "Failed to delete annotations", err)
  48. }
  49. return ApiSuccess("Annotations deleted")
  50. }