Skip to content

Commit e37b300

Browse files
committed
Updating aggregate pipeline to perform optimally with indexes. Using same pattern for both shared and patient resources.
1 parent b2a9a2f commit e37b300

2 files changed

Lines changed: 21 additions & 63 deletions

File tree

Java/measureeval/src/main/java/com/lantanagroup/link/measureeval/repositories/AbstractResourceRepository.java

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -80,75 +80,34 @@ public List<? extends AbstractResourceEntity> findAll(String facilityId, List<Pa
8080
return mongoOperations.find(query, entityType);
8181
}
8282

83-
public List<SharedResource> findSharedResources(String facilityId, List<PatientReportingEvaluationStatus.Resource> resources) {
84-
List<SharedResource> sharedResources = new ArrayList<>();
85-
if (resources == null || resources.isEmpty()) {
86-
return sharedResources;
87-
}
88-
89-
// Batch the searches so that it doesn't exceed mongo's query limitations
90-
List<PatientReportingEvaluationStatus.Resource> remainingResources = new ArrayList<>(resources);
91-
while (!remainingResources.isEmpty()) {
92-
int characterCount = 0;
93-
List<Criteria> batchCriteria = new ArrayList<>();
94-
95-
for (int i = remainingResources.size() - 1; i >= 0; i--) {
96-
PatientReportingEvaluationStatus.Resource resource = remainingResources.get(i);
97-
int resourceCharLength = resource.getResourceType().toString().length() + resource.getResourceId().length();
98-
99-
if (characterCount + resourceCharLength >= 10000) {
100-
break;
101-
}
102-
103-
characterCount += resourceCharLength;
104-
batchCriteria.add(Criteria.where("facilityId").is(facilityId)
105-
.and("resourceType").is(resource.getResourceType())
106-
.and("resourceId").is(resource.getResourceId()));
107-
remainingResources.remove(i);
108-
}
109-
110-
if (!batchCriteria.isEmpty()) {
111-
Criteria combinedCriteria = new Criteria().orOperator(batchCriteria.toArray(new Criteria[0]));
112-
Query query = new Query(combinedCriteria);
113-
sharedResources.addAll(mongoOperations.find(query, SharedResource.class));
114-
}
115-
}
116-
117-
return sharedResources;
118-
}
119-
120-
public List<PatientResource> findPatientResources(String facilityId, String patientId, List<String> reportIds) {
83+
public List<PatientResource> findResources(boolean isShared, String facilityId, String correlationId) {
12184
List<AggregationOperation> pipeline = new ArrayList<>();
12285

12386
pipeline.add(Aggregation.match(Criteria.where("facilityId").is(facilityId)
124-
.and("patientId").is(patientId)
125-
.and("reports.reportTrackingId").in(reportIds)));
87+
.and("correlationId").is(correlationId)));
12688
pipeline.add(Aggregation.unwind("resources"));
127-
pipeline.add(Aggregation.match(Criteria.where("resources.normalizationStatus").is("NORMALIZED")));
89+
pipeline.add(Aggregation.replaceRoot("resources"));
90+
pipeline.add(Aggregation.match(Criteria.where("isPatientResource").is(!isShared)
91+
.and("normalizationStatus").is("NORMALIZED")));
12892

12993
Document lookupStage = new Document("$lookup",
130-
new Document("from", "patientResource")
131-
.append("let", new Document("localResourceId", "$resources.resourceId")
132-
.append("localResourceType", "$resources.resourceType"))
133-
.append("pipeline", List.of(
134-
new Document("$match",
135-
new Document("$expr",
136-
new Document("$and", List.of(
137-
new Document("$eq", List.of("$resourceId", "$$localResourceId")),
138-
new Document("$eq", List.of("$resourceType", "$$localResourceType")),
139-
new Document("$eq", List.of("$facilityId", facilityId))
140-
))
141-
)
142-
)
143-
))
144-
.append("as", "matchedResources")
145-
);
94+
new Document("from", isShared ? "sharedResource" : "patientResource")
95+
.append("localField", "resourceId")
96+
.append("foreignField", "resourceId")
97+
.append("as", "patientResources"));
14698

14799
pipeline.add(new CustomAggregationOperation(lookupStage));
100+
pipeline.add(Aggregation.unwind("patientResources"));
101+
102+
Document matchExpr = new Document("$match",
103+
new Document("$expr",
104+
new Document("$and", List.of(
105+
new Document("$eq", List.of("$patientResources.facilityId", facilityId)),
106+
new Document("$eq", List.of("$patientResources.resourceType", "$resourceType"))
107+
))));
108+
pipeline.add(new CustomAggregationOperation(matchExpr));
148109

149-
pipeline.add(Aggregation.unwind("matchedResources"));
150-
pipeline.add(Aggregation.match(Criteria.where("matchedResources.facilityId").is(facilityId)));
151-
pipeline.add(Aggregation.replaceRoot("matchedResources"));
110+
pipeline.add(Aggregation.replaceRoot("patientResources"));
152111

153112
Aggregation aggregation = Aggregation.newAggregation(pipeline);
154113
return mongoOperations.aggregate(aggregation, "patientReportingEvaluationStatus", PatientResource.class).getMappedResults();

Java/measureeval/src/main/java/com/lantanagroup/link/measureeval/services/PatientStatusBundler.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ private List<AbstractResourceEntity> retrieveResources (PatientReportingEvaluati
4747

4848
logger.debug("Collecting patient resources for patient {} and {} shared resources from the database", patientStatus.getPatientId(), sharedResourcesRefs.size());
4949

50-
List<String> reportIds = patientStatus.getReports().stream().map(PatientReportingEvaluationStatus.Report::getReportTrackingId).toList();
51-
var patientResources = resourceRepository.findPatientResources(patientStatus.getFacilityId(), patientStatus.getPatientId(), reportIds);
52-
var sharedResources = resourceRepository.findSharedResources(patientStatus.getFacilityId(), sharedResourcesRefs);
50+
var patientResources = resourceRepository.findResources(false, patientStatus.getFacilityId(), patientStatus.getCorrelationId());
51+
var sharedResources = resourceRepository.findResources(true, patientStatus.getFacilityId(), patientStatus.getCorrelationId());
5352

5453
logger.debug("Retrieved {} patient resources and {} shared resources from the database", patientResources.size(), sharedResources.size());
5554

0 commit comments

Comments
 (0)