Просмотр исходного кода

Refactoring of multi-value datasource PR #15812

Torkel Ödegaard 6 лет назад
Родитель
Сommit
bf72b26c2c

+ 1 - 20
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))

+ 8 - 7
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<DataSourceApi> {
+  get(name?: string, scopedVars?: ScopedVars): Promise<DataSourceApi> {
     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<DataSourceApi> {
-    // 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' });