grafana/pkg/api/frontendsettings.go

238 lines
6.0 KiB
Go
Raw Normal View History

package api
import (
"strconv"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/util"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/bus"
"github.com/grafana/grafana/pkg/infra/log"
2015-02-05 03:37:13 -06:00
m "github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/plugins"
2015-02-05 03:37:13 -06:00
"github.com/grafana/grafana/pkg/setting"
)
// getFrontendSettingsMap returns a json object with all the settings needed for front end initialisation.
func (hs *HTTPServer) getFrontendSettingsMap(c *m.ReqContext) (map[string]interface{}, error) {
orgDataSources := make([]*m.DataSource, 0)
if c.OrgId != 0 {
query := m.GetDataSourcesQuery{OrgId: c.OrgId}
err := bus.Dispatch(&query)
if err != nil {
return nil, err
}
dsFilterQuery := m.DatasourcesPermissionFilterQuery{
User: c.SignedInUser,
Datasources: query.Result,
}
if err := bus.Dispatch(&dsFilterQuery); err != nil {
if err != bus.ErrHandlerNotFound {
return nil, err
}
orgDataSources = query.Result
} else {
orgDataSources = dsFilterQuery.Result
}
}
datasources := make(map[string]interface{})
2015-02-27 06:45:00 -06:00
var defaultDatasource string
enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
if err != nil {
return nil, err
}
2015-12-22 04:37:44 -06:00
pluginsToPreload := []string{}
for _, app := range enabledPlugins.Apps {
if app.Preload {
pluginsToPreload = append(pluginsToPreload, app.Module)
}
}
for _, ds := range orgDataSources {
url := ds.Url
if ds.Access == m.DS_ACCESS_PROXY {
url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
}
var dsMap = map[string]interface{}{
2017-03-29 15:54:07 -05:00
"id": ds.Id,
2015-02-27 06:45:00 -06:00
"type": ds.Type,
"name": ds.Name,
2015-02-27 06:45:00 -06:00
"url": url,
}
2015-12-22 04:37:44 -06:00
meta, exists := enabledPlugins.DataSources[ds.Type]
if !exists {
2015-03-27 08:23:23 -05:00
log.Error(3, "Could not find plugin definition for data source: %v", ds.Type)
continue
}
if meta.Preload {
pluginsToPreload = append(pluginsToPreload, meta.Module)
}
dsMap["meta"] = meta
2015-02-27 06:45:00 -06:00
if ds.IsDefault {
defaultDatasource = ds.Name
}
jsonData := ds.JsonData
if jsonData == nil {
jsonData = simplejson.New()
}
dsMap["jsonData"] = jsonData
if ds.Access == m.DS_ACCESS_DIRECT {
if ds.BasicAuth {
dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.DecryptedBasicAuthPassword())
}
if ds.WithCredentials {
dsMap["withCredentials"] = ds.WithCredentials
}
if ds.Type == m.DS_INFLUXDB_08 {
dsMap["username"] = ds.User
dsMap["password"] = ds.DecryptedPassword()
dsMap["url"] = url + "/db/" + ds.Database
}
if ds.Type == m.DS_INFLUXDB {
dsMap["username"] = ds.User
dsMap["password"] = ds.DecryptedPassword()
dsMap["url"] = url
}
}
if (ds.Type == m.DS_INFLUXDB) || (ds.Type == m.DS_ES) {
2016-01-18 00:28:01 -06:00
dsMap["database"] = ds.Database
}
if ds.Type == m.DS_PROMETHEUS {
// add unproxied server URL for link to Prometheus web UI
jsonData.Set("directUrl", ds.Url)
}
datasources[ds.Name] = dsMap
}
// add datasources that are built in (meaning they are not added via data sources page, nor have any entry in datasource table)
for _, ds := range plugins.DataSources {
if ds.BuiltIn {
datasources[ds.Name] = map[string]interface{}{
"type": ds.Type,
"name": ds.Name,
"meta": plugins.DataSources[ds.Id],
}
}
}
if defaultDatasource == "" {
defaultDatasource = "-- Grafana --"
}
panels := map[string]interface{}{}
2015-12-22 04:37:44 -06:00
for _, panel := range enabledPlugins.Panels {
if panel.State == plugins.PluginStateAlpha && !hs.Cfg.PluginsEnableAlpha {
continue
}
if panel.Preload {
pluginsToPreload = append(pluginsToPreload, panel.Module)
}
panels[panel.Id] = map[string]interface{}{
"module": panel.Module,
"baseUrl": panel.BaseUrl,
"name": panel.Name,
"id": panel.Id,
"info": panel.Info,
"hideFromList": panel.HideFromList,
"sort": getPanelSort(panel.Id),
"skipDataQuery": panel.SkipDataQuery,
"state": panel.State,
}
}
jsonObj := map[string]interface{}{
"defaultDatasource": defaultDatasource,
"datasources": datasources,
"panels": panels,
"appSubUrl": setting.AppSubUrl,
"allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
"authProxyEnabled": setting.AuthProxyEnabled,
"ldapEnabled": setting.LDAPEnabled,
"alertingEnabled": setting.AlertingEnabled,
"alertingErrorOrTimeout": setting.AlertingErrorOrTimeout,
"alertingNoDataOrNullValues": setting.AlertingNoDataOrNullValues,
"exploreEnabled": setting.ExploreEnabled,
"googleAnalyticsId": setting.GoogleAnalyticsId,
"disableLoginForm": setting.DisableLoginForm,
"externalUserMngInfo": setting.ExternalUserMngInfo,
"externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl,
"externalUserMngLinkName": setting.ExternalUserMngLinkName,
"viewersCanEdit": setting.ViewersCanEdit,
2019-03-12 01:32:47 -05:00
"editorsCanAdmin": hs.Cfg.EditorsCanAdmin,
"disableSanitizeHtml": hs.Cfg.DisableSanitizeHtml,
"pluginsToPreload": pluginsToPreload,
"buildInfo": map[string]interface{}{
2016-04-11 12:42:52 -05:00
"version": setting.BuildVersion,
"commit": setting.BuildCommit,
"buildstamp": setting.BuildStamp,
"latestVersion": plugins.GrafanaLatestVersion,
"hasUpdate": plugins.GrafanaHasUpdate,
"env": setting.Env,
"isEnterprise": setting.IsEnterprise,
},
"featureToggles": hs.Cfg.FeatureToggles,
}
return jsonObj, nil
}
func getPanelSort(id string) int {
sort := 100
switch id {
case "graph":
sort = 1
case "singlestat":
sort = 2
2018-12-17 10:54:40 -06:00
case "gauge":
sort = 3
2019-02-15 14:33:36 -06:00
case "bargauge":
sort = 4
2019-02-15 14:33:36 -06:00
case "table":
sort = 5
2019-02-15 14:33:36 -06:00
case "text":
sort = 6
2019-02-15 14:33:36 -06:00
case "heatmap":
2017-05-03 01:56:51 -05:00
sort = 7
2019-02-15 14:33:36 -06:00
case "alertlist":
2018-12-17 10:54:40 -06:00
sort = 8
2019-02-15 14:33:36 -06:00
case "dashlist":
sort = 9
}
return sort
}
func (hs *HTTPServer) GetFrontendSettings(c *m.ReqContext) {
settings, err := hs.getFrontendSettingsMap(c)
if err != nil {
c.JsonApiErr(400, "Failed to get frontend settings", err)
return
}
c.JSON(200, settings)
}