dashboard.go 5.6 KB

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