chore: add delay to useGetEntities batches and dedupe

Signed-off-by: Beth Griggs <bethanyngriggs@gmail.com>
This commit is contained in:
Beth Griggs
2024-07-01 15:41:24 +01:00
parent 5132d28818
commit 9a85f1cdaf
2 changed files with 31 additions and 23 deletions
@@ -266,7 +266,9 @@ describe('useGetEntities', () => {
},
);
await waitFor(() => expect(result.current.loading).toBe(false));
await waitFor(() => expect(result.current.loading).toBe(false), {
timeout: 5000,
});
const callArgs = (catalogApiMock.getEntities as jest.Mock).mock
.calls[0][0];
@@ -318,7 +320,9 @@ describe('useGetEntities', () => {
},
);
await waitFor(() => expect(result.current.loading).toBe(false));
await waitFor(() => expect(result.current.loading).toBe(false), {
timeout: 5000,
});
const callArgs = (catalogApiMock.getEntities as jest.Mock).mock
.calls[0][0];
@@ -32,7 +32,7 @@ import { useApi } from '@backstage/core-plugin-api';
import useAsync from 'react-use/esm/useAsync';
import qs from 'qs';
import { EntityRelationAggregation } from '../types';
import { uniq } from 'lodash';
import { uniq, uniqBy } from 'lodash';
const limiter = limiterFactory(5);
@@ -143,37 +143,41 @@ const getOwners = async (
return [stringifyEntityRef(entity)];
};
const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
const batchGetOwnedEntitiesByOwners = async (
owners: string[],
kinds: string[],
catalogApi: CatalogApi,
batchSize: number = 100,
delayMs: number = 100,
) => {
const batches = [];
const results = [];
for (let i = 0; i < owners.length; i += batchSize) {
const batch = owners.slice(i, i + batchSize);
batches.push(
catalogApi.getEntities({
filter: [
{
kind: kinds,
'relations.ownedBy': batch,
},
],
fields: [
'kind',
'metadata.name',
'metadata.namespace',
'spec.type',
'relations',
],
}),
);
const response = await catalogApi.getEntities({
filter: [
{
kind: kinds,
'relations.ownedBy': batch,
},
],
fields: [
'kind',
'metadata.name',
'metadata.namespace',
'spec.type',
'relations',
],
});
results.push(...response.items);
if (i + batchSize < owners.length) await delay(delayMs);
}
const results = await Promise.all(batches);
return results.flatMap(result => result.items);
return uniqBy(results, stringifyEntityRef);
};
export function useGetEntities(