dashboard.go 5.5 KB

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