Skip to content

Commit 667cdc1

Browse files
authored
feat: Support METRICS_INFO in AST & APIs (#49)
part of: elastic/kibana#255433 ## Summary This PR adds AST support for the new METRICS_INFO command as well as support in the visitor API ## Details - cast_to_ast_converter: just creates the command with the context since the grammar is just: ``` metricsInfoCommand : METRICS_INFO ; ``` - global_visitor_context: adds the `visitMetricsInfoCommand` in the same way as the other commands along with its types. ### Checklist <!-- Delete any items that are not applicable to this PR. --> - [x] Unit tests have been added or updated. - [x] The PR title has the correct [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/) label in the title, this is crucial for the correct versioning of this package. Check the following cheat shit. | Title Label | Release | |---|---| | `breaking: true (!)` | `major` | | `feat` | `minor` | | `fix` | `patch` | | `refactor` | `patch` | | `perf` | `patch` | | `build` | `patch` | | `docs` | `patch` | | `chore` | `patch` | | `revert` | `patch` |
1 parent f2a09db commit 667cdc1

6 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/ast/visitor/contexts.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import type {
1818
ESQLAstHeaderCommand,
1919
ESQLAstItem,
2020
ESQLAstJoinCommand,
21+
ESQLAstMetricsInfoCommand,
2122
ESQLAstQueryExpression,
2223
ESQLAstRegisteredDomainCommand,
2324
ESQLAstRerankCommand,
@@ -593,6 +594,12 @@ export class UriPartsCommandVisitorContext<
593594
Data extends SharedData = SharedData,
594595
> extends CommandVisitorContext<Methods, Data, ESQLAstUriPartsCommand> {}
595596

597+
// METRICS_INFO
598+
export class MetricsInfoCommandVisitorContext<
599+
Methods extends VisitorMethods = VisitorMethods,
600+
Data extends SharedData = SharedData,
601+
> extends CommandVisitorContext<Methods, Data, ESQLAstMetricsInfoCommand> {}
602+
596603
// Expressions -----------------------------------------------------------------
597604

598605
export class ExpressionVisitorContext<

src/ast/visitor/global_visitor_context.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type {
1212
ESQLAstCompletionCommand,
1313
ESQLAstHeaderCommand,
1414
ESQLAstJoinCommand,
15+
ESQLAstMetricsInfoCommand,
1516
ESQLAstQueryExpression,
1617
ESQLAstRegisteredDomainCommand,
1718
ESQLAstRerankCommand,
@@ -239,6 +240,14 @@ export class GlobalVisitorContext<
239240
input as any
240241
);
241242
}
243+
case 'metrics_info': {
244+
if (!this.methods.visitMetricsInfoCommand) break;
245+
return this.visitMetricsInfoCommand(
246+
parent,
247+
commandNode as ESQLAstMetricsInfoCommand,
248+
input as any
249+
);
250+
}
242251
}
243252
return this.visitCommandGeneric(parent, commandNode, input as any);
244253
}
@@ -527,6 +536,15 @@ export class GlobalVisitorContext<
527536
return this.visitWithSpecificContext('visitUriPartsCommand', context, input);
528537
}
529538

539+
public visitMetricsInfoCommand(
540+
parent: contexts.VisitorContext | null,
541+
node: ESQLAstMetricsInfoCommand,
542+
input: types.VisitorInput<Methods, 'visitMetricsInfoCommand'>
543+
): types.VisitorOutput<Methods, 'visitMetricsInfoCommand'> {
544+
const context = new contexts.MetricsInfoCommandVisitorContext(this, node, parent);
545+
return this.visitWithSpecificContext('visitMetricsInfoCommand', context, input);
546+
}
547+
530548
// #endregion
531549

532550
// #region Expression visiting -------------------------------------------------------

src/ast/visitor/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export type CommandVisitorInput<Methods extends VisitorMethods> = AnyToVoid<
112112
VisitorInput<Methods, 'visitRerankCommand'> &
113113
VisitorInput<Methods, 'visitChangePointCommand'> &
114114
VisitorInput<Methods, 'visitUriPartsCommand'> &
115+
VisitorInput<Methods, 'visitMetricsInfoCommand'> &
115116
VisitorInput<Methods, 'visitRegisteredDomainCommand'>
116117
>;
117118

