From bf72b26c2c854ef8c6b7dbf13dc71223d676a647 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 11 Mar 2019 15:44:11 +0100 Subject: [PATCH] Refactoring of multi-value datasource PR #15812 --- .../app/features/panel/metrics_panel_ctrl.ts | 21 +------------------ public/app/features/plugins/datasource_srv.ts | 15 ++++++------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/public/app/features/panel/metrics_panel_ctrl.ts b/public/app/features/panel/metrics_panel_ctrl.ts index 92412263f73..3e217369b15 100644 --- a/public/app/features/panel/metrics_panel_ctrl.ts +++ b/public/app/features/panel/metrics_panel_ctrl.ts @@ -79,28 +79,9 @@ class MetricsPanelCtrl extends PanelCtrl { delete this.error; 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 this.datasourceSrv - .get(mydatasource) + .get(this.panel.datasource, this.panel.scopedVars) .then(this.updateTimeRange.bind(this)) .then(this.issueQueries.bind(this)) .then(this.handleQueryResult.bind(this)) diff --git a/public/app/features/plugins/datasource_srv.ts b/public/app/features/plugins/datasource_srv.ts index aca87d8c63b..a98ac99eaf1 100644 --- a/public/app/features/plugins/datasource_srv.ts +++ b/public/app/features/plugins/datasource_srv.ts @@ -7,7 +7,7 @@ import config from 'app/core/config'; import { importPluginModule } from './plugin_loader'; // Types -import { DataSourceApi, DataSourceSelectItem } from '@grafana/ui/src/types'; +import { DataSourceApi, DataSourceSelectItem, ScopedVars } from '@grafana/ui/src/types'; export class DatasourceSrv { datasources: { [name: string]: DataSourceApi }; @@ -21,12 +21,17 @@ export class DatasourceSrv { this.datasources = {}; } - get(name?: string): Promise { + get(name?: string, scopedVars?: ScopedVars): Promise { if (!name) { 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') { return this.get(config.defaultDatasource); @@ -40,10 +45,6 @@ export class DatasourceSrv { } loadDatasource(name: string): Promise { - // if there are multiple datasources provided, just use the first one - const re = /{([^,}]+).*/; - name = name.replace(re, '$1'); - const dsConfig = config.datasources[name]; if (!dsConfig) { return this.$q.reject({ message: 'Datasource named ' + name + ' was not found' });