From 2416ee04c840c45a4c38087a6e6a2ef4053a7543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 20 May 2016 09:58:07 +0200 Subject: [PATCH 1/2] fix(templating): fixed detection of nested template variables, fixes #5103 --- public/app/features/templating/templateSrv.js | 7 +++++-- public/test/specs/templateSrv-specs.js | 5 +++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/public/app/features/templating/templateSrv.js b/public/app/features/templating/templateSrv.js index 7e96af22e2a..e2e628c1cd5 100644 --- a/public/app/features/templating/templateSrv.js +++ b/public/app/features/templating/templateSrv.js @@ -97,8 +97,11 @@ function (angular, _) { if (!str) { return false; } - var match = this._regex.exec(str); - return match && (match[1] === variableName || match[2] === variableName); + + variableName = regexEscape(variableName); + var findVarRegex = new RegExp('\\$(' + variableName + ')[\\W|$]|\\[\\[(' + variableName + ')\\]\\]', 'g'); + var match = findVarRegex.exec(str); + return match !== null; }; this.highlightVariablesAsHtml = function(str) { diff --git a/public/test/specs/templateSrv-specs.js b/public/test/specs/templateSrv-specs.js index fd7247cbd81..c1f0e25ff9e 100644 --- a/public/test/specs/templateSrv-specs.js +++ b/public/test/specs/templateSrv-specs.js @@ -200,6 +200,11 @@ define([ expect(contains).to.be(true); }); + it('should find it when part of segment', function() { + var contains = _templateSrv.containsVariable('metrics.$env.$group-*', 'group'); + expect(contains).to.be(true); + }); + }); describe('updateTemplateData with simple value', function() { From e0e8fd663776a995142aa87565c282c11b3a9ed0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 20 May 2016 10:24:24 +0200 Subject: [PATCH 2/2] fix(templating): fixed handling of numeric values in tempalting query results, fixes #5097 --- public/app/features/templating/templateValuesSrv.js | 8 ++++++++ public/test/specs/templateSrv-specs.js | 4 ++-- public/test/specs/templateValuesSrv-specs.js | 13 +++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/public/app/features/templating/templateValuesSrv.js b/public/app/features/templating/templateValuesSrv.js index d44f07ed366..489e6291011 100644 --- a/public/app/features/templating/templateValuesSrv.js +++ b/public/app/features/templating/templateValuesSrv.js @@ -313,6 +313,14 @@ function (angular, _, kbn) { var value = item.value || item.text; var text = item.text || item.value; + if (_.isNumber(value)) { + value = value.toString(); + } + + if (_.isNumber(text)) { + text = text.toString(); + } + if (regex) { matches = regex.exec(value); if (!matches) { continue; } diff --git a/public/test/specs/templateSrv-specs.js b/public/test/specs/templateSrv-specs.js index c1f0e25ff9e..c5f33d4f72f 100644 --- a/public/test/specs/templateSrv-specs.js +++ b/public/test/specs/templateSrv-specs.js @@ -141,8 +141,8 @@ define([ }); it('slash should be properly escaped in regex format', function() { - var result = _templateSrv.formatValue('Gi3/14', 'regex'); - expect(result).to.be('Gi3\\/14'); + var result = _templateSrv.formatValue('Gi3/14', 'regex'); + expect(result).to.be('Gi3\\/14'); }); }); diff --git a/public/test/specs/templateValuesSrv-specs.js b/public/test/specs/templateValuesSrv-specs.js index 2edcd03da9a..8652ec421b2 100644 --- a/public/test/specs/templateValuesSrv-specs.js +++ b/public/test/specs/templateValuesSrv-specs.js @@ -126,6 +126,19 @@ define([ }); }); + describeUpdateVariable('query variable with numeric results', function(scenario) { + scenario.setup(function() { + scenario.variable = { type: 'query', query: '', name: 'test', current: {} }; + scenario.queryResult = [{text: 12, value: 12}]; + }); + + it('should set current value to first option', function() { + expect(scenario.variable.current.value).to.be('12'); + expect(scenario.variable.options[0].value).to.be('12'); + expect(scenario.variable.options[0].text).to.be('12'); + }); + }); + describeUpdateVariable('interval variable without auto', function(scenario) { scenario.setup(function() { scenario.variable = { type: 'interval', query: '1s,2h,5h,1d', name: 'test' };