dashboard.go 4.9 KB

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