Refactoring of multi-value datasource PR #15812

This commit is contained in:
Torkel Ödegaard
2019-03-11 15:44:11 +01:00
parent a8486806aa
commit bf72b26c2c
2 changed files with 9 additions and 27 deletions

View File

@@ -79,28 +79,9 @@ class MetricsPanelCtrl extends PanelCtrl {
delete this.error; delete this.error;
this.loading = true; this.loading = true;
// set "mydatasource" to whatever the panel has defined
let mydatasource = this.panel.datasource;
let datasourceVarName = '';
// look for data source variables
for (let i = 0; i < this.templateSrv.variables.length; i++) {
const variable = this.templateSrv.variables[i];
if (variable.type !== 'datasource') {
continue;
}
datasourceVarName = variable.name;
}
// if a data source variable was found, use its value
if (datasourceVarName !== '' && this.panel.scopedVars && this.panel.scopedVars[datasourceVarName]) {
mydatasource = this.panel.scopedVars[datasourceVarName].value;
}
// load datasource service // load datasource service
this.datasourceSrv this.datasourceSrv
.get(mydatasource) .get(this.panel.datasource, this.panel.scopedVars)
.then(this.updateTimeRange.bind(this)) .then(this.updateTimeRange.bind(this))
.then(this.issueQueries.bind(this)) .then(this.issueQueries.bind(this))
.then(this.handleQueryResult.bind(this)) .then(this.handleQueryResult.bind(this))

View File

@@ -7,7 +7,7 @@ import config from 'app/core/config';
import { importPluginModule } from './plugin_loader'; import { importPluginModule } from './plugin_loader';
// Types // Types
import { DataSourceApi, DataSourceSelectItem } from '@grafana/ui/src/types'; import { DataSourceApi, DataSourceSelectItem, ScopedVars } from '@grafana/ui/src/types';
export class DatasourceSrv { export class DatasourceSrv {
datasources: { [name: string]: DataSourceApi }; datasources: { [name: string]: DataSourceApi };
@@ -21,12 +21,17 @@ export class DatasourceSrv {
this.datasources = {}; this.datasources = {};
} }
get(name?: string): Promise<DataSourceApi> { get(name?: string, scopedVars?: ScopedVars): Promise<DataSourceApi> {
if (!name) { if (!name) {
return this.get(config.defaultDatasource); return this.get(config.defaultDatasource);
} }
name = this.templateSrv.replace(name); name = this.templateSrv.replace(name, scopedVars, (value, variable) => {
if (Array.isArray(value)) {
return value[0];
}
return value;
});
if (name === 'default') { if (name === 'default') {
return this.get(config.defaultDatasource); return this.get(config.defaultDatasource);
@@ -40,10 +45,6 @@ export class DatasourceSrv {
} }
loadDatasource(name: string): Promise<DataSourceApi> { loadDatasource(name: string): Promise<DataSourceApi> {
// if there are multiple datasources provided, just use the first one
const re = /{([^,}]+).*/;
name = name.replace(re, '$1');
const dsConfig = config.datasources[name]; const dsConfig = config.datasources[name];
if (!dsConfig) { if (!dsConfig) {
return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' }); return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' });