annotations.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package api
  2. import (
  3. "github.com/grafana/grafana/pkg/api/dtos"
  4. "github.com/grafana/grafana/pkg/components/simplejson"
  5. "github.com/grafana/grafana/pkg/middleware"
  6. "github.com/grafana/grafana/pkg/services/annotations"
  7. )
  8. func GetAnnotations(c *middleware.Context) Response {
  9. query := &annotations.ItemQuery{
  10. From: c.QueryInt64("from") / 1000,
  11. To: c.QueryInt64("to") / 1000,
  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. Tags: c.QueryStrings("tags"),
  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. for _, item := range items {
  25. if item.Email != "" {
  26. item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
  27. }
  28. item.Time = item.Time * 1000
  29. }
  30. return Json(200, items)
  31. }
  32. func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
  33. repo := annotations.GetRepository()
  34. item := annotations.Item{
  35. OrgId: c.OrgId,
  36. UserId: c.UserId,
  37. DashboardId: cmd.DashboardId,
  38. PanelId: cmd.PanelId,
  39. Epoch: cmd.Time / 1000,
  40. Text: cmd.Text,
  41. Data: cmd.Data,
  42. Tags: cmd.Tags,
  43. }
  44. if err := repo.Save(&item); err != nil {
  45. return ApiError(500, "Failed to save annotation", err)
  46. }
  47. // handle regions
  48. if cmd.IsRegion {
  49. item.RegionId = item.Id
  50. if item.Data == nil {
  51. item.Data = simplejson.New()
  52. }
  53. if err := repo.Update(&item); err != nil {
  54. return ApiError(500, "Failed set regionId on annotation", err)
  55. }
  56. item.Id = 0
  57. item.Epoch = cmd.TimeEnd / 1000
  58. if err := repo.Save(&item); err != nil {
  59. return ApiError(500, "Failed save annotation for region end time", err)
  60. }
  61. }
  62. return ApiSuccess("Annotation added")
  63. }
  64. func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {
  65. annotationId := c.ParamsInt64(":annotationId")
  66. repo := annotations.GetRepository()
  67. item := annotations.Item{
  68. OrgId: c.OrgId,
  69. UserId: c.UserId,
  70. Id: annotationId,
  71. Epoch: cmd.Time / 1000,
  72. Text: cmd.Text,
  73. Tags: cmd.Tags,
  74. }
  75. if err := repo.Update(&item); err != nil {
  76. return ApiError(500, "Failed to update annotation", err)
  77. }
  78. if cmd.IsRegion {
  79. itemRight := item
  80. itemRight.RegionId = item.Id
  81. itemRight.Epoch = cmd.TimeEnd / 1000
  82. // We don't know id of region right event, so set it to 0 and find then using query like
  83. // ... WHERE region_id = <item.RegionId> AND id != <item.RegionId> ...
  84. itemRight.Id = 0
  85. if err := repo.Update(&itemRight); err != nil {
  86. return ApiError(500, "Failed to update annotation for region end time", err)
  87. }
  88. }
  89. return ApiSuccess("Annotation updated")
  90. }
  91. func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response {
  92. repo := annotations.GetRepository()
  93. err := repo.Delete(&annotations.DeleteParams{
  94. AlertId: cmd.PanelId,
  95. DashboardId: cmd.DashboardId,
  96. PanelId: cmd.PanelId,
  97. })
  98. if err != nil {
  99. return ApiError(500, "Failed to delete annotations", err)
  100. }
  101. return ApiSuccess("Annotations deleted")
  102. }
  103. func DeleteAnnotationById(c *middleware.Context) Response {
  104. repo := annotations.GetRepository()
  105. annotationId := c.ParamsInt64(":annotationId")
  106. err := repo.Delete(&annotations.DeleteParams{
  107. Id: annotationId,
  108. })
  109. if err != nil {
  110. return ApiError(500, "Failed to delete annotation", err)
  111. }
  112. return ApiSuccess("Annotation deleted")
  113. }
  114. func DeleteAnnotationRegion(c *middleware.Context) Response {
  115. repo := annotations.GetRepository()
  116. regionId := c.ParamsInt64(":regionId")
  117. err := repo.Delete(&annotations.DeleteParams{
  118. RegionId: regionId,
  119. })
  120. if err != nil {
  121. return ApiError(500, "Failed to delete annotation region", err)
  122. }
  123. return ApiSuccess("Annotation region deleted")
  124. }