diff --git a/public/app/plugins/datasource/loki/query_utils.test.ts b/public/app/plugins/datasource/loki/query_utils.test.ts index 43cb219f30d..2edac619023 100644 --- a/public/app/plugins/datasource/loki/query_utils.test.ts +++ b/public/app/plugins/datasource/loki/query_utils.test.ts @@ -97,4 +97,16 @@ describe('getNormalizedLokiQuery', () => { it('handles new<>old conflict (new wins), instant', () => { expectNormalized({ instant: true, range: false, queryType: LokiQueryType.Instant }, LokiQueryType.Instant); }); + + it('handles invalid new, range', () => { + expectNormalized({ queryType: 'invalid' }, LokiQueryType.Range); + }); + + it('handles invalid new, when old-range exists, use old', () => { + expectNormalized({ instant: false, range: true, queryType: 'invalid' }, LokiQueryType.Range); + }); + + it('handles invalid new, when old-instant exists, use old', () => { + expectNormalized({ instant: true, range: false, queryType: 'invalid' }, LokiQueryType.Instant); + }); }); diff --git a/public/app/plugins/datasource/loki/query_utils.ts b/public/app/plugins/datasource/loki/query_utils.ts index 1f9ede7022f..d22a04986f7 100644 --- a/public/app/plugins/datasource/loki/query_utils.ts +++ b/public/app/plugins/datasource/loki/query_utils.ts @@ -80,8 +80,13 @@ export function addParsedLabelToQuery(expr: string, key: string, value: string | // - does not have `.instant` // - does not have `.range` export function getNormalizedLokiQuery(query: LokiQuery): LokiQuery { + // if queryType field contains invalid data we behave as if the queryType is empty + const { queryType } = query; + const hasValidQueryType = + queryType === LokiQueryType.Range || queryType === LokiQueryType.Instant || queryType === LokiQueryType.Stream; + // if queryType exists, it is respected - if (query.queryType !== undefined) { + if (hasValidQueryType) { const { instant, range, ...rest } = query; return rest; }