prometheus nested query support

This commit is contained in:
Mitsuhiro Tanda 2017-11-25 02:35:29 +09:00
parent 5e11f1a2a1
commit 98bb8bf761
2 changed files with 23 additions and 0 deletions

View File

@ -155,6 +155,7 @@ export class PrometheusDatasource {
// Only replace vars in expression after having (possibly) updated interval vars
query.expr = this.templateSrv.replace(target.expr, scopedVars, this.interpolateQueryExpr);
query.expr = this.replaceNestedQuery(query.expr, options);
query.requestId = options.panelId + target.refId;
return query;
}
@ -269,6 +270,13 @@ export class PrometheusDatasource {
});
}
replaceNestedQuery(query, options) {
return query.replace(/\#([A-Z])/g, (match, g1) => {
let replaceTarget = options.targets.find((t) => { return t.refId === g1; });
return replaceTarget ? replaceTarget.expr : match;
});
}
transformMetricData(md, options, start, end, step) {
var dps = [],
metricLabel = null;

View File

@ -590,4 +590,19 @@ describe('PrometheusDatasource', function() {
expect(query.scopedVars.__interval_ms.value).to.be(5 * 1000);
});
});
describe('The nested query', function() {
it('should generate correct query', function() {
let query = 'sum(rate(#A[1m]))';
let options = {
targets: [
{
refId: 'A',
expr: 'http_requests_total'
}
]
};
let result = ctx.ds.replaceNestedQuery(query, options);
expect(result).to.be('sum(rate(http_requests_total[1m]))');
});
});
});