annotations.go 7.5 KB

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