From e49c742907661b5cc91ad37d06c6535993f99efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Erik=20Bergstr=C3=B6m?= Date: Wed, 5 Jul 2023 13:49:34 +0200 Subject: [PATCH] refactor(org): recursively fetch child group entities MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Carl-Erik Bergström --- .../Cards/OwnershipCard/useGetEntities.ts | 77 +++++++++---------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts b/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts index 660305e863..b18bf082a8 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts +++ b/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts @@ -71,53 +71,46 @@ const getMemberOfEntityRefs = (owner: Entity): string[] => { return ownerGroupsNames; }; -const getAggregatedOwnersEntityRef = async ( - parentGroup: Entity, +const isEntity = (entity: Entity | undefined): entity is Entity => + entity !== undefined; + +const getChildOwnershipEntityRefs = async ( + entity: Entity, catalogApi: CatalogApi, ): Promise => { - const requestedEntities: Entity[] = []; - const outstandingEntities = new Map>(); - const processedEntities = new Set(); - let currentEntity = parentGroup; + const childGroups = getEntityRelations(entity, RELATION_PARENT_OF, { + kind: 'Group', + }); - while (requestedEntities.length > 0) { - const childRelations = getEntityRelations( - currentEntity, - RELATION_PARENT_OF, - { - kind: 'Group', - }, - ); + const hasChildGroups = childGroups.length > 0; - await Promise.all( - childRelations.map(childGroup => - limiter(async () => { - const promise = catalogApi.getEntityByRef(childGroup); - outstandingEntities.set(childGroup.name, promise); - try { - const processedEntity = await promise; - if (processedEntity) { - requestedEntities.push(processedEntity); - } - } finally { - outstandingEntities.delete(childGroup.name); - } - }), - ), - ); - requestedEntities.shift(); - processedEntities.add( - stringifyEntityRef({ - kind: currentEntity.kind, - namespace: currentEntity.metadata.namespace, - name: currentEntity.metadata.name, - }), - ); - // always set currentEntity to the first element of array requestedEntities - currentEntity = requestedEntities[0]; + if (hasChildGroups) { + const childGroupEntities = ( + await Promise.all( + childGroups.map(childGroup => + limiter(() => catalogApi.getEntityByRef(childGroup)), + ), + ) + ).filter(isEntity); + + return ( + await Promise.all( + childGroupEntities.map(childGroupEntity => + limiter(() => + getChildOwnershipEntityRefs(childGroupEntity, catalogApi), + ), + ), + ) + ).flatMap(aggregated => aggregated); } - return Array.from(processedEntities); + return [ + stringifyEntityRef({ + kind: entity.kind, + namespace: entity.metadata.namespace, + name: entity.metadata.name, + }), + ]; }; const getOwners = async ( @@ -132,7 +125,7 @@ const getOwners = async ( const owners = [stringifyEntityRef(entity)]; if (isAggregated && isGroup) { - const childEntityRefs = await getAggregatedOwnersEntityRef( + const childEntityRefs = await getChildOwnershipEntityRefs( entity, catalogApi, );