Skip to content

Commit

Permalink
avniproject/avni-webapp#1300 | /web/subjectAssignment/search performa…
Browse files Browse the repository at this point in the history
…nce improvement

- removed join to title_lineage_locations_view
  • Loading branch information
1t5j0y committed Sep 2, 2024
1 parent 5309ddc commit 0401bab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ public LinkedHashMap<String, Object> searchSubjects(SubjectSearchRequest subject
List<Map<String, Object>> searchResults = subjectSearchRepository.search(subjectSearchRequest, new SubjectAssignmentSearchQueryBuilder());
List<Long> subjectIds = searchResults.stream().map(s -> Long.parseLong(s.get("id").toString())).collect(Collectors.toList());
List<UserSubjectAssignment> userSubjectAssignmentBySubjectIds = userSubjectAssignmentRepository.findUserSubjectAssignmentBySubject_IdIn(subjectIds);

List<Long> addressIds = searchResults.stream()
.map(searchResult -> ((BigInteger) searchResult.get("addressId")).longValue())
.collect(Collectors.toList());
Map<Long, String> titleLineages = addressLevelService.getTitleLineages(addressIds);
Map<String, List<User>> groupedSubjects = userSubjectAssignmentBySubjectIds.stream()
.filter(usa -> !usa.isVoided())
.collect(groupingBy(UserSubjectAssignment::getSubjectIdAsString, TreeMap::new,
Expand All @@ -120,6 +123,7 @@ public LinkedHashMap<String, Object> 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());
Expand Down

0 comments on commit 0401bab

Please sign in to comment.