Fix queries with repeated references (#56497)

This commit is contained in:
Beto Muniz 2022-10-06 17:59:14 -03:00 committed by GitHub
parent 356318e387
commit b5cb917dfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 16 deletions

View File

@ -6384,12 +6384,11 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "16"],
[0, 0, 0, "Unexpected any. Specify a different type.", "17"],
[0, 0, 0, "Unexpected any. Specify a different type.", "18"],
[0, 0, 0, "Unexpected any. Specify a different type.", "19"],
[0, 0, 0, "Do not use any type assertions.", "20"],
[0, 0, 0, "Do not use any type assertions.", "19"],
[0, 0, 0, "Unexpected any. Specify a different type.", "20"],
[0, 0, 0, "Unexpected any. Specify a different type.", "21"],
[0, 0, 0, "Unexpected any. Specify a different type.", "22"],
[0, 0, 0, "Unexpected any. Specify a different type.", "23"],
[0, 0, 0, "Unexpected any. Specify a different type.", "24"]
[0, 0, 0, "Unexpected any. Specify a different type.", "23"]
],
"public/app/plugins/datasource/graphite/lexer.ts:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],

View File

@ -212,19 +212,10 @@ export default class GraphiteQuery {
let targetWithNestedQueries = target.target;
// Use ref count to track circular references
function countTargetRefs(targetsByRefId: any, refId: string) {
let refCount = 0;
each(targetsByRefId, (t, id) => {
if (id !== refId) {
const match = nestedSeriesRefRegex.exec(t.target);
const count = match && match.length ? match.length - 1 : 0;
refCount += count;
}
});
targetsByRefId[refId].refCount = refCount;
}
each(targetsByRefId, (t, id) => {
countTargetRefs(targetsByRefId, id);
const regex = RegExp(`\#(${id})`, 'g');
const refMatches = targetWithNestedQueries.match(regex);
t.refCount = refMatches?.length ?? 0;
});
// Keep interpolating until there are no query references

View File

@ -39,6 +39,20 @@ describe('Graphite query model', () => {
expect(ctx.queryModel.target.targetFull).toBe(targetFullExpected);
});
it('targetFull should include nested queries with repeated subqueries', () => {
ctx.target = { refId: 'C', target: 'scale(asPercent(diffSeries(#B, #A), #B), 100)' };
ctx.targets = [
{ refId: 'A', target: 'first.query.count' },
{ refId: 'B', target: 'second.query.count' },
{ refId: 'C', target: 'scale(asPercent(diffSeries(#B, #A), #B), 100)' },
];
ctx.queryModel = new GraphiteQuery(ctx.datasource, ctx.target, ctx.templateSrv);
ctx.queryModel.updateRenderedTarget(ctx.target, ctx.targets);
const targetFullExpected =
'scale(asPercent(diffSeries(second.query.count, first.query.count), second.query.count), 100)';
expect(ctx.queryModel.target.targetFull).toBe(targetFullExpected);
});
it('should not hang on circular references', () => {
ctx.target.target = 'asPercent(#A, #B)';
ctx.targets = [