From f36ed878e982648bf499def12befaec9609bdc43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Farkas?= Date: Thu, 3 Feb 2022 10:25:37 +0100 Subject: [PATCH] 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> --- .../datasource/loki/query_utils.test.ts | 39 ++++++++++++++++++- .../plugins/datasource/loki/query_utils.ts | 24 ++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/public/app/plugins/datasource/loki/query_utils.test.ts b/public/app/plugins/datasource/loki/query_utils.test.ts index d69692462f6..4e087955488 100644 --- a/public/app/plugins/datasource/loki/query_utils.test.ts +++ b/public/app/plugins/datasource/loki/query_utils.test.ts @@ -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); + }); +}); diff --git a/public/app/plugins/datasource/loki/query_utils.ts b/public/app/plugins/datasource/loki/query_utils.ts index 7c2d2244c93..d9c9393fe5e 100644 --- a/public/app/plugins/datasource/loki/query_utils.ts +++ b/public/app/plugins/datasource/loki/query_utils.ts @@ -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 }; +}