forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ES|QL] Do not load fields on every keystroke, when editing just the …
…source command (elastic#184423) ## Summary Fixes elastic#184093 Closes elastic#184417 - Fixes the problem on language tools layer. - Prevents fields loading on every keystroke, when there is only a single source command, which does not need the fields (FROM, SHOW, ROW). It does it in two places: - Validation - Autocompletion ### Checklist Delete any items that are not applicable to this PR. - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or ### For maintainers - [x] This was checked for breaking API changes and was [labeled appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
839d611
commit 4397787
Showing
8 changed files
with
201 additions
and
61 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
packages/kbn-esql-validation-autocomplete/src/__tests__/helpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { camelCase } from 'lodash'; | ||
import { supportedFieldTypes } from '../definitions/types'; | ||
|
||
export const fields = [ | ||
...supportedFieldTypes.map((type) => ({ name: `${camelCase(type)}Field`, type })), | ||
{ name: 'any#Char$Field', type: 'number' }, | ||
{ name: 'kubernetes.something.something', type: 'number' }, | ||
{ name: '@timestamp', type: 'date' }, | ||
]; | ||
|
||
export const enrichFields = [ | ||
{ name: 'otherField', type: 'string' }, | ||
{ name: 'yetAnotherField', type: 'number' }, | ||
]; | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const unsupported_field = [{ name: 'unsupported_field', type: 'unsupported' }]; | ||
|
||
export const indexes = [ | ||
'a_index', | ||
'index', | ||
'other_index', | ||
'.secret_index', | ||
'my-index', | ||
'unsupported_index', | ||
]; | ||
|
||
export const policies = [ | ||
{ | ||
name: 'policy', | ||
sourceIndices: ['enrich_index'], | ||
matchField: 'otherStringField', | ||
enrichFields: ['otherField', 'yetAnotherField'], | ||
}, | ||
{ | ||
name: 'policy$', | ||
sourceIndices: ['enrich_index'], | ||
matchField: 'otherStringField', | ||
enrichFields: ['otherField', 'yetAnotherField'], | ||
}, | ||
]; | ||
|
||
export function getCallbackMocks() { | ||
return { | ||
getFieldsFor: jest.fn(async ({ query }) => { | ||
if (/enrich/.test(query)) { | ||
return enrichFields; | ||
} | ||
if (/unsupported_index/.test(query)) { | ||
return unsupported_field; | ||
} | ||
if (/dissect|grok/.test(query)) { | ||
return [{ name: 'firstWord', type: 'string' }]; | ||
} | ||
return fields; | ||
}), | ||
getSources: jest.fn(async () => | ||
indexes.map((name) => ({ | ||
name, | ||
hidden: name.startsWith('.'), | ||
})) | ||
), | ||
getPolicies: jest.fn(async () => policies), | ||
}; | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/kbn-esql-validation-autocomplete/src/autocomplete/autocomplete.suggest.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getAstAndSyntaxErrors } from '@kbn/esql-ast'; | ||
import { ESQLCallbacks } from '../shared/types'; | ||
import * as autocomplete from './autocomplete'; | ||
import { getCallbackMocks } from '../__tests__/helpers'; | ||
import { EditorContext } from './types'; | ||
|
||
const setup = async (caret = '?') => { | ||
if (caret.length !== 1) throw new Error('Caret must be a single character'); | ||
const callbacks = getCallbackMocks(); | ||
const suggest = async ( | ||
query: string, | ||
ctx: EditorContext = { | ||
triggerKind: 0, | ||
}, | ||
cb: ESQLCallbacks = callbacks | ||
) => { | ||
const pos = query.indexOf(caret); | ||
if (pos < 0) throw new Error(`User cursor/caret "${caret}" not found in query: ${query}`); | ||
const querySansCaret = query.slice(0, pos) + query.slice(pos + 1); | ||
return await autocomplete.suggest(querySansCaret, pos, ctx, getAstAndSyntaxErrors, cb); | ||
}; | ||
|
||
return { | ||
callbacks, | ||
suggest, | ||
}; | ||
}; | ||
|
||
describe('autocomplete.suggest', () => { | ||
test('does not load fields when suggesting within a single FROM, SHOW, ROW command', async () => { | ||
const { suggest, callbacks } = await setup(); | ||
|
||
await suggest('FROM kib, ? |'); | ||
await suggest('FROM ?'); | ||
await suggest('FROM ? |'); | ||
await suggest('sHoW ?'); | ||
await suggest('row ? |'); | ||
|
||
expect(callbacks.getFieldsFor.mock.calls.length).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/kbn-esql-validation-autocomplete/src/validation/validation.from.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getAstAndSyntaxErrors } from '@kbn/esql-ast'; | ||
import { ESQLCallbacks } from '../shared/types'; | ||
import { ValidationOptions } from './types'; | ||
import { validateQuery } from './validation'; | ||
import { getCallbackMocks } from '../__tests__/helpers'; | ||
|
||
const setup = async () => { | ||
const callbacks = getCallbackMocks(); | ||
const validate = async ( | ||
query: string, | ||
opts: ValidationOptions = {}, | ||
cb: ESQLCallbacks = callbacks | ||
) => { | ||
return await validateQuery(query, getAstAndSyntaxErrors, opts, cb); | ||
}; | ||
|
||
return { | ||
callbacks, | ||
validate, | ||
}; | ||
}; | ||
|
||
test('does not load fields when validating only a single FROM, SHOW, ROW command', async () => { | ||
const { validate, callbacks } = await setup(); | ||
|
||
await validate('FROM kib'); | ||
await validate('FROM kibana_ecommerce METADATA _i'); | ||
await validate('FROM kibana_ecommerce METADATA _id | '); | ||
await validate('SHOW'); | ||
await validate('ROW \t'); | ||
|
||
expect(callbacks.getFieldsFor.mock.calls.length).toBe(0); | ||
}); | ||
|
||
test('loads fields with FROM source when commands after pipe present', async () => { | ||
const { validate, callbacks } = await setup(); | ||
|
||
await validate('FROM kibana_ecommerce METADATA _id | eval'); | ||
|
||
expect(callbacks.getFieldsFor.mock.calls.length).toBe(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters