annotations.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package api
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/middleware"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/annotations"
  10. "github.com/grafana/grafana/pkg/services/guardian"
  11. "github.com/grafana/grafana/pkg/util"
  12. )
  13. func GetAnnotations(c *middleware.Context) Response {
  14. query := &annotations.ItemQuery{
  15. From: c.QueryInt64("from") / 1000,
  16. To: c.QueryInt64("to") / 1000,
  17. OrgId: c.OrgId,
  18. AlertId: c.QueryInt64("alertId"),
  19. DashboardId: c.QueryInt64("dashboardId"),
  20. PanelId: c.QueryInt64("panelId"),
  21. Limit: c.QueryInt64("limit"),
  22. Tags: c.QueryStrings("tags"),
  23. Type: c.Query("type"),
  24. }
  25. repo := annotations.GetRepository()
  26. items, err := repo.Find(query)
  27. if err != nil {
  28. return ApiError(500, "Failed to get annotations", err)
  29. }
  30. for _, item := range items {
  31. if item.Email != "" {
  32. item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
  33. }
  34. item.Time = item.Time * 1000
  35. }
  36. return Json(200, items)
  37. }
  38. type CreateAnnotationError struct {
  39. message string
  40. }
  41. func (e *CreateAnnotationError) Error() string {
  42. return e.message
  43. }
  44. func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
  45. if canSave, err := canSaveByDashboardId(c, cmd.DashboardId); err != nil || !canSave {
  46. return dashboardGuardianResponse(err)
  47. }
  48. repo := annotations.GetRepository()
  49. if cmd.Text == "" {
  50. err := &CreateAnnotationError{"text field should not be empty"}
  51. return ApiError(500, "Failed to save annotation", err)
  52. }
  53. item := annotations.Item{
  54. OrgId: c.OrgId,
  55. UserId: c.UserId,
  56. DashboardId: cmd.DashboardId,
  57. PanelId: cmd.PanelId,
  58. Epoch: cmd.Time / 1000,
  59. Text: cmd.Text,
  60. Data: cmd.Data,
  61. Tags: cmd.Tags,
  62. }
  63. if item.Epoch == 0 {
  64. item.Epoch = time.Now().Unix()
  65. }
  66. if err := repo.Save(&item); err != nil {
  67. return ApiError(500, "Failed to save annotation", err)
  68. }
  69. startID := item.Id
  70. // handle regions
  71. if cmd.IsRegion {
  72. item.RegionId = startID
  73. if item.Data == nil {
  74. item.Data = simplejson.New()
  75. }
  76. if err := repo.Update(&item); err != nil {
  77. return ApiError(500, "Failed set regionId on annotation", err)
  78. }
  79. item.Id = 0
  80. item.Epoch = cmd.TimeEnd / 1000
  81. if err := repo.Save(&item); err != nil {
  82. return ApiError(500, "Failed save annotation for region end time", err)
  83. }
  84. return Json(200, util.DynMap{
  85. "message": "Annotation added",
  86. "id": startID,
  87. "endId": item.Id,
  88. })
  89. }
  90. return Json(200, util.DynMap{
  91. "message": "Annotation added",
  92. "id": startID,
  93. })
  94. }
  95. func formatGraphiteAnnotation(what string, data string) string {
  96. text := what
  97. if data != "" {
  98. text = text + "\n" + data
  99. }
  100. return text
  101. }
  102. func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response {
  103. repo := annotations.GetRepository()
  104. if cmd.What == "" {
  105. err := &CreateAnnotationError{"what field should not be empty"}
  106. return ApiError(500, "Failed to save Graphite annotation", err)
  107. }
  108. if cmd.When == 0 {
  109. cmd.When = time.Now().Unix()
  110. }
  111. text := formatGraphiteAnnotation(cmd.What, cmd.Data)
  112. // Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
  113. var tagsArray []string
  114. switch tags := cmd.Tags.(type) {
  115. case string:
  116. if tags != "" {
  117. tagsArray = strings.Split(tags, " ")
  118. } else {
  119. tagsArray = []string{}
  120. }
  121. case []interface{}:
  122. for _, t := range tags {
  123. if tagStr, ok := t.(string); ok {
  124. tagsArray = append(tagsArray, tagStr)
  125. } else {
  126. err := &CreateAnnotationError{"tag should be a string"}
  127. return ApiError(500, "Failed to save Graphite annotation", err)
  128. }
  129. }
  130. default:
  131. err := &CreateAnnotationError{"unsupported tags format"}
  132. return ApiError(500, "Failed to save Graphite annotation", err)
  133. }
  134. item := annotations.Item{
  135. OrgId: c.OrgId,
  136. UserId: c.UserId,
  137. Epoch: cmd.When,
  138. Text: text,
  139. Tags: tagsArray,
  140. }
  141. if err := repo.Save(&item); err != nil {
  142. return ApiError(500, "Failed to save Graphite annotation", err)
  143. }
  144. return Json(200, util.DynMap{
  145. "message": "Graphite annotation added",
  146. "id": item.Id,
  147. })
  148. }
  149. func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {
  150. annotationId := c.ParamsInt64(":annotationId")
  151. repo := annotations.GetRepository()
  152. if resp := canSave(c, repo, annotationId); resp != nil {
  153. return resp
  154. }
  155. item := annotations.Item{
  156. OrgId: c.OrgId,
  157. UserId: c.UserId,
  158. Id: annotationId,
  159. Epoch: cmd.Time / 1000,
  160. Text: cmd.Text,
  161. Tags: cmd.Tags,
  162. }
  163. if err := repo.Update(&item); err != nil {
  164. return ApiError(500, "Failed to update annotation", err)
  165. }
  166. if cmd.IsRegion {
  167. itemRight := item
  168. itemRight.RegionId = item.Id
  169. itemRight.Epoch = cmd.TimeEnd / 1000
  170. // We don't know id of region right event, so set it to 0 and find then using query like
  171. // ... WHERE region_id = <item.RegionId> AND id != <item.RegionId> ...
  172. itemRight.Id = 0
  173. if err := repo.Update(&itemRight); err != nil {
  174. return ApiError(500, "Failed to update annotation for region end time", err)
  175. }
  176. }
  177. return ApiSuccess("Annotation updated")
  178. }
  179. func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response {
  180. repo := annotations.GetRepository()
  181. err := repo.Delete(&annotations.DeleteParams{
  182. AlertId: cmd.PanelId,
  183. DashboardId: cmd.DashboardId,
  184. PanelId: cmd.PanelId,
  185. })
  186. if err != nil {
  187. return ApiError(500, "Failed to delete annotations", err)
  188. }
  189. return ApiSuccess("Annotations deleted")
  190. }
  191. func DeleteAnnotationById(c *middleware.Context) Response {
  192. repo := annotations.GetRepository()
  193. annotationId := c.ParamsInt64(":annotationId")
  194. if resp := canSave(c, repo, annotationId); resp != nil {
  195. return resp
  196. }
  197. err := repo.Delete(&annotations.DeleteParams{
  198. Id: annotationId,
  199. })
  200. if err != nil {
  201. return ApiError(500, "Failed to delete annotation", err)
  202. }
  203. return ApiSuccess("Annotation deleted")
  204. }
  205. func DeleteAnnotationRegion(c *middleware.Context) Response {
  206. repo := annotations.GetRepository()
  207. regionId := c.ParamsInt64(":regionId")
  208. if resp := canSave(c, repo, regionId); resp != nil {
  209. return resp
  210. }
  211. err := repo.Delete(&annotations.DeleteParams{
  212. RegionId: regionId,
  213. })
  214. if err != nil {
  215. return ApiError(500, "Failed to delete annotation region", err)
  216. }
  217. return ApiSuccess("Annotation region deleted")
  218. }
  219. func canSaveByDashboardId(c *middleware.Context, dashboardId int64) (bool, error) {
  220. if dashboardId == 0 && !c.SignedInUser.HasRole(m.ROLE_EDITOR) {
  221. return false, nil
  222. }
  223. if dashboardId > 0 {
  224. guardian := guardian.New(dashboardId, c.OrgId, c.SignedInUser)
  225. if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
  226. return false, err
  227. }
  228. }
  229. return true, nil
  230. }
  231. func canSave(c *middleware.Context, repo annotations.Repository, annotationId int64) Response {
  232. items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationId, OrgId: c.OrgId})
  233. if err != nil || len(items) == 0 {
  234. return ApiError(500, "Could not find annotation to update", err)
  235. }
  236. dashboardId := items[0].DashboardId
  237. if canSave, err := canSaveByDashboardId(c, dashboardId); err != nil || !canSave {
  238. return dashboardGuardianResponse(err)
  239. }
  240. return nil
  241. }
  242. func canSaveByRegionId(c *middleware.Context, repo annotations.Repository, regionId int64) Response {
  243. items, err := repo.Find(&annotations.ItemQuery{RegionId: regionId, OrgId: c.OrgId})
  244. if err != nil || len(items) == 0 {
  245. return ApiError(500, "Could not find annotation to update", err)
  246. }
  247. dashboardId := items[0].DashboardId
  248. if canSave, err := canSaveByDashboardId(c, dashboardId); err != nil || !canSave {
  249. return dashboardGuardianResponse(err)
  250. }
  251. return nil
  252. }