Skip to content

Commit

Permalink
Abort discarded KQL value suggestion requests
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasolson committed Nov 22, 2019
1 parent 21d6cd3 commit 08b58af
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export class QueryBarInputUI extends Component<Props, State> {
public inputRef: HTMLInputElement | null = null;

private persistedLog: PersistedLog | undefined;
private abortController: AbortController | undefined;
private services = this.props.kibana.services;
private componentIsUnmounting = false;

Expand Down Expand Up @@ -167,12 +168,22 @@ export class QueryBarInputUI extends Component<Props, State> {
return;
}

const suggestions: AutocompleteSuggestion[] = await getAutocompleteSuggestions({
query: queryString,
selectionStart,
selectionEnd,
});
return [...suggestions, ...recentSearchSuggestions];
try {
if (this.abortController) this.abortController.abort();
this.abortController = new AbortController();
const suggestions: AutocompleteSuggestion[] = await getAutocompleteSuggestions({
query: queryString,
selectionStart,
selectionEnd,
signal: this.abortController.signal,
});
return [...suggestions, ...recentSearchSuggestions];
} catch (e) {
// TODO: Waiting on https://github.com/elastic/kibana/issues/51406 for a properly typed error
// Ignore aborted requests
if (e.message === 'The user aborted a request.') return;
throw e;
}
};

private getRecentSearchSuggestions = (query: string) => {
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/autocomplete_provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type GetSuggestions = (args: {
query: string;
selectionStart: number;
selectionEnd: number;
signal?: AbortSignal;
}) => Promise<AutocompleteSuggestion[]>;

/** @public **/
Expand Down
19 changes: 16 additions & 3 deletions src/plugins/data/public/suggestions_provider/value_suggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,36 @@ export function getSuggestionsProvider(
http: HttpServiceBase
): IGetSuggestions {
const requestSuggestions = memoize(
(index: string, field: IFieldType, query: string, boolFilter: any = []) => {
(
index: string,
field: IFieldType,
query: string,
boolFilter: any = [],
signal?: AbortSignal
) => {
return http.fetch(`/api/kibana/suggestions/values/${index}`, {
method: 'POST',
body: JSON.stringify({ query, field: field.name, boolFilter }),
signal,
});
},
resolver
);

return async (index: string, field: IFieldType, query: string, boolFilter?: any) => {
return async (
index: string,
field: IFieldType,
query: string,
boolFilter?: any,
signal?: AbortSignal
) => {
const shouldSuggestValues = uiSettings.get('filterEditor:suggestValues');
if (field.type === 'boolean') {
return [true, false];
} else if (!shouldSuggestValues || !field.aggregatable || field.type !== 'string') {
return [];
}
return await requestSuggestions(index, field, query, boolFilter);
return await requestSuggestions(index, field, query, boolFilter, signal);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const kueryProvider = ({ config, indexPatterns, boolFilter }) => {
return provider({ config, indexPatterns, boolFilter });
});

return function getSuggestions({ query, selectionStart, selectionEnd }) {
return function getSuggestions({ query, selectionStart, selectionEnd, signal }) {
const cursoredQuery = `${query.substr(0, selectionStart)}${cursorSymbol}${query.substr(selectionEnd)}`;

let cursorNode;
Expand All @@ -34,7 +34,7 @@ export const kueryProvider = ({ config, indexPatterns, boolFilter }) => {

const { suggestionTypes = [] } = cursorNode;
const suggestionsByType = suggestionTypes.map(type => {
return getSuggestionsByType[type](cursorNode);
return getSuggestionsByType[type](cursorNode, signal);
});
return Promise.all(suggestionsByType)
.then(suggestionsByType => dedup(flatten(suggestionsByType)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export function getSuggestionsProvider({ indexPatterns, boolFilter }) {
suffix,
fieldName,
nestedPath,
}) {
}, signal) {
const fullFieldName = nestedPath ? `${nestedPath}.${fieldName}` : fieldName;
const fields = allFields.filter(field => field.name === fullFieldName);
const query = `${prefix}${suffix}`.trim();
const { getSuggestions } = npStart.plugins.data;

const suggestionsByField = fields.map(field => {
return getSuggestions(field.indexPatternTitle, field, query, boolFilter).then(data => {
return getSuggestions(field.indexPatternTitle, field, query, boolFilter, signal).then(data => {
const quotedValues = data.map(value => typeof value === 'string' ? `"${escapeQuotes(value)}"` : `${value}`);
return wrapAsSuggestions(start, end, query, quotedValues);
});
Expand Down

0 comments on commit 08b58af

Please sign in to comment.