annotations.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 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
  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. if cmd.DashboardId == 0 {
  53. err := &CreateAnnotationError{"Missing DashboardID"}
  54. return ApiError(500, "Failed to save annotation", err)
  55. }
  56. item := annotations.Item{
  57. OrgId: c.OrgId,
  58. UserId: c.UserId,
  59. DashboardId: cmd.DashboardId,
  60. PanelId: cmd.PanelId,
  61. Epoch: cmd.Time,
  62. Text: cmd.Text,
  63. Data: cmd.Data,
  64. Tags: cmd.Tags,
  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
  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 *m.ReqContext, 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. text := formatGraphiteAnnotation(cmd.What, cmd.Data)
  109. // Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
  110. var tagsArray []string
  111. switch tags := cmd.Tags.(type) {
  112. case string:
  113. if tags != "" {
  114. tagsArray = strings.Split(tags, " ")
  115. } else {
  116. tagsArray = []string{}
  117. }
  118. case []interface{}:
  119. for _, t := range tags {
  120. if tagStr, ok := t.(string); ok {
  121. tagsArray = append(tagsArray, tagStr)
  122. } else {
  123. err := &CreateAnnotationError{"tag should be a string"}
  124. return ApiError(500, "Failed to save Graphite annotation", err)
  125. }
  126. }
  127. default:
  128. err := &CreateAnnotationError{"unsupported tags format"}
  129. return ApiError(500, "Failed to save Graphite annotation", err)
  130. }
  131. item := annotations.Item{
  132. OrgId: c.OrgId,
  133. UserId: c.UserId,
  134. Epoch: cmd.When,
  135. Text: text,
  136. Tags: tagsArray,
  137. }
  138. if err := repo.Save(&item); err != nil {
  139. return ApiError(500, "Failed to save Graphite annotation", err)
  140. }
  141. return Json(200, util.DynMap{
  142. "message": "Graphite annotation added",
  143. "id": item.Id,
  144. })
  145. }
  146. func UpdateAnnotation(c *m.ReqContext, cmd dtos.UpdateAnnotationsCmd) Response {
  147. annotationID := c.ParamsInt64(":annotationId")
  148. repo := annotations.GetRepository()
  149. if resp := canSave(c, repo, annotationID); resp != nil {
  150. return resp
  151. }
  152. item := annotations.Item{
  153. OrgId: c.OrgId,
  154. UserId: c.UserId,
  155. Id: annotationID,
  156. Epoch: cmd.Time,
  157. Text: cmd.Text,
  158. Tags: cmd.Tags,
  159. }
  160. if err := repo.Update(&item); err != nil {
  161. return ApiError(500, "Failed to update annotation", err)
  162. }
  163. if cmd.IsRegion {
  164. itemRight := item
  165. itemRight.RegionId = item.Id
  166. itemRight.Epoch = cmd.TimeEnd
  167. // We don't know id of region right event, so set it to 0 and find then using query like
  168. // ... WHERE region_id = <item.RegionId> AND id != <item.RegionId> ...
  169. itemRight.Id = 0
  170. if err := repo.Update(&itemRight); err != nil {
  171. return ApiError(500, "Failed to update annotation for region end time", err)
  172. }
  173. }
  174. return ApiSuccess("Annotation updated")
  175. }
  176. func DeleteAnnotations(c *m.ReqContext, cmd dtos.DeleteAnnotationsCmd) Response {
  177. repo := annotations.GetRepository()
  178. err := repo.Delete(&annotations.DeleteParams{
  179. AlertId: cmd.PanelId,
  180. DashboardId: cmd.DashboardId,
  181. PanelId: cmd.PanelId,
  182. })
  183. if err != nil {
  184. return ApiError(500, "Failed to delete annotations", err)
  185. }
  186. return ApiSuccess("Annotations deleted")
  187. }
  188. func DeleteAnnotationById(c *m.ReqContext) Response {
  189. repo := annotations.GetRepository()
  190. annotationId := c.ParamsInt64(":annotationId")
  191. if resp := canSave(c, repo, annotationId); resp != nil {
  192. return resp
  193. }
  194. err := repo.Delete(&annotations.DeleteParams{
  195. Id: annotationId,
  196. })
  197. if err != nil {
  198. return ApiError(500, "Failed to delete annotation", err)
  199. }
  200. return ApiSuccess("Annotation deleted")
  201. }
  202. func DeleteAnnotationRegion(c *m.ReqContext) Response {
  203. repo := annotations.GetRepository()
  204. regionID := c.ParamsInt64(":regionId")
  205. if resp := canSave(c, repo, regionID); resp != nil {
  206. return resp
  207. }
  208. err := repo.Delete(&annotations.DeleteParams{
  209. RegionId: regionID,
  210. })
  211. if err != nil {
  212. return ApiError(500, "Failed to delete annotation region", err)
  213. }
  214. return ApiSuccess("Annotation region deleted")
  215. }
  216. func canSaveByDashboardID(c *m.ReqContext, dashboardID int64) (bool, error) {
  217. if dashboardID == 0 && !c.SignedInUser.HasRole(m.ROLE_EDITOR) {
  218. return false, nil
  219. }
  220. if dashboardID > 0 {
  221. guardian := guardian.New(dashboardID, c.OrgId, c.SignedInUser)
  222. if canEdit, err := guardian.CanEdit(); err != nil || !canEdit {
  223. return false, err
  224. }
  225. }
  226. return true, nil
  227. }
  228. func canSave(c *m.ReqContext, repo annotations.Repository, annotationId int64) Response {
  229. items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationId, OrgId: c.OrgId})
  230. if err != nil || len(items) == 0 {
  231. return ApiError(500, "Could not find annotation to update", err)
  232. }
  233. dashboardID := items[0].DashboardId
  234. if canSave, err := canSaveByDashboardID(c, dashboardID); err != nil || !canSave {
  235. return dashboardGuardianResponse(err)
  236. }
  237. return nil
  238. }
  239. func canSaveByRegionID(c *m.ReqContext, repo annotations.Repository, regionID int64) Response {
  240. items, err := repo.Find(&annotations.ItemQuery{RegionId: regionID, OrgId: c.OrgId})
  241. if err != nil || len(items) == 0 {
  242. return ApiError(500, "Could not find annotation to update", err)
  243. }
  244. dashboardID := items[0].DashboardId
  245. if canSave, err := canSaveByDashboardID(c, dashboardID); err != nil || !canSave {
  246. return dashboardGuardianResponse(err)
  247. }
  248. return nil
  249. }