dashboard.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/log"
  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 isDashboardStarredByUser(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. 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 := isDashboardStarredByUser(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. // Finding creator and last updater of the dashboard
  42. updater, creator := "Anonymous", "Anonymous"
  43. if dash.UpdatedBy > 0 {
  44. updater = getUserLogin(dash.UpdatedBy)
  45. }
  46. if dash.CreatedBy > 0 {
  47. creator = getUserLogin(dash.CreatedBy)
  48. }
  49. dto := dtos.DashboardFullWithMeta{
  50. Dashboard: dash.Data,
  51. Meta: dtos.DashboardMeta{
  52. IsStarred: isStarred,
  53. Slug: slug,
  54. Type: m.DashTypeDB,
  55. CanStar: c.IsSignedIn,
  56. CanSave: c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR,
  57. CanEdit: canEditDashboard(c.OrgRole),
  58. Created: dash.Created,
  59. Updated: dash.Updated,
  60. UpdatedBy: updater,
  61. CreatedBy: creator,
  62. Version: dash.Version,
  63. },
  64. }
  65. c.TimeRequest(metrics.M_Api_Dashboard_Get)
  66. c.JSON(200, dto)
  67. }
  68. func getUserLogin(userId int64) string {
  69. query := m.GetUserByIdQuery{Id: userId}
  70. err := bus.Dispatch(&query)
  71. if err != nil {
  72. return "Anonymous"
  73. } else {
  74. user := query.Result
  75. return user.Login
  76. }
  77. }
  78. func DeleteDashboard(c *middleware.Context) {
  79. slug := c.Params(":slug")
  80. query := m.GetDashboardQuery{Slug: slug, OrgId: c.OrgId}
  81. if err := bus.Dispatch(&query); err != nil {
  82. c.JsonApiErr(404, "Dashboard not found", nil)
  83. return
  84. }
  85. cmd := m.DeleteDashboardCommand{Slug: slug, OrgId: c.OrgId}
  86. if err := bus.Dispatch(&cmd); err != nil {
  87. c.JsonApiErr(500, "Failed to delete dashboard", err)
  88. return
  89. }
  90. var resp = map[string]interface{}{"title": query.Result.Title}
  91. c.JSON(200, resp)
  92. }
  93. func PostDashboard(c *middleware.Context, cmd m.SaveDashboardCommand) {
  94. cmd.OrgId = c.OrgId
  95. if !c.IsSignedIn {
  96. cmd.UserId = -1
  97. } else {
  98. cmd.UserId = c.UserId
  99. }
  100. dash := cmd.GetDashboardModel()
  101. if dash.Id == 0 {
  102. limitReached, err := middleware.QuotaReached(c, "dashboard")
  103. if err != nil {
  104. c.JsonApiErr(500, "failed to get quota", err)
  105. return
  106. }
  107. if limitReached {
  108. c.JsonApiErr(403, "Quota reached", nil)
  109. return
  110. }
  111. }
  112. err := bus.Dispatch(&cmd)
  113. if err != nil {
  114. if err == m.ErrDashboardWithSameNameExists {
  115. c.JSON(412, util.DynMap{"status": "name-exists", "message": err.Error()})
  116. return
  117. }
  118. if err == m.ErrDashboardVersionMismatch {
  119. c.JSON(412, util.DynMap{"status": "version-mismatch", "message": err.Error()})
  120. return
  121. }
  122. if err == m.ErrDashboardNotFound {
  123. c.JSON(404, util.DynMap{"status": "not-found", "message": err.Error()})
  124. return
  125. }
  126. c.JsonApiErr(500, "Failed to save dashboard", err)
  127. return
  128. }
  129. c.TimeRequest(metrics.M_Api_Dashboard_Save)
  130. c.JSON(200, util.DynMap{"status": "success", "slug": cmd.Result.Slug, "version": cmd.Result.Version})
  131. }
  132. func canEditDashboard(role m.RoleType) bool {
  133. return role == m.ROLE_ADMIN || role == m.ROLE_EDITOR || role == m.ROLE_READ_ONLY_EDITOR
  134. }
  135. func GetHomeDashboard(c *middleware.Context) Response {
  136. prefsQuery := m.GetPreferencesWithDefaultsQuery{OrgId: c.OrgId, UserId: c.UserId}
  137. if err := bus.Dispatch(&prefsQuery); err != nil {
  138. return ApiError(500, "Failed to get preferences", err)
  139. }
  140. if prefsQuery.Result.HomeDashboardId != 0 {
  141. slugQuery := m.GetDashboardSlugByIdQuery{Id: prefsQuery.Result.HomeDashboardId}
  142. err := bus.Dispatch(&slugQuery)
  143. if err == nil {
  144. dashRedirect := dtos.DashboardRedirect{RedirectUri: "db/" + slugQuery.Result}
  145. return Json(200, &dashRedirect)
  146. } else {
  147. log.Warn("Failed to get slug from database, %s", err.Error())
  148. }
  149. }
  150. filePath := path.Join(setting.StaticRootPath, "dashboards/home.json")
  151. file, err := os.Open(filePath)
  152. if err != nil {
  153. return ApiError(500, "Failed to load home dashboard", err)
  154. }
  155. dash := dtos.DashboardFullWithMeta{}
  156. dash.Meta.IsHome = true
  157. dash.Meta.CanEdit = canEditDashboard(c.OrgRole)
  158. jsonParser := json.NewDecoder(file)
  159. if err := jsonParser.Decode(&dash.Dashboard); err != nil {
  160. return ApiError(500, "Failed to load home dashboard", err)
  161. }
  162. return Json(200, &dash)
  163. }
  164. func GetDashboardFromJsonFile(c *middleware.Context) {
  165. file := c.Params(":file")
  166. dashboard := search.GetDashboardFromJsonIndex(file)
  167. if dashboard == nil {
  168. c.JsonApiErr(404, "Dashboard not found", nil)
  169. return
  170. }
  171. dash := dtos.DashboardFullWithMeta{Dashboard: dashboard.Data}
  172. dash.Meta.Type = m.DashTypeJson
  173. dash.Meta.CanEdit = canEditDashboard(c.OrgRole)
  174. c.JSON(200, &dash)
  175. }
  176. func GetDashboardTags(c *middleware.Context) {
  177. query := m.GetDashboardTagsQuery{OrgId: c.OrgId}
  178. err := bus.Dispatch(&query)
  179. if err != nil {
  180. c.JsonApiErr(500, "Failed to get tags from database", err)
  181. return
  182. }
  183. c.JSON(200, query.Result)
  184. }