dashboard.go 4.4 KB

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