annotations.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. "github.com/grafana/grafana/pkg/middleware"
  8. "github.com/grafana/grafana/pkg/services/annotations"
  9. "github.com/grafana/grafana/pkg/util"
  10. )
  11. func GetAnnotations(c *middleware.Context) Response {
  12. query := &annotations.ItemQuery{
  13. From: c.QueryInt64("from") / 1000,
  14. To: c.QueryInt64("to") / 1000,
  15. OrgId: c.OrgId,
  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. }
  22. repo := annotations.GetRepository()
  23. items, err := repo.Find(query)
  24. if err != nil {
  25. return ApiError(500, "Failed to get annotations", err)
  26. }
  27. for _, item := range items {
  28. if item.Email != "" {
  29. item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
  30. }
  31. item.Time = item.Time * 1000
  32. }
  33. return Json(200, items)
  34. }
  35. type CreateAnnotationError struct {
  36. message string
  37. }
  38. func (e *CreateAnnotationError) Error() string {
  39. return e.message
  40. }
  41. func PostAnnotation(c *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
  42. repo := annotations.GetRepository()
  43. if cmd.Text == "" {
  44. err := &CreateAnnotationError{"text field should not be empty"}
  45. return ApiError(500, "Failed to save annotation", err)
  46. }
  47. item := annotations.Item{
  48. OrgId: c.OrgId,
  49. UserId: c.UserId,
  50. DashboardId: cmd.DashboardId,
  51. PanelId: cmd.PanelId,
  52. Epoch: cmd.Time / 1000,
  53. Text: cmd.Text,
  54. Data: cmd.Data,
  55. Tags: cmd.Tags,
  56. }
  57. if item.Epoch == 0 {
  58. item.Epoch = time.Now().Unix()
  59. }
  60. if err := repo.Save(&item); err != nil {
  61. return ApiError(500, "Failed to save annotation", err)
  62. }
  63. startID := item.Id
  64. // handle regions
  65. if cmd.IsRegion {
  66. item.RegionId = startID
  67. if item.Data == nil {
  68. item.Data = simplejson.New()
  69. }
  70. if err := repo.Update(&item); err != nil {
  71. return ApiError(500, "Failed set regionId on annotation", err)
  72. }
  73. item.Id = 0
  74. item.Epoch = cmd.TimeEnd / 1000
  75. if err := repo.Save(&item); err != nil {
  76. return ApiError(500, "Failed save annotation for region end time", err)
  77. }
  78. return Json(200, util.DynMap{
  79. "message": "Annotation added",
  80. "id": startID,
  81. "endId": item.Id,
  82. })
  83. }
  84. return Json(200, util.DynMap{
  85. "message": "Annotation added",
  86. "id": startID,
  87. })
  88. }
  89. func formatGraphiteAnnotation(what string, data string) string {
  90. text := what
  91. if data != "" {
  92. text = text + "\n" + data
  93. }
  94. return text
  95. }
  96. func PostGraphiteAnnotation(c *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response {
  97. repo := annotations.GetRepository()
  98. if cmd.What == "" {
  99. err := &CreateAnnotationError{"what field should not be empty"}
  100. return ApiError(500, "Failed to save Graphite annotation", err)
  101. }
  102. if cmd.When == 0 {
  103. cmd.When = time.Now().Unix()
  104. }
  105. text := formatGraphiteAnnotation(cmd.What, cmd.Data)
  106. // Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
  107. var tagsArray []string
  108. switch tags := cmd.Tags.(type) {
  109. case string:
  110. if tags != "" {
  111. tagsArray = strings.Split(tags, " ")
  112. } else {
  113. tagsArray = []string{}
  114. }
  115. case []interface{}:
  116. for _, t := range tags {
  117. if tagStr, ok := t.(string); ok {
  118. tagsArray = append(tagsArray, tagStr)
  119. } else {
  120. err := &CreateAnnotationError{"tag should be a string"}
  121. return ApiError(500, "Failed to save Graphite annotation", err)
  122. }
  123. }
  124. default:
  125. err := &CreateAnnotationError{"unsupported tags format"}
  126. return ApiError(500, "Failed to save Graphite annotation", err)
  127. }
  128. item := annotations.Item{
  129. OrgId: c.OrgId,
  130. UserId: c.UserId,
  131. Epoch: cmd.When,
  132. Text: text,
  133. Tags: tagsArray,
  134. }
  135. if err := repo.Save(&item); err != nil {
  136. return ApiError(500, "Failed to save Graphite annotation", err)
  137. }
  138. return Json(200, util.DynMap{
  139. "message": "Graphite annotation added",
  140. "id": item.Id,
  141. })
  142. }
  143. func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {
  144. annotationId := c.ParamsInt64(":annotationId")
  145. repo := annotations.GetRepository()
  146. item := annotations.Item{
  147. OrgId: c.OrgId,
  148. UserId: c.UserId,
  149. Id: annotationId,
  150. Epoch: cmd.Time / 1000,
  151. Text: cmd.Text,
  152. Tags: cmd.Tags,
  153. }
  154. if err := repo.Update(&item); err != nil {
  155. return ApiError(500, "Failed to update annotation", err)
  156. }
  157. if cmd.IsRegion {
  158. itemRight := item
  159. itemRight.RegionId = item.Id
  160. itemRight.Epoch = cmd.TimeEnd / 1000
  161. // We don't know id of region right event, so set it to 0 and find then using query like
  162. // ... WHERE region_id = <item.RegionId> AND id != <item.RegionId> ...
  163. itemRight.Id = 0
  164. if err := repo.Update(&itemRight); err != nil {
  165. return ApiError(500, "Failed to update annotation for region end time", err)
  166. }
  167. }
  168. return ApiSuccess("Annotation updated")
  169. }
  170. func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response {
  171. repo := annotations.GetRepository()
  172. err := repo.Delete(&annotations.DeleteParams{
  173. AlertId: cmd.PanelId,
  174. DashboardId: cmd.DashboardId,
  175. PanelId: cmd.PanelId,
  176. })
  177. if err != nil {
  178. return ApiError(500, "Failed to delete annotations", err)
  179. }
  180. return ApiSuccess("Annotations deleted")
  181. }
  182. func DeleteAnnotationById(c *middleware.Context) Response {
  183. repo := annotations.GetRepository()
  184. annotationId := c.ParamsInt64(":annotationId")
  185. err := repo.Delete(&annotations.DeleteParams{
  186. Id: annotationId,
  187. })
  188. if err != nil {
  189. return ApiError(500, "Failed to delete annotation", err)
  190. }
  191. return ApiSuccess("Annotation deleted")
  192. }
  193. func DeleteAnnotationRegion(c *middleware.Context) Response {
  194. repo := annotations.GetRepository()
  195. regionId := c.ParamsInt64(":regionId")
  196. err := repo.Delete(&annotations.DeleteParams{
  197. RegionId: regionId,
  198. })
  199. if err != nil {
  200. return ApiError(500, "Failed to delete annotation region", err)
  201. }
  202. return ApiSuccess("Annotation region deleted")
  203. }