Skip to content

Commit

Permalink
⏫ 2265 Update clinical data sort (#796)
Browse files Browse the repository at this point in the history
* Test Logging + Rough Draft PaginationUpdate

* Update Swagger

* First Working Updated Total

* Working All Docs Solution

* Clean Up Total Samples

* Clean up total samples 2

* Solution using Monggose Paginate + Manual Paginate

* Improved Comment

* Minor Comment Formatting

* First Sort Logic Update

* Draft Sort Update

* Sort by Completion Percentage

* Updated Sorting Logic from UI

* Update let to const
  • Loading branch information
demariadaniel authored Aug 10, 2022
1 parent 867fa58 commit 413d154
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
10 changes: 9 additions & 1 deletion src/clinical/donor-repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export const donorDao: DonorRepository = {
query: ClinicalQuery,
): Promise<DeepReadonly<{ donors: Donor[]; totalDonors: number }>> {
const {
sort,
sort: querySort,
page: queryPage,
pageSize,
entityTypes,
Expand All @@ -191,8 +191,16 @@ export const donorDao: DonorRepository = {
completionState,
} = query;

const sortQuery = querySort.includes('-') ? { [querySort.slice(1)]: -1 } : { [querySort]: 1 };
const sort = {
'schemaMetadata.isValid': 1,
'completionStats.coreCompletionPercentage': 1,
...sortQuery,
};

const projection = [
'-_id',
'schemaMetadata',
...DONOR_ENTITY_CORE_FIELDS,
...entityTypes,
...getRequiredDonorFieldsForEntityTypes(entityTypes),
Expand Down
50 changes: 43 additions & 7 deletions src/clinical/service-worker-thread/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,48 @@ const isEntityInQuery = (entityName: ClinicalEntitySchemaNames, entityTypes: str
queryEntityNames.includes(aliasEntityNames[entityName]) &&
entityTypes.includes(aliasEntityNames[entityName]);

const sortDocs = (sort: string) => (currentRecord: ClinicalInfo, nextRecord: ClinicalInfo) => {
const isDescending = sort.split('-')[1] !== undefined;
const key = !isDescending ? sort.split('-')[0] : sort.split('-')[1];
const first = currentRecord[key] !== undefined ? (currentRecord[key] as number) : -1;
const next = nextRecord[key] !== undefined ? (nextRecord[key] as number) : -1;
const order = first === next ? 0 : first > next && isDescending ? -1 : 1;
const sortDocs = (sort: string, entityName: string, completionStats: CompletionRecord[]) => (
currentRecord: ClinicalInfo,
nextRecord: ClinicalInfo,
) => {
const isDefaultSort = entityName === ClinicalEntitySchemaNames.DONOR && sort.includes('donorId');
const currentDonor = currentRecord['donor_id'];
const nextDonor = nextRecord['donor_id'];

// -1 Current lower index than Next, +1 Current higher index than Next
let order = 0;
const isDescending = sort.includes('-') ? -1 : 1;

if (isDefaultSort) {
// Sort Clinically Incomplete donors to top (sorted by donorId at DB level)
const completionA =
completionStats.find(record => record.donorId && record.donorId === currentDonor)
?.coreCompletionPercentage || 0;

const completionB =
completionStats.find(record => record.donorId && record.donorId === nextDonor)
?.coreCompletionPercentage || 0;

const completionSort = completionA === completionB ? 0 : completionA > completionB ? -1 : 1;
order = completionSort;
} else {
// Sort by Selected Column
const key = isDescending === -1 ? sort.split('-')[1] : sort;
const first: any = currentRecord[key];
const next: any = nextRecord[key];
const firstExists = !(first === undefined || first === null);
const nextExists = !(next === undefined || next === null);
const valueSort =
(!firstExists && !nextExists) || first === next
? 0
: first > next || (firstExists && !nextExists)
? 1
: -1;

order = valueSort;
}

order = isDescending * order;

return order;
};
Expand All @@ -124,7 +160,7 @@ const mapEntityDocuments = (
}

const totalDocs = entityName === ClinicalEntitySchemaNames.DONOR ? totalDonors : results.length;
let records = results.sort(sortDocs(sort));
let records = results.sort(sortDocs(sort, entityName, completionStats));

if (records.length > pageSize) {
// Manual Pagination
Expand Down

0 comments on commit 413d154

Please sign in to comment.