dashboard.go 5.8 KB

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