annotations.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. package api
  2. import (
  3. "strings"
  4. "github.com/grafana/grafana/pkg/api/dtos"
  5. m "github.com/grafana/grafana/pkg/models"
  6. "github.com/grafana/grafana/pkg/services/annotations"
  7. "github.com/grafana/grafana/pkg/services/guardian"
  8. "github.com/grafana/grafana/pkg/util"
  9. )
  10. func GetAnnotations(c *m.ReqContext) Response {
  11. query := &annotations.ItemQuery{
  12. From: c.QueryInt64("from"),
  13. To: c.QueryInt64("to"),
  14. OrgId: c.OrgId,
  15. UserId: c.QueryInt64("userId"),
  16. AlertId: c.QueryInt64("alertId"),
  17. DashboardId: c.QueryInt64("dashboardId"),
  18. PanelId: c.QueryInt64("panelId"),
  19. Limit: c.QueryInt64("limit"),
  20. Tags: c.QueryStrings("tags"),
  21. Type: c.Query("type"),
  22. MatchAny: c.QueryBool("matchAny"),
  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. EpochEnd: cmd.TimeEnd,
  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. return JSON(200, util.DynMap{
  67. "message": "Annotation added",
  68. "id": startID,
  69. })
  70. }
  71. func formatGraphiteAnnotation(what string, data string) string {
  72. text := what
  73. if data != "" {
  74. text = text + "\n" + data
  75. }
  76. return text
  77. }
  78. func PostGraphiteAnnotation(c *m.ReqContext, cmd dtos.PostGraphiteAnnotationsCmd) Response {
  79. repo := annotations.GetRepository()
  80. if cmd.What == "" {
  81. err := &CreateAnnotationError{"what field should not be empty"}
  82. return Error(500, "Failed to save Graphite annotation", err)
  83. }
  84. text := formatGraphiteAnnotation(cmd.What, cmd.Data)
  85. // Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
  86. var tagsArray []string
  87. switch tags := cmd.Tags.(type) {
  88. case string:
  89. if tags != "" {
  90. tagsArray = strings.Split(tags, " ")
  91. } else {
  92. tagsArray = []string{}
  93. }
  94. case []interface{}:
  95. for _, t := range tags {
  96. if tagStr, ok := t.(string); ok {
  97. tagsArray = append(tagsArray, tagStr)
  98. } else {
  99. err := &CreateAnnotationError{"tag should be a string"}
  100. return Error(500, "Failed to save Graphite annotation", err)
  101. }
  102. }
  103. default:
  104. err := &CreateAnnotationError{"unsupported tags format"}
  105. return Error(500, "Failed to save Graphite annotation", err)
  106. }
  107. item := annotations.Item{
  108. OrgId: c.OrgId,
  109. UserId: c.UserId,
  110. Epoch: cmd.When * 1000,
  111. Text: text,
  112. Tags: tagsArray,
  113. }
  114. if err := repo.Save(&item); err != nil {
  115. return Error(500, "Failed to save Graphite annotation", err)
  116. }
  117. return JSON(200, util.DynMap{
  118. "message": "Graphite annotation added",
  119. "id": item.Id,
  120. })
  121. }
  122. func UpdateAnnotation(c *m.ReqContext, cmd dtos.UpdateAnnotationsCmd) Response {
  123. annotationID := c.ParamsInt64(":annotationId")
  124. repo := annotations.GetRepository()
  125. if resp := canSave(c, repo, annotationID); resp != nil {
  126. return resp
  127. }
  128. item := annotations.Item{
  129. OrgId: c.OrgId,
  130. UserId: c.UserId,
  131. Id: annotationID,
  132. Epoch: cmd.Time,
  133. EpochEnd: cmd.TimeEnd,
  134. Text: cmd.Text,
  135. Tags: cmd.Tags,
  136. }
  137. if err := repo.Update(&item); err != nil {
  138. return Error(500, "Failed to update annotation", err)
  139. }
  140. return Success("Annotation updated")
  141. }
  142. func PatchAnnotation(c *m.ReqContext, cmd dtos.PatchAnnotationsCmd) Response {
  143. annotationID := c.ParamsInt64(":annotationId")
  144. repo := annotations.GetRepository()
  145. if resp := canSave(c, repo, annotationID); resp != nil {
  146. return resp
  147. }
  148. items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId})
  149. if err != nil || len(items) == 0 {
  150. return Error(404, "Could not find annotation to update", err)
  151. }
  152. existing := annotations.Item{
  153. OrgId: c.OrgId,
  154. UserId: c.UserId,
  155. Id: annotationID,
  156. Epoch: items[0].Time,
  157. EpochEnd: items[0].TimeEnd,
  158. Text: items[0].Text,
  159. Tags: items[0].Tags,
  160. }
  161. if cmd.Tags != nil {
  162. existing.Tags = cmd.Tags
  163. }
  164. if cmd.Text != "" && cmd.Text != existing.Text {
  165. existing.Text = cmd.Text
  166. }
  167. if cmd.Time > 0 && cmd.Time != existing.Epoch {
  168. existing.Epoch = cmd.Time
  169. }
  170. if cmd.TimeEnd > 0 && cmd.TimeEnd != existing.EpochEnd {
  171. existing.EpochEnd = cmd.TimeEnd
  172. }
  173. if err := repo.Update(&existing); err != nil {
  174. return Error(500, "Failed to update annotation", err)
  175. }
  176. return Success("Annotation patched")
  177. }
  178. func DeleteAnnotations(c *m.ReqContext, cmd dtos.DeleteAnnotationsCmd) Response {
  179. repo := annotations.GetRepository()
  180. err := repo.Delete(&annotations.DeleteParams{
  181. OrgId: c.OrgId,
  182. Id: cmd.AnnotationId,
  183. DashboardId: cmd.DashboardId,
  184. PanelId: cmd.PanelId,
  185. })
  186. if err != nil {
  187. return Error(500, "Failed to delete annotations", err)
  188. }
  189. return Success("Annotations deleted")
  190. }
  191. func DeleteAnnotationByID(c *m.ReqContext) Response {
  192. repo := annotations.GetRepository()
  193. annotationID := c.ParamsInt64(":annotationId")
  194. if resp := canSave(c, repo, annotationID); resp != nil {
  195. return resp
  196. }
  197. err := repo.Delete(&annotations.DeleteParams{
  198. OrgId: c.OrgId,
  199. Id: annotationID,
  200. })
  201. if err != nil {
  202. return Error(500, "Failed to delete annotation", err)
  203. }
  204. return Success("Annotation deleted")
  205. }
  206. func canSaveByDashboardID(c *m.ReqContext, dashboardID int64) (bool, error) {
  207. if dashboardID == 0 && !c.SignedInUser.HasRole(m.ROLE_EDITOR) {
  208. return false, nil
  209. }
  210. if dashboardID != 0 {
  211. guard := guardian.New(dashboardID, c.OrgId, c.SignedInUser)
  212. if canEdit, err := guard.CanEdit(); err != nil || !canEdit {
  213. return false, err
  214. }
  215. }
  216. return true, nil
  217. }
  218. func canSave(c *m.ReqContext, repo annotations.Repository, annotationID int64) Response {
  219. items, err := repo.Find(&annotations.ItemQuery{AnnotationId: annotationID, OrgId: c.OrgId})
  220. if err != nil || len(items) == 0 {
  221. return Error(500, "Could not find annotation to update", err)
  222. }
  223. dashboardID := items[0].DashboardId
  224. if canSave, err := canSaveByDashboardID(c, dashboardID); err != nil || !canSave {
  225. return dashboardGuardianResponse(err)
  226. }
  227. return nil
  228. }