index.go 10 KB

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