annotations.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. PanelId: item.PanelId,
  36. })
  37. }
  38. return Json(200, result)
  39. }
  40. func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
  41. repo := annotations.GetRepository()
  42. item := annotations.Item{
  43. OrgId: c.OrgId,
  44. DashboardId: cmd.DashboardId,
  45. PanelId: cmd.PanelId,
  46. Epoch: cmd.Time / 1000,
  47. Title: cmd.Title,
  48. Text: cmd.Text,
  49. CategoryId: cmd.CategoryId,
  50. Type: annotations.EventType,
  51. }
  52. err := repo.Save(&item)
  53. if err != nil {
  54. return ApiError(500, "Failed to save annotation", err)
  55. }
  56. return ApiSuccess("Annotation added")
  57. }
  58. func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response {
  59. repo := annotations.GetRepository()
  60. err := repo.Delete(&annotations.DeleteParams{
  61. AlertId: cmd.PanelId,
  62. DashboardId: cmd.DashboardId,
  63. PanelId: cmd.PanelId,
  64. })
  65. if err != nil {
  66. return ApiError(500, "Failed to delete annotations", err)
  67. }
  68. return ApiSuccess("Annotations deleted")
  69. }