Skip to content

Commit

Permalink
[ES|QL] Improve editor's performance by caching the data sources (ela…
Browse files Browse the repository at this point in the history
…stic#183886)

## Summary

Part of elastic#180588

Caching the sources in the editor in order to not retrieve them every
time a user is typing something. This is an important performance boost
for the editor.

There are more things wrong, for example we are always fetching the
metadata. I think this is wrong, the metadata is and will always be a
static list. This should be cleaned up in a follow up PR
  • Loading branch information
stratoula authored and rshen91 committed May 29, 2024
1 parent 7256d62 commit 4875e71
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/kbn-text-based-editor/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,11 @@ export const clearCacheWhenOld = (cache: MapCache, esqlQuery: string) => {
}
}
};

export const getESQLSources = async (dataViews: DataViewsPublicPluginStart) => {
const [remoteIndices, localIndices] = await Promise.all([
getRemoteIndicesList(dataViews),
getIndicesList(dataViews),
]);
return [...localIndices, ...remoteIndices];
};
30 changes: 21 additions & 9 deletions packages/kbn-text-based-editor/src/text_based_languages_editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ import {
type MonacoMessage,
getWrappedInPipesCode,
parseErrors,
getIndicesList,
getRemoteIndicesList,
clearCacheWhenOld,
getESQLSources,
} from './helpers';
import { EditorFooter } from './editor_footer';
import { ResizableButton } from './resizable_button';
Expand Down Expand Up @@ -376,14 +375,24 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
return { cache: fn.cache, memoizedFieldsFromESQL: fn };
}, []);

const { cache: dataSourcesCache, memoizedSources } = useMemo(() => {
const fn = memoize(
(...args: [DataViewsPublicPluginStart]) => ({
timestamp: Date.now(),
result: getESQLSources(...args),
}),
({ esql }) => esql
);

return { cache: fn.cache, memoizedSources: fn };
}, []);

const esqlCallbacks: ESQLCallbacks = useMemo(() => {
const callbacks: ESQLCallbacks = {
getSources: async () => {
const [remoteIndices, localIndices] = await Promise.all([
getRemoteIndicesList(dataViews),
getIndicesList(dataViews),
]);
return [...localIndices, ...remoteIndices];
clearCacheWhenOld(dataSourcesCache, queryString);
const sources = await memoizedSources(dataViews).result;
return sources;
},
getFieldsFor: async ({ query: queryToExecute }: { query?: string } | undefined = {}) => {
if (queryToExecute) {
Expand Down Expand Up @@ -419,12 +428,15 @@ export const TextBasedLanguagesEditor = memo(function TextBasedLanguagesEditor({
};
return callbacks;
}, [
queryString,
memoizedSources,
dataSourcesCache,
dataViews,
expressions,
indexManagementApiService,
esqlFieldsCache,
memoizedFieldsFromESQL,
expressions,
abortController,
indexManagementApiService,
]);

const parseMessages = useCallback(async () => {
Expand Down

0 comments on commit 4875e71

Please sign in to comment.