From d2437d3cf1d562610302f596eece02d2e4e66c6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 10 Aug 2017 12:04:46 +0200 Subject: [PATCH] fix: ad-hoc filters now works with data source variables, fixes #8052 --- .../templating/specs/template_srv_specs.ts | 25 +++++++++++++++ public/app/features/templating/templateSrv.js | 32 +++++++++++-------- .../app/features/templating/variable_srv.ts | 2 -- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/public/app/features/templating/specs/template_srv_specs.ts b/public/app/features/templating/specs/template_srv_specs.ts index 673273f240a..3c8b146dc18 100644 --- a/public/app/features/templating/specs/template_srv_specs.ts +++ b/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'] }}]); diff --git a/public/app/features/templating/templateSrv.js b/public/app/features/templating/templateSrv.js index f47afc81574..31f8bdcbb13 100644 --- a/public/app/features/templating/templateSrv.js +++ b/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) { diff --git a/public/app/features/templating/variable_srv.ts b/public/app/features/templating/variable_srv.ts index c147c5b6685..a0e2dfde23d 100644 --- a/public/app/features/templating/variable_srv.ts +++ b/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); }