Commit 8f5a7f17 by Torkel Ödegaard

feat(templating): more work on context specific varaiable formats, #2918

parent f3ad71d7
......@@ -25,10 +25,13 @@ function (angular, _) {
this.updateTemplateData = function() {
this._values = {};
_.each(this.variables, function(variable) {
if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
this._values[variable.name] = variable.current.value;
}, this);
for (var i = 0; i < this.variables.length; i++) {
var variable = this.variables[i];
if (!variable.current || !variable.current.isNone && !variable.current.value) {
continue;
}
this._values[variable.name] = variable.current;
}
};
function regexEscape(value) {
......@@ -42,6 +45,10 @@ function (angular, _) {
this.formatValue = function(value, format) {
switch(format) {
case "regex": {
if (typeof value === 'string') {
return regexEscape(value);
}
var escapedValues = _.map(value, regexEscape);
return '(' + escapedValues.join('|') + ')';
}
......@@ -115,13 +122,12 @@ function (angular, _) {
return match;
}
systemValue = self._grafanaVariables[value];
systemValue = self._grafanaVariables[value.value];
if (systemValue) {
return self.formatValue(systemValue);
}
var res = self.formatValue(value, format);
console.log('replace: ' + value, res);
var res = self.formatValue(value.value, format);
return res;
});
};
......@@ -130,7 +136,6 @@ function (angular, _) {
if (!target) { return target; }
var value;
var text;
this._regex.lastIndex = 0;
return target.replace(this._regex, function(match, g1, g2) {
......@@ -140,10 +145,9 @@ function (angular, _) {
}
value = self._values[g1 || g2];
text = self._texts[g1 || g2];
if (!value) { return match; }
return self._grafanaVariables[value] || text;
return self._grafanaVariables[value.value] || value.text;
});
};
......
......@@ -225,58 +225,18 @@ function (angular, _, kbn) {
return _.map(_.keys(options).sort(), function(key) {
var option = { text: key, value: key };
// // check if values need to be regex escaped
// if (self.shouldRegexEscape(variable)) {
// option.value = self.regexEscape(option.value);
// }
return option;
});
};
// this.shouldRegexEscape = function(variable) {
// return (variable.includeAll || variable.multi) && variable.allFormat.indexOf('regex') !== -1;
// };
//
this.addAllOption = function(variable) {
// var allValue = '';
// switch(variable.allFormat) {
// case 'wildcard': {
// allValue = '*';
// break;
// }
// case 'regex wildcard': {
// allValue = '.*';
// break;
// }
// case 'lucene': {
// var quotedValues = _.map(variable.options, function(val) {
// return '\\\"' + val.text + '\\\"';
// });
// allValue = '(' + quotedValues.join(' OR ') + ')';
// break;
// }
// case 'regex values': {
// allValue = '(' + _.map(variable.options, function(option) {
// return self.regexEscape(option.text);
// }).join('|') + ')';
// break;
// }
// case 'pipe': {
// allValue = _.pluck(variable.options, 'text').join('|');
// break;
// }
// default: {
// allValue = '{';
// allValue += _.pluck(variable.options, 'text').join(',');
// allValue += '}';
// }
// }
//
if (variable.allValue) {
variable.options.unshift({text: 'All', value: variable.allValue, isAll: true});
return;
}
var value =_.pluck(variable.options, 'text');
variable.options.unshift({text: 'All', value: value});
variable.options.unshift({text: 'All', value: value, isAll: true});
};
});
......
......@@ -33,7 +33,7 @@ describe('ElasticDatasource', function() {
var requestOptions;
ctx.backendSrv.datasourceRequest = function(options) {
requestOptions = options;
return ctx.$q.when({});
return ctx.$q.when({data: {}});
};
ctx.ds.testDatasource();
......
......@@ -66,7 +66,7 @@ define([
});
});
describe.only('lucene format', function() {
describe('lucene format', function() {
it('should properly escape $test with lucene escape sequences', function() {
_templateSrv.init([{name: 'test', current: {value: 'value/4' }}]);
var target = _templateSrv.replace('this:$test', {}, 'lucene');
......@@ -74,49 +74,29 @@ define([
});
});
describe('render variable to string values', function() {
describe('format variable to string values', function() {
it('single value should return value', function() {
var result = _templateSrv.renderVariableValue({current: {value: 'test'}});
var result = _templateSrv.formatValue('test');
expect(result).to.be('test');
});
it('multi value and glob format should render glob string', function() {
var result = _templateSrv.renderVariableValue({
multiFormat: 'glob',
current: {
value: ['test','test2'],
}
});
var result = _templateSrv.formatValue(['test','test2'], 'glob');
expect(result).to.be('{test,test2}');
});
it('multi value and lucene should render as lucene expr', function() {
var result = _templateSrv.renderVariableValue({
multiFormat: 'lucene',
current: {
value: ['test','test2'],
}
});
expect(result).to.be('(\\\"test\\\" OR \\\"test2\\\")');
var result = _templateSrv.formatValue(['test','test2'], 'lucene');
expect(result).to.be('("test" OR "test2")');
});
it('multi value and regex format should render regex string', function() {
var result = _templateSrv.renderVariableValue({
multiFormat: 'regex values',
current: {
value: ['test','test2'],
}
});
expect(result).to.be('(test|test2)');
var result = _templateSrv.formatValue(['test.','test2'], 'regex');
expect(result).to.be('(test\\.|test2)');
});
it('multi value and pipe should render pipe string', function() {
var result = _templateSrv.renderVariableValue({
multiFormat: 'pipe',
current: {
value: ['test','test2'],
}
});
var result = _templateSrv.formatValue(['test','test2'], 'pipe');
expect(result).to.be('test|test2');
});
......@@ -223,7 +203,6 @@ define([
});
});
});
});
......@@ -247,7 +247,7 @@ define([
});
});
describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
describeUpdateVariable('regex pattern remove duplicates', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
scenario.variable.regex = 'backend_01';
......@@ -259,107 +259,30 @@ define([
});
});
describeUpdateVariable('with include All glob syntax', function(scenario) {
describeUpdateVariable('with include All', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
scenario.variable = {type: 'query', query: 'apps.*', name: 'test', includeAll: true};
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add All Glob option', function() {
expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
it('should add All option', function() {
expect(scenario.variable.options[0].text).to.be('All');
expect(scenario.variable.options[0].value).to.eql(['backend1', 'backend2', 'backend3']);
expect(scenario.variable.options[0].isAll).to.be(true);
});
});
describeUpdateVariable('with include all wildcard', function(scenario) {
describeUpdateVariable('with include all and custom value', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allValue: '*' };
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add All wildcard option', function() {
it('should add All option with custom value', function() {
expect(scenario.variable.options[0].value).to.be('*');
});
});
describeUpdateVariable('with include all wildcard', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex wildcard' };
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add All wildcard option', function() {
expect(scenario.variable.options[0].value).to.be('.*');
});
});
describeUpdateVariable('with include all regex values', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'wildcard' };
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add All wildcard option', function() {
expect(scenario.variable.options[0].value).to.be('*');
});
});
describeUpdateVariable('with include all glob no values', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'glob' };
scenario.queryResult = [];
});
it('should add empty glob', function() {
expect(scenario.variable.options[0].value).to.be('{}');
});
});
describeUpdateVariable('with include all lucene and values', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'lucene' };
scenario.queryResult = [{text: 'backend1'}, { text: 'backend2'}];
});
it('should add lucene glob', function() {
expect(scenario.variable.options[0].value).to.be('(\\\"backend1\\\" OR \\\"backend2\\\")');
});
});
describeUpdateVariable('with include all regex all values', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add empty glob', function() {
expect(scenario.variable.options[0].value).to.be('(backend1|backend2|backend3)');
});
});
describeUpdateVariable('with include all regex values and values require escaping', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'regex values' };
scenario.queryResult = [{text: '/root'}, {text: '/var'}, { text: '/lib'}];
});
it('should regex escape options', function() {
expect(scenario.variable.options[0].value).to.be('(\\/lib|\\/root|\\/var)');
expect(scenario.variable.options[1].value).to.be('\\/lib');
expect(scenario.variable.options[1].text).to.be('/lib');
});
});
describeUpdateVariable('with include all pipe all values', function(scenario) {
scenario.setup(function() {
scenario.variable = { type: 'query', query: 'apps.*', name: 'test', includeAll: true, allFormat: 'pipe' };
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
});
it('should add pipe delimited string', function() {
expect(scenario.variable.options[0].value).to.be('backend1|backend2|backend3');
});
});
});
});
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