Skip to content

Commit

Permalink
Reduce api calls for generic assay meta in study view page
Browse files Browse the repository at this point in the history
  • Loading branch information
dippindots committed Oct 15, 2024
1 parent f3125f8 commit b7d4fa5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 53 deletions.
60 changes: 27 additions & 33 deletions src/pages/studyView/StudyViewPageStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ import {
} from 'pages/resultsView/enrichments/EnrichmentsUtil';
import {
fetchGenericAssayMetaByMolecularProfileIdsGroupByMolecularProfileId,
fetchGenericAssayMetaByMolecularProfileIdsGroupedByGenericAssayType,
fetchGenericAssayMetaGroupedByMolecularProfileIdSuffix,
} from 'shared/lib/GenericAssayUtils/GenericAssayCommonUtils';
import {
buildDriverAnnotationSettings,
Expand Down Expand Up @@ -6013,17 +6013,6 @@ export class StudyViewPageStore
default: [],
});

readonly genericAssayEntitiesGroupedByGenericAssayType = remoteData<{
[genericAssayType: string]: GenericAssayMeta[];
}>({
await: () => [this.genericAssayProfiles],
invoke: async () => {
return await fetchGenericAssayMetaByMolecularProfileIdsGroupedByGenericAssayType(
this.genericAssayProfiles.result
);
},
});

readonly genericAssayEntitiesGroupedByProfileId = remoteData<{
[profileId: string]: GenericAssayMeta[];
}>({
Expand All @@ -6035,20 +6024,13 @@ export class StudyViewPageStore
},
});

