index.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. 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. AppUrl: appUrl,
  56. AppSubUrl: appSubUrl,
  57. GoogleAnalyticsId: setting.GoogleAnalyticsId,
  58. GoogleTagManagerId: setting.GoogleTagManagerId,
  59. BuildVersion: setting.BuildVersion,
  60. BuildCommit: setting.BuildCommit,
  61. NewGrafanaVersion: plugins.GrafanaLatestVersion,
  62. NewGrafanaVersionExists: plugins.GrafanaHasUpdate,
  63. }
  64. if setting.DisableGravatar {
  65. data.User.GravatarUrl = setting.AppSubUrl + "/public/img/transparent.png"
  66. }
  67. if len(data.User.Name) == 0 {
  68. data.User.Name = data.User.Login
  69. }
  70. themeUrlParam := c.Query("theme")
  71. if themeUrlParam == "light" {
  72. data.User.LightTheme = true
  73. }
  74. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  75. data.NavTree = append(data.NavTree, &dtos.NavLink{
  76. Text: "New",
  77. Icon: "fa fa-fw fa-plus",
  78. Url: "",
  79. Children: []*dtos.NavLink{
  80. {Text: "Dashboard", Icon: "fa fa-fw fa-plus", Url: setting.AppSubUrl + "/dashboard/new"},
  81. {Text: "Folder", Icon: "fa fa-fw fa-plus", Url: setting.AppSubUrl + "/dashboard/new/?editview=new-folder"},
  82. {Text: "Import", Icon: "fa fa-fw fa-plus", Url: setting.AppSubUrl + "/dashboard/new/?editview=import"},
  83. },
  84. })
  85. }
  86. dashboardChildNavs := []*dtos.NavLink{
  87. {Text: "Home", Url: setting.AppSubUrl + "/", Icon: "fa fa-fw fa-home"},
  88. {Text: "Playlists", Id: "playlists", Url: setting.AppSubUrl + "/playlists", Icon: "fa fa-fw fa-film"},
  89. {Text: "Snapshots", Id: "snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots", Icon: "icon-gf icon-gf-snapshot"},
  90. }
  91. data.NavTree = append(data.NavTree, &dtos.NavLink{
  92. Text: "Dashboards",
  93. Id: "dashboards",
  94. Icon: "icon-gf icon-gf-dashboard",
  95. Url: setting.AppSubUrl + "/",
  96. Children: dashboardChildNavs,
  97. })
  98. if c.IsSignedIn {
  99. data.NavTree = append(data.NavTree, &dtos.NavLink{
  100. Text: c.SignedInUser.Login,
  101. Id: "profile",
  102. Img: data.User.GravatarUrl,
  103. Url: setting.AppSubUrl + "/profile",
  104. HideFromMenu: true,
  105. Children: []*dtos.NavLink{
  106. {Text: "Signout", Url: setting.AppSubUrl + "/logout", Icon: "fa fa-fw fa-sign-out", Target: "_self"},
  107. {Text: "Your profile", Url: setting.AppSubUrl + "/profile", Icon: "fa fa-fw fa-sliders"},
  108. {Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true},
  109. },
  110. })
  111. }
  112. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  113. alertChildNavs := []*dtos.NavLink{
  114. {Text: "Alert List", Id: "alert-list", Url: setting.AppSubUrl + "/alerting/list", Icon: "fa fa-fw fa-list-ul"},
  115. {Text: "Notification channels", Id: "channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "fa fa-fw fa-bell-o"},
  116. }
  117. data.NavTree = append(data.NavTree, &dtos.NavLink{
  118. Text: "Alerting",
  119. Id: "alerting",
  120. Icon: "icon-gf icon-gf-alert",
  121. Url: setting.AppSubUrl + "/alerting/list",
  122. Children: alertChildNavs,
  123. })
  124. }
  125. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  126. if err != nil {
  127. return nil, err
  128. }
  129. for _, plugin := range enabledPlugins.Apps {
  130. if plugin.Pinned {
  131. appLink := &dtos.NavLink{
  132. Text: plugin.Name,
  133. Id: "plugin-page-" + plugin.Id,
  134. Url: plugin.DefaultNavUrl,
  135. Img: plugin.Info.Logos.Small,
  136. }
  137. for _, include := range plugin.Includes {
  138. if !c.HasUserRole(include.Role) {
  139. continue
  140. }
  141. if include.Type == "page" && include.AddToNav {
  142. link := &dtos.NavLink{
  143. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  144. Text: include.Name,
  145. }
  146. appLink.Children = append(appLink.Children, link)
  147. }
  148. if include.Type == "dashboard" && include.AddToNav {
  149. link := &dtos.NavLink{
  150. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  151. Text: include.Name,
  152. }
  153. appLink.Children = append(appLink.Children, link)
  154. }
  155. }
  156. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  157. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  158. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  159. }
  160. if len(appLink.Children) > 0 {
  161. data.NavTree = append(data.NavTree, appLink)
  162. }
  163. }
  164. }
  165. if c.OrgRole == m.ROLE_ADMIN {
  166. cfgNode := &dtos.NavLink{
  167. Id: "cfg",
  168. Text: "Configuration",
  169. Icon: "fa fa-fw fa-cogs",
  170. Url: setting.AppSubUrl + "/configuration",
  171. Children: []*dtos.NavLink{
  172. {
  173. Text: "Data Sources",
  174. Icon: "icon-gf icon-gf-datasources",
  175. Description: "Add and configure data sources",
  176. Id: "datasources",
  177. Url: setting.AppSubUrl + "/datasources",
  178. Children: []*dtos.NavLink{
  179. {Text: "List", Url: setting.AppSubUrl + "/datasources", Icon: "icon-gf icon-gf-datasources"},
  180. {Text: "New", Url: setting.AppSubUrl + "/datasources", Icon: "fa fa-fw fa-plus"},
  181. },
  182. },
  183. {
  184. Text: "Preferences",
  185. Id: "org",
  186. Description: "Organization preferences",
  187. Icon: "fa fa-fw fa-sliders",
  188. Url: setting.AppSubUrl + "/org",
  189. },
  190. {
  191. Text: "Plugins",
  192. Id: "plugins",
  193. Description: "View and configure plugins",
  194. Icon: "icon-gf icon-gf-apps",
  195. Url: setting.AppSubUrl + "/plugins",
  196. Children: []*dtos.NavLink{
  197. {Text: "Panels", Url: setting.AppSubUrl + "/plugins?type=panel", Icon: "fa fa-fw fa-stop"},
  198. {Text: "Data sources", Url: setting.AppSubUrl + "/plugins?type=datasource", Icon: "icon-gf icon-gf-datasources"},
  199. {Text: "Apps", Url: setting.AppSubUrl + "/plugins?type=app", Icon: "icon-gf icon-gf-apps"},
  200. },
  201. },
  202. {
  203. Text: "User Management",
  204. Id: "users",
  205. Description: "Manage users & user groups",
  206. Icon: "fa fa-fw fa-users",
  207. Url: setting.AppSubUrl + "/org/users",
  208. },
  209. {
  210. Text: "API Keys",
  211. Id: "apikeys",
  212. Description: "Create & manage API keys",
  213. Icon: "fa fa-fw fa-key",
  214. Url: setting.AppSubUrl + "/org/apikeys",
  215. },
  216. },
  217. }
  218. if c.IsGrafanaAdmin {
  219. cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  220. Text: "Server Admin",
  221. Id: "admin",
  222. Icon: "fa fa-fw fa-shield",
  223. Url: setting.AppSubUrl + "/admin",
  224. Children: []*dtos.NavLink{
  225. {Text: "Global Users", Id: "global-users", Url: setting.AppSubUrl + "/admin/users"},
  226. {Text: "Global Orgs", Id: "global-orgs", Url: setting.AppSubUrl + "/admin/orgs"},
  227. {Text: "Server Settings", Id: "server-settings", Url: setting.AppSubUrl + "/admin/settings"},
  228. {Text: "Server Stats", Id: "server-stats", Url: setting.AppSubUrl + "/admin/stats"},
  229. {Text: "Style Guide", Id: "styleguide", Url: setting.AppSubUrl + "/admin/styleguide"},
  230. },
  231. })
  232. }
  233. data.NavTree = append(data.NavTree, &dtos.NavLink{
  234. Text: "Help",
  235. Id: "help",
  236. Url: "/help",
  237. Icon: "fa fa-fw fa-question",
  238. HideFromMenu: true,
  239. Children: []*dtos.NavLink{
  240. {Text: "Shortcuts", Url: "/shortcuts", Icon: "fa fa-fw fa-keyboard-o", Target: "_self"},
  241. {Text: "Community site", Url: "http://community.grafana.com", Icon: "fa fa-fw fa-comment", Target: "_blank"},
  242. {Text: "Documentation", Url: "http://docs.grafana.org", Icon: "fa fa-fw fa-file", Target: "_blank"},
  243. },
  244. })
  245. data.NavTree = append(data.NavTree, cfgNode)
  246. }
  247. return &data, nil
  248. }
  249. func Index(c *middleware.Context) {
  250. if data, err := setIndexViewData(c); err != nil {
  251. c.Handle(500, "Failed to get settings", err)
  252. return
  253. } else {
  254. c.HTML(200, "index", data)
  255. }
  256. }
  257. func NotFoundHandler(c *middleware.Context) {
  258. if c.IsApiRequest() {
  259. c.JsonApiErr(404, "Not found", nil)
  260. return
  261. }
  262. if data, err := setIndexViewData(c); err != nil {
  263. c.Handle(500, "Failed to get settings", err)
  264. return
  265. } else {
  266. c.HTML(404, "index", data)
  267. }
  268. }