2015-01-29 05:10:34 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-09-23 05:29:53 -05:00
|
|
|
"fmt"
|
2016-07-05 10:59:43 -05:00
|
|
|
"strings"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2016-04-02 15:54:06 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2015-11-20 02:43:10 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
2015-10-06 04:20:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-01-29 05:10:34 -06:00
|
|
|
)
|
|
|
|
|
2015-11-20 02:43:10 -06:00
|
|
|
func setIndexViewData(c *middleware.Context) (*dtos.IndexViewData, error) {
|
2015-02-18 07:06:44 -06:00
|
|
|
settings, err := getFrontendSettingsMap(c)
|
2015-01-29 05:10:34 -06:00
|
|
|
if err != nil {
|
2015-11-20 02:43:10 -06:00
|
|
|
return nil, err
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|
|
|
|
|
2016-04-02 15:54:06 -05: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 10:59:43 -05: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 05:29:53 -05: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 02:43:10 -06: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-02 15:54:06 -05:00
|
|
|
LightTheme: prefs.Theme == "light",
|
|
|
|
Timezone: prefs.Timezone,
|
2016-07-05 10:59:43 -05:00
|
|
|
Locale: locale,
|
2016-11-09 03:41:39 -06:00
|
|
|
HelpFlags1: c.HelpFlags1,
|
2015-11-20 02:43:10 -06:00
|
|
|
},
|
2016-05-03 02:00:58 -05:00
|
|
|
Settings: settings,
|
2016-09-23 05:29:53 -05:00
|
|
|
AppUrl: appUrl,
|
|
|
|
AppSubUrl: appSubUrl,
|
2016-05-03 02:00:58 -05:00
|
|
|
GoogleAnalyticsId: setting.GoogleAnalyticsId,
|
|
|
|
GoogleTagManagerId: setting.GoogleTagManagerId,
|
|
|
|
BuildVersion: setting.BuildVersion,
|
|
|
|
BuildCommit: setting.BuildCommit,
|
|
|
|
NewGrafanaVersion: plugins.GrafanaLatestVersion,
|
|
|
|
NewGrafanaVersionExists: plugins.GrafanaHasUpdate,
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|
|
|
|
|
2015-05-01 01:40:13 -05:00
|
|
|
if setting.DisableGravatar {
|
2016-02-20 16:51:22 -06:00
|
|
|
data.User.GravatarUrl = setting.AppSubUrl + "/public/img/transparent.png"
|
2015-05-01 01:40:13 -05:00
|
|
|
}
|
|
|
|
|
2015-11-20 02:43:10 -06:00
|
|
|
if len(data.User.Name) == 0 {
|
|
|
|
data.User.Name = data.User.Login
|
2015-02-03 03:46:52 -06:00
|
|
|
}
|
|
|
|
|
2015-04-02 02:21:38 -05:00
|
|
|
themeUrlParam := c.Query("theme")
|
|
|
|
if themeUrlParam == "light" {
|
2015-11-20 02:43:10 -06:00
|
|
|
data.User.LightTheme = true
|
2015-04-02 02:21:38 -05:00
|
|
|
}
|
|
|
|
|
2016-03-10 09:38:16 -06: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-21 13:07:08 -05:00
|
|
|
dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "New", Icon: "fa fa-plus", Url: setting.AppSubUrl + "/dashboard/new"})
|
2016-05-19 04:03:10 -05:00
|
|
|
dashboardChildNavs = append(dashboardChildNavs, &dtos.NavLink{Text: "Import", Icon: "fa fa-download", Url: setting.AppSubUrl + "/dashboard/new/?editview=import"})
|
2016-03-10 09:38:16 -06:00
|
|
|
}
|
|
|
|
|
2015-11-20 02:43:10 -06:00
|
|
|
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
2016-03-10 09:38:16 -06:00
|
|
|
Text: "Dashboards",
|
|
|
|
Icon: "icon-gf icon-gf-dashboard",
|
|
|
|
Url: setting.AppSubUrl + "/",
|
|
|
|
Children: dashboardChildNavs,
|
2015-11-20 02:43:10 -06:00
|
|
|
})
|
|
|
|
|
2017-01-25 06:32:26 -06:00
|
|
|
if setting.AlertingEnabled && (c.OrgRole == m.ROLE_ADMIN || c.OrgRole == m.ROLE_EDITOR) {
|
2016-06-16 08:21:23 -05:00
|
|
|
alertChildNavs := []*dtos.NavLink{
|
2017-06-12 14:11:22 -05:00
|
|
|
{Text: "Alert List", Url: setting.AppSubUrl + "/alerting/list", Icon: "fa fa-fw fa-list-ul"},
|
|
|
|
{Text: "Notification channels", Url: setting.AppSubUrl + "/alerting/notifications", Icon: "fa fa-fw fa-bell-o"},
|
2016-06-16 08:21:23 -05:00
|
|
|
}
|
|
|
|
|
2016-05-03 09:46:10 -05:00
|
|
|
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
2016-06-16 08:21:23 -05:00
|
|
|
Text: "Alerting",
|
2016-08-16 09:50:36 -05:00
|
|
|
Icon: "icon-gf icon-gf-alert",
|
2016-08-16 02:52:45 -05:00
|
|
|
Url: setting.AppSubUrl + "/alerting/list",
|
2016-06-16 08:21:23 -05:00
|
|
|
Children: alertChildNavs,
|
2016-05-03 09:46:10 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-20 02:43:10 -06:00
|
|
|
if c.OrgRole == m.ROLE_ADMIN {
|
|
|
|
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
|
|
|
Text: "Data Sources",
|
2016-02-11 08:53:48 -06:00
|
|
|
Icon: "icon-gf icon-gf-datasources",
|
2016-02-09 07:06:23 -06:00
|
|
|
Url: setting.AppSubUrl + "/datasources",
|
2017-06-22 17:11:29 -05:00
|
|
|
Children: []*dtos.NavLink{
|
|
|
|
{Text: "List", Url: setting.AppSubUrl + "/datasources", Icon: "icon-gf icon-gf-datasources"},
|
|
|
|
},
|
2015-11-20 02:43:10 -06:00
|
|
|
})
|
2016-01-08 05:45:12 -06:00
|
|
|
data.MainNavLinks = append(data.MainNavLinks, &dtos.NavLink{
|
2016-02-11 08:53:48 -06:00
|
|
|
Text: "Plugins",
|
|
|
|
Icon: "icon-gf icon-gf-apps",
|
2016-02-25 08:56:28 -06:00
|
|
|
Url: setting.AppSubUrl + "/plugins",
|
2017-06-22 17:11:29 -05:00
|
|
|
Children: []*dtos.NavLink{
|
|
|
|
{Text: "List", Url: setting.AppSubUrl + "/datasources", Icon: "icon-gf icon-gf-apps"},
|
|
|
|
},
|
2016-01-08 05:45:12 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-10 14:37:11 -06:00
|
|
|
enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
|
2015-12-03 09:43:55 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-21 16:09:27 -06:00
|
|
|
|
|
|
|
for _, plugin := range enabledPlugins.Apps {
|
2016-01-11 11:03:08 -06:00
|
|
|
if plugin.Pinned {
|
2016-03-21 13:07:08 -05:00
|
|
|
appLink := &dtos.NavLink{
|
2016-01-11 11:03:08 -06:00
|
|
|
Text: plugin.Name,
|
2016-03-22 04:15:47 -05:00
|
|
|
Url: plugin.DefaultNavUrl,
|
2016-01-11 11:03:08 -06:00
|
|
|
Img: plugin.Info.Logos.Small,
|
2016-02-09 07:06:23 -06:00
|
|
|
}
|
|
|
|
|
2016-03-21 13:07:08 -05:00
|
|
|
for _, include := range plugin.Includes {
|
2016-04-25 07:00:49 -05:00
|
|
|
if !c.HasUserRole(include.Role) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-21 13:07:08 -05: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 04:07:51 -06:00
|
|
|
}
|
2016-04-25 07:00:49 -05:00
|
|
|
|
2016-03-21 13:07:08 -05: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 07:39:51 -05:00
|
|
|
if len(appLink.Children) > 0 && c.OrgRole == m.ROLE_ADMIN {
|
2016-03-21 13:07:08 -05:00
|
|
|
appLink.Children = append(appLink.Children, &dtos.NavLink{Divider: true})
|
2016-03-22 04:15:47 -05: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 07:06:23 -06:00
|
|
|
}
|
|
|
|
|
2016-04-25 07:00:49 -05:00
|
|
|
if len(appLink.Children) > 0 {
|
|
|
|
data.MainNavLinks = append(data.MainNavLinks, appLink)
|
|
|
|
}
|
2015-10-06 04:20:50 -05:00
|
|
|
}
|
2015-08-21 02:30:39 -05:00
|
|
|
}
|
2015-11-11 00:30:07 -06:00
|
|
|
|
2016-02-14 10:37:05 -06: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-19 21:00:29 -06:00
|
|
|
Children: []*dtos.NavLink{
|
2016-03-22 04:15:47 -05: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-20 11:31:09 -06:00
|
|
|
},
|
2016-02-14 10:37:05 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-11-20 02:43:10 -06:00
|
|
|
return &data, nil
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func Index(c *middleware.Context) {
|
2015-11-20 02:43:10 -06:00
|
|
|
if data, err := setIndexViewData(c); err != nil {
|
2015-01-29 05:10:34 -06:00
|
|
|
c.Handle(500, "Failed to get settings", err)
|
|
|
|
return
|
2015-11-20 02:43:10 -06:00
|
|
|
} else {
|
|
|
|
c.HTML(200, "index", data)
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 10:28:15 -05:00
|
|
|
func NotFoundHandler(c *middleware.Context) {
|
2015-01-29 05:10:34 -06:00
|
|
|
if c.IsApiRequest() {
|
2015-03-22 14:14:00 -05:00
|
|
|
c.JsonApiErr(404, "Not found", nil)
|
2015-01-29 05:10:34 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-11-20 02:43:10 -06:00
|
|
|
if data, err := setIndexViewData(c); err != nil {
|
2015-01-29 05:10:34 -06:00
|
|
|
c.Handle(500, "Failed to get settings", err)
|
|
|
|
return
|
2015-11-20 02:43:10 -06:00
|
|
|
} else {
|
|
|
|
c.HTML(404, "index", data)
|
2015-01-29 05:10:34 -06:00
|
|
|
}
|
|
|
|
}
|