Loki: Add support for distinct operation in autocomplete and query builder (#69003)

* Loki Autocomplete: add support for suggesting distinct

* Loki query builder: add distinct as format operation

* Remove unused import

* Loki visual query: add support to parse distinct filters

* Query builder: use label param editor for distinct

* Loki Autocomplete: Improve distinct label suggestions

* Query Builder: improve distinct parsing

* Fix tests

* Update tests
This commit is contained in:
Matias Chomicki
2023-05-25 14:22:16 +02:00
committed by GitHub
parent 5717d8954f
commit 9abf8140c7
8 changed files with 180 additions and 0 deletions

View File

@@ -8,6 +8,8 @@ import {
By,
ConvOp,
Decolorize,
DistinctFilter,
DistinctLabel,
Filter,
FilterOp,
Grouping,
@@ -205,6 +207,11 @@ export function handleExpression(expr: string, node: SyntaxNode, context: Contex
break;
}
case DistinctFilter: {
visQuery.operations.push(handleDistinctFilter(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.
@@ -422,6 +429,7 @@ function handleUnwrapExpr(
return {};
}
function handleRangeAggregation(expr: string, node: SyntaxNode, context: Context) {
const nameNode = node.getChild(RangeOp);
const funcName = getString(expr, nameNode);
@@ -632,3 +640,20 @@ function isEmptyQuery(query: LokiVisualQuery) {
}
return false;
}
function handleDistinctFilter(expr: string, node: SyntaxNode, context: Context): QueryBuilderOperation {
const labels: string[] = [];
let exploringNode = node.getChild(DistinctLabel);
while (exploringNode) {
const label = getString(expr, exploringNode.getChild(Identifier));
if (label) {
labels.push(label);
}
exploringNode = exploringNode?.getChild(DistinctLabel);
}
labels.reverse();
return {
id: LokiOperationId.Distinct,
params: labels,
};
}