Loki search words: process negative expressions as negative search words

This commit is contained in:
Matias Chomicki 2023-04-12 15:39:43 +02:00
parent f612a72f96
commit d875b25049
2 changed files with 12 additions and 5 deletions

View File

@ -63,6 +63,10 @@ describe('getHighlighterExpressionsFromQuery', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ `y`')).toEqual(['x', 'y']); expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ `y`')).toEqual(['x', 'y']);
}); });
it('returns expressions for query with negative filter chain using both backticks and quotes', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} != "x" !~ `y`')).toEqual(['-x', '-y']);
});
it('returns expression for query with log parser', () => { it('returns expression for query with log parser', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" | logfmt')).toEqual(['x']); expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" | logfmt')).toEqual(['x']);
}); });
@ -71,8 +75,8 @@ describe('getHighlighterExpressionsFromQuery', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ "y" | logfmt')).toEqual(['x', 'y']); expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" |~ "y" | logfmt')).toEqual(['x', 'y']);
}); });
it('returns drops expressions for query with negative filter chain using quotes', () => { it('returns expressions for query with negative filter chain using quotes', () => {
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" != "y"')).toEqual(['x']); expect(getHighlighterExpressionsFromQuery('{foo="bar"} |= "x" != "y"')).toEqual(['x', '-y']);
}); });
it('returns expressions for query with filter chain using backticks', () => { it('returns expressions for query with filter chain using backticks', () => {

View File

@ -17,6 +17,8 @@ import {
MetricExpr, MetricExpr,
Matcher, Matcher,
Identifier, Identifier,
Nre,
Neq,
} from '@grafana/lezer-logql'; } from '@grafana/lezer-logql';
import { DataQuery } from '@grafana/schema'; import { DataQuery } from '@grafana/schema';
@ -31,7 +33,7 @@ export function formatQuery(selector: string | undefined): string {
/** /**
* Returns search terms from a LogQL query. * Returns search terms from a LogQL query.
* E.g., `{} |= foo |=bar != baz` returns `['foo', 'bar']`. * E.g.: `{} |= foo |=bar != baz` returns `['foo', 'bar', '-baz']`.
*/ */
export function getHighlighterExpressionsFromQuery(input: string): string[] { export function getHighlighterExpressionsFromQuery(input: string): string[] {
const results = []; const results = [];
@ -49,9 +51,10 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
for (let filter of filters) { for (let filter of filters) {
const pipeExact = filter.getChild(Filter)?.getChild(PipeExact); const pipeExact = filter.getChild(Filter)?.getChild(PipeExact);
const pipeMatch = filter.getChild(Filter)?.getChild(PipeMatch); const pipeMatch = filter.getChild(Filter)?.getChild(PipeMatch);
const negativeExp = filter.getChild(Filter)?.getChild(Neq) || filter.getChild(Filter)?.getChild(Nre);
const string = filter.getChild(String); const string = filter.getChild(String);
if ((!pipeExact && !pipeMatch) || !string) { if ((!pipeExact && !pipeMatch && !negativeExp) || !string) {
continue; continue;
} }
@ -76,7 +79,7 @@ export function getHighlighterExpressionsFromQuery(input: string): string[] {
} }
if (resultTerm) { if (resultTerm) {
results.push(resultTerm); results.push(negativeExp ? `-${resultTerm}` : resultTerm);
} }
} }
return results; return results;