annotations.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. Type: c.Query("type"),
  22. }
  23. repo := annotations.GetRepository()
  24. items, err := repo.Find(query)
  25. if err != nil {
  26. return ApiError(500, "Failed to get annotations", err)
  27. }
  28. for _, item := range items {
  29. if item.Email != "" {
  30. item.AvatarUrl = dtos.GetGravatarUrl(item.Email)
  31. }
  32. item.Time = item.Time * 1000
  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 *middleware.Context, cmd dtos.PostAnnotationsCmd) Response {
  43. repo := annotations.GetRepository()
  44. if cmd.Text == "" {
  45. err := &CreateAnnotationError{"text field should not be empty"}
  46. return ApiError(500, "Failed to save annotation", err)
  47. }
  48. item := annotations.Item{
  49. OrgId: c.OrgId,
  50. UserId: c.UserId,
  51. DashboardId: cmd.DashboardId,
  52. PanelId: cmd.PanelId,
  53. Epoch: cmd.Time / 1000,
  54. Text: cmd.Text,
  55. Data: cmd.Data,
  56. Tags: cmd.Tags,
  57. }
  58. if item.Epoch == 0 {
  59. item.Epoch = time.Now().Unix()
  60. }
  61. if err := repo.Save(&item); err != nil {
  62. return ApiError(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 ApiError(500, "Failed set regionId on annotation", err)
  73. }
  74. item.Id = 0
  75. item.Epoch = cmd.TimeEnd / 1000
  76. if err := repo.Save(&item); err != nil {
  77. return ApiError(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 *middleware.Context, cmd dtos.PostGraphiteAnnotationsCmd) Response {
  98. repo := annotations.GetRepository()
  99. if cmd.What == "" {
  100. err := &CreateAnnotationError{"what field should not be empty"}
  101. return ApiError(500, "Failed to save Graphite annotation", err)
  102. }
  103. if cmd.When == 0 {
  104. cmd.When = time.Now().Unix()
  105. }
  106. text := formatGraphiteAnnotation(cmd.What, cmd.Data)
  107. // Support tags in prior to Graphite 0.10.0 format (string of tags separated by space)
  108. var tagsArray []string
  109. switch tags := cmd.Tags.(type) {
  110. case string:
  111. if tags != "" {
  112. tagsArray = strings.Split(tags, " ")
  113. } else {
  114. tagsArray = []string{}
  115. }
  116. case []interface{}:
  117. for _, t := range tags {
  118. if tagStr, ok := t.(string); ok {
  119. tagsArray = append(tagsArray, tagStr)
  120. } else {
  121. err := &CreateAnnotationError{"tag should be a string"}
  122. return ApiError(500, "Failed to save Graphite annotation", err)
  123. }
  124. }
  125. default:
  126. err := &CreateAnnotationError{"unsupported tags format"}
  127. return ApiError(500, "Failed to save Graphite annotation", err)
  128. }
  129. item := annotations.Item{
  130. OrgId: c.OrgId,
  131. UserId: c.UserId,
  132. Epoch: cmd.When,
  133. Text: text,
  134. Tags: tagsArray,
  135. }
  136. if err := repo.Save(&item); err != nil {
  137. return ApiError(500, "Failed to save Graphite annotation", err)
  138. }
  139. return Json(200, util.DynMap{
  140. "message": "Graphite annotation added",
  141. "id": item.Id,
  142. })
  143. }
  144. func UpdateAnnotation(c *middleware.Context, cmd dtos.UpdateAnnotationsCmd) Response {
  145. annotationId := c.ParamsInt64(":annotationId")
  146. repo := annotations.GetRepository()
  147. item := annotations.Item{
  148. OrgId: c.OrgId,
  149. UserId: c.UserId,
  150. Id: annotationId,
  151. Epoch: cmd.Time / 1000,
  152. Text: cmd.Text,
  153. Tags: cmd.Tags,
  154. }
  155. if err := repo.Update(&item); err != nil {
  156. return ApiError(500, "Failed to update annotation", err)
  157. }
  158. if cmd.IsRegion {
  159. itemRight := item
  160. itemRight.RegionId = item.Id
  161. itemRight.Epoch = cmd.TimeEnd / 1000
  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 ApiError(500, "Failed to update annotation for region end time", err)
  167. }
  168. }
  169. return ApiSuccess("Annotation updated")
  170. }
  171. func DeleteAnnotations(c *middleware.Context, cmd dtos.DeleteAnnotationsCmd) Response {
  172. repo := annotations.GetRepository()
  173. err := repo.Delete(&annotations.DeleteParams{
  174. AlertId: cmd.PanelId,
  175. DashboardId: cmd.DashboardId,
  176. PanelId: cmd.PanelId,
  177. })
  178. if err != nil {
  179. return ApiError(500, "Failed to delete annotations", err)
  180. }
  181. return ApiSuccess("Annotations deleted")
  182. }
  183. func DeleteAnnotationById(c *middleware.Context) Response {
  184. repo := annotations.GetRepository()
  185. annotationId := c.ParamsInt64(":annotationId")
  186. err := repo.Delete(&annotations.DeleteParams{
  187. Id: annotationId,
  188. })
  189. if err != nil {
  190. return ApiError(500, "Failed to delete annotation", err)
  191. }
  192. return ApiSuccess("Annotation deleted")
  193. }
  194. func DeleteAnnotationRegion(c *middleware.Context) Response {
  195. repo := annotations.GetRepository()
  196. regionId := c.ParamsInt64(":regionId")
  197. err := repo.Delete(&annotations.DeleteParams{
  198. RegionId: regionId,
  199. })
  200. if err != nil {
  201. return ApiError(500, "Failed to delete annotation region", err)
  202. }
  203. return ApiSuccess("Annotation region deleted")
  204. }