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

This commit is contained in:
Torkel Ödegaard 2017-08-10 12:04:46 +02:00
parent 43fa852cc1
commit d2437d3cf1
3 changed files with 44 additions and 15 deletions

View File

@ -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'] }}]);

View File

@ -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) {

View File

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