From 4bd4a134a0c4702d24b2e694803a424766e6c24f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl-Erik=20Bergstr=C3=B6m?= Date: Fri, 18 Aug 2023 10:56:13 +0200 Subject: [PATCH] refactor(org): batch 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 chore: add types to OwnershipCard Signed-off-by: Carl-Erik Bergström --- .../Cards/OwnershipCard/ComponentsGrid.tsx | 3 ++- .../Cards/OwnershipCard/OwnershipCard.tsx | 4 +-- .../components/Cards/OwnershipCard/index.ts | 1 + .../components/Cards/OwnershipCard/types.ts | 25 ++++++++++++++++++ .../OwnershipCard/useGetEntities.test.ts | 8 +++--- .../Cards/OwnershipCard/useGetEntities.ts | 26 +++++++------------ 6 files changed, 43 insertions(+), 24 deletions(-) create mode 100644 plugins/org/src/components/Cards/OwnershipCard/types.ts diff --git a/plugins/org/src/components/Cards/OwnershipCard/ComponentsGrid.tsx b/plugins/org/src/components/Cards/OwnershipCard/ComponentsGrid.tsx index ce2f12eaaf..c4869c849c 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/ComponentsGrid.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/ComponentsGrid.tsx @@ -33,7 +33,8 @@ import { import React from 'react'; import pluralize from 'pluralize'; import { catalogIndexRouteRef } from '../../../routes'; -import { RelationType, useGetEntities } from './useGetEntities'; +import { useGetEntities } from './useGetEntities'; +import { RelationType } from './types'; const useStyles = makeStyles((theme: BackstageTheme) => createStyles({ diff --git a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx index 5fb555087c..4de423575e 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx +++ b/plugins/org/src/components/Cards/OwnershipCard/OwnershipCard.tsx @@ -27,9 +27,7 @@ import { } from '@material-ui/core'; import React, { useState } from 'react'; import { ComponentsGrid } from './ComponentsGrid'; -import { type RelationType, DefaultRelationType } from './useGetEntities'; - -export { type RelationType, DefaultRelationType } from './useGetEntities'; +import { DefaultRelationType, RelationType } from './types'; const useStyles = makeStyles(theme => ({ list: { diff --git a/plugins/org/src/components/Cards/OwnershipCard/index.ts b/plugins/org/src/components/Cards/OwnershipCard/index.ts index dc0cef0226..a4798cc4bf 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/index.ts +++ b/plugins/org/src/components/Cards/OwnershipCard/index.ts @@ -14,3 +14,4 @@ * limitations under the License. */ export * from './OwnershipCard'; +export * from './types'; diff --git a/plugins/org/src/components/Cards/OwnershipCard/types.ts b/plugins/org/src/components/Cards/OwnershipCard/types.ts new file mode 100644 index 0000000000..09095346f5 --- /dev/null +++ b/plugins/org/src/components/Cards/OwnershipCard/types.ts @@ -0,0 +1,25 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** @public */ +export const DefaultRelationType = { + Direct: 'direct', + Aggregated: 'aggregated', +} as const; + +/** @public */ +export type RelationType = + (typeof DefaultRelationType)[keyof typeof DefaultRelationType]; diff --git a/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.test.ts b/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.test.ts index ef2ebe3127..191bd10284 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.test.ts +++ b/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.test.ts @@ -41,10 +41,12 @@ const givenUserEntity = { }, } as Partial as Entity; -const catalogApiMock: Pick = { +const catalogApiMock: Pick = { getEntities: jest.fn(async () => Promise.resolve({ items: [] })), - getEntityByRef: jest.fn(async ({ name }: CompoundEntityRef) => - name === givenParentGroup ? givenParentGroupEntity : givenLeafGroupEntity, + getEntitiesByRefs: jest.fn(async ({ entityRefs: [ref] }) => + ref.includes(givenParentGroup) + ? { items: [givenParentGroupEntity] } + : { items: [givenLeafGroupEntity] }, ), }; diff --git a/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts b/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts index c66518866a..361294ea1e 100644 --- a/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts +++ b/plugins/org/src/components/Cards/OwnershipCard/useGetEntities.ts @@ -31,6 +31,7 @@ import limiterFactory from 'p-limit'; import { useApi } from '@backstage/core-plugin-api'; import useAsync from 'react-use/lib/useAsync'; import qs from 'qs'; +import { RelationType } from './types'; const limiter = limiterFactory(10); @@ -92,13 +93,14 @@ const getChildOwnershipEntityRefs = async ( const hasChildGroups = childGroups.length > 0; if (hasChildGroups) { - const childGroupEntities = ( - await Promise.all( - childGroups.map(childGroup => - limiter(() => catalogApi.getEntityByRef(childGroup)), - ), - ) - ).filter(isEntity); + const entityRefs = childGroups.map( + ({ kind, namespace, name }) => `${kind}:${namespace}/${name}`, + ); + const childGroupResponse = await catalogApi.getEntitiesByRefs({ + fields: ['kind', 'metadata.namespace', 'metadata.name'], + entityRefs, + }); + const childGroupEntities = childGroupResponse.items.filter(isEntity); return ( await Promise.all( @@ -148,16 +150,6 @@ const getOwners = async ( return owners; }; -/** @public */ -export const DefaultRelationType = { - Direct: 'direct', - Aggregated: 'aggregated', -} as const; - -/** @public */ -export type RelationType = - (typeof DefaultRelationType)[keyof typeof DefaultRelationType]; - const getOwnedEntitiesByOwners = ( owners: string[], kinds: string[],