2014-12-29 06:36:08 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
|
2015-02-05 03:37:13 -06:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2015-03-27 08:23:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/log"
|
2015-02-05 03:37:13 -06:00
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
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"
|
2015-03-02 02:58:35 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2014-12-29 06:36:08 -06:00
|
|
|
)
|
|
|
|
|
2018-10-09 10:47:43 -05:00
|
|
|
func (hs *HTTPServer) getFrontendSettingsMap(c *m.ReqContext) (map[string]interface{}, error) {
|
2015-02-23 13:07:49 -06:00
|
|
|
orgDataSources := make([]*m.DataSource, 0)
|
2014-12-29 06:36:08 -06:00
|
|
|
|
2015-03-11 11:34:11 -05:00
|
|
|
if c.OrgId != 0 {
|
2015-02-23 13:07:49 -06:00
|
|
|
query := m.GetDataSourcesQuery{OrgId: c.OrgId}
|
2014-12-29 06:36:08 -06:00
|
|
|
err := bus.Dispatch(&query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-10 08:54:37 -05:00
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
2015-02-23 13:07:49 -06:00
|
|
|
for _, ds := range orgDataSources {
|
2014-12-29 06:36:08 -06:00
|
|
|
url := ds.Url
|
|
|
|
|
|
|
|
if ds.Access == m.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,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-03 07:32:15 -05:00
|
|
|
if ds.JsonData != nil {
|
2015-05-20 11:29:20 -05:00
|
|
|
dsMap["jsonData"] = ds.JsonData
|
2017-09-18 02:38:02 -05:00
|
|
|
} else {
|
|
|
|
dsMap["jsonData"] = make(map[string]string)
|
2015-05-20 11:29:20 -05:00
|
|
|
}
|
|
|
|
|
2015-03-02 02:58:35 -06:00
|
|
|
if ds.Access == m.DS_ACCESS_DIRECT {
|
|
|
|
if ds.BasicAuth {
|
|
|
|
dsMap["basicAuth"] = util.GetBasicAuthHeader(ds.BasicAuthUser, ds.BasicAuthPassword)
|
|
|
|
}
|
2015-12-08 21:37:08 -06:00
|
|
|
if ds.WithCredentials {
|
|
|
|
dsMap["withCredentials"] = ds.WithCredentials
|
|
|
|
}
|
2015-03-02 02:58:35 -06:00
|
|
|
|
|
|
|
if ds.Type == m.DS_INFLUXDB_08 {
|
2014-12-29 06:36:08 -06:00
|
|
|
dsMap["username"] = ds.User
|
|
|
|
dsMap["password"] = ds.Password
|
|
|
|
dsMap["url"] = url + "/db/" + ds.Database
|
|
|
|
}
|
|
|
|
|
2015-03-02 02:58:35 -06:00
|
|
|
if ds.Type == m.DS_INFLUXDB {
|
2015-02-25 11:43:44 -06:00
|
|
|
dsMap["username"] = ds.User
|
|
|
|
dsMap["password"] = ds.Password
|
|
|
|
dsMap["database"] = ds.Database
|
|
|
|
dsMap["url"] = url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-17 03:39:26 -06:00
|
|
|
if ds.Type == m.DS_ES {
|
|
|
|
dsMap["index"] = ds.Database
|
|
|
|
}
|
|
|
|
|
2016-01-18 00:28:01 -06:00
|
|
|
if ds.Type == m.DS_INFLUXDB {
|
|
|
|
dsMap["database"] = ds.Database
|
|
|
|
}
|
|
|
|
|
2015-10-01 11:01:09 -05:00
|
|
|
if ds.Type == m.DS_PROMETHEUS {
|
|
|
|
// add unproxied server URL for link to Prometheus web UI
|
|
|
|
dsMap["directUrl"] = ds.Url
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-11-15 04:10:47 -06:00
|
|
|
if panel.State == plugins.PluginStateAlpha && !hs.Cfg.EnableAlphaPanels {
|
2018-10-09 10:47:43 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-08 16:15:44 -06:00
|
|
|
panels[panel.Id] = map[string]interface{}{
|
2016-12-08 03:25:05 -06:00
|
|
|
"module": panel.Module,
|
|
|
|
"baseUrl": panel.BaseUrl,
|
|
|
|
"name": panel.Name,
|
|
|
|
"id": panel.Id,
|
|
|
|
"info": panel.Info,
|
|
|
|
"hideFromList": panel.HideFromList,
|
|
|
|
"sort": getPanelSort(panel.Id),
|
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,
|
|
|
|
"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,
|
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,
|
|
|
|
"latestVersion": plugins.GrafanaLatestVersion,
|
|
|
|
"hasUpdate": plugins.GrafanaHasUpdate,
|
2016-05-17 08:00:48 -05:00
|
|
|
"env": setting.Env,
|
2018-07-02 06:33:39 -05:00
|
|
|
"isEnterprise": setting.IsEnterprise,
|
2015-01-05 03:46:58 -06:00
|
|
|
},
|
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
|
|
|
|
case "singlestat":
|
|
|
|
sort = 2
|
2018-12-17 10:54:40 -06:00
|
|
|
case "gauge":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 3
|
2018-12-17 10:54:40 -06:00
|
|
|
case "table":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 4
|
2018-12-17 10:54:40 -06:00
|
|
|
case "text":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 5
|
2018-12-17 10:54:40 -06:00
|
|
|
case "heatmap":
|
2016-10-26 10:42:39 -05:00
|
|
|
sort = 6
|
2018-12-17 10:54:40 -06:00
|
|
|
case "alertlist":
|
2017-05-03 01:56:51 -05:00
|
|
|
sort = 7
|
2018-12-17 10:54:40 -06:00
|
|
|
case "dashlist":
|
|
|
|
sort = 8
|
2016-10-26 10:42:39 -05:00
|
|
|
}
|
|
|
|
return sort
|
|
|
|
}
|
|
|
|
|
2018-10-09 10:47:43 -05:00
|
|
|
func (hs *HTTPServer) GetFrontendSettings(c *m.ReqContext) {
|
|
|
|
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)
|
|
|
|
}
|