index.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package api
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/grafana/grafana/pkg/api/dtos"
  6. "github.com/grafana/grafana/pkg/bus"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/plugins"
  9. "github.com/grafana/grafana/pkg/setting"
  10. )
  11. const (
  12. // Themes
  13. lightName = "light"
  14. darkName = "dark"
  15. )
  16. func (hs *HTTPServer) setIndexViewData(c *m.ReqContext) (*dtos.IndexViewData, error) {
  17. settings, err := getFrontendSettingsMap(c)
  18. if err != nil {
  19. return nil, err
  20. }
  21. prefsQuery := m.GetPreferencesWithDefaultsQuery{OrgId: c.OrgId, UserId: c.UserId}
  22. if err := bus.Dispatch(&prefsQuery); err != nil {
  23. return nil, err
  24. }
  25. prefs := prefsQuery.Result
  26. // Read locale from acccept-language
  27. acceptLang := c.Req.Header.Get("Accept-Language")
  28. locale := "en-US"
  29. if len(acceptLang) > 0 {
  30. parts := strings.Split(acceptLang, ",")
  31. locale = parts[0]
  32. }
  33. appURL := setting.AppUrl
  34. appSubURL := setting.AppSubUrl
  35. // special case when doing localhost call from phantomjs
  36. if c.IsRenderCall {
  37. appURL = fmt.Sprintf("%s://localhost:%s", setting.Protocol, setting.HttpPort)
  38. appSubURL = ""
  39. settings["appSubUrl"] = ""
  40. }
  41. hasEditPermissionInFoldersQuery := m.HasEditPermissionInFoldersQuery{SignedInUser: c.SignedInUser}
  42. if err := bus.Dispatch(&hasEditPermissionInFoldersQuery); err != nil {
  43. return nil, err
  44. }
  45. var data = dtos.IndexViewData{
  46. User: &dtos.CurrentUser{
  47. Id: c.UserId,
  48. IsSignedIn: c.IsSignedIn,
  49. Login: c.Login,
  50. Email: c.Email,
  51. Name: c.Name,
  52. OrgCount: c.OrgCount,
  53. OrgId: c.OrgId,
  54. OrgName: c.OrgName,
  55. OrgRole: c.OrgRole,
  56. GravatarUrl: dtos.GetGravatarUrl(c.Email),
  57. IsGrafanaAdmin: c.IsGrafanaAdmin,
  58. LightTheme: prefs.Theme == lightName,
  59. Timezone: prefs.Timezone,
  60. Locale: locale,
  61. HelpFlags1: c.HelpFlags1,
  62. HasEditPermissionInFolders: hasEditPermissionInFoldersQuery.Result,
  63. },
  64. Settings: settings,
  65. Theme: prefs.Theme,
  66. AppUrl: appURL,
  67. AppSubUrl: appSubURL,
  68. GoogleAnalyticsId: setting.GoogleAnalyticsId,
  69. GoogleTagManagerId: setting.GoogleTagManagerId,
  70. BuildVersion: setting.BuildVersion,
  71. BuildCommit: setting.BuildCommit,
  72. NewGrafanaVersion: plugins.GrafanaLatestVersion,
  73. NewGrafanaVersionExists: plugins.GrafanaHasUpdate,
  74. AppName: setting.ApplicationName,
  75. }
  76. if setting.DisableGravatar {
  77. data.User.GravatarUrl = setting.AppSubUrl + "/public/img/user_profile.png"
  78. }
  79. if len(data.User.Name) == 0 {
  80. data.User.Name = data.User.Login
  81. }
  82. themeURLParam := c.Query("theme")
  83. if themeURLParam == lightName {
  84. data.User.LightTheme = true
  85. data.Theme = lightName
  86. } else if themeURLParam == darkName {
  87. data.User.LightTheme = false
  88. data.Theme = darkName
  89. }
  90. if hasEditPermissionInFoldersQuery.Result {
  91. children := []*dtos.NavLink{
  92. {Text: "Dashboard", Icon: "gicon gicon-dashboard-new", Url: setting.AppSubUrl + "/dashboard/new"},
  93. }
  94. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  95. children = append(children, &dtos.NavLink{Text: "Folder", SubTitle: "Create a new folder to organize your dashboards", Id: "folder", Icon: "gicon gicon-folder-new", Url: setting.AppSubUrl + "/dashboards/folder/new"})
  96. }
  97. children = append(children, &dtos.NavLink{Text: "Import", SubTitle: "Import dashboard from file or Grafana.com", Id: "import", Icon: "gicon gicon-dashboard-import", Url: setting.AppSubUrl + "/dashboard/import"})
  98. data.NavTree = append(data.NavTree, &dtos.NavLink{
  99. Text: "Create",
  100. Id: "create",
  101. Icon: "fa fa-fw fa-plus",
  102. Url: setting.AppSubUrl + "/dashboard/new",
  103. Children: children,
  104. })
  105. }
  106. dashboardChildNavs := []*dtos.NavLink{
  107. {Text: "Home", Id: "home", Url: setting.AppSubUrl + "/", Icon: "gicon gicon-home", HideFromTabs: true},
  108. {Text: "Divider", Divider: true, Id: "divider", HideFromTabs: true},
  109. {Text: "Manage", Id: "manage-dashboards", Url: setting.AppSubUrl + "/dashboards", Icon: "gicon gicon-manage"},
  110. {Text: "Playlists", Id: "playlists", Url: setting.AppSubUrl + "/playlists", Icon: "gicon gicon-playlists"},
  111. {Text: "Snapshots", Id: "snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots", Icon: "gicon gicon-snapshots"},
  112. }
  113. data.NavTree = append(data.NavTree, &dtos.NavLink{
  114. Text: "Dashboards",
  115. Id: "dashboards",
  116. SubTitle: "Manage dashboards & folders",
  117. Icon: "gicon gicon-dashboard",
  118. Url: setting.AppSubUrl + "/",
  119. Children: dashboardChildNavs,
  120. })
  121. if setting.ExploreEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  122. data.NavTree = append(data.NavTree, &dtos.NavLink{
  123. Text: "Explore",
  124. Id: "explore",
  125. SubTitle: "Explore your data",
  126. Icon: "fa fa-rocket",
  127. Url: setting.AppSubUrl + "/explore",
  128. Children: []*dtos.NavLink{
  129. {Text: "New tab", Icon: "gicon gicon-dashboard-new", Url: setting.AppSubUrl + "/explore"},
  130. },
  131. })
  132. }
  133. if c.IsSignedIn {
  134. // Only set login if it's different from the name
  135. var login string
  136. if c.SignedInUser.Login != c.SignedInUser.NameOrFallback() {
  137. login = c.SignedInUser.Login
  138. }
  139. profileNode := &dtos.NavLink{
  140. Text: c.SignedInUser.NameOrFallback(),
  141. SubTitle: login,
  142. Id: "profile",
  143. Img: data.User.GravatarUrl,
  144. Url: setting.AppSubUrl + "/profile",
  145. HideFromMenu: true,
  146. Children: []*dtos.NavLink{
  147. {Text: "Preferences", Id: "profile-settings", Url: setting.AppSubUrl + "/profile", Icon: "gicon gicon-preferences"},
  148. {Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true},
  149. },
  150. }
  151. if !setting.DisableSignoutMenu {
  152. // add sign out first
  153. profileNode.Children = append(profileNode.Children, &dtos.NavLink{
  154. Text: "Sign out", Id: "sign-out", Url: setting.AppSubUrl + "/logout", Icon: "fa fa-fw fa-sign-out", Target: "_self",
  155. })
  156. }
  157. data.NavTree = append(data.NavTree, profileNode)
  158. }
  159. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  160. alertChildNavs := []*dtos.NavLink{
  161. {Text: "Alert Rules", Id: "alert-list", Url: setting.AppSubUrl + "/alerting/list", Icon: "gicon gicon-alert-rules"},
  162. {Text: "Notification channels", Id: "channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "gicon gicon-alert-notification-channel"},
  163. }
  164. data.NavTree = append(data.NavTree, &dtos.NavLink{
  165. Text: "Alerting",
  166. SubTitle: "Alert rules & notifications",
  167. Id: "alerting",
  168. Icon: "gicon gicon-alert",
  169. Url: setting.AppSubUrl + "/alerting/list",
  170. Children: alertChildNavs,
  171. })
  172. }
  173. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  174. if err != nil {
  175. return nil, err
  176. }
  177. for _, plugin := range enabledPlugins.Apps {
  178. if plugin.Pinned {
  179. appLink := &dtos.NavLink{
  180. Text: plugin.Name,
  181. Id: "plugin-page-" + plugin.Id,
  182. Url: plugin.DefaultNavUrl,
  183. Img: plugin.Info.Logos.Small,
  184. }
  185. for _, include := range plugin.Includes {
  186. if !c.HasUserRole(include.Role) {
  187. continue
  188. }
  189. if include.Type == "page" && include.AddToNav {
  190. link := &dtos.NavLink{
  191. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  192. Text: include.Name,
  193. }
  194. appLink.Children = append(appLink.Children, link)
  195. }
  196. if include.Type == "dashboard" && include.AddToNav {
  197. link := &dtos.NavLink{
  198. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  199. Text: include.Name,
  200. }
  201. appLink.Children = append(appLink.Children, link)
  202. }
  203. }
  204. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  205. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  206. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "gicon gicon-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  207. }
  208. if len(appLink.Children) > 0 {
  209. data.NavTree = append(data.NavTree, appLink)
  210. }
  211. }
  212. }
  213. if c.IsGrafanaAdmin || c.OrgRole == m.ROLE_ADMIN {
  214. cfgNode := &dtos.NavLink{
  215. Id: "cfg",
  216. Text: "Configuration",
  217. SubTitle: "Organization: " + c.OrgName,
  218. Icon: "gicon gicon-cog",
  219. Url: setting.AppSubUrl + "/datasources",
  220. Children: []*dtos.NavLink{
  221. {
  222. Text: "Data Sources",
  223. Icon: "gicon gicon-datasources",
  224. Description: "Add and configure data sources",
  225. Id: "datasources",
  226. Url: setting.AppSubUrl + "/datasources",
  227. },
  228. {
  229. Text: "Users",
  230. Id: "users",
  231. Description: "Manage org members",
  232. Icon: "gicon gicon-user",
  233. Url: setting.AppSubUrl + "/org/users",
  234. },
  235. {
  236. Text: "Teams",
  237. Id: "teams",
  238. Description: "Manage org groups",
  239. Icon: "gicon gicon-team",
  240. Url: setting.AppSubUrl + "/org/teams",
  241. },
  242. {
  243. Text: "Plugins",
  244. Id: "plugins",
  245. Description: "View and configure plugins",
  246. Icon: "gicon gicon-plugins",
  247. Url: setting.AppSubUrl + "/plugins",
  248. },
  249. {
  250. Text: "Preferences",
  251. Id: "org-settings",
  252. Description: "Organization preferences",
  253. Icon: "gicon gicon-preferences",
  254. Url: setting.AppSubUrl + "/org",
  255. },
  256. {
  257. Text: "API Keys",
  258. Id: "apikeys",
  259. Description: "Create & manage API keys",
  260. Icon: "gicon gicon-apikeys",
  261. Url: setting.AppSubUrl + "/org/apikeys",
  262. },
  263. },
  264. }
  265. if c.OrgRole != m.ROLE_ADMIN {
  266. cfgNode = &dtos.NavLink{
  267. Id: "cfg",
  268. Text: "Configuration",
  269. SubTitle: "Organization: " + c.OrgName,
  270. Icon: "gicon gicon-cog",
  271. Url: setting.AppSubUrl + "/admin/users",
  272. Children: make([]*dtos.NavLink, 0),
  273. }
  274. }
  275. if c.OrgRole == m.ROLE_ADMIN && c.IsGrafanaAdmin {
  276. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  277. Divider: true, HideFromTabs: true, Id: "admin-divider", Text: "Text",
  278. })
  279. }
  280. if c.IsGrafanaAdmin {
  281. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  282. Text: "Server Admin",
  283. HideFromTabs: true,
  284. SubTitle: "Manage all users & orgs",
  285. Id: "admin",
  286. Icon: "gicon gicon-shield",
  287. Url: setting.AppSubUrl + "/admin/users",
  288. Children: []*dtos.NavLink{
  289. {Text: "Users", Id: "global-users", Url: setting.AppSubUrl + "/admin/users", Icon: "gicon gicon-user"},
  290. {Text: "Orgs", Id: "global-orgs", Url: setting.AppSubUrl + "/admin/orgs", Icon: "gicon gicon-org"},
  291. {Text: "Settings", Id: "server-settings", Url: setting.AppSubUrl + "/admin/settings", Icon: "gicon gicon-preferences"},
  292. {Text: "Stats", Id: "server-stats", Url: setting.AppSubUrl + "/admin/stats", Icon: "fa fa-fw fa-bar-chart"},
  293. {Text: "Style Guide", Id: "styleguide", Url: setting.AppSubUrl + "/styleguide", Icon: "fa fa-fw fa-eyedropper"},
  294. },
  295. })
  296. }
  297. data.NavTree = append(data.NavTree, cfgNode)
  298. }
  299. data.NavTree = append(data.NavTree, &dtos.NavLink{
  300. Text: "Help",
  301. SubTitle: fmt.Sprintf(`%s v%s (%s)`, setting.ApplicationName, setting.BuildVersion, setting.BuildCommit),
  302. Id: "help",
  303. Url: "#",
  304. Icon: "gicon gicon-question",
  305. HideFromMenu: true,
  306. Children: []*dtos.NavLink{
  307. {Text: "Keyboard shortcuts", Url: "/shortcuts", Icon: "fa fa-fw fa-keyboard-o", Target: "_self"},
  308. {Text: "Community site", Url: "http://community.grafana.com", Icon: "fa fa-fw fa-comment", Target: "_blank"},
  309. {Text: "Documentation", Url: "http://docs.grafana.org", Icon: "fa fa-fw fa-file", Target: "_blank"},
  310. },
  311. })
  312. hs.HooksService.RunIndexDataHooks(&data)
  313. return &data, nil
  314. }
  315. func (hs *HTTPServer) Index(c *m.ReqContext) {
  316. data, err := hs.setIndexViewData(c)
  317. if err != nil {
  318. c.Handle(500, "Failed to get settings", err)
  319. return
  320. }
  321. c.HTML(200, "index", data)
  322. }
  323. func (hs *HTTPServer) NotFoundHandler(c *m.ReqContext) {
  324. if c.IsApiRequest() {
  325. c.JsonApiErr(404, "Not found", nil)
  326. return
  327. }
  328. data, err := hs.setIndexViewData(c)
  329. if err != nil {
  330. c.Handle(500, "Failed to get settings", err)
  331. return
  332. }
  333. c.HTML(404, "index", data)
  334. }