@@ -145,6 +146,7 @@ export type CommandVisitorOutput<Methods extends VisitorMethods> =
145146
| VisitorOutput<Methods, 'visitChangePointCommand'>
146147
| VisitorOutput<Methods, 'visitCompletionCommand'>
147148
| VisitorOutput<Methods, 'visitUriPartsCommand'>
149+
| VisitorOutput<Methods, 'visitMetricsInfoCommand'>
148150
| VisitorOutput<Methods, 'visitRegisteredDomainCommand'>;
149151

150152
/* eslint-disable @typescript-eslint/no-explicit-any */
@@ -206,6 +208,11 @@ export interface VisitorMethods<
206208
visitFuseCommand?: Visitor<contexts.FuseCommandVisitorContext<Visitors, Data>, any, any>;
207209
visitMmrCommand?: Visitor<contexts.MmrCommandVisitorContext<Visitors, Data>, any, any>;
208210
visitUriPartsCommand?: Visitor<contexts.UriPartsCommandVisitorContext<Visitors, Data>, any, any>;
211+
visitMetricsInfoCommand?: Visitor<
212+
contexts.MetricsInfoCommandVisitorContext<Visitors, Data>,
213+
any,
214+
any
215+
>;
209216
visitExpression?: Visitor<contexts.ExpressionVisitorContext<Visitors, Data>, any, any>;
210217
visitSourceExpression?: Visitor<
211218
contexts.SourceExpressionVisitorContext<Visitors, Data>,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
import { EsqlQuery } from '../../composer/query';
9+
import { Walker } from '../../ast/walker';
10+
import type { ESQLAstQueryExpression, ESQLAstMetricsInfoCommand } from '../../types';
11+
12+
describe('METRICS_INFO', () => {
13+
const getMetricsInfo = (ast: ESQLAstQueryExpression): ESQLAstMetricsInfoCommand =>
14+
Walker.match(ast, {
15+
type: 'command',
16+
name: 'metrics_info',
17+
}) as ESQLAstMetricsInfoCommand;
18+
19+
it('parses metrics_info command', () => {
20+
const src = `TS index | metrics_info`;
21+
const { ast, errors } = EsqlQuery.fromSrc(src);
22+
const metricsInfo = getMetricsInfo(ast);
23+
24+
expect(errors.length).toBe(0);
25+
expect(metricsInfo).toMatchObject({
26+
type: 'command',
27+
name: 'metrics_info',
28+
incomplete: false,
29+
});
30+
expect(metricsInfo.args).toHaveLength(0);
31+
});
32+
});

src/parser/core/cst_to_ast_converter.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,12 @@ export class CstToAstConverter {
523523
if (uriPartsCommandCtx) {
524524
return this.fromUriPartsCommand(uriPartsCommandCtx);
525525
}
526+
527+
const metricsInfoCommandCtx = ctx.metricsInfoCommand();
528+
529+
if (metricsInfoCommandCtx) {
530+
return this.fromMetricsInfoCommand(metricsInfoCommandCtx);
531+
}
526532
// throw new Error(`Unknown processing command: ${this.getSrc(ctx)}`;
527533
}
528534

@@ -2293,6 +2299,15 @@ export class CstToAstConverter {
22932299

22942300
return command;
22952301
}
2302+
2303+
// --------------------------------------------------------------- METRICS_INFO
2304+
2305+
private fromMetricsInfoCommand(
2306+
ctx: cst.MetricsInfoCommandContext
2307+
): ast.ESQLAstMetricsInfoCommand {
2308+
return this.createCommand<'metrics_info', ast.ESQLAstMetricsInfoCommand>('metrics_info', ctx);
2309+
}
2310+
22962311
// -------------------------------------------------------------- expressions
22972312

22982313
private toColumnsFromCommand(

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type ESQLAstCommand =
2121
| ESQLAstFuseCommand
2222
| ESQLAstForkCommand
2323
| ESQLAstUriPartsCommand
24+
| ESQLAstMetricsInfoCommand
2425
| ESQLAstRegisteredDomainCommand;
2526

2627
export type ESQLAstAllCommands = ESQLAstCommand | ESQLAstHeaderCommand;
@@ -164,6 +165,8 @@ export interface ESQLAstUriPartsCommand extends ESQLCommand<'uri_parts'> {
164165
expression?: ESQLAstExpression;
165166
}
166167

168+
export interface ESQLAstMetricsInfoCommand extends ESQLCommand<'metrics_info'> {}
169+
167170
export interface ESQLAstRegisteredDomainCommand extends ESQLCommand<'registered_domain'> {
168171
targetField: ESQLColumn;
169172
expression?: ESQLAstExpression;

0 commit comments

Comments
 (0)