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.
…211101) ## Summary Part of elastic#195418 Gives `GROK` and `DISSECT` autocomplete logic its own home 🏡 ### Checklist - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios ### Identify risks - [ ] As with any refactor, there's a possibility this will introduce a regression in the behavior of commands. However, all automated tests are passing and I have tested the behavior manually and can detect no regression.
- Loading branch information
1 parent
c44580f
commit 1223926
Showing
12 changed files
with
261 additions
and
126 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
...l-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.dissect.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,71 @@ | ||
/* | ||
* 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 { ESQL_STRING_TYPES } from '../../shared/esql_types'; | ||
import { attachTriggerCommand, getFieldNamesByType, setup } from './helpers'; | ||
|
||
describe('autocomplete.suggest', () => { | ||
describe('DISSECT', () => { | ||
it('suggests fields after DISSECT', async () => { | ||
const { assertSuggestions } = await setup(); | ||
await assertSuggestions( | ||
'from a | DISSECT /', | ||
getFieldNamesByType(ESQL_STRING_TYPES).map((name) => `${name} `) | ||
); | ||
await assertSuggestions( | ||
'from a | DISSECT /', | ||
getFieldNamesByType(ESQL_STRING_TYPES).map((name) => `${name} `), | ||
{ triggerCharacter: ' ' } | ||
); | ||
await assertSuggestions( | ||
'from a | DISSECT key/', | ||
getFieldNamesByType(ESQL_STRING_TYPES).map((name) => `${name} `) | ||
); | ||
await assertSuggestions( | ||
'from a | DISSECT keywordField/', | ||
['keywordField ', 'textField '].map(attachTriggerCommand) | ||
); | ||
}); | ||
|
||
const constantPattern = '"%{firstWord}" '; | ||
it('suggests a pattern after a field name', async () => { | ||
const { assertSuggestions } = await setup(); | ||
await assertSuggestions('from a | DISSECT keywordField /', [constantPattern]); | ||
}); | ||
|
||
it('suggests an append separator or pipe after a pattern', async () => { | ||
const { assertSuggestions } = await setup(); | ||
assertSuggestions( | ||
`from a | DISSECT keywordField ${constantPattern} /`, | ||
['APPEND_SEPARATOR = ', '| '].map(attachTriggerCommand), | ||
{ triggerCharacter: ' ' } | ||
); | ||
assertSuggestions( | ||
`from a | DISSECT keywordField ${constantPattern} /`, | ||
['APPEND_SEPARATOR = ', '| '].map(attachTriggerCommand) | ||
); | ||
}); | ||
|
||
it('suggests append separators', async () => { | ||
const { assertSuggestions } = await setup(); | ||
await assertSuggestions( | ||
`from a | DISSECT keywordField ${constantPattern} append_separator = /`, | ||
['":" ', '";" '].map(attachTriggerCommand) | ||
); | ||
}); | ||
|
||
it('suggests a pipe after an append separator', async () => { | ||
const { assertSuggestions } = await setup(); | ||
await assertSuggestions( | ||
`from a | DISSECT keywordField ${constantPattern} append_separator = ":" /`, | ||
['| '] | ||
); | ||
}); | ||
}); | ||
}); |
42 changes: 42 additions & 0 deletions
42
...esql-validation-autocomplete/src/autocomplete/__tests__/autocomplete.command.grok.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,42 @@ | ||
/* | ||
* 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 { ESQL_STRING_TYPES } from '../../shared/esql_types'; | ||
import { attachTriggerCommand, getFieldNamesByType, setup } from './helpers'; | ||
|
||
describe('autocomplete.suggest', () => { | ||
describe('GROK', () => { | ||
it('suggests fields after GROK', async () => { | ||
const { assertSuggestions } = await setup(); | ||
await assertSuggestions( | ||
'from a | grok /', | ||
getFieldNamesByType(ESQL_STRING_TYPES).map((name) => `${name} `) | ||
); | ||
await assertSuggestions( | ||
'from a | grok key/', | ||
getFieldNamesByType(ESQL_STRING_TYPES).map((name) => `${name} `) | ||
); | ||
await assertSuggestions( | ||
'from a | grok keywordField/', | ||
['keywordField ', 'textField '].map(attachTriggerCommand) | ||
); | ||
}); | ||
|
||
const constantPattern = '"%{WORD:firstWord}"'; | ||
it('suggests a pattern after a field name', async () => { | ||
const { assertSuggestions } = await setup(); | ||
await assertSuggestions('from a | grok keywordField /', [constantPattern + ' ']); | ||
}); | ||
|
||
it('suggests a pipe after a pattern', async () => { | ||
const { assertSuggestions } = await setup(); | ||
await assertSuggestions(`from a | grok keywordField ${constantPattern} /`, ['| ']); | ||
}); | ||
}); | ||
}); |
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
77 changes: 77 additions & 0 deletions
77
...ckages/shared/kbn-esql-validation-autocomplete/src/autocomplete/commands/dissect/index.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,77 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
import { EDITOR_MARKER } from '../../../shared/constants'; | ||
import { isSingleItem } from '../../../..'; | ||
import { ESQL_STRING_TYPES } from '../../../shared/esql_types'; | ||
import { CommandSuggestParams } from '../../../definitions/types'; | ||
|
||
import type { SuggestionRawDefinition } from '../../types'; | ||
import { TRIGGER_SUGGESTION_COMMAND, buildConstantsDefinitions } from '../../factories'; | ||
import { colonCompleteItem, pipeCompleteItem, semiColonCompleteItem } from '../../complete_items'; | ||
|
||
export async function suggest({ | ||
command, | ||
innerText, | ||
getColumnsByType, | ||
}: CommandSuggestParams<'dissect'>): Promise<SuggestionRawDefinition[]> { | ||
const commandArgs = command.args.filter( | ||
(arg) => isSingleItem(arg) && arg.text !== EDITOR_MARKER && arg.text !== '' | ||
); | ||
|
||
// DISSECT field / | ||
if (commandArgs.length === 1) { | ||
return buildConstantsDefinitions( | ||
['"%{firstWord}"'], | ||
i18n.translate('kbn-esql-validation-autocomplete.esql.autocomplete.aPatternString', { | ||
defaultMessage: 'A pattern string', | ||
}), | ||
undefined, | ||
{ | ||
advanceCursorAndOpenSuggestions: true, | ||
} | ||
); | ||
} | ||
// DISSECT field pattern / | ||
else if (commandArgs.length === 2) { | ||
return [ | ||
{ ...pipeCompleteItem, command: TRIGGER_SUGGESTION_COMMAND }, | ||
appendSeparatorCompletionItem, | ||
]; | ||
} | ||
// DISSECT field APPEND_SEPARATOR = / | ||
else if (/append_separator\s*=\s*$/i.test(innerText)) { | ||
return [colonCompleteItem, semiColonCompleteItem]; | ||
} | ||
// DISSECT field APPEND_SEPARATOR = ":" / | ||
else if (commandArgs.some((arg) => isSingleItem(arg) && arg.type === 'option')) { | ||
return [{ ...pipeCompleteItem, command: TRIGGER_SUGGESTION_COMMAND }]; | ||
} | ||
|
||
// DISSECT / | ||
const fieldSuggestions = await getColumnsByType(ESQL_STRING_TYPES); | ||
return fieldSuggestions.map((sug) => ({ | ||
...sug, | ||
text: `${sug.text} `, | ||
command: TRIGGER_SUGGESTION_COMMAND, | ||
})); | ||
} | ||
|
||
const appendSeparatorCompletionItem: SuggestionRawDefinition = { | ||
command: TRIGGER_SUGGESTION_COMMAND, | ||
detail: i18n.translate('kbn-esql-validation-autocomplete.esql.definitions.appendSeparatorDoc', { | ||
defaultMessage: | ||
'The character(s) that separate the appended fields. Default to empty string ("").', | ||
}), | ||
kind: 'Reference', | ||
label: 'APPEND_SEPARATOR', | ||
sortText: '1', | ||
text: 'APPEND_SEPARATOR = ', | ||
}; |
Oops, something went wrong.