Commit d2437d3c by Torkel Ödegaard

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

parent 43fa852c
...@@ -51,6 +51,31 @@ describe('templateSrv', function() { ...@@ -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() { describe('replace can pass multi / all format', function() {
beforeEach(function() { beforeEach(function() {
initTemplateSrv([{type: 'query', name: 'test', current: {value: ['value1', 'value2'] }}]); initTemplateSrv([{type: 'query', name: 'test', current: {value: ['value1', 'value2'] }}]);
......
...@@ -15,7 +15,6 @@ function (angular, _, kbn) { ...@@ -15,7 +15,6 @@ function (angular, _, kbn) {
this._index = {}; this._index = {};
this._texts = {}; this._texts = {};
this._grafanaVariables = {}; this._grafanaVariables = {};
this._adhocVariables = {};
// default built ins // default built ins
this._builtIns = {}; this._builtIns = {};
...@@ -30,24 +29,16 @@ function (angular, _, kbn) { ...@@ -30,24 +29,16 @@ function (angular, _, kbn) {
this.updateTemplateData = function() { this.updateTemplateData = function() {
this._index = {}; this._index = {};
this._filters = {}; this._filters = {};
this._adhocVariables = {};
for (var i = 0; i < this.variables.length; i++) { for (var i = 0; i < this.variables.length; i++) {
var variable = this.variables[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) { if (!variable.current || !variable.current.isNone && !variable.current.value) {
continue; continue;
} }
this._index[variable.name] = variable; this._index[variable.name] = variable;
} }
}; };
this.variableInitialized = function(variable) { this.variableInitialized = function(variable) {
...@@ -55,11 +46,26 @@ function (angular, _, kbn) { ...@@ -55,11 +46,26 @@ function (angular, _, kbn) {
}; };
this.getAdhocFilters = function(datasourceName) { this.getAdhocFilters = function(datasourceName) {
var variable = this._adhocVariables[datasourceName]; var filters = [];
if (variable) {
return variable.filters || []; for (var i = 0; i < this.variables.length; i++) {
var variable = this.variables[i];
if (variable.type !== 'adhoc') {
continue;
} }
return [];
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 filters;
}; };
function luceneEscape(value) { function luceneEscape(value) {
......
...@@ -247,8 +247,6 @@ export class VariableSrv { ...@@ -247,8 +247,6 @@ export class VariableSrv {
} }
filter.operator = options.operator; filter.operator = options.operator;
variable.setFilters(filters);
this.variableUpdated(variable, true); this.variableUpdated(variable, true);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment