Skip to content

Commit

Permalink
Add "Files & Links" tab (#10467) with suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelliney committed Aug 19, 2024
1 parent c9cedb7 commit 4353fdc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 69 deletions.
39 changes: 20 additions & 19 deletions src/pages/studyView/StudyViewPageStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5796,28 +5796,29 @@ export class StudyViewPageStore
return Promise.resolve({});
}

const samples = this.samples.result!;
const ret: { [sampleId: string]: ResourceData[] } = {};
const promises = [];
for (const sample of samples) {
for (const resource of sampleResourceDefinitions) {
promises.push(
internalClient
.getAllResourceDataOfSampleInStudyUsingGET({
const res = _(this.samples.result!)
.map(sample =>
sampleResourceDefinitions.map(resource =>
internalClient.getAllResourceDataOfSampleInStudyUsingGET(
{
sampleId: sample.sampleId,
studyId: sample.studyId, // TODO:
studyId: sample.studyId,
resourceId: resource.resourceId,
projection: 'DETAILED',
})
.then(data => {
ret[sample.sampleId] =
ret[sample.sampleId] || [];
ret[sample.sampleId].push(...data);
})
);
}
}
return Promise.all(promises).then(() => ret);
}
)
)
)
.flatten()
.value();

return Promise.all(res).then(resData =>
_(resData)
.flatMap()
.groupBy('sampleId')
.mapValues(data => data)
.value()
);
},
});

Expand Down
86 changes: 36 additions & 50 deletions src/pages/studyView/resources/FilesAndLinks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,6 @@ class FilesLinksTableComponent extends LazyMobXTable<{

const RECORD_LIMIT = 500;

async function fetchResourceDataOfPatient(patientIds: Map<string, string>) {
const ret: { [key: string]: ResourceData[] } = {};
const promises = [];
for (let [patientId, studyId] of patientIds) {
promises.push(
internalClient
.getAllResourceDataOfPatientInStudyUsingGET({
studyId: studyId,
patientId: patientId,
projection: 'DETAILED',
})
.then(data => {
if (patientId in ret) {
ret[patientId].push(...data);
} else {
ret[patientId] = data;
}
})
);
}

return Promise.all(promises).then(() => ret);
}

async function fetchFilesLinksData(
filters: StudyViewFilter,
sampleIdResourceData: { [sampleId: string]: ResourceData[] },
Expand All @@ -61,51 +37,62 @@ async function fetchFilesLinksData(
sortDirection: 'asc' | 'desc' | undefined,
recordLimit: number
) {
const sampleClinicalDataResponse = await getAllClinicalDataByStudyViewFilter(
const studyClinicalDataResponse = await getAllClinicalDataByStudyViewFilter(
filters,
searchTerm,
sortAttributeId,
sortDirection,
recordLimit,
0
);
const getResourceDataOfPatients = async () => {
const resourcesPerPatient = _(studyClinicalDataResponse.data)
.flatMap(clinicaldataItems => clinicaldataItems)
.uniqBy('patientId')
.map(resource =>
internalClient.getAllResourceDataOfPatientInStudyUsingGET({
studyId: resource.studyId,
patientId: resource.patientId,
projection: 'DETAILED',
})
)
.flatten()
.value();

// get unique patient Ids from clinical data to get their resources
// via fetchResourceDataOfPatient.
const patientIds = new Map<string, string>();
_.forEach(sampleClinicalDataResponse.data, data => {
_.forEach(data, item => {
patientIds.set(item.patientId, item.studyId);
});
});
return Promise.all(resourcesPerPatient).then(resourcesPerPatient =>
_(resourcesPerPatient)
.flatMap()
.groupBy('patientId')
.value()
);
};

// get all resources for patients.
// improvement: use one request for getting a page of patients
// with their samples and resources.
const resourcesForPatients = await fetchResourceDataOfPatient(patientIds);
const resourcesForPatients = await getResourceDataOfPatients();
const buildItemsAndResources = (resourceData: {
[key: string]: ResourceData[];
}) => {
const resourcesPerPatient: { [key: string]: number } = {};
const items: { [attributeId: string]: string | number }[] = [];
_.forEach(resourceData, (data: ResourceData[]) => {
_.forEach(data, (resource: ResourceData) => {
items.push({
const items: { [attributeId: string]: string | number }[] = _(
resourceData
)
.flatMap(data =>
data.map(resource => ({
studyId: resource.studyId,
patientId: resource.patientId,
sampleId: resource.sampleId,
resourcesPerPatient: 0,
typeOfResource: resource?.resourceDefinition?.displayName,
description: resource?.resourceDefinition?.description,
url: resource?.url,
} as { [attributeId: string]: string | number });
});

if (data && data.length > 0) {
if (!(data[0].patientId in resourcesPerPatient))
resourcesPerPatient[data[0].patientId] = 0;
}))
)
.value();

resourcesPerPatient[data[0].patientId] += data.length;
_(resourceData).forEach(data => {
const patientId = data[0]?.patientId;
if (patientId) {
resourcesPerPatient[patientId] =
(resourcesPerPatient[patientId] || 0) + data.length;
}
});

Expand Down Expand Up @@ -183,7 +170,6 @@ export class FilesAndLinks extends React.Component<IFilesLinksTable, {}> {
.includes(filterStringUpper);
}
}

return false;
},
};
Expand All @@ -202,6 +188,7 @@ export class FilesAndLinks extends React.Component<IFilesLinksTable, {}> {
if (this.props.store.selectedSamples.result.length === 0) {
return Promise.resolve({ totalItems: 0, data: [] });
}

const resources = await fetchFilesLinksData(
this.props.store.filters,
this.props.store.sampleResourceData.result!,
Expand All @@ -210,7 +197,6 @@ export class FilesAndLinks extends React.Component<IFilesLinksTable, {}> {
'asc',
RECORD_LIMIT
);

return Promise.resolve(resources);
},
});
Expand Down

0 comments on commit 4353fdc

Please sign in to comment.