From 0401bab851f4d316ed3858100feccb16c54fe631 Mon Sep 17 00:00:00 2001 From: Joy A Date: Mon, 2 Sep 2024 11:09:07 +0530 Subject: [PATCH] avniproject/avni-webapp#1300 | /web/subjectAssignment/search performance improvement - removed join to title_lineage_locations_view --- .../dao/search/SubjectAssignmentSearchQueryBuilder.java | 3 +-- .../avni/server/service/UserSubjectAssignmentService.java | 6 +++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/avni-server-api/src/main/java/org/avni/server/dao/search/SubjectAssignmentSearchQueryBuilder.java b/avni-server-api/src/main/java/org/avni/server/dao/search/SubjectAssignmentSearchQueryBuilder.java index 4564ef2c2..d392c3c62 100644 --- a/avni-server-api/src/main/java/org/avni/server/dao/search/SubjectAssignmentSearchQueryBuilder.java +++ b/avni-server-api/src/main/java/org/avni/server/dao/search/SubjectAssignmentSearchQueryBuilder.java @@ -15,12 +15,11 @@ public SqlQuery build() { String SUBJECT_ASSIGNMENT_SEARCH_BASE_QUERY = "select i.id as \"id\",\n" + " cast(concat_ws(' ', i.first_name, i.middle_name, i.last_name) as text) as \"fullName\",\n" + " i.uuid as \"uuid\",\n" + - " cast(tllv.title_lineage as text) as \"addressLevel\",\n" + + " i.address_id as \"addressId\",\n" + " string_agg(distinct p.name || ':' || p.colour, ', ') as \"programs\",\n" + " string_agg(distinct assigned_to.name || ':' || g.name, ', ') as \"assignedTo\"\n" + " $CUSTOM_FIELDS\n" + "from individual i\n" + - " left outer join title_lineage_locations_view tllv on i.address_id = tllv.lowestpoint_id\n" + " left outer join subject_type st on i.subject_type_id = st.id and st.is_voided is false\n" + " left outer join program_enrolment penr on i.id = penr.individual_id and penr.is_voided is false\n" + " left outer join program p on p.id = penr.program_id\n" + diff --git a/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java b/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java index 319831c06..c1f9dc180 100644 --- a/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java +++ b/avni-server-api/src/main/java/org/avni/server/service/UserSubjectAssignmentService.java @@ -106,7 +106,10 @@ public LinkedHashMap searchSubjects(SubjectSearchRequest subject List> searchResults = subjectSearchRepository.search(subjectSearchRequest, new SubjectAssignmentSearchQueryBuilder()); List subjectIds = searchResults.stream().map(s -> Long.parseLong(s.get("id").toString())).collect(Collectors.toList()); List userSubjectAssignmentBySubjectIds = userSubjectAssignmentRepository.findUserSubjectAssignmentBySubject_IdIn(subjectIds); - + List addressIds = searchResults.stream() + .map(searchResult -> ((BigInteger) searchResult.get("addressId")).longValue()) + .collect(Collectors.toList()); + Map titleLineages = addressLevelService.getTitleLineages(addressIds); Map> groupedSubjects = userSubjectAssignmentBySubjectIds.stream() .filter(usa -> !usa.isVoided()) .collect(groupingBy(UserSubjectAssignment::getSubjectIdAsString, TreeMap::new, @@ -120,6 +123,7 @@ public LinkedHashMap searchSubjects(SubjectSearchRequest subject .orElseGet(Stream::empty) .map(uw -> pf.createProjection(UserWebProjection.class, uw)).collect(Collectors.toList()); searchResult.put("assignedUsers", userWebProjections); + searchResult.put("addressLevel", titleLineages.get(((BigInteger) searchResult.get("addressId")).longValue())); } BigInteger totalCount = subjectSearchRepository.getTotalCount(subjectSearchRequest, new SubjectAssignmentSearchQueryBuilder());