annotations.go 8.5 KB

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