index.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. dashboardChildNavs := []*dtos.NavLink{
  75. {Text: "Home", Url: setting.AppSubUrl + "/"},
  76. {Text: "Playlists", Url: setting.AppSubUrl + "/playlists"},
  77. {Text: "Snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots"},
  78. }
  79. if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR {
  80. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Divider: true})
  81. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "New", Icon: "fa fa-plus", Url: setting.AppSubUrl + "/dashboard/new"})
  82. dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "Import", Icon: "fa fa-download", Url: setting.AppSubUrl + "/dashboard/new/?editview=import"})
  83. }
  84. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  85. Text: "Dashboards",
  86. Icon: "icon-gf icon-gf-dashboard",
  87. Url: setting.AppSubUrl + "/",
  88. Children: dashboardChildNavs,
  89. })
  90. if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
  91. alertChildNavs := []*dtos.NavLink{
  92. {Text: "Alert List", Url: setting.AppSubUrl + "/alerting/list", Icon: "fa fa-fw fa-list-ul"},
  93. {Text: "Notification channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "fa fa-fw fa-bell-o"},
  94. }
  95. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  96. Text: "Alerting",
  97. Icon: "icon-gf icon-gf-alert",
  98. Url: setting.AppSubUrl + "/alerting/list",
  99. Children: alertChildNavs,
  100. })
  101. }
  102. if c.OrgRole == m.ROLE_ADMIN {
  103. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  104. Text: "Data Sources",
  105. Icon: "icon-gf icon-gf-datasources",
  106. Url: setting.AppSubUrl + "/datasources",
  107. Children: []*dtos.NavLink{
  108. {Text: "List", Url: setting.AppSubUrl + "/datasources", Icon: "icon-gf icon-gf-datasources"},
  109. },
  110. })
  111. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  112. Text: "Plugins",
  113. Icon: "icon-gf icon-gf-apps",
  114. Url: setting.AppSubUrl + "/plugins",
  115. Children: []*dtos.NavLink{
  116. {Text: "List", Url: setting.AppSubUrl + "/datasources", Icon: "icon-gf icon-gf-apps"},
  117. },
  118. })
  119. }
  120. enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
  121. if err != nil {
  122. return nil, err
  123. }
  124. for _, plugin := range enabledPlugins.Apps {
  125. if plugin.Pinned {
  126. appLink := &dtos.NavLink{
  127. Text: plugin.Name,
  128. Url: plugin.DefaultNavUrl,
  129. Img: plugin.Info.Logos.Small,
  130. }
  131. for _, include := range plugin.Includes {
  132. if !c.HasUserRole(include.Role) {
  133. continue
  134. }
  135. if include.Type == "page" && include.AddToNav {
  136. link := &dtos.NavLink{
  137. Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug,
  138. Text: include.Name,
  139. }
  140. appLink.Children = append(appLink.Children, link)
  141. }
  142. if include.Type == "dashboard" && include.AddToNav {
  143. link := &dtos.NavLink{
  144. Url: setting.AppSubUrl + "/dashboard/db/" + include.Slug,
  145. Text: include.Name,
  146. }
  147. appLink.Children = append(appLink.Children, link)
  148. }
  149. }
  150. if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
  151. appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
  152. appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"})
  153. }
  154. if len(appLink.Children) > 0 {
  155. data.MainNavLinks = append(data.MainNavLinks, appLink)
  156. }
  157. }
  158. }
  159. if c.IsGrafanaAdmin {
  160. data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
  161. Text: "Admin",
  162. Icon: "fa fa-fw fa-cogs",
  163. Url: setting.AppSubUrl + "/admin",
  164. Children: []*dtos.NavLink{
  165. {Text: "Global Users", Url: setting.AppSubUrl + "/admin/users"},
  166. {Text: "Global Orgs", Url: setting.AppSubUrl + "/admin/orgs"},
  167. {Text: "Server Settings", Url: setting.AppSubUrl + "/admin/settings"},
  168. {Text: "Server Stats", Url: setting.AppSubUrl + "/admin/stats"},
  169. },
  170. })
  171. }
  172. return &data, nil
  173. }
  174. func Index(c *middleware.Context) {
  175. if data, err := setIndexViewData(c); err != nil {
  176. c.Handle(500, "Failed to get settings", err)
  177. return
  178. } else {
  179. c.HTML(200, "index", data)
  180. }
  181. }
  182. func NotFoundHandler(c *middleware.Context) {
  183. if c.IsApiRequest() {
  184. c.JsonApiErr(404, "Not found", nil)
  185. return
  186. }
  187. if data, err := setIndexViewData(c); err != nil {
  188. c.Handle(500, "Failed to get settings", err)
  189. return
  190. } else {
  191. c.HTML(404, "index", data)
  192. }
  193. }