-
Notifications
You must be signed in to change notification settings - Fork 15
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
Birkbjo
wants to merge
7
commits into
master
Choose a base branch
from
fix/LIBS-702/deduplicate-roots
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
db4f7b3
fix(OrganisationUnitTree): deduplicate orgunit roots
Birkbjo bcd9bd4
fix: add tests for deduplicate orgunits
Birkbjo 50be6d7
fix: add strictEqual checks for test
Birkbjo bd6afbd
refactor: rename to findCommoOrgUnitRoots
Birkbjo a9cd023
fix: add level to test data
Birkbjo a6c3b35
fix: fix cy tests and make sure filter works on root org units
flaminic 13292e8
fix: fix tests
Birkbjo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
components/organisation-unit-tree/src/__e2e__/get-organisation-unit-data.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
components/organisation-unit-tree/src/organisation-unit-tree/find-common-orgunit-roots.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Find the minimum/common root units from a list of orgunits. | ||
* This is used to "deduplicate" a list of units, where some unit in the list | ||
* may be a parent of another, and thus only the parent should be included as a root. | ||
* | ||
* This is mainly because of the /me API returning the verbatim selected units, | ||
* where the user is able to select children deep in a tree, even if an ancestor above is selected | ||
* @param {Array} unitsWithLevel - List of orgunits with their level | ||
* @returns {Array} - A filtered list of the minimum root units | ||
*/ | ||
export const findCommonOrgUnitRoots = (unitsWithLevel) => { | ||
const sorted = unitsWithLevel.sort((a, b) => a.level - b.level) | ||
|
||
const minimumRoots = sorted.filter((ou, index, array) => { | ||
// since the array is sorted by level we can just check the previous units, | ||
// because we want to get the "minimum" level | ||
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)) | ||
}) | ||
|
||
return minimumRoots | ||
} |
118 changes: 118 additions & 0 deletions
118
...nents/organisation-unit-tree/src/organisation-unit-tree/find-common-orgunit-roots.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { findCommonOrgUnitRoots } from './find-common-orgunit-roots.js' | ||
|
||
const unitToPath = { | ||
sierra: '/ImspTQPwCqd', | ||
tanzania: '/N5hLlID8ihI', | ||
ethiopia: '/LK1v9z1Jt3k', | ||
'sierra/bo': '/ImspTQPwCqd/O6uvpzGd5pu', | ||
'sierra/bo/bargbe': '/ImspTQPwCqd/O6uvpzGd5pu/dGheVylzol6', | ||
'sierra/bo/bargbe/barlie': | ||
'/ImspTQPwCqd/O6uvpzGd5pu/dGheVylzol6/y5hLlID8ihI', | ||
'sierra/bargbe/barlie': '/ImspTQPwCqd/dGheVylzol6/y5hLlID8ihI', | ||
'sierra/bargbe/barlie/ngalu': | ||
'/ImspTQPwCqd/dGheVylzol6/y5hLlID8ihI/Aj5v9z1Jt3k', | ||
'sierra/bo/baoma': '/ImspTQPwCqd/O6uvpzGd5pu/vWbkYPRmKyS', | ||
'sierra/bo/baoma/faabu': '/ImspTQPwCqd/O6uvpzGd5pu/vWbkYPRmKyS/ZpE2POxvl9P', | ||
'sierra/bo/badjia': '/ImspTQPwCqd/O6uvpzGd5pu/YuQRtpLP10I', | ||
'sierra/bo/badjia/ngelehun': | ||
'/ImspTQPwCqd/O6uvpzGd5pu/YuQRtpLP10I/DiszpKrYNg8', | ||
'sierra/bombali': '/ImspTQPwCqd/fdc6uOvgoji', | ||
} | ||
|
||
describe('findCommonOrgUnitRoots', () => { | ||
it('should return a single root unit when there is only one unit', () => { | ||
const units = [{ path: unitToPath.sierra, level: 1 }] | ||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([{ path: unitToPath.sierra, level: 1 }]) | ||
// should not mutate the input | ||
expect(units).toStrictEqual(units) | ||
}) | ||
|
||
it('should return two root units when there are two sibling units', () => { | ||
const units = [ | ||
{ path: unitToPath['sierra/bo'], level: 2 }, | ||
{ path: unitToPath['sierra/bombali'], level: 2 }, | ||
] | ||
|
||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([ | ||
{ path: unitToPath['sierra/bo'], level: 2 }, | ||
{ path: unitToPath['sierra/bombali'], level: 2 }, | ||
]) | ||
expect(units).toStrictEqual(units) | ||
}) | ||
|
||
it('should return only the root unit when one unit is a child of another', () => { | ||
const units = [ | ||
{ path: unitToPath['sierra'], level: 1 }, | ||
{ path: unitToPath['sierra/bo'], level: 2 }, | ||
] | ||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([{ path: unitToPath['sierra'], level: 1 }]) | ||
expect(units).toStrictEqual(units) | ||
}) | ||
|
||
it('should return only the root unit when one unit is a deep child of another', () => { | ||
const units = [ | ||
{ path: unitToPath['sierra'], level: 1 }, | ||
{ path: unitToPath['sierra/bo/badjia/ngelehun'], level: 4 }, | ||
] | ||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([{ path: unitToPath['sierra'], level: 1 }]) | ||
expect(units).toStrictEqual(units) | ||
}) | ||
|
||
it('should return multiple root units when paths do not overlap', () => { | ||
const units = [ | ||
{ path: unitToPath['sierra'], level: 1 }, | ||
{ path: unitToPath['tanzania'], level: 1 }, | ||
{ path: unitToPath['ethiopia'], level: 1 }, | ||
] | ||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([ | ||
{ path: unitToPath['sierra'], level: 1 }, | ||
{ path: unitToPath['tanzania'], level: 1 }, | ||
{ path: unitToPath['ethiopia'], level: 1 }, | ||
]) | ||
expect(units).toStrictEqual(units) | ||
}) | ||
|
||
it('should return the correct root units when there is a mix of roots and children', () => { | ||
const units = [ | ||
{ path: unitToPath['sierra/bo/badjia/ngelehun'], level: 4 }, | ||
{ path: unitToPath['sierra/bo/baoma/faabu'], level: 4 }, | ||
{ path: unitToPath['sierra/bo/baoma'], level: 3 }, | ||
{ path: unitToPath['sierra/bo/bargbe'], level: 3 }, | ||
] | ||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([ | ||
{ path: unitToPath['sierra/bo/baoma'], level: 3 }, | ||
{ path: unitToPath['sierra/bo/bargbe'], level: 3 }, | ||
{ path: unitToPath['sierra/bo/badjia/ngelehun'], level: 4 }, | ||
]) | ||
expect(units).toStrictEqual(units) | ||
}) | ||
|
||
it('should return the root units when multiple nested children exist', () => { | ||
const units = [ | ||
{ path: unitToPath['sierra/bo'], level: 2 }, | ||
{ path: unitToPath['sierra/bo/badjia'], level: 3 }, | ||
{ path: unitToPath['sierra/bo/baoma'], level: 3 }, | ||
{ path: unitToPath['sierra/bargbe/barlie'], level: 3 }, | ||
{ path: unitToPath['sierra/bargbe/barlie/ngalu'], level: 4 }, | ||
] | ||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([ | ||
{ path: unitToPath['sierra/bo'], level: 2 }, | ||
{ path: unitToPath['sierra/bargbe/barlie'], level: 3 }, | ||
]) | ||
expect(units).toStrictEqual(units) | ||
}) | ||
|
||
it('should handle empty input and return an empty array', () => { | ||
const units = [] | ||
const result = findCommonOrgUnitRoots(units) | ||
expect(result).toEqual([]) | ||
expect(units).toStrictEqual(units) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.