annotations.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. MatchAny: c.QueryBool("matchAny"),
  24. }
  25. repo := annotations.GetRepository()
  26. items, err := repo.Find(query)
  27. if err != nil {
  28. return Error(500, "Failed to get annotations", err)
  29. }
  30. for _, item := range items {
  31. if item.Email != "" {
  32. item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
  33. }
  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. OrgId: c.OrgId,
  176. Id: cmd.AnnotationId,
  177. RegionId: cmd.RegionId,
  178. DashboardId: cmd.DashboardId,
  179. PanelId: cmd.PanelId,
  180. })
  181. if err != nil {
  182. return Error(500, "Failed to delete annotations", err)
  183. }
  184. return Success("Annotations deleted")
  185. }
  186. func DeleteAnnotationByID(c *m.ReqContext) Response {
  187. repo := annotations.GetRepository()
  188. annotationID := c.ParamsInt64(":annotationId")
  189. if resp := canSave(c, repo, annotationID); resp != nil {
  190. return resp
  191. }
  192. err := repo.Delete(&annotations.DeleteParams{
  193. OrgId: c.OrgId,
  194. Id: annotationID,
  195. })
  196. if err != nil {
  197. return Error(500, "Failed to delete annotation", err)
  198. }
  199. return Success("Annotation deleted")
  200. }
  201. func DeleteAnnotationRegion(c *m.ReqContext) Response {
  202. repo := annotations.GetRepository()
  203. regionID := c.ParamsInt64(":regionId")
  204. if resp := canSave(c, repo, regionID); resp != nil {
  205. return resp
  206. }
  207. err := repo.Delete(&annotations.DeleteParams{
  208. OrgId: c.OrgId,
  209. RegionId: regionID,
  210. })
  211. if err != nil {
  212. return Error(500, "Failed to delete annotation region", err)
  213. }
  214. return Success("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. guard := guardian.New(dashboardID, c.OrgId, c.SignedInUser)
  222. if canEdit, err := guard.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 Error(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. }