mirror of
https://github.com/grafana/grafana.git
synced 2024-11-24 09:50:29 -06:00
Loki: Fix missing parameters on Query Builder operations (#60677)
* add missing mandatory params * improve naming * change let to const
This commit is contained in:
parent
f990be58cb
commit
9f9bf4650d
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user