mirror of
https://github.com/grafana/grafana.git
synced 2024-12-01 21:19:28 -06:00
Loki: add helper function to handle instant/range queries (#44785)
* loki: add helper function to handle range/instant queries * improved comment Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
This commit is contained in:
parent
3314178a0a
commit
f36ed878e9
@ -1,4 +1,5 @@
|
||||
import { getHighlighterExpressionsFromQuery } from './query_utils';
|
||||
import { LokiQuery, LokiQueryType } from './types';
|
||||
import { getHighlighterExpressionsFromQuery, getNormalizedLokiQuery } from './query_utils';
|
||||
|
||||
describe('getHighlighterExpressionsFromQuery', () => {
|
||||
it('returns no expressions for empty query', () => {
|
||||
@ -61,3 +62,39 @@ describe('getHighlighterExpressionsFromQuery', () => {
|
||||
expect(getHighlighterExpressionsFromQuery('{foo="bar"} |~ `\\w+`')).toEqual(['\\w+']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNormalizedLokiQuery', () => {
|
||||
function expectNormalized(inputProps: Object, outputQueryType: LokiQueryType) {
|
||||
const input: LokiQuery = { refId: 'A', expr: 'test1', ...inputProps };
|
||||
const output = getNormalizedLokiQuery(input);
|
||||
expect(output).toStrictEqual({ refId: 'A', expr: 'test1', queryType: outputQueryType });
|
||||
}
|
||||
|
||||
it('handles no props case', () => {
|
||||
expectNormalized({}, LokiQueryType.Range);
|
||||
});
|
||||
|
||||
it('handles old-style instant case', () => {
|
||||
expectNormalized({ instant: true, range: false }, LokiQueryType.Instant);
|
||||
});
|
||||
|
||||
it('handles old-style range case', () => {
|
||||
expectNormalized({ instant: false, range: true }, LokiQueryType.Range);
|
||||
});
|
||||
|
||||
it('handles new+old style instant', () => {
|
||||
expectNormalized({ instant: true, range: false, queryType: LokiQueryType.Range }, LokiQueryType.Range);
|
||||
});
|
||||
|
||||
it('handles new+old style range', () => {
|
||||
expectNormalized({ instant: false, range: true, queryType: LokiQueryType.Instant }, LokiQueryType.Instant);
|
||||
});
|
||||
|
||||
it('handles new<>old conflict (new wins), range', () => {
|
||||
expectNormalized({ instant: false, range: true, queryType: LokiQueryType.Range }, LokiQueryType.Range);
|
||||
});
|
||||
|
||||
it('handles new<>old conflict (new wins), instant', () => {
|
||||
expectNormalized({ instant: true, range: false, queryType: LokiQueryType.Instant }, LokiQueryType.Instant);
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { escapeRegExp } from 'lodash';
|
||||
import { PIPE_PARSERS } from './syntax';
|
||||
import { LokiQuery, LokiQueryType } from './types';
|
||||
|
||||
export function formatQuery(selector: string | undefined): string {
|
||||
return `${selector || ''}`.trim();
|
||||
@ -71,3 +72,26 @@ export function queryHasPipeParser(expr: string): boolean {
|
||||
export function addParsedLabelToQuery(expr: string, key: string, value: string | number, operator: string) {
|
||||
return expr + ` | ${key}${operator}"${value.toString()}"`;
|
||||
}
|
||||
|
||||
// we are migrating from `.instant` and `.range` to `.queryType`
|
||||
// this function returns a new query object that:
|
||||
// - has `.queryType`
|
||||
// - does not have `.instant`
|
||||
// - does not have `.range`
|
||||
export function getNormalizedLokiQuery(query: LokiQuery): LokiQuery {
|
||||
// if queryType exists, it is respected
|
||||
if (query.queryType !== undefined) {
|
||||
const { instant, range, ...rest } = query;
|
||||
return rest;
|
||||
}
|
||||
|
||||
// if no queryType, and instant===true, it's instant
|
||||
if (query.instant === true) {
|
||||
const { instant, range, ...rest } = query;
|
||||
return { ...rest, queryType: LokiQueryType.Instant };
|
||||
}
|
||||
|
||||
// otherwise it is range
|
||||
const { instant, range, ...rest } = query;
|
||||
return { ...rest, queryType: LokiQueryType.Range };
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user