Loki: Fix missing parameters on Query Builder operations (#60677)

* add missing mandatory params

* improve naming

* change let to const
This commit is contained in:
Sven Grossmann 2022-12-22 15:31:41 +01:00 committed by GitHub
parent f990be58cb
commit 9f9bf4650d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 101 additions and 3 deletions

View File

@ -2,7 +2,7 @@ import {
createAggregationOperation,
createAggregationOperationWithParam,
} from '../../prometheus/querybuilder/shared/operationUtils';
import { QueryBuilderOperationDef } from '../../prometheus/querybuilder/shared/types';
import { QueryBuilderOperationDef, QueryBuilderOperationParamValue } from '../../prometheus/querybuilder/shared/types';
import { binaryScalarOperations } from './binaryScalarOperations';
import { UnwrapParamEditor } from './components/UnwrapParamEditor';
@ -480,3 +480,16 @@ export function explainOperator(id: LokiOperationId | string): string {
// Strip markdown links
return explain.replace(/\[(.*)\]\(.*\)/g, '$1');
}
export function getDefinitionById(id: string): QueryBuilderOperationDef | undefined {
return definitions.find((x) => x.id === id);
}
export function checkParamsAreValid(def: QueryBuilderOperationDef, params: QueryBuilderOperationParamValue[]): boolean {
// For now we only check if the operation has all the required params.
if (params.length < def.params.filter((param) => !param.optional).length) {
return false;
}
return true;
}

View File

@ -586,6 +586,81 @@ describe('buildVisualQueryFromString', () => {
})
);
});
it('parses a regexp with empty string param', () => {
expect(buildVisualQueryFromString('{app="frontend"} | regexp "" ')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.Regexp, params: [''] }],
})
);
});
it('parses a regexp with no param', () => {
expect(buildVisualQueryFromString('{app="frontend"} | regexp ')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.Regexp, params: [''] }],
})
);
});
it('parses a pattern with empty string param', () => {
expect(buildVisualQueryFromString('{app="frontend"} | pattern "" ')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.Pattern, params: [''] }],
})
);
});
it('parses a pattern with no param', () => {
expect(buildVisualQueryFromString('{app="frontend"} | pattern ')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.Pattern, params: [''] }],
})
);
});
it('parses a json with no param', () => {
expect(buildVisualQueryFromString('{app="frontend"} | json ')).toEqual(
noErrors({
labels: [
{
op: '=',
value: 'frontend',
label: 'app',
},
],
operations: [{ id: LokiOperationId.Json, params: [] }],
})
);
});
});
function noErrors(query: LokiVisualQuery) {

View File

@ -51,9 +51,14 @@ import {
makeError,
replaceVariables,
} from '../../prometheus/querybuilder/shared/parsingUtils';
import { QueryBuilderLabelFilter, QueryBuilderOperation } from '../../prometheus/querybuilder/shared/types';
import {
QueryBuilderLabelFilter,
QueryBuilderOperation,
QueryBuilderOperationParamValue,
} from '../../prometheus/querybuilder/shared/types';
import { binaryScalarDefs } from './binaryScalarOperations';
import { checkParamsAreValid, getDefinitionById } from './operations';
import { LokiOperationId, LokiVisualQuery, LokiVisualQueryBinary } from './types';
interface Context {
@ -256,7 +261,12 @@ function getLabelParser(expr: string, node: SyntaxNode): QueryBuilderOperation {
const parser = getString(expr, parserNode);
const string = handleQuotes(getString(expr, node.getChild(String)));
const params = !!string ? [string] : [];
let params: QueryBuilderOperationParamValue[] = !!string ? [string] : [];
const opDef = getDefinitionById(parser);
if (opDef && !checkParamsAreValid(opDef, params)) {
params = opDef?.defaultParams || [];
}
return {
id: parser,
params,