Skip to content

Commit

Permalink
fix(queryAssist): allow dataset with DATA_SOURCE type (opensearch-p…
Browse files Browse the repository at this point in the history
…roject#7890) (opensearch-project#7892)

* fix(queryAssist): allow dataset with DATA_SOURCE type
* prevent multiple calls to get available languages

---------

(cherry picked from commit 8033aa4)

Signed-off-by: Joshua Li <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent abed5ba commit e8683ca
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@ import assistantMark from '../../assets/query_assist_mark.svg';
*/
const getAvailableLanguagesForDataSource = (() => {
const availableLanguagesByDataSource: Map<string | undefined, string[]> = new Map();
const pendingRequests: Map<string | undefined, Promise<string[]>> = new Map();

return async (http: HttpSetup, dataSourceId: string | undefined) => {
const cached = availableLanguagesByDataSource.get(dataSourceId);
if (cached !== undefined) return cached;
const languages = await http

const pendingRequest = pendingRequests.get(dataSourceId);
if (pendingRequest !== undefined) return pendingRequest;

const languagesPromise = http
.get<{ configuredLanguages: string[] }>(API.QUERY_ASSIST.LANGUAGES, {
query: { dataSourceId },
})
.then((response) => response.configuredLanguages)
.catch(() => []);
.catch(() => [])
.finally(() => pendingRequests.delete(dataSourceId));
pendingRequests.set(dataSourceId, languagesPromise);

const languages = await languagesPromise;
availableLanguagesByDataSource.set(dataSourceId, languages);
return languages;
};
Expand All @@ -47,7 +57,11 @@ const getAvailableLanguages$ = (http: HttpSetup, data: DataPublicPluginSetup) =>
switchMap(async (query) => {
// currently query assist tool relies on opensearch API to get index
// mappings, external data source types (e.g. s3) are not supported
if (query.dataset?.dataSource?.type !== DEFAULT_DATA.SOURCE_TYPES.OPENSEARCH) return [];
if (
query.dataset?.dataSource?.type !== DEFAULT_DATA.SOURCE_TYPES.OPENSEARCH &&
query.dataset?.dataSource?.type !== 'DATA_SOURCE'
)
return [];

const dataSourceId = query.dataset?.dataSource?.id;
return getAvailableLanguagesForDataSource(http, dataSourceId);
Expand Down

0 comments on commit e8683ca

Please sign in to comment.