readonly genericAssayStableIdToMeta = remoteData<{
[genericAssayStableId: string]: GenericAssayMeta;
readonly genericAssayEntitiesGroupedByProfileIdSuffix = remoteData<{
[profileIdSuffix: string]: GenericAssayMeta[];
}>({
await: () => [this.genericAssayEntitiesGroupedByGenericAssayType],
invoke: () => {
return Promise.resolve(
_.chain(
this.genericAssayEntitiesGroupedByGenericAssayType.result
)
.values()
.flatten()
.uniqBy(meta => meta.stableId)
.keyBy(meta => meta.stableId)
.value()
await: () => [this.genericAssayProfiles],
invoke: async () => {
return await fetchGenericAssayMetaGroupedByMolecularProfileIdSuffix(
this.genericAssayProfiles.result
);
},
});
Expand All @@ -6067,20 +6049,32 @@ export class StudyViewPageStore
default: [],
});

readonly genericAssayProfilesGroupedByGenericAssayType = remoteData({
await: () => [this.genericAssayProfiles],
invoke: () => {
return Promise.resolve(
_.groupBy(
this.genericAssayProfiles.result,
profile => profile.genericAssayType
)
);
},
default: {},
});

readonly genericAssayProfileOptionsByType = remoteData({
await: () => [
this.genericAssayProfiles,
this.genericAssayProfilesGroupedByGenericAssayType,
this.molecularProfileSampleCountSet,
],
invoke: () => {
return Promise.resolve(
_.chain(this.genericAssayProfiles.result)
.filter(
profile =>
profile.molecularAlterationType ===
AlterationTypeConstants.GENERIC_ASSAY
)
.groupBy(profile => profile.genericAssayType)
// Each Generic Assay Profile has a type "profile.genericAssayType"
// But one Generic Assay Type can then have different suffix, meaning they are the same Generic Assay Type but different kind of data
// Then we need to distinguish them using suffix of the profile id
_.chain(
this.genericAssayProfilesGroupedByGenericAssayType.result
)
.mapValues(profiles => {
return _.chain(profiles)
.groupBy(molecularProfile =>
Expand Down
33 changes: 13 additions & 20 deletions src/pages/studyView/addChartButton/AddChartButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class AddChartTabs extends React.Component<IAddChartTabsProps, {}> {
return (
this.props.disableGenericAssayTabs ||
!this.props.store.genericAssayProfiles.isComplete ||
!this.props.store.genericAssayEntitiesGroupedByProfileId
!this.props.store.genericAssayEntitiesGroupedByProfileIdSuffix
.isComplete ||
(this.props.store.genericAssayProfiles.isComplete &&
_.isEmpty(this.props.store.genericAssayProfiles.result))
Expand Down Expand Up @@ -536,25 +536,17 @@ class AddChartTabs extends React.Component<IAddChartTabsProps, {}> {
// And one tab can only has one selected profile at a time
// selectedGenericAssayProfileIdByType been initialzed at the begining
// so we know we can always find a selected profile for each Generic Assay type
const molecularProfileIdsInType =
options.find(
option =>
option.value ===
this.selectedGenericAssayProfileIdByType.get(type)
)?.profileIds || [];

const entityMap = molecularProfileIdsInType.reduce(
(acc, profileId) => {
this.props.store.genericAssayEntitiesGroupedByProfileId.result![
profileId
].forEach(meta => {
acc[meta.stableId] = meta;
});
return acc;
},
{} as { [stableId: string]: GenericAssayMeta }
const molecularProfileIdSuffix = this.selectedGenericAssayProfileIdByType.get(
type
)!;

const entityMap = _.keyBy(
this.props.store
.genericAssayEntitiesGroupedByProfileIdSuffix.result![
molecularProfileIdSuffix
],
meta => meta.stableId
);

const genericAssayEntityOptions = _.map(
entityMap,
makeGenericAssayOption
Expand Down Expand Up @@ -1158,7 +1150,8 @@ export default class AddChartButton extends React.Component<
return (
this.props.store.genericAssayProfileOptionsByType.isPending ||
this.props.store.molecularProfileOptions.isPending ||
this.props.store.genericAssayEntitiesGroupedByProfileId.isPending
this.props.store.genericAssayEntitiesGroupedByProfileIdSuffix
.isPending
);
}

Expand Down
33 changes: 33 additions & 0 deletions src/shared/lib/GenericAssayUtils/GenericAssayCommonUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
GenericAssayTypeConstants,
} from 'shared/lib/GenericAssayUtils/GenericAssayConfig';
import * as Pluralize from 'pluralize';
import { getSuffixOfMolecularProfile } from 'shared/lib/molecularProfileUtils';

export const NOT_APPLICABLE_VALUE = 'NA';
export const COMMON_GENERIC_ASSAY_PROPERTY = {
Expand Down Expand Up @@ -109,6 +110,38 @@ export async function fetchGenericAssayMetaByMolecularProfileIdsGroupByMolecular
return genericAssayMetaGroupByMolecularProfileId;
}

export async function fetchGenericAssayMetaGroupedByMolecularProfileIdSuffix(
genericAssayProfiles: MolecularProfile[]
) {
const genericAssayProfilesGroupedByProfileIdSuffix = _.groupBy(
genericAssayProfiles,
getSuffixOfMolecularProfile
);
const profileSuffixes = _.keys(
genericAssayProfilesGroupedByProfileIdSuffix
);

const genericAssayMetaGroupedByProfileIdSuffix: {
[profileIdSuffix: string]: GenericAssayMeta[];
} = {};

await Promise.all(
profileSuffixes.map(profileSuffix =>
fetchGenericAssayMetaByProfileIds(
_.map(
genericAssayProfilesGroupedByProfileIdSuffix[profileSuffix],
profile => profile.molecularProfileId
)
).then(genericAssayMeta => {
genericAssayMetaGroupedByProfileIdSuffix[
profileSuffix
] = genericAssayMeta;
})
)
);
return genericAssayMetaGroupedByProfileIdSuffix;
}

export function fetchGenericAssayMetaByProfileIds(
genericAssayProfileIds: string[]
) {
Expand Down

0 comments on commit b7d4fa5

Please sign in to comment.