2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-01-30 10:35:32 -06:00
|
|
|
|
2019-05-10 04:37:43 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-10 00:46:14 -05:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2015-02-27 15:29:00 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
2019-04-15 04:11:17 -05:00
|
|
|
// getFrontendSettingsMap returns a json object with all the settings needed for front end initialisation.
|
2020-03-04 05:57:20 -06:00
|
|
|
func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]interface{}, error) {
|
|
|
|
orgDataSources := make([]*models.DataSource, 0)
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2015-03-11 11:34:11 -05:00
|
|
|
if c.OrgId != 0 {
|
2020-03-04 05:57:20 -06:00
|
|
|
query := models.GetDataSourcesQuery{OrgId: c.OrgId}
|
2014-12-29 06:36:08 -06:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
dsFilterQuery := models.DatasourcesPermissionFilterQuery{
|
2018-10-10 08:54:37 -05:00
|
|
|
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
|
|
|
|
}
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
datasources := make(map[string]interface{})
|
2015-02-27 06:45:00 -06:00
|
|
|
var defaultDatasource string
|
2014-12-29 06:36:08 -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-22 04:37:44 -06:00
|
|
|
|
2019-04-11 01:02:22 -05:00
|
|
|
pluginsToPreload := []string{}
|
|
|
|
|
|
|
|
for _, app := range enabledPlugins.Apps {
|
|
|
|
if app.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, app.Module)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
for _, ds := range orgDataSources {
|
2014-12-29 06:36:08 -06:00
|
|
|
url := ds.Url
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Access == models.DS_ACCESS_PROXY {
|
2016-09-23 05:29:53 -05:00
|
|
|
url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var dsMap = map[string]interface{}{
|
2017-03-29 15:54:07 -05:00
|
|
|
"id": ds.Id,
|
2020-04-20 08:48:38 -05:00
|
|
|
"uid": ds.Uid,
|
2015-02-27 06:45:00 -06:00
|
|
|
"type": ds.Type,
|
2015-02-28 03:52:25 -06:00
|
|
|
"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]
|
2015-02-27 15:29:00 -06:00
|
|
|
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
|
2015-02-27 15:29:00 -06:00
|
|
|
}
|
|
|
|
|
2019-04-11 01:02:22 -05:00
|
|
|
if meta.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, meta.Module)
|
|
|
|
}
|
|
|
|
|
2015-02-27 15:29:00 -06:00
|
|
|
dsMap["meta"] = meta
|
|
|
|
|
2015-02-27 06:45:00 -06:00
|
|
|
if ds.IsDefault {
|
|
|
|
defaultDatasource = ds.Name
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2019-05-10 04:37:43 -05:00
|
|
|
jsonData := ds.JsonData
|
|
|
|
if jsonData == nil {
|
|
|
|
jsonData = simplejson.New()
|
2015-05-20 11:29:20 -05:00
|
|
|
}
|
|
|
|
|
2019-05-10 04:37:43 -05:00
|
|
|
dsMap["jsonData"] = jsonData
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Access == models.DS_ACCESS_DIRECT {
|
2015-03-02 02:58:35 -06:00
|
|
|
if ds.BasicAuth {
|
2019-04-15 04:11:17 -05:00
|
|
|
dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.DecryptedBasicAuthPassword())
|
2015-03-02 02:58:35 -06:00
|
|
|
}
|
2015-12-08 21:37:08 -06:00
|
|
|
if ds.WithCredentials {
|
|
|
|
dsMap["withCredentials"] = ds.WithCredentials
|
|
|
|
}
|
2015-03-02 02:58:35 -06:00
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Type == models.DS_INFLUXDB_08 {
|
2014-12-29 06:36:08 -06:00
|
|
|
dsMap["username"] = ds.User
|
2019-04-15 04:11:17 -05:00
|
|
|
dsMap["password"] = ds.DecryptedPassword()
|
2014-12-29 06:36:08 -06:00
|
|
|
dsMap["url"] = url + "/db/" + ds.Database
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Type == models.DS_INFLUXDB {
|
2015-02-25 11:43:44 -06:00
|
|
|
dsMap["username"] = ds.User
|
2019-04-15 04:11:17 -05:00
|
|
|
dsMap["password"] = ds.DecryptedPassword()
|
2015-02-25 11:43:44 -06:00
|
|
|
dsMap["url"] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if (ds.Type == models.DS_INFLUXDB) || (ds.Type == models.DS_ES) {
|
2016-01-18 00:28:01 -06:00
|
|
|
dsMap["database"] = ds.Database
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
if ds.Type == models.DS_PROMETHEUS {
|
2015-10-01 11:01:09 -05:00
|
|
|
// add unproxied server URL for link to Prometheus web UI
|
2019-05-10 04:37:43 -05:00
|
|
|
jsonData.Set("directUrl", ds.Url)
|
2015-10-01 11:01:09 -05:00
|
|
|
}
|
|
|
|
|
2014-12-29 06:36:08 -06:00
|
|
|
datasources[ds.Name] = dsMap
|
|
|
|
}
|
|
|
|
|
2017-04-12 01:23:44 -05:00
|
|
|
// add datasources that are built in (meaning they are not added via data sources page, nor have any entry in datasource table)
|
2017-04-11 15:47:53 -05:00
|
|
|
for _, ds := range plugins.DataSources {
|
2017-04-12 01:23:44 -05:00
|
|
|
if ds.BuiltIn {
|
2017-04-11 15:47:53 -05:00
|
|
|
datasources[ds.Name] = map[string]interface{}{
|
|
|
|
"type": ds.Type,
|
|
|
|
"name": ds.Name,
|
|
|
|
"meta": plugins.DataSources[ds.Id],
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 13:52:30 -05:00
|
|
|
}
|
2015-02-28 02:46:37 -06:00
|
|
|
|
|
|
|
if defaultDatasource == "" {
|
2015-08-18 07:54:56 -05:00
|
|
|
defaultDatasource = "-- Grafana --"
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
2015-11-21 06:46:18 -06:00
|
|
|
panels := map[string]interface{}{}
|
2015-12-22 04:37:44 -06:00
|
|
|
for _, panel := range enabledPlugins.Panels {
|
2019-04-12 06:46:42 -05:00
|
|
|
if panel.State == plugins.PluginStateAlpha && !hs.Cfg.PluginsEnableAlpha {
|
2018-10-09 10:47:43 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-11 01:02:22 -05:00
|
|
|
if panel.Preload {
|
|
|
|
pluginsToPreload = append(pluginsToPreload, panel.Module)
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:15:44 -06:00
|
|
|
panels[panel.Id] = map[string]interface{}{
|
2019-05-10 00:46:14 -05:00
|
|
|
"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,
|
2015-11-21 06:46:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 06:36:08 -06:00
|
|
|
jsonObj := map[string]interface{}{
|
2018-09-06 04:20:38 -05:00
|
|
|
"defaultDatasource": defaultDatasource,
|
|
|
|
"datasources": datasources,
|
2020-02-28 07:32:01 -06:00
|
|
|
"minRefreshInterval": setting.MinRefreshInterval,
|
2018-09-06 04:20:38 -05:00
|
|
|
"panels": panels,
|
|
|
|
"appSubUrl": setting.AppSubUrl,
|
|
|
|
"allowOrgCreate": (setting.AllowUserOrgCreate && c.IsSignedIn) || c.IsGrafanaAdmin,
|
|
|
|
"authProxyEnabled": setting.AuthProxyEnabled,
|
2019-05-22 07:30:03 -05:00
|
|
|
"ldapEnabled": setting.LDAPEnabled,
|
2018-09-06 04:20:38 -05:00
|
|
|
"alertingEnabled": setting.AlertingEnabled,
|
|
|
|
"alertingErrorOrTimeout": setting.AlertingErrorOrTimeout,
|
|
|
|
"alertingNoDataOrNullValues": setting.AlertingNoDataOrNullValues,
|
2020-01-14 03:13:34 -06:00
|
|
|
"alertingMinInterval": setting.AlertingMinInterval,
|
2020-02-17 04:13:13 -06:00
|
|
|
"autoAssignOrg": setting.AutoAssignOrg,
|
|
|
|
"verfiyEmailEnabled": setting.VerifyEmailEnabled,
|
2018-09-06 04:20:38 -05:00
|
|
|
"exploreEnabled": setting.ExploreEnabled,
|
|
|
|
"googleAnalyticsId": setting.GoogleAnalyticsId,
|
|
|
|
"disableLoginForm": setting.DisableLoginForm,
|
2019-09-16 06:38:03 -05:00
|
|
|
"disableUserSignUp": !setting.AllowUserSignUp,
|
|
|
|
"loginHint": setting.LoginHint,
|
|
|
|
"passwordHint": setting.PasswordHint,
|
2018-09-06 04:20:38 -05:00
|
|
|
"externalUserMngInfo": setting.ExternalUserMngInfo,
|
|
|
|
"externalUserMngLinkUrl": setting.ExternalUserMngLinkUrl,
|
|
|
|
"externalUserMngLinkName": setting.ExternalUserMngLinkName,
|
2019-01-21 01:47:41 -06:00
|
|
|
"viewersCanEdit": setting.ViewersCanEdit,
|
2019-03-12 01:32:47 -05:00
|
|
|
"editorsCanAdmin": hs.Cfg.EditorsCanAdmin,
|
2019-01-22 04:56:35 -06:00
|
|
|
"disableSanitizeHtml": hs.Cfg.DisableSanitizeHtml,
|
2019-04-11 01:02:22 -05:00
|
|
|
"pluginsToPreload": pluginsToPreload,
|
2015-01-05 03:46:58 -06:00
|
|
|
"buildInfo": map[string]interface{}{
|
2016-04-11 12:42:52 -05:00
|
|
|
"version": setting.BuildVersion,
|
|
|
|
"commit": setting.BuildCommit,
|
|
|
|
"buildstamp": setting.BuildStamp,
|
2020-01-27 02:24:44 -06:00
|
|
|
"edition": hs.License.Edition(),
|
2016-04-11 12:42:52 -05:00
|
|
|
"latestVersion": plugins.GrafanaLatestVersion,
|
|
|
|
"hasUpdate": plugins.GrafanaHasUpdate,
|
2016-05-17 08:00:48 -05:00
|
|
|
"env": setting.Env,
|
2019-11-01 08:56:12 -05:00
|
|
|
"isEnterprise": hs.License.HasValidLicense(),
|
|
|
|
},
|
|
|
|
"licenseInfo": map[string]interface{}{
|
|
|
|
"hasLicense": hs.License.HasLicense(),
|
|
|
|
"expiry": hs.License.Expiry(),
|
2020-01-27 02:24:44 -06:00
|
|
|
"stateInfo": hs.License.StateInfo(),
|
|
|
|
"licenseUrl": hs.License.LicenseURL(c.SignedInUser),
|
2015-01-05 03:46:58 -06:00
|
|
|
},
|
2020-01-30 10:35:32 -06:00
|
|
|
"featureToggles": hs.Cfg.FeatureToggles,
|
2020-04-15 15:17:41 -05:00
|
|
|
"rendererAvailable": hs.RenderService.IsAvailable(),
|
2014-12-29 06:36:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonObj, nil
|
|
|
|
}
|
2015-02-18 07:06:44 -06:00
|
|
|
|
2016-10-26 10:42:39 -05:00
|
|
|
func getPanelSort(id string) int {
|
|
|
|
sort := 100
|
|
|
|
switch id {
|
|
|
|
case "graph":
|
|
|
|
sort = 1
|
2020-01-17 02:43:17 -06:00
|
|
|
case "stat":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 2
|
2018-12-17 10:54:40 -06:00
|
|
|
case "gauge":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 3
|
2019-02-15 14:33:36 -06:00
|
|
|
case "bargauge":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 4
|
2019-02-15 14:33:36 -06:00
|
|
|
case "table":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 5
|
2020-01-17 02:43:17 -06:00
|
|
|
case "singlestat":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 6
|
2020-01-17 02:43:17 -06:00
|
|
|
case "text":
|
2017-05-03 01:56:51 -05:00
|
|
|
sort = 7
|
2020-01-17 02:43:17 -06:00
|
|
|
case "heatmap":
|
2018-12-17 10:54:40 -06:00
|
|
|
sort = 8
|
2020-01-17 02:43:17 -06:00
|
|
|
case "alertlist":
|
2019-02-15 14:33:36 -06:00
|
|
|
sort = 9
|
2020-01-17 02:43:17 -06:00
|
|
|
case "dashlist":
|
|
|
|
sort = 10
|
|
|
|
case "news":
|
|
|
|
sort = 10
|
2016-10-26 10:42:39 -05:00
|
|
|
}
|
|
|
|
return sort
|
|
|
|
}
|
|
|
|
|
2020-03-04 05:57:20 -06:00
|
|
|
func (hs *HTTPServer) GetFrontendSettings(c *models.ReqContext) {
|
2018-10-09 10:47:43 -05:00
|
|
|
settings, err := hs.getFrontendSettingsMap(c)
|
2015-02-18 07:06:44 -06:00
|
|
|
if err != nil {
|
|
|
|
c.JsonApiErr(400, "Failed to get frontend settings", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(200, settings)
|
|
|
|
}
|