annotations.go 7.0 KB

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