mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
feat(templating): more work on context specific varaiable formats, #2918
This commit is contained in:
parent
f3ad71d751
commit
8f5a7f1764
@ -25,10 +25,13 @@ function (angular, _) {
|
|||||||
this.updateTemplateData = function() {
|
this.updateTemplateData = function() {
|
||||||
this._values = {};
|
this._values = {};
|
||||||
|
|
||||||
_.each(this.variables, function(variable) {
|
for (var i = 0; i < this.variables.length; i++) {
|
||||||
if (!variable.current || !variable.current.isNone && !variable.current.value) { return; }
|
var variable = this.variables[i];
|
||||||
this._values[variable.name] = variable.current.value;
|
if (!variable.current || !variable.current.isNone && !variable.current.value) {
|
||||||
}, this);
|
continue;
|
||||||
|
}
|
||||||
|
this._values[variable.name] = variable.current;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function regexEscape(value) {
|
function regexEscape(value) {
|
||||||
@ -42,6 +45,10 @@ function (angular, _) {
|
|||||||
this.formatValue = function(value, format) {
|
this.formatValue = function(value, format) {
|
||||||
switch(format) {
|
switch(format) {
|
||||||
case "regex": {
|
case "regex": {
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
return regexEscape(value);
|
||||||
|
}
|
||||||
|
|
||||||
var escapedValues = _.map(value, regexEscape);
|
var escapedValues = _.map(value, regexEscape);
|
||||||
return '(' + escapedValues.join('|') + ')';
|
return '(' + escapedValues.join('|') + ')';
|
||||||
}
|
}
|
||||||
@ -115,13 +122,12 @@ function (angular, _) {
|
|||||||
return match;
|
return match;
|
||||||
}
|
}
|
||||||
|
|
||||||
systemValue = self._grafanaVariables[value];
|
systemValue = self._grafanaVariables[value.value];
|
||||||
if (systemValue) {
|
if (systemValue) {
|
||||||
return self.formatValue(systemValue);
|
return self.formatValue(systemValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
var res = self.formatValue(value, format);
|
var res = self.formatValue(value.value, format);
|
||||||
console.log('replace: ' + value, res);
|
|
||||||
return res;
|
return res;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@ -130,7 +136,6 @@ function (angular, _) {
|
|||||||
if (!target) { return target; }
|
if (!target) { return target; }
|
||||||
|
|
||||||
var value;
|
var value;
|
||||||
var text;
|
|
||||||
this._regex.lastIndex = 0;
|
this._regex.lastIndex = 0;
|
||||||
|
|
||||||
return target.replace(this._regex, function(match, g1, g2) {
|
return target.replace(this._regex, function(match, g1, g2) {
|
||||||
@ -140,10 +145,9 @@ function (angular, _) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
value = self._values[g1 || g2];
|
value = self._values[g1 || g2];
|
||||||
text = self._texts[g1 || g2];
|
|
||||||
if (!value) { return match; }
|
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) {
|
return _.map(_.keys(options).sort(), function(key) {
|
||||||
var option = { text: key, value: 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;
|
return option;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// this.shouldRegexEscape = function(variable) {
|
|
||||||
// return (variable.includeAll || variable.multi) && variable.allFormat.indexOf('regex') !== -1;
|
|
||||||
// };
|
|
||||||
//
|
|
||||||
|
|
||||||
this.addAllOption = function(variable) {
|
this.addAllOption = function(variable) {
|
||||||
// var allValue = '';
|
if (variable.allValue) {
|
||||||
// switch(variable.allFormat) {
|
variable.options.unshift({text: 'All', value: variable.allValue, isAll: true});
|
||||||
// case 'wildcard': {
|
return;
|
||||||
// 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 += '}';
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
var value =_.pluck(variable.options, 'text');
|
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;
|
var requestOptions;
|
||||||
ctx.backendSrv.datasourceRequest = function(options) {
|
ctx.backendSrv.datasourceRequest = function(options) {
|
||||||
requestOptions = options;
|
requestOptions = options;
|
||||||
return ctx.$q.when({});
|
return ctx.$q.when({data: {}});
|
||||||
};
|
};
|
||||||
|
|
||||||
ctx.ds.testDatasource();
|
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() {
|
it('should properly escape $test with lucene escape sequences', function() {
|
||||||
_templateSrv.init([{name: 'test', current: {value: 'value/4' }}]);
|
_templateSrv.init([{name: 'test', current: {value: 'value/4' }}]);
|
||||||
var target = _templateSrv.replace('this:$test', {}, 'lucene');
|
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() {
|
it('single value should return value', function() {
|
||||||
var result = _templateSrv.renderVariableValue({current: {value: 'test'}});
|
var result = _templateSrv.formatValue('test');
|
||||||
expect(result).to.be('test');
|
expect(result).to.be('test');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('multi value and glob format should render glob string', function() {
|
it('multi value and glob format should render glob string', function() {
|
||||||
var result = _templateSrv.renderVariableValue({
|
var result = _templateSrv.formatValue(['test','test2'], 'glob');
|
||||||
multiFormat: 'glob',
|
|
||||||
current: {
|
|
||||||
value: ['test','test2'],
|
|
||||||
}
|
|
||||||
});
|
|
||||||
expect(result).to.be('{test,test2}');
|
expect(result).to.be('{test,test2}');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('multi value and lucene should render as lucene expr', function() {
|
it('multi value and lucene should render as lucene expr', function() {
|
||||||
var result = _templateSrv.renderVariableValue({
|
var result = _templateSrv.formatValue(['test','test2'], 'lucene');
|
||||||
multiFormat: 'lucene',
|
expect(result).to.be('("test" OR "test2")');
|
||||||
current: {
|
|
||||||
value: ['test','test2'],
|
|
||||||
}
|
|
||||||
});
|
|
||||||
expect(result).to.be('(\\\"test\\\" OR \\\"test2\\\")');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('multi value and regex format should render regex string', function() {
|
it('multi value and regex format should render regex string', function() {
|
||||||
var result = _templateSrv.renderVariableValue({
|
var result = _templateSrv.formatValue(['test.','test2'], 'regex');
|
||||||
multiFormat: 'regex values',
|
expect(result).to.be('(test\\.|test2)');
|
||||||
current: {
|
|
||||||
value: ['test','test2'],
|
|
||||||
}
|
|
||||||
});
|
|
||||||
expect(result).to.be('(test|test2)');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('multi value and pipe should render pipe string', function() {
|
it('multi value and pipe should render pipe string', function() {
|
||||||
var result = _templateSrv.renderVariableValue({
|
var result = _templateSrv.formatValue(['test','test2'], 'pipe');
|
||||||
multiFormat: 'pipe',
|
|
||||||
current: {
|
|
||||||
value: ['test','test2'],
|
|
||||||
}
|
|
||||||
});
|
|
||||||
expect(result).to.be('test|test2');
|
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.setup(function() {
|
||||||
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
scenario.variable = { type: 'query', query: 'apps.*', name: 'test' };
|
||||||
scenario.variable.regex = 'backend_01';
|
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.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'}];
|
scenario.queryResult = [{text: 'backend1'}, {text: 'backend2'}, { text: 'backend3'}];
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add All Glob option', function() {
|
it('should add All option', function() {
|
||||||
expect(scenario.variable.options[0].value).to.be('{backend1,backend2,backend3}');
|
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.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'}];
|
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('*');
|
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');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user