mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: Return false from isMetricsQuery if query is empty (#47024)
This commit is contained in:
@@ -13,7 +13,7 @@ import {
|
||||
toUtc,
|
||||
} from '@grafana/data';
|
||||
import { BackendSrvRequest, FetchResponse } from '@grafana/runtime';
|
||||
import { LokiDatasource, RangeQueryOptions } from './datasource';
|
||||
import { isMetricsQuery, LokiDatasource, RangeQueryOptions } from './datasource';
|
||||
import { LokiQuery, LokiResponse, LokiResultType } from './types';
|
||||
import { getQueryOptions } from 'test/helpers/getQueryOptions';
|
||||
import { TemplateSrv } from 'app/features/templating/template_srv';
|
||||
@@ -1029,6 +1029,23 @@ describe('LokiDatasource', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('isMetricsQuery', () => {
|
||||
it('should return true for metrics query', () => {
|
||||
const query = 'rate({label=value}[1m])';
|
||||
expect(isMetricsQuery(query)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should return false for logs query', () => {
|
||||
const query = '{label=value}';
|
||||
expect(isMetricsQuery(query)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should not blow up on empty query', () => {
|
||||
const query = '';
|
||||
expect(isMetricsQuery(query)).toBeFalsy();
|
||||
});
|
||||
});
|
||||
|
||||
function assertAdHocFilters(query: string, expectedResults: string, ds: LokiDatasource) {
|
||||
const lokiQuery: LokiQuery = { refId: 'A', expr: query };
|
||||
const result = ds.addAdHocFilters(lokiQuery.expr);
|
||||
|
||||
@@ -811,6 +811,9 @@ export function lokiSpecialRegexEscape(value: any) {
|
||||
* Sometimes important to know that before we actually do the query.
|
||||
*/
|
||||
export function isMetricsQuery(query: string): boolean {
|
||||
if (!query) {
|
||||
return false;
|
||||
}
|
||||
const tokens = Prism.tokenize(query, syntax);
|
||||
return tokens.some((t) => {
|
||||
// Not sure in which cases it can be string maybe if nothing matched which means it should not be a function
|
||||
|
||||
Reference in New Issue
Block a user