improve handling of query references

This commit is contained in:
Dan Cech 2017-12-09 16:27:05 -05:00
parent 4fcf79a3a6
commit 3a1700cbee
3 changed files with 35 additions and 15 deletions

View File

@ -91,28 +91,26 @@ export default class GraphiteQuery {
this.functions.push(innerFunc);
break;
case 'series-ref':
this.addFunctionParameter(func, astNode.value, index, this.segments.length > 0);
if (this.segments.length > 0) {
this.addFunctionParameter(func, astNode.value, index, true);
} else {
this.segments.push(astNode);
}
break;
case 'bool':
case 'string':
case 'number':
if ((index-1) >= func.def.params.length) {
throw { message: 'invalid number of parameters to method ' + func.def.name };
}
var shiftBack = this.isShiftParamsBack(func);
this.addFunctionParameter(func, astNode.value, index, shiftBack);
break;
case 'metric':
if (this.segments.length > 0) {
if (astNode.segments.length !== 1) {
throw { message: 'Multiple metric params not supported, use text editor.' };
}
this.addFunctionParameter(func, astNode.segments[0].value, index, true);
break;
}
this.addFunctionParameter(func, _.join(_.map(astNode.segments, 'value'), '.'), index, true);
} else {
this.segments = astNode.segments;
}
break;
}
}
isShiftParamsBack(func) {
@ -149,6 +147,9 @@ export default class GraphiteQuery {
if (shiftBack) {
index = Math.max(index - 1, 0);
}
if (index > func.def.params.length) {
throw { message: 'too many parameters for function ' + func.def.name };
}
func.params[index] = value;
}

View File

@ -62,6 +62,10 @@ export class GraphiteQueryCtrl extends QueryCtrl {
}
checkOtherSegments(fromIndex) {
if (this.queryModel.segments.length === 1 && this.queryModel.segments[0].type === 'series-ref') {
return;
}
if (fromIndex === 0) {
this.addSelectMetricSegment();
return;
@ -108,8 +112,23 @@ export class GraphiteQueryCtrl extends QueryCtrl {
if (altSegments.length === 0) { return altSegments; }
// add query references
if (index === 0) {
_.eachRight(this.panelCtrl.panel.targets, target => {
if (target.refId === this.queryModel.target.refId) {
return;
}
altSegments.unshift(this.uiSegmentSrv.newSegment({
type: 'series-ref',
value: '#' + target.refId,
expandable: false,
}));
});
}
// add template variables
_.each(this.templateSrv.variables, variable => {
_.eachRight(this.templateSrv.variables, variable => {
altSegments.unshift(this.uiSegmentSrv.newSegment({
type: 'template',
value: '$' + variable.name,

View File

@ -95,11 +95,11 @@ describe('GraphiteQueryCtrl', function() {
});
it('should not add select metric segment', function() {
expect(ctx.ctrl.segments.length).to.be(0);
expect(ctx.ctrl.segments.length).to.be(1);
});
it('should add both series refs as params', function() {
expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(2);
it('should add second series ref as param', function() {
expect(ctx.ctrl.queryModel.functions[0].params.length).to.be(1);
});
});