Skip to content

Commit

Permalink
use value suggestion provider through utils
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sebastian <[email protected]>
  • Loading branch information
paulstn committed Jul 31, 2024
1 parent a6b6aab commit 3e4e40a
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/plugins/data/public/antlr/dql/code_completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IndexPattern, IndexPatternField } from '../../index_patterns';
import { QuerySuggestionGetFnArgs } from '../../autocomplete';
import { DQLParserVisitor } from './.generated/DQLParserVisitor';
import { getUiService } from '../../services';
import { IDataPluginServices } from '../..';

const findCursorIndex = (
tokenStream: TokenStream,
Expand Down Expand Up @@ -59,24 +60,13 @@ const findFieldSuggestions = (indexPattern: IndexPattern) => {
return fieldSuggestions;
};

const getFieldSuggestedValues = async (
indexTitle: string,
fieldName: string,
currentValue: string,
http?: HttpSetup
) => {
if (!http) return [];
return await http.fetch(`/api/opensearch-dashboards/suggestions/values/${indexTitle}`, {
method: 'POST',
body: JSON.stringify({ query: currentValue, field: fieldName, boolFilter: [] }),
});
};

const findValueSuggestions = async (
index: IndexPattern,
field: string,
value: string,
http?: HttpSetup
services: IDataPluginServices,
boolFilter?: any,
signal?: AbortSignal
) => {
// check to see if last field is within index and if it can suggest values, first check
// if .keyword appended field exists because that has values
Expand All @@ -90,14 +80,15 @@ const findValueSuggestions = async (
if (idxField.name === field) return idxField;
});

if (matchedField?.type === 'boolean') {
return ['true', 'false'];
}
if (!matchedField) return;

if (!matchedField || !matchedField.aggregatable || matchedField.type !== 'string') return;

// ask api for suggestions
return await getFieldSuggestedValues(index.title, matchedField.name, value, http);
return await services?.data.autocomplete.getValueSuggestions({
indexPattern: index,
field: matchedField,
query: value,
boolFilter,
signal,
});
};

// visitor for parsing the current query
Expand Down Expand Up @@ -128,6 +119,8 @@ export const getSuggestions = async ({
position,
selectionEnd,
services,
boolFilter,
signal,
}: QuerySuggestionGetFnArgs) => {
if (
!services ||
Expand All @@ -138,8 +131,6 @@ export const getSuggestions = async ({
return [];
}
try {
const http = services.http;

const inputStream = CharStream.fromString(query);
const lexer = new DQLLexer(inputStream);
const tokenStream = new CommonTokenStream(lexer);
Expand Down Expand Up @@ -184,7 +175,14 @@ export const getSuggestions = async ({
// find suggested values for the last found field (only for kvexpression rule)
const { field: lastField = '', value: lastValue = '' } = visitor.visit(tree) ?? {};
if (!!lastField && candidates.tokens.has(DQLParser.PHRASE)) {
const values = await findValueSuggestions(indexPattern, lastField, lastValue ?? '', http);
const values = await findValueSuggestions(
indexPattern,
lastField,
lastValue ?? '',
services,
boolFilter,
signal
);
if (!!values) {
completions.push(
...values?.map((val: any) => {
Expand Down

0 comments on commit 3e4e40a

Please sign in to comment.