Skip to content

Commit

Permalink
fix: 🐛 Sort merge results based on default data source (input) (#3903)
Browse files Browse the repository at this point in the history
  • Loading branch information
igoroctaviano authored Feb 5, 2024
1 parent 1678593 commit 5bba98e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
42 changes: 36 additions & 6 deletions extensions/default/src/MergeDataSource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const mergeMap: MergeMap = {
* @param {unknown[]} options.args - The arguments to be passed to the function.
* @param {ExtensionManager} options.extensionManager - The extension manager.
* @param {string[]} options.dataSourceNames - The names of the data sources to be called.
* @param {string} options.defaultDataSourceName - The name of the default data source.
* @returns {Promise<unknown[]>} - A promise that resolves to the merged data from all data sources.
*/
export const callForAllDataSourcesAsync = async ({
Expand All @@ -41,24 +42,32 @@ export const callForAllDataSourcesAsync = async ({
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}: CallForAllDataSourcesAsyncOptions) => {
const { mergeKey, tagFunc } = mergeMap[path] || { tagFunc: x => x };

const dataSourceDefs = Object.values(extensionManager.dataSourceDefs);
/** Sort by default data source */
const defs = Object.values(extensionManager.dataSourceDefs);
const defaultDataSourceDef = defs.find(def => def.sourceName === defaultDataSourceName);
const dataSourceDefs = defs.filter(def => def.sourceName !== defaultDataSourceName);
dataSourceDefs.unshift(defaultDataSourceDef);

const promises = [];
const mergedData = [];
const sourceNames = [];

for (const dataSourceDef of dataSourceDefs) {
const { configuration, sourceName } = dataSourceDef;
if (!!configuration && dataSourceNames.includes(sourceName)) {
const [dataSource] = extensionManager.getDataSources(sourceName);
const func = get(dataSource, path);
const promise = func.apply(dataSource, args);
promises.push(promise.then(data => mergedData.push(tagFunc(data, sourceName))));
promises.push(promise);
sourceNames.push(sourceName);
}
}

await Promise.allSettled(promises);
const data = await Promise.allSettled(promises);
const mergedData = data.map((data, i) => tagFunc(data.value, sourceNames[i]));

let results = [];
if (mergeKey) {
Expand All @@ -77,15 +86,22 @@ export const callForAllDataSourcesAsync = async ({
* @param options.args - The arguments to be passed to the function.
* @param options.extensionManager - The extension manager instance.
* @param options.dataSourceNames - The names of the data sources to be called.
* @param options.defaultDataSourceName - The name of the default data source.
* @returns The merged data from all the matching data sources.
*/
export const callForAllDataSources = ({
path,
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}: CallForAllDataSourcesOptions) => {
const dataSourceDefs = Object.values(extensionManager.dataSourceDefs);
/** Sort by default data source */
const defs = Object.values(extensionManager.dataSourceDefs);
const defaultDataSourceDef = defs.find(def => def.sourceName === defaultDataSourceName);
const dataSourceDefs = defs.filter(def => def.sourceName !== defaultDataSourceName);
dataSourceDefs.unshift(defaultDataSourceDef);

const mergedData = [];
for (const dataSourceDef of dataSourceDefs) {
const { configuration, sourceName } = dataSourceDef;
Expand All @@ -96,6 +112,7 @@ export const callForAllDataSources = ({
mergedData.push(data);
}
}

return mergedData.flat();
};

Expand Down Expand Up @@ -154,7 +171,13 @@ function createMergeDataSourceApi(

const implementation = {
initialize: (...args: unknown[]) =>
callForAllDataSources({ path: 'initialize', args, extensionManager, dataSourceNames }),
callForAllDataSources({
path: 'initialize',
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
query: {
studies: {
search: (...args: unknown[]) =>
Expand All @@ -164,6 +187,7 @@ function createMergeDataSourceApi(
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
},
series: {
Expand All @@ -174,6 +198,7 @@ function createMergeDataSourceApi(
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
},
instances: {
Expand All @@ -184,6 +209,7 @@ function createMergeDataSourceApi(
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
},
},
Expand All @@ -195,6 +221,7 @@ function createMergeDataSourceApi(
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
directURL: (...args: unknown[]) =>
callForDefaultDataSource({
Expand All @@ -211,6 +238,7 @@ function createMergeDataSourceApi(
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
},
},
Expand All @@ -229,6 +257,7 @@ function createMergeDataSourceApi(
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
getImageIdsForDisplaySet: (...args: unknown[]) =>
callByRetrieveAETitle({
Expand All @@ -250,6 +279,7 @@ function createMergeDataSourceApi(
args,
extensionManager,
dataSourceNames,
defaultDataSourceName,
}),
};

Expand Down
2 changes: 2 additions & 0 deletions extensions/default/src/MergeDataSource/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export type CallForAllDataSourcesAsyncOptions = {
args: unknown[];
dataSourceNames: string[];
extensionManager: ExtensionManager;
defaultDataSourceName: string;
};

export type CallForAllDataSourcesOptions = {
path: string;
args: unknown[];
dataSourceNames: string[];
extensionManager: ExtensionManager;
defaultDataSourceName: string;
};

export type CallForDefaultDataSourceOptions = {
Expand Down

0 comments on commit 5bba98e

Please sign in to comment.