[8.x] [ES|QL] Improve performance for ECS schema information in editor (#192646) (#193199)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ES|QL] Improve performance for ECS schema information in editor
(#192646)](https://github.com/elastic/kibana/pull/192646)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Quynh Nguyen
(Quinn)","email":"43350163+qn895@users.noreply.github.com"},"sourceCommit":{"committedDate":"2024-09-17T16:22:37Z","message":"[ES|QL]
Improve performance for ECS schema information in editor (#192646)\n\n##
Summary\r\n\r\nThis PR improves performance for ECS schema fetching in
the ES|QL\r\neditor. Changes include:\r\n- First fetch a list of all ECS
fields and their type. This full list is\r\nfetched once and is stored
in a cache.\r\n- Then, if the field name (e.g. `field.keyword` and
`field`) are\r\ndetected to be in this list of ECS fields, then
description is fetched\r\nlazily for the
field.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/08f8cdf1-6147-47cc-b738-08011f67eb5b\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by:
Stratoula Kalafateli
<efstratia.kalafateli@elastic.co>\r\nCo-authored-by: Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"8e4017779d7cd030f1fcd53a89043030018ff0ed","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","Feature:ES|QL","Team:ESQL","v8.16.0"],"title":"[ES|QL]
Improve performance for ECS schema information in
editor","number":192646,"url":"https://github.com/elastic/kibana/pull/192646","mergeCommit":{"message":"[ES|QL]
Improve performance for ECS schema information in editor (#192646)\n\n##
Summary\r\n\r\nThis PR improves performance for ECS schema fetching in
the ES|QL\r\neditor. Changes include:\r\n- First fetch a list of all ECS
fields and their type. This full list is\r\nfetched once and is stored
in a cache.\r\n- Then, if the field name (e.g. `field.keyword` and
`field`) are\r\ndetected to be in this list of ECS fields, then
description is fetched\r\nlazily for the
field.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/08f8cdf1-6147-47cc-b738-08011f67eb5b\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by:
Stratoula Kalafateli
<efstratia.kalafateli@elastic.co>\r\nCo-authored-by: Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"8e4017779d7cd030f1fcd53a89043030018ff0ed"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192646","number":192646,"mergeCommit":{"message":"[ES|QL]
Improve performance for ECS schema information in editor (#192646)\n\n##
Summary\r\n\r\nThis PR improves performance for ECS schema fetching in
the ES|QL\r\neditor. Changes include:\r\n- First fetch a list of all ECS
fields and their type. This full list is\r\nfetched once and is stored
in a cache.\r\n- Then, if the field name (e.g. `field.keyword` and
`field`) are\r\ndetected to be in this list of ECS fields, then
description is fetched\r\nlazily for the
field.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/08f8cdf1-6147-47cc-b738-08011f67eb5b\r\n\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [ ] Any text added follows [EUI's
writing\r\nguidelines](https://elastic.github.io/eui/#/guidelines/writing),
uses\r\nsentence case text and includes
[i18n\r\nsupport](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)\r\n-
[
]\r\n[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)\r\nwas
added for features that require explanation or tutorials\r\n- [ ] [Unit
or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n- [ ] [Flaky
Test\r\nRunner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1)
was\r\nused on any tests changed\r\n- [ ] Any UI touched in this PR is
usable by keyboard only (learn more\r\nabout [keyboard
accessibility](https://webaim.org/techniques/keyboard/))\r\n- [ ] Any UI
touched in this PR does not create any new axe failures\r\n(run axe in
browser:\r\n[FF](https://addons.mozilla.org/en-US/firefox/addon/axe-devtools/),\r\n[Chrome](https://chrome.google.com/webstore/detail/axe-web-accessibility-tes/lhdoppojpmngadmnindnejefpokejbdd?hl=en-US))\r\n-
[ ] If a plugin configuration key changed, check if it needs to
be\r\nallowlisted in the cloud and added to the
[docker\r\nlist](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)\r\n-
[ ] This renders correctly on smaller devices using a
responsive\r\nlayout. (You can test this [in
your\r\nbrowser](https://www.browserstack.com/guide/responsive-testing-on-local-server))\r\n-
[ ] This was checked for
[cross-browser\r\ncompatibility](https://www.elastic.co/support/matrix#matrix_browsers)\r\n\r\n\r\n###
Risk Matrix\r\n\r\nDelete this section if it is not applicable to this
PR.\r\n\r\nBefore closing this PR, invite QA, stakeholders, and other
developers to\r\nidentify risks that should be tested prior to the
change/feature\r\nrelease.\r\n\r\nWhen forming the risk matrix, consider
some of the following examples\r\nand how they may potentially impact
the change:\r\n\r\n| Risk | Probability | Severity | Mitigation/Notes
|\r\n\r\n|---------------------------|-------------|----------|-------------------------|\r\n|
Multiple Spaces&mdash;unexpected behavior in non-default Kibana
Space.\r\n| Low | High | Integration tests will verify that all features
are still\r\nsupported in non-default Kibana Space and when user
switches between\r\nspaces. |\r\n| Multiple nodes&mdash;Elasticsearch
polling might have race conditions\r\nwhen multiple Kibana nodes are
polling for the same tasks. | High | Low\r\n| Tasks are idempotent, so
executing them multiple times will not result\r\nin logical error, but
will degrade performance. To test for this case we\r\nadd plenty of unit
tests around this logic and document manual testing\r\nprocedure. |\r\n|
Code should gracefully handle cases when feature X or plugin Y
are\r\ndisabled. | Medium | High | Unit tests will verify that any
feature flag\r\nor plugin combination still results in our service
operational. |\r\n| [See more potential
risk\r\nexamples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
|\r\n\r\n\r\n### For maintainers\r\n\r\n- [ ] This was checked for
breaking API changes and was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)\r\n\r\n---------\r\n\r\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\r\nCo-authored-by:
Stratoula Kalafateli
<efstratia.kalafateli@elastic.co>\r\nCo-authored-by: Elastic Machine
<elasticmachine@users.noreply.github.com>","sha":"8e4017779d7cd030f1fcd53a89043030018ff0ed"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Quynh Nguyen (Quinn) <43350163+qn895@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2024-09-18 03:51:09 +10:00 committed by GitHub
parent 513a12e076
commit 0b26510386
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 325 additions and 133 deletions

View file

@ -140,8 +140,6 @@ export const buildFieldsDefinitionsWithMetadata = (
options?: { advanceCursor?: boolean; openSuggestions?: boolean; addComma?: boolean }
): SuggestionRawDefinition[] => {
return fields.map((field) => {
const description = field.metadata?.description;
const titleCaseType = field.type.charAt(0).toUpperCase() + field.type.slice(1);
return {
label: field.name,
@ -151,16 +149,8 @@ export const buildFieldsDefinitionsWithMetadata = (
(options?.advanceCursor ? ' ' : ''),
kind: 'Variable',
detail: titleCaseType,
documentation: description
? {
value: `
---
${description}`,
}
: undefined,
// If there is a description, it is a field from ECS, so it should be sorted to the top
sortText: description ? '1D' : 'D',
// If detected to be an ECS field, push it up to the top of the list
sortText: field.isEcs ? '1D' : 'D',
command: options?.openSuggestions ? TRIGGER_SUGGESTION_COMMAND : undefined,
};
});

View file

@ -7,11 +7,10 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { getColumnsWithMetadata } from './ecs_metadata_helper';
import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/public';
import { ESQLRealField } from '@kbn/esql-validation-autocomplete';
import type { ESQLRealField } from '../../validation/types';
import { type ECSMetadata, enrichFieldsWithECSInfo } from './ecs_metadata_helper';
describe('getColumnsWithMetadata', () => {
describe('enrichFieldsWithECSInfo', () => {
it('should return original columns if fieldsMetadata is not provided', async () => {
const columns: ESQLRealField[] = [
{ name: 'ecs.version', type: 'keyword' },
@ -19,7 +18,7 @@ describe('getColumnsWithMetadata', () => {
{ name: 'field2', type: 'double' },
];
const result = await getColumnsWithMetadata(columns);
const result = await enrichFieldsWithECSInfo(columns);
expect(result).toEqual(columns);
});
@ -42,15 +41,23 @@ describe('getColumnsWithMetadata', () => {
},
}),
}),
} as unknown as FieldsMetadataPublicStart;
};
const fieldsMetadataCache = await (
await fieldsMetadata.getClient()
).find({
attributes: ['type'],
});
const result = await getColumnsWithMetadata(columns, fieldsMetadata);
const result = await enrichFieldsWithECSInfo(
columns,
fieldsMetadataCache.fields as ECSMetadata
);
expect(result).toEqual([
{
name: 'ecs.field',
type: 'text',
metadata: { description: 'ECS field description' },
isEcs: true,
},
{ name: 'ecs.fakeBooleanField', type: 'boolean' },
{ name: 'field2', type: 'double' },
@ -68,19 +75,27 @@ describe('getColumnsWithMetadata', () => {
find: jest.fn().mockResolvedValue({
fields: {
'ecs.version': { description: 'ECS version field', type: 'keyword' },
},
} as unknown as ECSMetadata,
}),
}),
} as unknown as FieldsMetadataPublicStart;
};
const result = await getColumnsWithMetadata(columns, fieldsMetadata);
const fieldsMetadataCache = await (
await fieldsMetadata.getClient()
).find({
attributes: ['type'],
});
const result = await enrichFieldsWithECSInfo(
columns,
fieldsMetadataCache.fields as ECSMetadata
);
expect(result).toEqual([
{ name: 'ecs.version', type: 'keyword', metadata: { description: 'ECS version field' } },
{ name: 'ecs.version', type: 'keyword', isEcs: true },
{
name: 'ecs.version.keyword',
type: 'keyword',
metadata: { description: 'ECS version field' },
isEcs: true,
},
{ name: 'field2', type: 'double' },
]);

View file

@ -0,0 +1,60 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import type { ESQLRealField } from '../../validation/types';
const removeKeywordSuffix = (name: string) => {
return name.endsWith('.keyword') ? name.slice(0, -8) : name;
};
export interface ECSMetadata {
[key: string]: {
type?: string;
source?: string;
description?: string;
};
}
/**
* Returns columns with the metadata/description (e.g ECS info)
* if available
*
* @param columns
* @param fieldsMetadata
* @returns
*/
export function enrichFieldsWithECSInfo(
columns: Array<Omit<ESQLRealField, 'metadata'>>,
ecsMetadataCache?: ECSMetadata
): ESQLRealField[] {
if (!ecsMetadataCache) return columns;
try {
if (ecsMetadataCache) {
return columns.map((c) => {
// Metadata services gives description for
// 'ecs.version' but not 'ecs.version.keyword'
// but both should show description if available
const metadata = ecsMetadataCache[removeKeywordSuffix(c.name)];
// Need to convert metadata's type (e.g. keyword) to ES|QL type (e.g. string) to check if they are the same
if (!metadata || (metadata?.type && metadata.type !== c.type)) return c;
return {
...c,
isEcs: true,
};
});
}
return columns;
} catch (error) {
// eslint-disable-next-line no-console
console.error('Unable to fetch field metadata', error);
}
return columns;
}

View file

@ -11,11 +11,12 @@ import { getActions } from './actions';
import { validateQuery } from '../validation/validation';
import { getAllFunctions } from '../shared/helpers';
import { getAstAndSyntaxErrors } from '@kbn/esql-ast';
import { CodeActionOptions } from './types';
import { ESQLRealField } from '../validation/types';
import { FieldType } from '../definitions/types';
import type { CodeActionOptions } from './types';
import type { ESQLRealField } from '../validation/types';
import type { FieldType } from '../definitions/types';
import type { ESQLCallbacks, PartialFieldsMetadataClient } from '../shared/types';
function getCallbackMocks() {
function getCallbackMocks(): jest.Mocked<ESQLCallbacks> {
return {
getFieldsFor: jest.fn<Promise<ESQLRealField[]>, any>(async ({ query }) => {
if (/enrich/.test(query)) {
@ -65,6 +66,11 @@ function getCallbackMocks() {
enrichFields: ['other-field', 'yetAnotherField'],
},
]),
getFieldsMetadata: jest.fn(async () => ({
find: jest.fn(async () => ({
fields: {},
})),
})) as unknown as Promise<PartialFieldsMetadataClient>,
};
}
@ -395,6 +401,7 @@ describe('quick fixes logic', () => {
const { errors } = await validateQuery(statement, getAstAndSyntaxErrors, undefined, {
...callbackMocks,
getFieldsFor: undefined,
getFieldsMetadata: undefined,
});
const actions = await getActions(
statement,
@ -403,7 +410,11 @@ describe('quick fixes logic', () => {
{
relaxOnMissingCallbacks: true,
},
{ ...callbackMocks, getFieldsFor: undefined }
{
...callbackMocks,
getFieldsFor: undefined,
getFieldsMetadata: undefined,
}
);
const edits = actions.map(({ edits: actionEdits }) => actionEdits[0].text);
expect(edits).toEqual(['`any#Char$Field`']);
@ -452,6 +463,7 @@ describe('quick fixes logic', () => {
getFieldsFor: undefined,
getSources: undefined,
getPolicies: undefined,
getFieldsMetadata: undefined,
}
);
} catch {

View file

@ -10,6 +10,7 @@
import type { ESQLAst } from '@kbn/esql-ast';
import type { ESQLCallbacks } from './types';
import type { ESQLRealField } from '../validation/types';
import { enrichFieldsWithECSInfo } from '../autocomplete/utils/ecs_metadata_helper';
export function buildQueryUntilPreviousCommand(ast: ESQLAst, queryString: string) {
const prevCommand = ast[Math.max(ast.length - 2, 0)];
@ -18,14 +19,31 @@ export function buildQueryUntilPreviousCommand(ast: ESQLAst, queryString: string
export function getFieldsByTypeHelper(queryText: string, resourceRetriever?: ESQLCallbacks) {
const cacheFields = new Map<string, ESQLRealField>();
const getEcsMetadata = async () => {
if (!resourceRetriever?.getFieldsMetadata) {
return undefined;
}
const client = await resourceRetriever?.getFieldsMetadata;
if (client.find) {
// Fetch full list of ECS field
// This list should be cached already by fieldsMetadataClient
const results = await client.find({ attributes: ['type'] });
return results?.fields;
}
};
const getFields = async () => {
const metadata = await getEcsMetadata();
if (!cacheFields.size && queryText) {
const fieldsOfType = await resourceRetriever?.getFieldsFor?.({ query: queryText });
for (const field of fieldsOfType || []) {
const fieldsWithMetadata = enrichFieldsWithECSInfo(fieldsOfType || [], metadata);
for (const field of fieldsWithMetadata || []) {
cacheFields.set(field.name, field);
}
}
};
return {
getFieldsByType: async (
expectedType: string | string[] = 'any',

View file

@ -12,6 +12,22 @@ import type { ESQLRealField } from '../validation/types';
/** @internal **/
type CallbackFn<Options = {}, Result = string> = (ctx?: Options) => Result[] | Promise<Result[]>;
/**
* Partial fields metadata client, used to avoid circular dependency with @kbn/monaco
/** @internal **/
export interface PartialFieldsMetadataClient {
find: ({ fieldNames, attributes }: { fieldNames?: string[]; attributes: string[] }) => Promise<{
fields: Record<
string,
{
type: string;
source: string;
description?: string;
}
>;
}>;
}
/** @public **/
export interface ESQLSourceResult {
name: string;
@ -29,6 +45,7 @@ export interface ESQLCallbacks {
{ name: string; sourceIndices: string[]; matchField: string; enrichFields: string[] }
>;
getPreferences?: () => Promise<{ histogramBarTarget: number }>;
getFieldsMetadata?: Promise<PartialFieldsMetadataClient>;
}
export type ReasonTypes = 'missingCommand' | 'unsupportedFunction' | 'unknownFunction';

View file

@ -20,6 +20,7 @@ export interface ESQLVariable {
export interface ESQLRealField {
name: string;
type: FieldType;
isEcs?: boolean;
metadata?: {
description?: string;
};

View file

@ -1709,6 +1709,7 @@ describe('validation logic', () => {
getPolicies: /Unknown policy/,
getFieldsFor: /Unknown column|Argument of|it is unsupported or not indexed/,
getPreferences: /Unknown/,
getFieldsMetadata: /Unknown/,
};
return excludedCallback.map((callback) => contentByCallback[callback]) || [];
}

View file

@ -1097,6 +1097,7 @@ export const ignoreErrorsMap: Record<keyof ESQLCallbacks, ErrorTypes[]> = {
getSources: ['unknownIndex'],
getPolicies: ['unknownPolicy'],
getPreferences: [],
getFieldsMetadata: [],
};
/**

View file

@ -14,7 +14,7 @@
"kbn_references": [
"@kbn/i18n",
"@kbn/esql-ast",
"@kbn/utility-types",
"@kbn/utility-types"
],
"exclude": [
"target/**/*",

View file

@ -0,0 +1,133 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import { PartialFieldsMetadataClient } from '@kbn/esql-validation-autocomplete/src/shared/types';
import { monaco } from '../monaco_imports';
import { ESQLLang } from './language';
describe('ESQLLang', () => {
describe('getSuggestionProvider', () => {
describe('resolveCompletionItem', () => {
it('should resolve completion item with field metadata', async () => {
const mockGetFieldsMetadata: Promise<PartialFieldsMetadataClient> = Promise.resolve({
find: jest.fn().mockResolvedValue({
fields: {
'test.field': {
type: 'keyword',
description: 'Test field description',
},
},
}),
});
const suggestionProvider = ESQLLang.getSuggestionProvider({
getFieldsMetadata: mockGetFieldsMetadata,
});
const ecsItem: monaco.languages.CompletionItem = {
label: 'test.field',
kind: 4,
insertText: 'test.field',
range: new monaco.Range(0, 0, 0, 0),
};
const resolvedItem = await suggestionProvider.resolveCompletionItem!(ecsItem, {} as any);
expect(resolvedItem).toEqual({
...ecsItem,
documentation: {
value: 'Test field description',
},
});
const ecsItemWithKeywordSuffix: monaco.languages.CompletionItem = {
label: 'test.field.keyword',
kind: 4,
insertText: 'test.field.keyword',
range: new monaco.Range(0, 0, 0, 0),
};
const resolvedItemWithKeywordSuffix = await suggestionProvider.resolveCompletionItem!(
ecsItemWithKeywordSuffix,
{} as any
);
expect(resolvedItemWithKeywordSuffix).toEqual({
...ecsItemWithKeywordSuffix,
documentation: {
value: 'Test field description',
},
});
});
it('should return original item if field metadata is not available', async () => {
const mockGetFieldsMetadata: Promise<PartialFieldsMetadataClient> = Promise.resolve({
find: jest.fn().mockResolvedValue({
fields: {},
}),
});
const suggestionProvider = ESQLLang.getSuggestionProvider({
getFieldsMetadata: mockGetFieldsMetadata,
});
const item: monaco.languages.CompletionItem = {
label: 'test.field',
kind: 4,
insertText: 'test.field',
range: new monaco.Range(0, 0, 0, 0),
};
const resolvedItem = await suggestionProvider.resolveCompletionItem!(item, {} as any);
expect(resolvedItem).toEqual(item);
});
it('should never call metadata find API if not needed', async () => {
const mockFind = jest.fn().mockResolvedValue({
fields: {},
});
const mockGetFieldsMetadata: Promise<PartialFieldsMetadataClient> = Promise.resolve({
find: mockFind,
});
const suggestionProvider = ESQLLang.getSuggestionProvider({
getFieldsMetadata: mockGetFieldsMetadata,
});
const notFieldItem: monaco.languages.CompletionItem = {
label: 'CASE',
kind: 1,
insertText: 'CASE',
range: new monaco.Range(0, 0, 0, 0),
};
const notFieldResolvedItem = await suggestionProvider.resolveCompletionItem!(
notFieldItem,
{} as any
);
expect(mockFind).toBeCalledTimes(1);
expect(notFieldResolvedItem).toEqual(notFieldItem);
mockFind.mockClear();
const notECSFieldItem: monaco.languages.CompletionItem = {
label: 'not.ecs.field',
kind: 4,
insertText: 'not.ecs.field',
range: new monaco.Range(0, 0, 0, 0),
};
const notECSFieldResolvedItem = await suggestionProvider.resolveCompletionItem!(
notECSFieldItem,
{} as any
);
expect(mockFind).toBeCalledTimes(1);
expect(notECSFieldResolvedItem).toEqual(notECSFieldItem);
});
});
});
});

View file

@ -21,6 +21,9 @@ import { wrapAsMonacoSuggestions } from './lib/converters/suggestions';
import { wrapAsMonacoCodeActions } from './lib/converters/actions';
const workerProxyService = new WorkerProxyService<ESQLWorker>();
const removeKeywordSuffix = (name: string) => {
return name.endsWith('.keyword') ? name.slice(0, -8) : name;
};
export const ESQLLang: CustomLangModuleType<ESQLCallbacks> = {
ID: ESQL_LANG_ID,
@ -107,6 +110,42 @@ export const ESQLLang: CustomLangModuleType<ESQLCallbacks> = {
suggestions: wrapAsMonacoSuggestions(suggestions),
};
},
async resolveCompletionItem(item, token): Promise<monaco.languages.CompletionItem> {
if (!callbacks?.getFieldsMetadata) return item;
const fieldsMetadataClient = await callbacks?.getFieldsMetadata;
const fullEcsMetadataList = await fieldsMetadataClient?.find({
attributes: ['type'],
});
if (!fullEcsMetadataList || !fieldsMetadataClient || typeof item.label !== 'string')
return item;
const strippedFieldName = removeKeywordSuffix(item.label);
if (
// If item is not a field, no need to fetch metadata
item.kind === monaco.languages.CompletionItemKind.Variable &&
// If not ECS, no need to fetch description
Object.hasOwn(fullEcsMetadataList?.fields, strippedFieldName)
) {
const ecsMetadata = await fieldsMetadataClient.find({
fieldNames: [strippedFieldName],
attributes: ['description'],
});
const fieldMetadata = ecsMetadata.fields[strippedFieldName];
if (fieldMetadata && fieldMetadata.description) {
const completionItem: monaco.languages.CompletionItem = {
...item,
documentation: {
value: fieldMetadata.description,
},
};
return completionItem;
}
}
return item;
},
};
},

View file

@ -1,98 +0,0 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
import type { ESQLRealField } from '@kbn/esql-validation-autocomplete';
import type { FieldsMetadataPublicStart } from '@kbn/fields-metadata-plugin/public';
import { chunk } from 'lodash';
const removeKeywordSuffix = (name: string) => {
return name.endsWith('.keyword') ? name.slice(0, -8) : name;
};
/**
* Returns columns with the metadata/description (e.g ECS info)
* if available
*
* @param columns
* @param fieldsMetadata
* @returns
*/
export async function getColumnsWithMetadata(
columns: Array<Omit<ESQLRealField, 'metadata'>>,
fieldsMetadata?: FieldsMetadataPublicStart
): Promise<ESQLRealField[]> {
if (!fieldsMetadata) return columns;
try {
const fieldsMetadataClient = await fieldsMetadata?.getClient();
if (fieldsMetadataClient) {
const fields = await fieldsMetadataClient.find({
fieldNames: columns.map((c) => c.name),
attributes: ['description', 'type'],
});
if (fields?.fields) {
return columns.map((c) => {
// Metadata services gives description for
// 'ecs.version' but not 'ecs.version.keyword'
// but both should show description if available
const metadata = fields.fields[removeKeywordSuffix(c.name)];
// Need to convert metadata's type (e.g. keyword) to ES|QL type (e.g. string) to check if they are the same
if (!metadata || (metadata?.type && metadata.type !== c.type)) return c;
return {
...c,
metadata: { description: metadata.description },
};
});
}
}
} catch (error) {
// eslint-disable-next-line no-console
console.error('Unable to fetch field metadata', error);
}
return columns;
}
/**
* Returns columns with the metadata/description (e.g ECS info)
* if available. Safely partition the requests to avoid 400 payload too big errors.
*
* @param columns
* @param fieldsMetadata
* @returns
*/
export async function getRateLimitedColumnsWithMetadata(
columns: Array<Omit<ESQLRealField, 'metadata'>>,
fieldsMetadata?: FieldsMetadataPublicStart,
maxFieldsPerRequest = 250,
maxConcurrentRequests = 10
): Promise<ESQLRealField[]> {
if (!fieldsMetadata) return columns;
try {
// Chunking requests here since we are calling fieldsMetadata.find with list of fields,
// and we need to make sure payload is not too big, or else get 400 error
const chunkedColumns = chunk(columns, maxFieldsPerRequest);
const result: Array<PromiseSettledResult<ESQLRealField[]>> = [];
// Also only make max of n at a time to avoid too many concurrent requests
for (let i = 0; i < chunkedColumns.length; i += maxConcurrentRequests) {
const cols = chunkedColumns.slice(i, i + maxConcurrentRequests);
const chunkResult = await Promise.allSettled(
cols.map((c) => getColumnsWithMetadata(c, fieldsMetadata))
);
result.push(...chunkResult);
}
return result.flatMap((r, idx) => (r.status === 'fulfilled' ? r.value : chunkedColumns[idx]));
} catch (error) {
// eslint-disable-next-line no-console
console.error('Unable to fetch field metadata', error);
}
return columns;
}

View file

@ -51,7 +51,6 @@ import {
EDITOR_MIN_HEIGHT,
textBasedLanguageEditorStyles,
} from './text_based_languages_editor.styles';
import { getRateLimitedColumnsWithMetadata } from './ecs_metadata_helper';
import type { TextBasedLanguagesEditorProps, TextBasedEditorDeps } from './types';
import './overwrite.scss';
@ -112,6 +111,7 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
const [isLanguagePopoverOpen, setIsLanguagePopoverOpen] = useState(false);
const [isQueryLoading, setIsQueryLoading] = useState(true);
const [abortController, setAbortController] = useState(new AbortController());
// contains both client side validation and server messages
const [editorMessages, setEditorMessages] = useState<{
errors: MonacoMessage[];
@ -353,7 +353,8 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
type: c.meta.esType as FieldType,
};
}) || [];
return await getRateLimitedColumnsWithMetadata(columns, fieldsMetadata);
return columns;
} catch (e) {
// no action yet
}
@ -373,6 +374,8 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
histogramBarTarget,
};
},
// @ts-expect-error To prevent circular type import, type defined here is partial of full client
getFieldsMetadata: fieldsMetadata?.getClient(),
};
return callbacks;
}, [
@ -386,8 +389,8 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
expressions,
abortController,
indexManagementApiService,
fieldsMetadata,
histogramBarTarget,
fieldsMetadata,
]);
const queryRunButtonProperties = useMemo(() => {