index.go 11 KB

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