| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-09-23 18:29:53 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2016-07-05 23:59:43 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/dtos" | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/middleware" | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-10-06 17:20:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) { | 
					
						
							| 
									
										
										
										
											2015-02-18 21:06:44 +08:00
										 |  |  | 	settings, err := getFrontendSettingsMap(c) | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 		return nil, err | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | 	prefsQuery := m.GetPreferencesWithDefaultsQuery{OrgId: c.OrgId, UserId: c.UserId} | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&prefsQuery); err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	prefs := prefsQuery.Result | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-05 23:59:43 +08:00
										 |  |  | 	// Read locale from acccept-language
 | 
					
						
							|  |  |  | 	acceptLang := c.Req.Header.Get("Accept-Language") | 
					
						
							|  |  |  | 	locale := "en-US" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if len(acceptLang) > 0 { | 
					
						
							|  |  |  | 		parts := strings.Split(acceptLang, ",") | 
					
						
							|  |  |  | 		locale = parts[0] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-23 18:29:53 +08:00
										 |  |  | 	appUrl := setting.AppUrl | 
					
						
							|  |  |  | 	appSubUrl := setting.AppSubUrl | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// special case when doing localhost call from phantomjs
 | 
					
						
							|  |  |  | 	if c.IsRenderCall { | 
					
						
							|  |  |  | 		appUrl = fmt.Sprintf("%s://localhost:%s", setting.Protocol, setting.HttpPort) | 
					
						
							|  |  |  | 		appSubUrl = "" | 
					
						
							|  |  |  | 		settings["appSubUrl"] = "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	var data = dtos.IndexViewData{ | 
					
						
							|  |  |  | 		User: &dtos.CurrentUser{ | 
					
						
							|  |  |  | 			Id:             c.UserId, | 
					
						
							|  |  |  | 			IsSignedIn:     c.IsSignedIn, | 
					
						
							|  |  |  | 			Login:          c.Login, | 
					
						
							|  |  |  | 			Email:          c.Email, | 
					
						
							|  |  |  | 			Name:           c.Name, | 
					
						
							|  |  |  | 			OrgId:          c.OrgId, | 
					
						
							|  |  |  | 			OrgName:        c.OrgName, | 
					
						
							|  |  |  | 			OrgRole:        c.OrgRole, | 
					
						
							|  |  |  | 			GravatarUrl:    dtos.GetGravatarUrl(c.Email), | 
					
						
							|  |  |  | 			IsGrafanaAdmin: c.IsGrafanaAdmin, | 
					
						
							| 
									
										
										
										
											2016-04-03 04:54:06 +08:00
										 |  |  | 			LightTheme:     prefs.Theme == "light", | 
					
						
							|  |  |  | 			Timezone:       prefs.Timezone, | 
					
						
							| 
									
										
										
										
											2016-07-05 23:59:43 +08:00
										 |  |  | 			Locale:         locale, | 
					
						
							| 
									
										
										
										
											2016-11-09 17:41:39 +08:00
										 |  |  | 			HelpFlags1:     c.HelpFlags1, | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-05-03 15:00:58 +08:00
										 |  |  | 		Settings:                settings, | 
					
						
							| 
									
										
										
										
											2016-09-23 18:29:53 +08:00
										 |  |  | 		AppUrl:                  appUrl, | 
					
						
							|  |  |  | 		AppSubUrl:               appSubUrl, | 
					
						
							| 
									
										
										
										
											2016-05-03 15:00:58 +08:00
										 |  |  | 		GoogleAnalyticsId:       setting.GoogleAnalyticsId, | 
					
						
							|  |  |  | 		GoogleTagManagerId:      setting.GoogleTagManagerId, | 
					
						
							|  |  |  | 		BuildVersion:            setting.BuildVersion, | 
					
						
							|  |  |  | 		BuildCommit:             setting.BuildCommit, | 
					
						
							|  |  |  | 		NewGrafanaVersion:       plugins.GrafanaLatestVersion, | 
					
						
							|  |  |  | 		NewGrafanaVersionExists: plugins.GrafanaHasUpdate, | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-01 14:40:13 +08:00
										 |  |  | 	if setting.DisableGravatar { | 
					
						
							| 
									
										
										
										
											2016-02-21 06:51:22 +08:00
										 |  |  | 		data.User.GravatarUrl = setting.AppSubUrl + "/public/img/transparent.png" | 
					
						
							| 
									
										
										
										
											2015-05-01 14:40:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	if len(data.User.Name) == 0 { | 
					
						
							|  |  |  | 		data.User.Name = data.User.Login | 
					
						
							| 
									
										
										
										
											2015-02-03 17:46:52 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 15:21:38 +08:00
										 |  |  | 	themeUrlParam := c.Query("theme") | 
					
						
							|  |  |  | 	if themeUrlParam == "light" { | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 		data.User.LightTheme = true | 
					
						
							| 
									
										
										
										
											2015-04-02 15:21:38 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-10 23:38:16 +08:00
										 |  |  | 	dashboardChildNavs := []*dtos.NavLink{ | 
					
						
							|  |  |  | 		{Text: "Home", Url: setting.AppSubUrl + "/"}, | 
					
						
							|  |  |  | 		{Text: "Playlists", Url: setting.AppSubUrl + "/playlists"}, | 
					
						
							|  |  |  | 		{Text: "Snapshots", Url: setting.AppSubUrl + "/dashboard/snapshots"}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR { | 
					
						
							|  |  |  | 		dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Divider: true}) | 
					
						
							| 
									
										
										
										
											2016-03-22 02:07:08 +08:00
										 |  |  | 		dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "New", Icon: "fa fa-plus", Url: setting.AppSubUrl + "/dashboard/new"}) | 
					
						
							| 
									
										
										
										
											2016-05-19 17:03:10 +08:00
										 |  |  | 		dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "Import", Icon: "fa fa-download", Url: setting.AppSubUrl + "/dashboard/new/?editview=import"}) | 
					
						
							| 
									
										
										
										
											2016-03-10 23:38:16 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ | 
					
						
							| 
									
										
										
										
											2016-03-10 23:38:16 +08:00
										 |  |  | 		Text:     "Dashboards", | 
					
						
							|  |  |  | 		Icon:     "icon-gf icon-gf-dashboard", | 
					
						
							|  |  |  | 		Url:      setting.AppSubUrl + "/", | 
					
						
							|  |  |  | 		Children: dashboardChildNavs, | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-25 20:32:26 +08:00
										 |  |  | 	if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) { | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:23 +08:00
										 |  |  | 		alertChildNavs := []*dtos.NavLink{ | 
					
						
							| 
									
										
										
										
											2016-08-16 15:52:45 +08:00
										 |  |  | 			{Text: "Alert List", Url: setting.AppSubUrl + "/alerting/list"}, | 
					
						
							| 
									
										
										
										
											2017-01-06 19:04:25 +08:00
										 |  |  | 			{Text: "Notification channels", Url: setting.AppSubUrl + "/alerting/notifications"}, | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:23 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-03 22:46:10 +08:00
										 |  |  | 		data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:23 +08:00
										 |  |  | 			Text:     "Alerting", | 
					
						
							| 
									
										
										
										
											2016-08-16 22:50:36 +08:00
										 |  |  | 			Icon:     "icon-gf icon-gf-alert", | 
					
						
							| 
									
										
										
										
											2016-08-16 15:52:45 +08:00
										 |  |  | 			Url:      setting.AppSubUrl + "/alerting/list", | 
					
						
							| 
									
										
										
										
											2016-06-16 21:21:23 +08:00
										 |  |  | 			Children: alertChildNavs, | 
					
						
							| 
									
										
										
										
											2016-05-03 22:46:10 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	if c.OrgRole == m.ROLE_ADMIN { | 
					
						
							|  |  |  | 		data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ | 
					
						
							|  |  |  | 			Text: "Data Sources", | 
					
						
							| 
									
										
										
										
											2016-02-11 22:53:48 +08:00
										 |  |  | 			Icon: "icon-gf icon-gf-datasources", | 
					
						
							| 
									
										
										
										
											2016-02-09 21:06:23 +08:00
										 |  |  | 			Url:  setting.AppSubUrl + "/datasources", | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-01-08 19:45:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ | 
					
						
							| 
									
										
										
										
											2016-02-11 22:53:48 +08:00
										 |  |  | 			Text: "Plugins", | 
					
						
							|  |  |  | 			Icon: "icon-gf icon-gf-apps", | 
					
						
							| 
									
										
										
										
											2016-02-25 22:56:28 +08:00
										 |  |  | 			Url:  setting.AppSubUrl + "/plugins", | 
					
						
							| 
									
										
										
										
											2016-01-08 19:45:12 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-11 04:37:11 +08:00
										 |  |  | 	enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId) | 
					
						
							| 
									
										
										
										
											2015-12-03 23:43:55 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-22 06:09:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for _, plugin := range enabledPlugins.Apps { | 
					
						
							| 
									
										
										
										
											2016-01-12 01:03:08 +08:00
										 |  |  | 		if plugin.Pinned { | 
					
						
							| 
									
										
										
										
											2016-03-22 02:07:08 +08:00
										 |  |  | 			appLink := &dtos.NavLink{ | 
					
						
							| 
									
										
										
										
											2016-01-12 01:03:08 +08:00
										 |  |  | 				Text: plugin.Name, | 
					
						
							| 
									
										
										
										
											2016-03-22 17:15:47 +08:00
										 |  |  | 				Url:  plugin.DefaultNavUrl, | 
					
						
							| 
									
										
										
										
											2016-01-12 01:03:08 +08:00
										 |  |  | 				Img:  plugin.Info.Logos.Small, | 
					
						
							| 
									
										
										
										
											2016-02-09 21:06:23 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 02:07:08 +08:00
										 |  |  | 			for _, include := range plugin.Includes { | 
					
						
							| 
									
										
										
										
											2016-04-25 20:00:49 +08:00
										 |  |  | 				if !c.HasUserRole(include.Role) { | 
					
						
							|  |  |  | 					continue | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 02:07:08 +08:00
										 |  |  | 				if include.Type == "page" && include.AddToNav { | 
					
						
							|  |  |  | 					link := &dtos.NavLink{ | 
					
						
							|  |  |  | 						Url:  setting.AppSubUrl + "/plugins/" + plugin.Id + "/page/" + include.Slug, | 
					
						
							|  |  |  | 						Text: include.Name, | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					appLink.Children = append(appLink.Children, link) | 
					
						
							| 
									
										
										
										
											2016-03-01 18:07:51 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-04-25 20:00:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-22 02:07:08 +08:00
										 |  |  | 				if include.Type == "dashboard" && include.AddToNav { | 
					
						
							|  |  |  | 					link := &dtos.NavLink{ | 
					
						
							|  |  |  | 						Url:  setting.AppSubUrl + "/dashboard/db/" + include.Slug, | 
					
						
							|  |  |  | 						Text: include.Name, | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					appLink.Children = append(appLink.Children, link) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 			if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN { | 
					
						
							| 
									
										
										
										
											2016-03-22 02:07:08 +08:00
										 |  |  | 				appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true}) | 
					
						
							| 
									
										
										
										
											2016-03-22 17:15:47 +08:00
										 |  |  | 				appLink.Children = append(appLink.Children, &dtos.NavLink{Text: "Plugin Config", Icon: "fa fa-cog", Url: setting.AppSubUrl + "/plugins/" + plugin.Id + "/edit"}) | 
					
						
							| 
									
										
										
										
											2016-02-09 21:06:23 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 20:00:49 +08:00
										 |  |  | 			if len(appLink.Children) > 0 { | 
					
						
							|  |  |  | 				data.MainNavLinks = append(data.MainNavLinks, appLink) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-06 17:20:50 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-08-21 15:30:39 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-11 14:30:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-15 00:37:05 +08:00
										 |  |  | 	if c.IsGrafanaAdmin { | 
					
						
							|  |  |  | 		data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{ | 
					
						
							|  |  |  | 			Text: "Admin", | 
					
						
							|  |  |  | 			Icon: "fa fa-fw fa-cogs", | 
					
						
							|  |  |  | 			Url:  setting.AppSubUrl + "/admin", | 
					
						
							| 
									
										
										
										
											2016-02-20 11:00:29 +08:00
										 |  |  | 			Children: []*dtos.NavLink{ | 
					
						
							| 
									
										
										
										
											2016-03-22 17:15:47 +08:00
										 |  |  | 				{Text: "Global Users", Url: setting.AppSubUrl + "/admin/users"}, | 
					
						
							|  |  |  | 				{Text: "Global Orgs", Url: setting.AppSubUrl + "/admin/orgs"}, | 
					
						
							|  |  |  | 				{Text: "Server Settings", Url: setting.AppSubUrl + "/admin/settings"}, | 
					
						
							|  |  |  | 				{Text: "Server Stats", Url: setting.AppSubUrl + "/admin/stats"}, | 
					
						
							| 
									
										
										
										
											2016-02-21 01:31:09 +08:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-02-15 00:37:05 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	return &data, nil | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func Index(c *middleware.Context) { | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	if data, err := setIndexViewData(c); err != nil { | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 		c.Handle(500, "Failed to get settings", err) | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		c.HTML(200, "index", data) | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-18 23:28:15 +08:00
										 |  |  | func NotFoundHandler(c *middleware.Context) { | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	if c.IsApiRequest() { | 
					
						
							| 
									
										
										
										
											2015-03-23 03:14:00 +08:00
										 |  |  | 		c.JsonApiErr(404, "Not found", nil) | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	if data, err := setIndexViewData(c); err != nil { | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 		c.Handle(500, "Failed to get settings", err) | 
					
						
							|  |  |  | 		return | 
					
						
							| 
									
										
										
										
											2015-11-20 16:43:10 +08:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		c.HTML(404, "index", data) | 
					
						
							| 
									
										
										
										
											2015-01-29 19:10:34 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |