Chore: Enable gocyclo linter (#26395)
* Chore: Enable gocyclo linter Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * API: Fix linting issue Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * API: Refactor to reduce complexity Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com> * Fix type assertion Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
+48
-39
@@ -14,8 +14,7 @@ import (
|
||||
"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 *models.ReqContext) (map[string]interface{}, error) {
|
||||
func getFSDataSources(c *models.ReqContext, enabledPlugins *plugins.EnabledPlugins) (map[string]interface{}, error) {
|
||||
orgDataSources := make([]*models.DataSource, 0)
|
||||
|
||||
if c.OrgId != 0 {
|
||||
@@ -42,21 +41,7 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
}
|
||||
}
|
||||
|
||||
datasources := make(map[string]interface{})
|
||||
var defaultDatasource string
|
||||
|
||||
enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pluginsToPreload := []string{}
|
||||
|
||||
for _, app := range enabledPlugins.Apps {
|
||||
if app.Preload {
|
||||
pluginsToPreload = append(pluginsToPreload, app.Module)
|
||||
}
|
||||
}
|
||||
dataSources := make(map[string]interface{})
|
||||
|
||||
for _, ds := range orgDataSources {
|
||||
url := ds.Url
|
||||
@@ -65,12 +50,13 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
url = "/api/datasources/proxy/" + strconv.FormatInt(ds.Id, 10)
|
||||
}
|
||||
|
||||
var dsMap = map[string]interface{}{
|
||||
"id": ds.Id,
|
||||
"uid": ds.Uid,
|
||||
"type": ds.Type,
|
||||
"name": ds.Name,
|
||||
"url": url,
|
||||
dsMap := map[string]interface{}{
|
||||
"id": ds.Id,
|
||||
"uid": ds.Uid,
|
||||
"type": ds.Type,
|
||||
"name": ds.Name,
|
||||
"url": url,
|
||||
"isDefault": ds.IsDefault,
|
||||
}
|
||||
|
||||
meta, exists := enabledPlugins.DataSources[ds.Type]
|
||||
@@ -78,17 +64,8 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
log.Errorf(3, "Could not find plugin definition for data source: %v", ds.Type)
|
||||
continue
|
||||
}
|
||||
|
||||
if meta.Preload {
|
||||
pluginsToPreload = append(pluginsToPreload, meta.Module)
|
||||
}
|
||||
|
||||
dsMap["meta"] = meta
|
||||
|
||||
if ds.IsDefault {
|
||||
defaultDatasource = ds.Name
|
||||
}
|
||||
|
||||
jsonData := ds.JsonData
|
||||
if jsonData == nil {
|
||||
jsonData = simplejson.New()
|
||||
@@ -126,13 +103,14 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
jsonData.Set("directUrl", ds.Url)
|
||||
}
|
||||
|
||||
datasources[ds.Name] = dsMap
|
||||
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)
|
||||
// add data sources that are built in (meaning they are not added via data sources page, nor have any entry in
|
||||
// the datasource table)
|
||||
for _, ds := range plugins.DataSources {
|
||||
if ds.BuiltIn {
|
||||
datasources[ds.Name] = map[string]interface{}{
|
||||
dataSources[ds.Name] = map[string]interface{}{
|
||||
"type": ds.Type,
|
||||
"name": ds.Name,
|
||||
"meta": plugins.DataSources[ds.Id],
|
||||
@@ -140,8 +118,39 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
}
|
||||
}
|
||||
|
||||
if defaultDatasource == "" {
|
||||
defaultDatasource = "-- Grafana --"
|
||||
return dataSources, nil
|
||||
}
|
||||
|
||||
// getFrontendSettingsMap returns a json object with all the settings needed for front end initialisation.
|
||||
func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]interface{}, error) {
|
||||
enabledPlugins, err := plugins.GetEnabledPlugins(c.OrgId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pluginsToPreload := []string{}
|
||||
for _, app := range enabledPlugins.Apps {
|
||||
if app.Preload {
|
||||
pluginsToPreload = append(pluginsToPreload, app.Module)
|
||||
}
|
||||
}
|
||||
|
||||
dataSources, err := getFSDataSources(c, enabledPlugins)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defaultDS := "-- Grafana --"
|
||||
for n, ds := range dataSources {
|
||||
dsM := ds.(map[string]interface{})
|
||||
if isDefault, _ := dsM["isDefault"].(bool); isDefault {
|
||||
defaultDS = n
|
||||
}
|
||||
delete(dsM, "isDefault")
|
||||
|
||||
meta := dsM["meta"].(*plugins.DataSourcePlugin)
|
||||
if meta.Preload {
|
||||
pluginsToPreload = append(pluginsToPreload, meta.Module)
|
||||
}
|
||||
}
|
||||
|
||||
panels := map[string]interface{}{}
|
||||
@@ -179,8 +188,8 @@ func (hs *HTTPServer) getFrontendSettingsMap(c *models.ReqContext) (map[string]i
|
||||
}
|
||||
|
||||
jsonObj := map[string]interface{}{
|
||||
"defaultDatasource": defaultDatasource,
|
||||
"datasources": datasources,
|
||||
"defaultDatasource": defaultDS,
|
||||
"datasources": dataSources,
|
||||
"minRefreshInterval": setting.MinRefreshInterval,
|
||||
"panels": panels,
|
||||
"appUrl": setting.AppUrl,
|
||||
|
||||
Reference in New Issue
Block a user