Loki: Implement keep and drop operations (#73636)

* Update lezer

* Add functionalities for code and builder

* Add comment
This commit is contained in:
Ivana Huckova
2023-08-23 14:52:19 +02:00
committed by GitHub
parent 356d8872bd
commit fc9b8f6be1
10 changed files with 341 additions and 6 deletions

View File

@@ -10,6 +10,9 @@ import {
Decolorize,
DistinctFilter,
DistinctLabel,
DropLabel,
DropLabels,
DropLabelsExpr,
Filter,
FilterOp,
Grouping,
@@ -21,6 +24,9 @@ import {
Json,
JsonExpression,
JsonExpressionParser,
KeepLabel,
KeepLabels,
KeepLabelsExpr,
LabelFilter,
LabelFormatMatcher,
LabelParser,
@@ -212,6 +218,16 @@ export function handleExpression(expr: string, node: SyntaxNode, context: Contex
break;
}
case DropLabelsExpr: {
visQuery.operations.push(handleDropFilter(expr, node, context));
break;
}
case KeepLabelsExpr: {
visQuery.operations.push(handleKeepFilter(expr, node, context));
break;
}
default: {
// Any other nodes we just ignore and go to its children. This should be fine as there are lots of wrapper
// nodes that can be skipped.
@@ -660,3 +676,37 @@ function handleDistinctFilter(expr: string, node: SyntaxNode, context: Context):
params: labels,
};
}
function handleDropFilter(expr: string, node: SyntaxNode, context: Context): QueryBuilderOperation {
const labels: string[] = [];
let exploringNode = node.getChild(DropLabels);
while (exploringNode) {
const label = getString(expr, exploringNode.getChild(DropLabel));
if (label) {
labels.push(label);
}
exploringNode = exploringNode?.getChild(DropLabels);
}
labels.reverse();
return {
id: LokiOperationId.Drop,
params: labels,
};
}
function handleKeepFilter(expr: string, node: SyntaxNode, context: Context): QueryBuilderOperation {
const labels: string[] = [];
let exploringNode = node.getChild(KeepLabels);
while (exploringNode) {
const label = getString(expr, exploringNode.getChild(KeepLabel));
if (label) {
labels.push(label);
}
exploringNode = exploringNode?.getChild(KeepLabels);
}
labels.reverse();
return {
id: LokiOperationId.Keep,
params: labels,
};
}