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,27 +91,25 @@ export default class GraphiteQuery {
this.functions.push(innerFunc); this.functions.push(innerFunc);
break; break;
case 'series-ref': 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; break;
case 'bool': case 'bool':
case 'string': case 'string':
case 'number': 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); var shiftBack = this.isShiftParamsBack(func);
this.addFunctionParameter(func, astNode.value, index, shiftBack); this.addFunctionParameter(func, astNode.value, index, shiftBack);
break; break;
case 'metric': case 'metric':
if (this.segments.length > 0) { if (this.segments.length > 0) {
if (astNode.segments.length !== 1) { this.addFunctionParameter(func, _.join(_.map(astNode.segments, 'value'), '.'), index, true);
throw { message: 'Multiple metric params not supported, use text editor.' }; } else {
this.segments = astNode.segments;
} }
this.addFunctionParameter(func, astNode.segments[0].value, index, true);
break; break;
}
this.segments = astNode.segments;
} }
} }
@ -149,6 +147,9 @@ export default class GraphiteQuery {
if (shiftBack) { if (shiftBack) {
index = Math.max(index - 1, 0); 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; func.params[index] = value;
} }

View File

@ -62,6 +62,10 @@ export class GraphiteQueryCtrl extends QueryCtrl {
} }
checkOtherSegments(fromIndex) { checkOtherSegments(fromIndex) {
if (this.queryModel.segments.length === 1 && this.queryModel.segments[0].type === 'series-ref') {
return;
}
if (fromIndex === 0) { if (fromIndex === 0) {
this.addSelectMetricSegment(); this.addSelectMetricSegment();
return; return;
@ -108,8 +112,23 @@ export class GraphiteQueryCtrl extends QueryCtrl {
if (altSegments.length === 0) { return altSegments; } 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 // add template variables
_.each(this.templateSrv.variables, variable => { _.eachRight(this.templateSrv.variables, variable => {
altSegments.unshift(this.uiSegmentSrv.newSegment({ altSegments.unshift(this.uiSegmentSrv.newSegment({
type: 'template', type: 'template',
value: '$' + variable.name, value: '$' + variable.name,

View File

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