From cc96cfe0c768fb7cd6012041c2781603f4c66df1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Fri, 5 Sep 2014 14:03:36 +0200 Subject: [PATCH] Templating: Ability to use template variables for function parameters via custom variable type, can be used as parameter for movingAverage or scaleToSeconds for example, Closes #262 --- CHANGELOG.md | 2 ++ src/app/partials/templating_editor.html | 9 +++++++++ src/app/services/templateSrv.js | 1 + src/app/services/templateValuesSrv.js | 17 ++++++++++++----- src/test/specs/templateSrv-specs.js | 5 +++++ src/test/specs/templateValuesSrv-specs.js | 18 ++++++++++++++++++ 6 files changed, 47 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 07c1e5bbf5d..ca08e5a744d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - [Issue #296](https://github.com/grafana/grafana/issues/296). Templating: Can now retrieve variable values from a non-default data source - [Issue #219](https://github.com/grafana/grafana/issues/219). Templating: Template variable value selection is now a typeahead autocomplete dropdown - [Issue #760](https://github.com/grafana/grafana/issues/760). Templating: Extend template variable syntax to include $variable syntax replacement +- [Issue #234](https://github.com/grafana/grafana/issues/234). Templating: Interval variable type for time intervals summarize/group by parameter, included "auto" option, and auto step counts option. +- [Issue #262](https://github.com/grafana/grafana/issues/262). Templating: Ability to use template variables for function parameters via custom variable type, can be used as parameter for movingAverage or scaleToSeconds for example **InfluxDB Breaking changes** - To better support templating, fill(0) and group by time low limit some changes has been made to the editor and query model schema diff --git a/src/app/partials/templating_editor.html b/src/app/partials/templating_editor.html index 8a3ea086eca..b0431b8d1e3 100644 --- a/src/app/partials/templating_editor.html +++ b/src/app/partials/templating_editor.html @@ -89,6 +89,15 @@ +
+
+
+ + +
+
+
+
diff --git a/src/app/services/templateSrv.js b/src/app/services/templateSrv.js index 26263780406..f8247d1c5cb 100644 --- a/src/app/services/templateSrv.js +++ b/src/app/services/templateSrv.js @@ -58,6 +58,7 @@ function (angular, _) { if (self._templateData[g1 || g2]) { return '' + match + ''; } + return match; }); }; diff --git a/src/app/services/templateValuesSrv.js b/src/app/services/templateValuesSrv.js index e1dc2b89741..52d3f51ccb3 100644 --- a/src/app/services/templateValuesSrv.js +++ b/src/app/services/templateValuesSrv.js @@ -71,13 +71,20 @@ function (angular, _, kbn) { return $q.all(promises); }; - this.updateOptions = function(variable) { - if (variable.type === 'interval') { - variable.options = _.map(variable.query.split(','), function(text) { - return { text: text, value: text }; - }); + this._updateNonQueryVariable = function(variable) { + // extract options in comma seperated string + variable.options = _.map(variable.query.split(/[\s,]+/), function(text) { + return { text: text, value: text }; + }); + if (variable.type === 'interval') { self.updateAutoInterval(variable); + } + }; + + this.updateOptions = function(variable) { + if (variable.type !== 'query') { + self._updateNonQueryVariable(variable); self.setVariableValue(variable, variable.options[0]); return $q.when([]); } diff --git a/src/test/specs/templateSrv-specs.js b/src/test/specs/templateSrv-specs.js index ecd046ac8fe..ac711dce66a 100644 --- a/src/test/specs/templateSrv-specs.js +++ b/src/test/specs/templateSrv-specs.js @@ -55,6 +55,11 @@ define([ expect(result).to.be('this $test ok'); }); + it('should ignore if variables does not exist', function() { + var result = _templateSrv.highlightVariablesAsHtml('this $google ok'); + expect(result).to.be('this $google ok'); + }); + }); describe('updateTemplateData with simple value', function() { diff --git a/src/test/specs/templateValuesSrv-specs.js b/src/test/specs/templateValuesSrv-specs.js index abf44442db5..f1faa8d595a 100644 --- a/src/test/specs/templateValuesSrv-specs.js +++ b/src/test/specs/templateValuesSrv-specs.js @@ -86,6 +86,24 @@ define([ }); }); + describeUpdateVariable('update custom variable', function(scenario) { + scenario.setup(function() { + scenario.variable = { type: 'custom', query: 'hej, hop, asd', name: 'test'}; + }); + + it('should update options array', function() { + expect(scenario.variable.options.length).to.be(3); + expect(scenario.variable.options[0].text).to.be('hej'); + expect(scenario.variable.options[1].value).to.be('hop'); + }); + + it('should set $__auto_interval', function() { + var call = ctx.templateSrv.setGrafanaVariable.getCall(0); + expect(call.args[0]).to.be('$__auto_interval'); + expect(call.args[1]).to.be('12h'); + }); + }); + describeUpdateVariable('basic query variable', function(scenario) { scenario.setup(function() { scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };