index.go 7.2 KB

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