Allow for multiple auto interval template variables (#9216)

* Allow for multiple auto interval template variables without them overwriting each other's value.

* Add test for multiple auto interval template variables.

* Correctly handle old links with .
This commit is contained in:
Alin Sinpalean
2017-10-24 14:39:10 +02:00
committed by Torkel Ödegaard
parent 834446260f
commit d99f4f95c0
3 changed files with 80 additions and 5 deletions

View File

@@ -254,9 +254,9 @@ describe('templateSrv', function() {
beforeEach(function() {
initTemplateSrv([
{type: 'query', name: 'server', current: { value: '{asd,asd2}', text: 'All' } },
{type: 'interval', name: 'period', current: { value: '$__auto_interval', text: 'auto' } }
{type: 'interval', name: 'period', current: { value: '$__auto_interval_interval', text: 'auto' } }
]);
_templateSrv.setGrafanaVariable('$__auto_interval', '13m');
_templateSrv.setGrafanaVariable('$__auto_interval_interval', '13m');
_templateSrv.updateTemplateData();
});

View File

@@ -84,11 +84,19 @@ describe('VariableSrv', function() {
it('should update options array', function() {
expect(scenario.variable.options.length).to.be(5);
expect(scenario.variable.options[0].text).to.be('auto');
expect(scenario.variable.options[0].value).to.be('$__auto_interval');
expect(scenario.variable.options[0].value).to.be('$__auto_interval_test');
});
it('should set $__auto_interval_test', function() {
var call = ctx.templateSrv.setGrafanaVariable.firstCall;
expect(call.args[0]).to.be('$__auto_interval_test');
expect(call.args[1]).to.be('12h');
});
// updateAutoValue() gets called twice: once directly once via VariableSrv.validateVariableSelectionState()
// So use lastCall instead of a specific call number
it('should set $__auto_interval', function() {
var call = ctx.templateSrv.setGrafanaVariable.getCall(0);
var call = ctx.templateSrv.setGrafanaVariable.lastCall;
expect(call.args[0]).to.be('$__auto_interval');
expect(call.args[1]).to.be('12h');
});
@@ -395,4 +403,68 @@ describe('VariableSrv', function() {
expect(scenario.variable.options[1].value).to.be('hop');
});
});
describe('multiple interval variables with auto', function() {
var variable1, variable2;
beforeEach(function() {
var range = {
from: moment(new Date()).subtract(7, 'days').toDate(),
to: new Date()
};
ctx.timeSrv.timeRange = sinon.stub().returns(range);
ctx.templateSrv.setGrafanaVariable = sinon.spy();
var variableModel1 = {type: 'interval', query: '1s,2h,5h,1d', name: 'variable1', auto: true, auto_count: 10 };
variable1 = ctx.variableSrv.createVariableFromModel(variableModel1);
ctx.variableSrv.addVariable(variable1);
var variableModel2 = {type: 'interval', query: '1s,2h,5h', name: 'variable2', auto: true, auto_count: 1000 };
variable2 = ctx.variableSrv.createVariableFromModel(variableModel2);
ctx.variableSrv.addVariable(variable2);
ctx.variableSrv.updateOptions(variable1);
ctx.variableSrv.updateOptions(variable2);
ctx.$rootScope.$digest();
});
it('should update options array', function() {
expect(variable1.options.length).to.be(5);
expect(variable1.options[0].text).to.be('auto');
expect(variable1.options[0].value).to.be('$__auto_interval_variable1');
expect(variable2.options.length).to.be(4);
expect(variable2.options[0].text).to.be('auto');
expect(variable2.options[0].value).to.be('$__auto_interval_variable2');
});
it('should correctly set $__auto_interval_variableX', function() {
var variable1Set, variable2Set, legacySet, unknownSet = false;
// updateAutoValue() gets called repeatedly: once directly once via VariableSrv.validateVariableSelectionState()
// So check that all calls are valid rather than expect a specific number and/or ordering of calls
for (var i = 0; i < ctx.templateSrv.setGrafanaVariable.callCount; i++) {
var call = ctx.templateSrv.setGrafanaVariable.getCall(i);
switch (call.args[0]) {
case '$__auto_interval_variable1':
expect(call.args[1]).to.be('12h');
variable1Set = true;
break;
case '$__auto_interval_variable2':
expect(call.args[1]).to.be('10m');
variable2Set = true;
break;
case '$__auto_interval':
expect(call.args[1]).to.match(/^(12h|10m)$/);
legacySet = true;
break;
default:
unknownSet = true;
break;
}
}
expect(variable1Set).to.be.equal(true);
expect(variable2Set).to.be.equal(true);
expect(legacySet).to.be.equal(true);
expect(unknownSet).to.be.equal(false);
});
});
});