Skip to content

Commit

Permalink
#898 | getUnderlyingRealmCollection directly instead of asArray since…
Browse files Browse the repository at this point in the history
… it slows down

sort by realm works differently from lodash. Lodash case-insensitive sorting done when custom filters need to be applied. Else the previous realm sorting behaviour is retained.
  • Loading branch information
mahalakshme committed Mar 28, 2023
1 parent 51605f5 commit 8a39a26
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/openchs-android/src/service/IndividualService.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import PrivilegeService from "./PrivilegeService";
import EntityApprovalStatusService from "./EntityApprovalStatusService";
import GroupSubjectService from "./GroupSubjectService";
import OrganisationConfigService from './OrganisationConfigService';
import {getUnderlyingRealmCollection} from "openchs-models";

@Service("individualService")
class IndividualService extends BaseService {
Expand Down Expand Up @@ -52,10 +53,11 @@ class IndividualService extends BaseService {
let searchResults, finalSearchResults = [];

if (_.isEmpty(filterCriteria)) {
searchResults = this.db.objects(Individual.schema.name);
finalSearchResults = _.concat(finalSearchResults, searchResults.asArray());
searchResults = this.db.objects(Individual.schema.name).sorted("name");
finalSearchResults = getUnderlyingRealmCollection(searchResults);
} else {
function filterIndividualsByChunks(baseResult) {

This comment has been minimized.

Copy link
@himeshr

himeshr Mar 28, 2023

Contributor

Move this method out, to enable its reuse in future

// if chunkSize is less/more than 500, processing is slower
const chunkSize = 500, noOfChunks = individualUUIDs.length / chunkSize;
for (let chunk = 0; chunk < noOfChunks; chunk++) {
let individualUuidsChunk = _.slice(individualUUIDs, chunk * chunkSize, ((chunk + 1) * chunkSize) - 1);
Expand All @@ -71,13 +73,17 @@ class IndividualService extends BaseService {
filterCriteria,
criteria.getMinDateOfBirth(),
criteria.getMaxDateOfBirth()
);
).sorted("name");

_.isEmpty(individualUUIDs) ? finalSearchResults = _.concat(finalSearchResults, baseResult.asArray()) :
if(_.isEmpty(individualUUIDs))
finalSearchResults = getUnderlyingRealmCollection(baseResult);
else {
filterIndividualsByChunks(baseResult);

This comment has been minimized.

Copy link
@himeshr

himeshr Mar 28, 2023

Contributor

Make filterIndividualsByChunks return modified finalSearchResults

finalSearchResults = _.orderBy(finalSearchResults, [individual => individual.name.toLowerCase()]);

This comment has been minimized.

Copy link
@himeshr

himeshr Mar 28, 2023

Contributor

Use filterIndividualsByChunks here as it returns latest finalSearchResults

}
}

return _.sortBy(finalSearchResults, ['name']);
return finalSearchResults;
}

register(individual, nextScheduledVisits, skipCreatingPendingStatus, groupSubjectObservations) {
Expand Down

0 comments on commit 8a39a26

Please sign in to comment.