dashboard.go 4.3 KB

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