mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: Implement keep and drop operations (#73636)
* Update lezer * Add functionalities for code and builder * Add comment
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user