annotations.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. }
  24. repo := annotations.GetRepository()
  25. items, err := repo.Find(query)
  26. if err != nil {
  27. return Error(500, "Failed to get annotations", err)
  28. }
  29. for _, item := range items {
  30. if item.Email != "" {
  31. item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
  32. }
  33. item.Time = item.Time
  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 DeleteAnnotations(c *m.ReqContext, cmd dtos.DeleteAnnotationsCmd) Response {
  173. repo := annotations.GetRepository()
  174. err := repo.Delete(&annotations.DeleteParams{
  175. AlertId: cmd.PanelId,
  176. DashboardId: cmd.DashboardId,
  177. PanelId: cmd.PanelId,
  178. })
  179. if err != nil {
  180. return Error(500, "Failed to delete annotations", err)
  181. }
  182. return Success("Annotations deleted")
  183. }
  184. func DeleteAnnotationByID(c *m.ReqContext) Response {
  185. repo := annotations.GetRepository()
  186. annotationID := c.ParamsInt64(":annotationId")
  187. if resp := canSave(c, repo, annotationID); resp != nil {
  188. return resp
  189. }
  190. err := repo.Delete(&annotations.DeleteParams{
  191. Id: annotationID,
  192. })
  193. if err != nil {
  194. return Error(500, "Failed to delete annotation", err)
  195. }
  196. return Success("Annotation deleted")
  197. }
  198. func DeleteAnnotationRegion(c *m.ReqContext) Response {
  199. repo := annotations.GetRepository()
  200. regionID := c.ParamsInt64(":regionId")
  201. if resp := canSave(c, repo, regionID); resp != nil {
  202. return resp
  203. }
  204. err := repo.Delete(&annotations.DeleteParams{
  205. RegionId: regionID,
  206. })
  207. if err != nil {
  208. return Error(500, "Failed to delete annotation region", err)
  209. }
  210. return Success("Annotation region deleted")
  211. }
  212. func canSaveByDashboardID(c *m.ReqContext, dashboardID int64) (bool, error) {
  213. if dashboardID == 0 && !c.SignedInUser.HasRole(m.ROLE_EDITOR) {
  214. return false, nil
  215. }
  216. if dashboardID > 0 {
  217. guardian := guardian.New(dashboardID, c.OrgId, c.SignedInUser)
  218. if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
  219. return false, err
  220. }
  221. }
  222. return true, nil
  223. }
  224. func canSave(c *m.ReqContext, repo annotations.Repository, annotationID int64) Response {
  225. items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId})
  226. if err != nil || len(items) == 0 {
  227. return Error(500, "Could not find annotation to update", err)
  228. }
  229. dashboardID := items[0].DashboardId
  230. if canSave, err := canSaveByDashboardID(c, dashboardID); err != nil || !canSave {
  231. return dashboardGuardianResponse(err)
  232. }
  233. return nil
  234. }
  235. func canSaveByRegionID(c *m.ReqContext, repo annotations.Repository, regionID int64) Response {
  236. items, err := repo.Find(&annotations.ItemQuery{RegionId: regionID, OrgId: c.OrgId})
  237. if err != nil || len(items) == 0 {
  238. return Error(500, "Could not find annotation to update", err)
  239. }
  240. dashboardID := items[0].DashboardId
  241. if canSave, err := canSaveByDashboardID(c, dashboardID); err != nil || !canSave {
  242. return dashboardGuardianResponse(err)
  243. }
  244. return nil
  245. }