mirror of
https://github.com/grafana/grafana.git
synced 2025-01-27 08:47:12 -06:00
Loki: getHighlighterExpressionsFromQuery Returns null if filter term is not quoted (#17852)
Closes #17687
This commit is contained in:
parent
8f150c67aa
commit
0a1bb97a2f
@ -71,17 +71,25 @@ describe('getHighlighterExpressionsFromQuery', () => {
|
||||
it('returns no expressions for empty query', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('')).toEqual([]);
|
||||
});
|
||||
|
||||
it('returns a single expressions for legacy query', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{} x')).toEqual(['(?i)x']);
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} x')).toEqual(['(?i)x']);
|
||||
});
|
||||
|
||||
it('returns an expression for query with filter', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x"')).toEqual(['x']);
|
||||
});
|
||||
|
||||
it('returns expressions for query with filter chain', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ "y"')).toEqual(['x', 'y']);
|
||||
});
|
||||
|
||||
it('returns drops expressions for query with negative filter chain', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" != "y"')).toEqual(['x']);
|
||||
});
|
||||
|
||||
it('returns null if filter term is not wrapped in double quotes', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= x')).toEqual(null);
|
||||
});
|
||||
});
|
||||
|
@ -59,12 +59,19 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
|
||||
if (filterEnd === -1) {
|
||||
filterTerm = expression.trim();
|
||||
} else {
|
||||
filterTerm = expression.substr(0, filterEnd);
|
||||
filterTerm = expression.substr(0, filterEnd).trim();
|
||||
expression = expression.substr(filterEnd);
|
||||
}
|
||||
|
||||
// Unwrap the filter term by removing quotes
|
||||
results.push(filterTerm.replace(/^\s*"/g, '').replace(/"\s*$/g, ''));
|
||||
const quotedTerm = filterTerm.match(/^"((?:[^\\"]|\\")*)"$/);
|
||||
|
||||
if (quotedTerm) {
|
||||
const unwrappedFilterTerm = quotedTerm[1];
|
||||
results.push(unwrappedFilterTerm);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user