Loki Query Builder: Fix bug parsing range params (#61678)

feat(loki-query-builder): fix bug parsing range params
This commit is contained in:
Matias Chomicki 2023-01-18 15:54:18 +01:00 committed by GitHub
parent d042c0b30b
commit 89e77d072f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 3 deletions

View File

@ -661,6 +661,27 @@ describe('buildVisualQueryFromString', () => {
})
);
});
it.each(['$__interval', '5m'])('parses query range with unwrap and regex', (range) => {
expect(
buildVisualQueryFromString(
'avg_over_time({test="value"} |= `restart counter is at` | regexp `restart counter is at (?P<restart_counter>[0-9]+)s*.*.*?$` | unwrap restart_counter [' +
range +
'])'
)
).toEqual({
errors: [],
query: {
labels: [{ label: 'test', op: '=', value: 'value' }],
operations: [
{ id: '__line_contains', params: ['restart counter is at'] },
{ id: 'regexp', params: ['restart counter is at (?P<restart_counter>[0-9]+)s*.*.*?$'] },
{ id: 'unwrap', params: ['restart_counter', ''] },
{ id: 'avg_over_time', params: [range] },
],
},
});
});
});
function noErrors(query: LokiVisualQuery) {

View File

@ -413,10 +413,11 @@ function handleRangeAggregation(expr: string, node: SyntaxNode, context: Context
const number = node.getChild(NumberLezer);
const logExpr = node.getChild(LogRangeExpr);
const params = number !== null && number !== undefined ? [getString(expr, number)] : [];
const range = logExpr?.getChild(Range);
const rangeValue = range ? getString(expr, range) : null;
let match = getString(expr, node).match(/\[(.+)\]/);
if (match?.[1]) {
params.unshift(match[1]);
if (rangeValue) {
params.unshift(rangeValue.substring(1, rangeValue.length - 1));
}
const op = {