mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Logging: fix query parsing for selectors with multiple labels
- simplify selector parsing - added tests
This commit is contained in:
@@ -35,4 +35,11 @@ describe('parseQuery', () => {
|
|||||||
regexp: 'x|y',
|
regexp: 'x|y',
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns query for selector with two labels', () => {
|
||||||
|
expect(parseQuery('{foo="bar", baz="42"}')).toEqual({
|
||||||
|
query: '{foo="bar", baz="42"}',
|
||||||
|
regexp: '',
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,26 +16,15 @@ const DEFAULT_QUERY_PARAMS = {
|
|||||||
query: '',
|
query: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const QUERY_REGEXP = /({\w+="[^"]+"})?\s*(\w[^{]+)?\s*({\w+="[^"]+"})?/;
|
const selectorRegexp = /{[^{]*}/g;
|
||||||
export function parseQuery(input: string) {
|
export function parseQuery(input: string) {
|
||||||
const match = input.match(QUERY_REGEXP);
|
const match = input.match(selectorRegexp);
|
||||||
let query = '';
|
let query = '';
|
||||||
let regexp = '';
|
let regexp = input;
|
||||||
|
|
||||||
if (match) {
|
if (match) {
|
||||||
if (match[1]) {
|
query = match[0];
|
||||||
query = match[1];
|
regexp = input.replace(selectorRegexp, '').trim();
|
||||||
}
|
|
||||||
if (match[2]) {
|
|
||||||
regexp = match[2].trim();
|
|
||||||
}
|
|
||||||
if (match[3]) {
|
|
||||||
if (match[1]) {
|
|
||||||
query = `${match[1].slice(0, -1)},${match[3].slice(1)}`;
|
|
||||||
} else {
|
|
||||||
query = match[3];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return { query, regexp };
|
return { query, regexp };
|
||||||
|
|||||||
Reference in New Issue
Block a user