Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use alternative isDescendant check where possible #15715

Merged
merged 16 commits into from
Nov 21, 2023

Conversation

david-mackessy
Copy link
Contributor

@david-mackessy david-mackessy commented Nov 16, 2023

Summary

While working on #15639 it was discovered that a lot of other areas of the code were also making unnecessary DB calls (get OrgUnit) when checking if an OrgUnit was a descendant of another OrgUnit.
This PR removes the OrganisationUnitService methods (that contain DB calls):

  • boolean isDescendant(OrganisationUnit organisationUnit, Set<OrganisationUnit> ancestors)
  • boolean isDescendant(OrganisationUnit organisationUnit, OrganisationUnit ancestor)

and replaces them with the OrganisationUnit methods directly (that perform checks on in-memory Objects):

  • boolean isDescendant(Collection<OrganisationUnit> ancestors)
  • boolean isDescendant(OrganisationUnit ancestor)

Testing

  • These changes are covered by a multitude of existing tests (both directly & indirectly)
  • Also added a few more assertions in existing test

Notes

Added a parent mapping in the TrackedEntityMapper class. This helped resolve a Tracker test with some setup issues. Tagged a Tracker Dev in the PR to check if it looks ok.

Copy link

codecov bot commented Nov 16, 2023

Codecov Report

Merging #15715 (c68e604) into master (719ce54) will decrease coverage by 0.01%.
The diff coverage is 64.70%.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #15715      +/-   ##
============================================
- Coverage     66.30%   66.29%   -0.01%     
+ Complexity    31392    31385       -7     
============================================
  Files          3486     3486              
  Lines        130010   129991      -19     
  Branches      15207    15201       -6     
============================================
- Hits          86201    86184      -17     
+ Misses        36718    36715       -3     
- Partials       7091     7092       +1     
Flag Coverage Δ
integration 50.07% <58.82%> (-0.01%) ⬇️
integration-h2 32.33% <41.17%> (-0.01%) ⬇️
unit 30.27% <11.76%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
...dhis/organisationunit/OrganisationUnitService.java 100.00% <ø> (ø)
...hisp/dhis/merge/orgunit/OrgUnitMergeValidator.java 85.71% <100.00%> (ø)
...hisp/dhis/split/orgunit/OrgUnitSplitValidator.java 90.00% <100.00%> (ø)
...approval/hibernate/HibernateDataApprovalStore.java 96.05% <100.00%> (-0.03%) ⬇️
...ort/trackedentity/DefaultTrackedEntityService.java 65.08% <100.00%> (ø)
...r/imports/preheat/mappers/TrackedEntityMapper.java 100.00% <ø> (ø)
.../dataapproval/DefaultDataApprovalLevelService.java 69.43% <0.00%> (ø)
...ganisationunit/DefaultOrganisationUnitService.java 48.16% <80.00%> (-2.36%) ⬇️
...his/trackedentity/DefaultTrackedEntityService.java 47.10% <0.00%> (ø)
...ultCompleteDataSetRegistrationExchangeService.java 44.08% <50.00%> (+0.09%) ⬆️
... and 1 more

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 719ce54...c68e604. Read the comment docs.

@david-mackessy david-mackessy marked this pull request as ready for review November 20, 2023 16:09
@@ -63,5 +63,6 @@ public interface TrackedEntityMapper extends PreheatMapper<TrackedEntity> {
@Mapping(target = "name")
@Mapping(target = "attributeValues")
@Mapping(target = "user")
@Mapping(target = "parent")
Copy link
Contributor Author

@david-mackessy david-mackessy Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enricocolasante does this update look ok to you? It helped resolve what seemed to be a troublesome Tracker Test which I also updated below.
The TrackedEntity import wasn't getting its OrgUnit parent set.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is ok.
We always need to be careful around that because it generates a recursive call through the orgUnit hierarchy but the number of levels should be around 7, 10 maximum.
So we are not loading too many objects in memory.

Copy link
Contributor Author

@david-mackessy david-mackessy Nov 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, thanks Enrico. At least ye are aware of the change in case it does cause any issues. There were a couple of Tracker tests that were failing until I made this change (as part of this PR change).

User user = userService.getUser(USER_8);
user.setTeiSearchOrganisationUnits(new HashSet<>(user.getDataViewOrganisationUnits()));
userService.updateUser(user);
dbmsManager.clearSession();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@enricocolasante the change to the TrackedEntityMapper above seems to have resolved this setup issue. Does it look ok to you?

Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

Copy link
Contributor

@jbee jbee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@@ -63,5 +63,6 @@ public interface TrackedEntityMapper extends PreheatMapper<TrackedEntity> {
@Mapping(target = "name")
@Mapping(target = "attributeValues")
@Mapping(target = "user")
@Mapping(target = "parent")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it is ok.
We always need to be careful around that because it generates a recursive call through the orgUnit hierarchy but the number of levels should be around 7, 10 maximum.
So we are not loading too many objects in memory.

@david-mackessy david-mackessy merged commit a0262b5 into master Nov 21, 2023
18 checks passed
@david-mackessy david-mackessy deleted the migrate_is_descendant_check branch November 21, 2023 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants