dashboard.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package api
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path"
  6. "strings"
  7. "github.com/grafana/grafana/pkg/api/dtos"
  8. "github.com/grafana/grafana/pkg/bus"
  9. "github.com/grafana/grafana/pkg/metrics"
  10. "github.com/grafana/grafana/pkg/middleware"
  11. m "github.com/grafana/grafana/pkg/models"
  12. "github.com/grafana/grafana/pkg/services/search"
  13. "github.com/grafana/grafana/pkg/setting"
  14. "github.com/grafana/grafana/pkg/util"
  15. )
  16. func isDasboardStarredByUser(c *middleware.Context, dashId int64) (bool, error) {
  17. if !c.IsSignedIn {
  18. return false, nil
  19. }
  20. query := m.IsStarredByUserQuery{UserId: c.UserId, DashboardId: dashId}
  21. if err := bus.Dispatch(&query); err != nil {
  22. return false, err
  23. }
  24. return query.Result, nil
  25. }
  26. func GetDashboard(c *middleware.Context) {
  27. metrics.M_Api_Dashboard_Get.Inc(1)
  28. slug := strings.ToLower(c.Params(":slug"))
  29. query := m.GetDashboardQuery{Slug: slug, OrgId: c.OrgId}
  30. err := bus.Dispatch(&query)
  31. if err != nil {
  32. c.JsonApiErr(404, "Dashboard not found", nil)
  33. return
  34. }
  35. isStarred, err := isDasboardStarredByUser(c, query.Result.Id)
  36. if err != nil {
  37. c.JsonApiErr(500, "Error while checking if dashboard was starred by user", err)
  38. return
  39. }
  40. dash := query.Result
  41. dto := dtos.DashboardFullWithMeta{
  42. Dashboard: dash.Data,
  43. Meta: dtos.DashboardMeta{
  44. IsStarred: isStarred,
  45. Slug: slug,
  46. Type: m.DashTypeDB,
  47. CanStar: c.IsSignedIn,
  48. CanSave: c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR,
  49. CanEdit: c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR || c.OrgRole == m.ROLE_READ_ONLY_EDITOR,
  50. },
  51. }
  52. c.JSON(200, dto)
  53. }
  54. func DeleteDashboard(c *middleware.Context) {
  55. slug := c.Params(":slug")
  56. query := m.GetDashboardQuery{Slug: slug, OrgId: c.OrgId}
  57. if err := bus.Dispatch(&query); err != nil {
  58. c.JsonApiErr(404, "Dashboard not found", nil)
  59. return
  60. }
  61. cmd := m.DeleteDashboardCommand{Slug: slug, OrgId: c.OrgId}
  62. if err := bus.Dispatch(&cmd); err != nil {
  63. c.JsonApiErr(500, "Failed to delete dashboard", err)
  64. return
  65. }
  66. var resp = map[string]interface{}{"title": query.Result.Title}
  67. c.JSON(200, resp)
  68. }
  69. func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
  70. cmd.OrgId = c.OrgId
  71. dash := cmd.GetDashboardModel()
  72. if dash.Id == 0 {
  73. limitReached, err := middleware.QuotaReached(c, "dashboard")
  74. if err != nil {
  75. c.JsonApiErr(500, "failed to get quota", err)
  76. return
  77. }
  78. if limitReached {
  79. c.JsonApiErr(403, "Quota reached", nil)
  80. return
  81. }
  82. }
  83. err := bus.Dispatch(&cmd)
  84. if err != nil {
  85. if err == m.ErrDashboardWithSameNameExists {
  86. c.JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
  87. return
  88. }
  89. if err == m.ErrDashboardVersionMismatch {
  90. c.JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
  91. return
  92. }
  93. if err == m.ErrDashboardNotFound {
  94. c.JSON(404, util.DynMap{"status": "not-found", "message": err.Error()})
  95. return
  96. }
  97. c.JsonApiErr(500, "Failed to save dashboard", err)
  98. return
  99. }
  100. metrics.M_Api_Dashboard_Post.Inc(1)
  101. c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version})
  102. }
  103. func GetHomeDashboard(c *middleware.Context) {
  104. filePath := path.Join(setting.StaticRootPath, "dashboards/home.json")
  105. file, err := os.Open(filePath)
  106. if err != nil {
  107. c.JsonApiErr(500, "Failed to load home dashboard", err)
  108. return
  109. }
  110. dash := dtos.DashboardFullWithMeta{}
  111. dash.Meta.IsHome = true
  112. jsonParser := json.NewDecoder(file)
  113. if err := jsonParser.Decode(&dash.Dashboard); err != nil {
  114. c.JsonApiErr(500, "Failed to load home dashboard", err)
  115. return
  116. }
  117. c.JSON(200, &dash)
  118. }
  119. func GetDashboardFromJsonFile(c *middleware.Context) {
  120. file := c.Params(":file")
  121. dashboard := search.GetDashboardFromJsonIndex(file)
  122. if dashboard == nil {
  123. c.JsonApiErr(404, "Dashboard not found", nil)
  124. return
  125. }
  126. dash := dtos.DashboardFullWithMeta{Dashboard: dashboard.Data}
  127. dash.Meta.Type = m.DashTypeJson
  128. c.JSON(200, &dash)
  129. }
  130. func GetDashboardTags(c *middleware.Context) {
  131. query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
  132. err := bus.Dispatch(&query)
  133. if err != nil {
  134. c.JsonApiErr(500, "Failed to get tags from database", err)
  135. return
  136. }
  137. c.JSON(200, query.Result)
  138. }