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

fix(OrganisationUnitTree): deduplicate orgunit roots #1625

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

Birkbjo
Copy link
Member

@Birkbjo Birkbjo commented Oct 29, 2024

Implements LIBS-702


Description

In OrganisationUnitTree component, you can pass roots that are descendants of other roots, e.g. if you pass <OrganisationUnitTree roots=[OrgUnitA, OrgUnitB, OrgUnitC] /> where OrgUnitB is a descendant of OrgUnitA and where OrgUnitC is a descendant of OrgUnitA, then you presumably want to display only OrgUnitA as a root (e.g. the deduplicated root).

This PR implements a function findCommonOrgUnitRoots that will check the levels of the units, and only return the true "minimal roots" - and ignore rendering input IDs as roots, if another root contains it.

Know issues

  • This will still send a request for each input orgunit. But will filter the "nested" units before rendering, once we know the "level".
    • Sidenote: I really think we should update how the roots are fetched. We do not need a separate request for each unit - those can be grabbed by using an in-filter. It quite wasteful currently.
      • This would be simple in it self, but would probably require a lot of work to update tests - and some apps' test might break if we were to do that.

Checklist

  • API docs are generated
  • Tests were added
  • Storybook demos were added

All points above should be relevant for feature PRs. For bugfixes, some points might not be relevant. In that case, just check them anyway to signal the work is done.


@Birkbjo Birkbjo requested a review from a team as a code owner October 29, 2024 15:20
@Birkbjo Birkbjo changed the title Fix/libs 702/deduplicate roots fix(OrganisationUnitTree): deduplicate orgunit roots Oct 29, 2024
@dhis2-bot
Copy link
Contributor

dhis2-bot commented Oct 29, 2024

🚀 Deployed on https://pr-1625--dhis2-ui.netlify.app

@dhis2-bot dhis2-bot temporarily deployed to netlify October 29, 2024 15:27 Inactive
* @returns {Array} - A filtered list of the minimum root units
*/
export const deduplicateOrgUnitRoots = (unitsWithLevel) => {
const sorted = unitsWithLevel.sort((a, b) => a.level - b.level)
Copy link
Member Author

@Birkbjo Birkbjo Oct 29, 2024

Choose a reason for hiding this comment

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

Note that we could use the "path" instead of level here - because path consists of the ids, delimited by / for each level. So we could just count those (that's actually the implementation of the backend, see https://github.com/dhis2/dhis2-core/blob/master/dhis-2/dhis-api/src/main/java/org/hisp/dhis/organisationunit/OrganisationUnit.java#L610) .
But in my opinion it's more semantically correct to use the level, even if I had to add another field to the request.

@dhis2-bot dhis2-bot temporarily deployed to netlify October 29, 2024 15:48 Inactive
@dhis2-bot dhis2-bot temporarily deployed to netlify October 30, 2024 10:57 Inactive
const previousUnits = array.slice(0, index)
// if a previous unit has a path that is a prefix of the current path,
// then the current path is a child and should not be included
return !previousUnits.some((pu) => ou.path.startsWith(pu.path))
Copy link
Collaborator

Choose a reason for hiding this comment

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

this would be run on all orgUnits from all levels, right? it seems like an expensive operation having a nested loop if it's performed on hundreds or thousands of org units

Copy link
Member Author

@Birkbjo Birkbjo Nov 4, 2024

Choose a reason for hiding this comment

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

Not really, this is run on the rootUnits passed to the component. This is usually just a couple of units - eg. the assigned organisation units to an user from /me api. If you're passing thousands of units, something is not right with the configuration of the instance.

Also the optimization of just checking previousUnits would prevent it from being unnecessary expensive. The inner .some() would most likely return pretty early due to an identified root. However it's probably possible to optimize it further by imperatively adding identified roots to an array - and loop through those instead of previous units - even though practically it shouldn't matter that much - since it should find an "ancestor" within the first couple of units - practically it's bound by the number of units on the lowest level.

But again; if you passed thousands of units to the component, you would send a request for each unit - which would cause more trouble than this loop. Please see my sidenote in description about that.

Copy link
Collaborator

@kabaros kabaros left a comment

Choose a reason for hiding this comment

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

lgtm - but maybe someone (@HendrikThePendric or @Mohammer5?) who know more about this component can have a look too

Copy link
Contributor

@Mohammer5 Mohammer5 left a comment

Choose a reason for hiding this comment

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

Code-wise this looks good to me.

I went through the discussion on slack linked in the Jira issue and now I'm somewhat confused that this PR exists:

I guess my only question for general implementation if anyone would ever explicitly want to have the descendant roots as independent nodes in the tree
[...]
According to Morten it's a feature that is used by some. [...] The adding to user root org units when splitting seems 100% like a bug. The deduplication is actually less clear cut. I would say it's a bug, some might say it's a feature. If we can settle on this discussion, we should probably fix it in the Web API rather than the frontend. If we want to keep the "feature" around in the backend, do we also want to keep supporting it in the UI in the same way? Or should we then have a "change root dropdown" or sth? The more I am thinking about it the more nuances I see

This is where the discussion about the deduplication ends.
So is this a feature or a bug? How many users would be negatively affected by this change?
Should we make this "fix" optional (add a prop for enabling this fix rather than making it the default behavior)?

To me it seems that the issue is not the deduplication, but bugs like the one in the splitting logic, that seem to be wrong. So I'd err on the side of caution here and turn this into a feature that needs to be actively chosen (by using a prop) and focus on fixing the bugs that are true bugs.

Copy link

sonarcloud bot commented Nov 25, 2024

Quality Gate Failed Quality Gate failed

Failed conditions
1 New issue
1 New Code Smells (required ≤ 0)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

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.

5 participants