Browse Source

fix: ad-hoc filters now works with data source variables, fixes #8052

Torkel Ödegaard 8 năm trước cách đây
mục cha
commit
d2437d3cf1

+ 25 - 0
public/app/features/templating/specs/template_srv_specs.ts

@@ -51,6 +51,31 @@ describe('templateSrv', function() {
     });
   });
 
+  describe('getAdhocFilters', function() {
+    beforeEach(function() {
+      initTemplateSrv([
+        {type: 'datasource', name: 'ds', current: {value: 'logstash', text: 'logstash'}},
+        {type: 'adhoc', name: 'test', datasource: 'oogle', filters: [1]},
+        {type: 'adhoc', name: 'test2', datasource: '$ds', filters: [2]},
+      ]);
+    });
+
+    it('should return filters if datasourceName match', function() {
+      var filters = _templateSrv.getAdhocFilters('oogle');
+      expect(filters).to.eql([1]);
+    });
+
+    it('should return empty array if datasourceName does not match', function() {
+      var filters = _templateSrv.getAdhocFilters('oogleasdasd');
+      expect(filters).to.eql([]);
+    });
+
+    it('should return filters when datasourceName match via data source variable', function() {
+      var filters = _templateSrv.getAdhocFilters('logstash');
+      expect(filters).to.eql([2]);
+    });
+  });
+
   describe('replace can pass multi / all format', function() {
     beforeEach(function() {
       initTemplateSrv([{type: 'query', name: 'test', current: {value: ['value1', 'value2'] }}]);

+ 19 - 13
public/app/features/templating/templateSrv.js

@@ -15,7 +15,6 @@ function (angular, _, kbn) {
     this._index = {};
     this._texts = {};
     this._grafanaVariables = {};
-    this._adhocVariables = {};
 
     // default built ins
     this._builtIns = {};
@@ -30,24 +29,16 @@ function (angular, _, kbn) {
     this.updateTemplateData = function() {
       this._index = {};
       this._filters = {};
-      this._adhocVariables = {};
 
       for (var i = 0; i < this.variables.length; i++) {
         var variable = this.variables[i];
 
-        // add adhoc filters to it's own index
-        if (variable.type === 'adhoc') {
-          this._adhocVariables[variable.datasource] = variable;
-          continue;
-        }
-
         if (!variable.current || !variable.current.isNone && !variable.current.value) {
           continue;
         }
 
         this._index[variable.name] = variable;
       }
-
     };
 
     this.variableInitialized = function(variable) {
@@ -55,11 +46,26 @@ function (angular, _, kbn) {
     };
 
     this.getAdhocFilters = function(datasourceName) {
-      var variable = this._adhocVariables[datasourceName];
-      if (variable) {
-        return variable.filters || [];
+      var filters = [];
+
+      for (var i = 0; i < this.variables.length; i++) {
+        var variable = this.variables[i];
+        if (variable.type !== 'adhoc') {
+          continue;
+        }
+
+        if (variable.datasource === datasourceName) {
+          filters = filters.concat(variable.filters);
+        }
+
+        if (variable.datasource.indexOf('$') === 0) {
+          if (this.replace(variable.datasource) === datasourceName) {
+            filters = filters.concat(variable.filters);
+          }
+        }
       }
-      return [];
+
+      return filters;
     };
 
     function luceneEscape(value) {

+ 0 - 2
public/app/features/templating/variable_srv.ts

@@ -247,8 +247,6 @@ export class VariableSrv {
     }
 
     filter.operator = options.operator;
-
-    variable.setFilters(filters);
     this.variableUpdated(variable, true);
   }