dashboard.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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: canEditDashboard(c.OrgRole),
  50. Created: dash.Created,
  51. Updated: dash.Updated,
  52. CreatedBy: dash.CreatedBy,
  53. UpdatedBy: dash.UpdatedBy,
  54. },
  55. }
  56. c.JSON(200, dto)
  57. }
  58. func DeleteDashboard(c *middleware.Context) {
  59. slug := c.Params(":slug")
  60. query := m.GetDashboardQuery{Slug: slug, OrgId: c.OrgId}
  61. if err := bus.Dispatch(&query); err != nil {
  62. c.JsonApiErr(404, "Dashboard not found", nil)
  63. return
  64. }
  65. cmd := m.DeleteDashboardCommand{Slug: slug, OrgId: c.OrgId}
  66. if err := bus.Dispatch(&cmd); err != nil {
  67. c.JsonApiErr(500, "Failed to delete dashboard", err)
  68. return
  69. }
  70. var resp = map[string]interface{}{"title": query.Result.Title}
  71. c.JSON(200, resp)
  72. }
  73. func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
  74. cmd.OrgId = c.OrgId
  75. dash := cmd.GetDashboardModel()
  76. if dash.Id == 0 {
  77. limitReached, err := middleware.QuotaReached(c, "dashboard")
  78. if err != nil {
  79. c.JsonApiErr(500, "failed to get quota", err)
  80. return
  81. }
  82. if limitReached {
  83. c.JsonApiErr(403, "Quota reached", nil)
  84. return
  85. }
  86. }
  87. err := bus.Dispatch(&cmd)
  88. if err != nil {
  89. if err == m.ErrDashboardWithSameNameExists {
  90. c.JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
  91. return
  92. }
  93. if err == m.ErrDashboardVersionMismatch {
  94. c.JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
  95. return
  96. }
  97. if err == m.ErrDashboardNotFound {
  98. c.JSON(404, util.DynMap{"status": "not-found", "message": err.Error()})
  99. return
  100. }
  101. c.JsonApiErr(500, "Failed to save dashboard", err)
  102. return
  103. }
  104. metrics.M_Api_Dashboard_Post.Inc(1)
  105. c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version})
  106. }
  107. func canEditDashboard(role m.RoleType) bool {
  108. return role == m.ROLE_ADMIN || role == m.ROLE_EDITOR || role == m.ROLE_READ_ONLY_EDITOR
  109. }
  110. func GetHomeDashboard(c *middleware.Context) {
  111. filePath := path.Join(setting.StaticRootPath, "dashboards/home.json")
  112. file, err := os.Open(filePath)
  113. if err != nil {
  114. c.JsonApiErr(500, "Failed to load home dashboard", err)
  115. return
  116. }
  117. dash := dtos.DashboardFullWithMeta{}
  118. dash.Meta.IsHome = true
  119. dash.Meta.CanEdit = canEditDashboard(c.OrgRole)
  120. jsonParser := json.NewDecoder(file)
  121. if err := jsonParser.Decode(&dash.Dashboard); err != nil {
  122. c.JsonApiErr(500, "Failed to load home dashboard", err)
  123. return
  124. }
  125. c.JSON(200, &dash)
  126. }
  127. func GetDashboardFromJsonFile(c *middleware.Context) {
  128. file := c.Params(":file")
  129. dashboard := search.GetDashboardFromJsonIndex(file)
  130. if dashboard == nil {
  131. c.JsonApiErr(404, "Dashboard not found", nil)
  132. return
  133. }
  134. dash := dtos.DashboardFullWithMeta{Dashboard: dashboard.Data}
  135. dash.Meta.Type = m.DashTypeJson
  136. dash.Meta.CanEdit = canEditDashboard(c.OrgRole)
  137. c.JSON(200, &dash)
  138. }
  139. func GetDashboardTags(c *middleware.Context) {
  140. query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
  141. err := bus.Dispatch(&query)
  142. if err != nil {
  143. c.JsonApiErr(500, "Failed to get tags from database", err)
  144. return
  145. }
  146. c.JSON(200, query.Result)
  147. }