fix: 🐛 Sort merge results based on default data source (input) (#3903)
This commit is contained in:
parent
16785936d9
commit
5bba98ed84
@ -33,6 +33,7 @@ export const mergeMap: MergeMap = {
|
|||||||
* @param {unknown[]} options.args - The arguments to be passed to the function.
|
* @param {unknown[]} options.args - The arguments to be passed to the function.
|
||||||
* @param {ExtensionManager} options.extensionManager - The extension manager.
|
* @param {ExtensionManager} options.extensionManager - The extension manager.
|
||||||
* @param {string[]} options.dataSourceNames - The names of the data sources to be called.
|
* @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.
|
* @returns {Promise<unknown[]>} - A promise that resolves to the merged data from all data sources.
|
||||||
*/
|
*/
|
||||||
export const callForAllDataSourcesAsync = async ({
|
export const callForAllDataSourcesAsync = async ({
|
||||||
@ -41,12 +42,18 @@ export const callForAllDataSourcesAsync = async ({
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}: CallForAllDataSourcesAsyncOptions) => {
|
}: CallForAllDataSourcesAsyncOptions) => {
|
||||||
const { mergeKey, tagFunc } = mergeMap[path] || { tagFunc: x => x };
|
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 promises = [];
|
||||||
const mergedData = [];
|
const sourceNames = [];
|
||||||
|
|
||||||
for (const dataSourceDef of dataSourceDefs) {
|
for (const dataSourceDef of dataSourceDefs) {
|
||||||
const { configuration, sourceName } = dataSourceDef;
|
const { configuration, sourceName } = dataSourceDef;
|
||||||
@ -54,11 +61,13 @@ export const callForAllDataSourcesAsync = async ({
|
|||||||
const [dataSource] = extensionManager.getDataSources(sourceName);
|
const [dataSource] = extensionManager.getDataSources(sourceName);
|
||||||
const func = get(dataSource, path);
|
const func = get(dataSource, path);
|
||||||
const promise = func.apply(dataSource, args);
|
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 = [];
|
let results = [];
|
||||||
if (mergeKey) {
|
if (mergeKey) {
|
||||||
@ -77,6 +86,7 @@ export const callForAllDataSourcesAsync = async ({
|
|||||||
* @param options.args - The arguments to be passed to the function.
|
* @param options.args - The arguments to be passed to the function.
|
||||||
* @param options.extensionManager - The extension manager instance.
|
* @param options.extensionManager - The extension manager instance.
|
||||||
* @param options.dataSourceNames - The names of the data sources to be called.
|
* @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.
|
* @returns The merged data from all the matching data sources.
|
||||||
*/
|
*/
|
||||||
export const callForAllDataSources = ({
|
export const callForAllDataSources = ({
|
||||||
@ -84,8 +94,14 @@ export const callForAllDataSources = ({
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}: CallForAllDataSourcesOptions) => {
|
}: 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 = [];
|
const mergedData = [];
|
||||||
for (const dataSourceDef of dataSourceDefs) {
|
for (const dataSourceDef of dataSourceDefs) {
|
||||||
const { configuration, sourceName } = dataSourceDef;
|
const { configuration, sourceName } = dataSourceDef;
|
||||||
@ -96,6 +112,7 @@ export const callForAllDataSources = ({
|
|||||||
mergedData.push(data);
|
mergedData.push(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mergedData.flat();
|
return mergedData.flat();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -154,7 +171,13 @@ function createMergeDataSourceApi(
|
|||||||
|
|
||||||
const implementation = {
|
const implementation = {
|
||||||
initialize: (...args: unknown[]) =>
|
initialize: (...args: unknown[]) =>
|
||||||
callForAllDataSources({ path: 'initialize', args, extensionManager, dataSourceNames }),
|
callForAllDataSources({
|
||||||
|
path: 'initialize',
|
||||||
|
args,
|
||||||
|
extensionManager,
|
||||||
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
|
}),
|
||||||
query: {
|
query: {
|
||||||
studies: {
|
studies: {
|
||||||
search: (...args: unknown[]) =>
|
search: (...args: unknown[]) =>
|
||||||
@ -164,6 +187,7 @@ function createMergeDataSourceApi(
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
series: {
|
series: {
|
||||||
@ -174,6 +198,7 @@ function createMergeDataSourceApi(
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
instances: {
|
instances: {
|
||||||
@ -184,6 +209,7 @@ function createMergeDataSourceApi(
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -195,6 +221,7 @@ function createMergeDataSourceApi(
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}),
|
}),
|
||||||
directURL: (...args: unknown[]) =>
|
directURL: (...args: unknown[]) =>
|
||||||
callForDefaultDataSource({
|
callForDefaultDataSource({
|
||||||
@ -211,6 +238,7 @@ function createMergeDataSourceApi(
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -229,6 +257,7 @@ function createMergeDataSourceApi(
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}),
|
}),
|
||||||
getImageIdsForDisplaySet: (...args: unknown[]) =>
|
getImageIdsForDisplaySet: (...args: unknown[]) =>
|
||||||
callByRetrieveAETitle({
|
callByRetrieveAETitle({
|
||||||
@ -250,6 +279,7 @@ function createMergeDataSourceApi(
|
|||||||
args,
|
args,
|
||||||
extensionManager,
|
extensionManager,
|
||||||
dataSourceNames,
|
dataSourceNames,
|
||||||
|
defaultDataSourceName,
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -13,6 +13,7 @@ export type CallForAllDataSourcesAsyncOptions = {
|
|||||||
args: unknown[];
|
args: unknown[];
|
||||||
dataSourceNames: string[];
|
dataSourceNames: string[];
|
||||||
extensionManager: ExtensionManager;
|
extensionManager: ExtensionManager;
|
||||||
|
defaultDataSourceName: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CallForAllDataSourcesOptions = {
|
export type CallForAllDataSourcesOptions = {
|
||||||
@ -20,6 +21,7 @@ export type CallForAllDataSourcesOptions = {
|
|||||||
args: unknown[];
|
args: unknown[];
|
||||||
dataSourceNames: string[];
|
dataSourceNames: string[];
|
||||||
extensionManager: ExtensionManager;
|
extensionManager: ExtensionManager;
|
||||||
|
defaultDataSourceName: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CallForDefaultDataSourceOptions = {
|
export type CallForDefaultDataSourceOptions = {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user