index.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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"},
  91. {Text: "Playlists", Id: "playlists", Url: setting.AppSubUrl + "/playlists", Icon: "fa fa-fw fa-film"},
  92. {Text: "Snapshots", Id: "snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots", Icon: "icon-gf icon-gf-fw icon-gf-snapshot"},
  93. {Text: "Dashboard List", Id: "dashboards", Url: setting.AppSubUrl + "/dashboards", Icon: "fa fa-fw fa-bars"},
  94. }
  95. data.NavTree = append(data.NavTree, &dtos.NavLink{
  96. Text: "Dashboards",
  97. Id: "dashboards",
  98. Icon: "gicon gicon-dashboard",
  99. Url: setting.AppSubUrl + "/",
  100. Children: dashboardChildNavs,
  101. })
  102. if c.IsSignedIn {
  103. profileNode := &dtos.NavLink{
  104. Text: c.SignedInUser.Name,
  105. SubTitle: c.SignedInUser.Login,
  106. Id: "profile",
  107. Img: data.User.GravatarUrl,
  108. Url: setting.AppSubUrl + "/profile",
  109. HideFromMenu: true,
  110. Children: []*dtos.NavLink{
  111. {Text: "Preferences", Id: "profile-settings", Url: setting.AppSubUrl + "/profile", Icon: "fa fa-fw fa-sliders"},
  112. {Text: "Change Password", Id: "change-password", Url: setting.AppSubUrl + "/profile/password", Icon: "fa fa-fw fa-lock", HideFromMenu: true},
  113. },
  114. }
  115. if !setting.DisableSignoutMenu {
  116. // add sign out first
  117. profileNode.Children = append(profileNode.Children, &dtos.NavLink{
  118. Text: "Sign out", Id: "sign-out", Url: setting.AppSubUrl + "/logout", Icon: "fa fa-fw fa-sign-out", Target: "_self",
  119. })
  120. }
  121. data.NavTree = append(data.NavTree, profileNode)
  122. }
  123. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  124. alertChildNavs := []*dtos.NavLink{
  125. {Text: "Alert Rules", Id: "alert-list", Url: setting.AppSubUrl + "/alerting/list", Icon: "fa fa-fw fa-list-ul"},
  126. {Text: "Notification channels", Id: "channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "gicon gicon-alert-notification-channel"},
  127. }
  128. data.NavTree = append(data.NavTree, &dtos.NavLink{
  129. Text: "Alerting",
  130. SubTitle: "Alert rules & notifications",
  131. Id: "alerting",
  132. Icon: "gicon gicon-alert",
  133. Url: setting.AppSubUrl + "/alerting/list",
  134. Children: alertChildNavs,
  135. })
  136. }
  137. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  138. if err != nil {
  139. return nil, err
  140. }
  141. for _, plugin := range enabledPlugins.Apps {
  142. if plugin.Pinned {
  143. appLink := &dtos.NavLink{
  144. Text: plugin.Name,
  145. Id: "plugin-page-" + plugin.Id,
  146. Url: plugin.DefaultNavUrl,
  147. Img: plugin.Info.Logos.Small,
  148. }
  149. for _, include := range plugin.Includes {
  150. if !c.HasUserRole(include.Role) {
  151. continue
  152. }
  153. if include.Type == "page" && include.AddToNav {
  154. link := &dtos.NavLink{
  155. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  156. Text: include.Name,
  157. }
  158. appLink.Children = append(appLink.Children, link)
  159. }
  160. if include.Type == "dashboard" && include.AddToNav {
  161. link := &dtos.NavLink{
  162. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  163. Text: include.Name,
  164. }
  165. appLink.Children = append(appLink.Children, link)
  166. }
  167. }
  168. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  169. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  170. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-fw fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  171. }
  172. if len(appLink.Children) > 0 {
  173. data.NavTree = append(data.NavTree, appLink)
  174. }
  175. }
  176. }
  177. if c.OrgRole == m.ROLE_ADMIN {
  178. cfgNode := &dtos.NavLink{
  179. Id: "cfg",
  180. Text: "Configuration",
  181. SubTitle: "Organization: " + c.OrgName,
  182. Icon: "fa fa-fw fa-cog",
  183. Url: setting.AppSubUrl + "/datasources",
  184. Children: []*dtos.NavLink{
  185. {
  186. Text: "Data Sources",
  187. Icon: "gicon gicon-datasources",
  188. Description: "Add and configure data sources",
  189. Id: "datasources",
  190. Url: setting.AppSubUrl + "/datasources",
  191. },
  192. {
  193. Text: "Members",
  194. Id: "users",
  195. Description: "Manage org members",
  196. Icon: "icon-gf icon-gf-fw icon-gf-users",
  197. Url: setting.AppSubUrl + "/org/users",
  198. },
  199. {
  200. Text: "Teams",
  201. Id: "teams",
  202. Description: "Manage org groups",
  203. Icon: "gicon gicon-user-group",
  204. Url: setting.AppSubUrl + "/org/user-groups",
  205. },
  206. {
  207. Text: "Plugins",
  208. Id: "plugins",
  209. Description: "View and configure plugins",
  210. Icon: "icon-gf icon-gf-fw icon-gf-apps",
  211. Url: setting.AppSubUrl + "/plugins",
  212. // Children: []*dtos.NavLink{
  213. // {Text: "Panels", Url: setting.AppSubUrl + "/plugins?type=panel", Icon: "fa fa-fw fa-stop"},
  214. // {Text: "Data sources", Url: setting.AppSubUrl + "/plugins?type=datasource", Icon: "icon-gf icon-gf-datasources"},
  215. // {Text: "Apps", Url: setting.AppSubUrl + "/plugins?type=app", Icon: "icon-gf icon-gf-apps"},
  216. // },
  217. },
  218. {
  219. Text: "Preferences",
  220. Id: "org-settings",
  221. Description: "Organization preferences",
  222. Icon: "fa fa-fw fa-sliders",
  223. Url: setting.AppSubUrl + "/org",
  224. },
  225. {
  226. Text: "API Keys",
  227. Id: "apikeys",
  228. Description: "Create & manage API keys",
  229. Icon: "fa fa-fw fa-key",
  230. Url: setting.AppSubUrl + "/org/apikeys",
  231. },
  232. },
  233. }
  234. // if c.IsGrafanaAdmin {
  235. // cfgNode.Children = append(cfgNode.Children, &dtos.NavLink{
  236. // Text: "Server Admin",
  237. // Id: "admin",
  238. // Icon: "fa fa-fw fa-shield",
  239. // Url: setting.AppSubUrl + "/admin",
  240. // Children: []*dtos.NavLink{
  241. // {Text: "Users", Id: "global-users", Url: setting.AppSubUrl + "/admin/users"},
  242. // {Text: "Orgs", Id: "global-orgs", Url: setting.AppSubUrl + "/admin/orgs"},
  243. // {Text: "Server Settings", Id: "server-settings", Url: setting.AppSubUrl + "/admin/settings"},
  244. // {Text: "Server Stats", Id: "server-stats", Url: setting.AppSubUrl + "/admin/stats"},
  245. // {Text: "Style Guide", Id: "styleguide", Url: setting.AppSubUrl + "/styleguide"},
  246. // },
  247. // })
  248. // }
  249. data.NavTree = append(data.NavTree, cfgNode)
  250. }
  251. data.NavTree = append(data.NavTree, &dtos.NavLink{
  252. Text: "Help",
  253. Id: "help",
  254. Url: "#",
  255. Icon: "fa fa-fw fa-question",
  256. HideFromMenu: true,
  257. Children: []*dtos.NavLink{
  258. {Text: "Keyboard shortcuts", Url: "/shortcuts", Icon: "fa fa-fw fa-keyboard-o", Target: "_self"},
  259. {Text: "Community site", Url: "http://community.grafana.com", Icon: "fa fa-fw fa-comment", Target: "_blank"},
  260. {Text: "Documentation", Url: "http://docs.grafana.org", Icon: "fa fa-fw fa-file", Target: "_blank"},
  261. },
  262. })
  263. return &data, nil
  264. }
  265. func Index(c *middleware.Context) {
  266. if data, err := setIndexViewData(c); err != nil {
  267. c.Handle(500, "Failed to get settings", err)
  268. return
  269. } else {
  270. c.HTML(200, "index", data)
  271. }
  272. }
  273. func NotFoundHandler(c *middleware.Context) {
  274. if c.IsApiRequest() {
  275. c.JsonApiErr(404, "Not found", nil)
  276. return
  277. }
  278. if data, err := setIndexViewData(c); err != nil {
  279. c.Handle(500, "Failed to get settings", err)
  280. return
  281. } else {
  282. c.HTML(404, "index", data)
  283. }
  284. }