From 39d6f01f3af5e3814516f31ee76add76c51f4bef Mon Sep 17 00:00:00 2001 From: Yury Saukou Date: Mon, 30 Dec 2024 12:59:55 +0400 Subject: [PATCH] UIREC-427 Chunk HTTP requests to get pieces 'requests' by piece IDs (#625) --- CHANGELOG.md | 4 +- src/common/constants/constants.js | 3 ++ src/common/hooks/usePaginatedPieces/util.js | 37 ++++++++++++++----- .../usePiecesRequests/usePiecesRequests.js | 31 ++++++++++++++-- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4392a3e5..215504fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Change history for ui-receiving -## 6.1.0 (IN PROGRESS) +## 7.0.0 (IN PROGRESS) + +* Chunk HTTP requests to get pieces "requests" by piece IDs. Refs UIREC-427. ## [6.0.5](https://github.com/folio-org/ui-receiving/tree/v6.0.5) (2024-12-13) [Full Changelog](https://github.com/folio-org/ui-receiving/compare/v6.0.4...v6.0.5) diff --git a/src/common/constants/constants.js b/src/common/constants/constants.js index 06d399c9..bc698f9e 100644 --- a/src/common/constants/constants.js +++ b/src/common/constants/constants.js @@ -16,3 +16,6 @@ export const BATCH_IDENTIFIER_TYPE = { instanceHrid: 'instanceHrid', itemBarcode: 'itemBarcode', }; + +export const CHUNK_SIZE = 25; +export const MAX_PARALLEL_REQUESTS = 5; diff --git a/src/common/hooks/usePaginatedPieces/util.js b/src/common/hooks/usePaginatedPieces/util.js index 53d9df5e..c3d4090e 100644 --- a/src/common/hooks/usePaginatedPieces/util.js +++ b/src/common/hooks/usePaginatedPieces/util.js @@ -1,9 +1,13 @@ +import chunk from 'lodash/chunk'; + import { batchRequest, ITEMS_API, } from '@folio/stripes-acq-components'; import { + CHUNK_SIZE, + MAX_PARALLEL_REQUESTS, PIECE_REQUESTS_API, TENANT_ITEMS_API, } from '../../constants'; @@ -38,15 +42,30 @@ export const fetchConsortiumPieceItems = (ky, { pieces }) => { .then(({ tenantItems }) => tenantItems.map(({ item, tenantId }) => ({ tenantId, ...item }))); }; -export const fetchLocalPieceRequests = (ky, { pieces }) => { - if (!pieces.length) { - return Promise.resolve([]); +export const fetchLocalPieceRequests = async (ky, { pieces }) => { + const results = []; + + if (!pieces?.length) { + return Promise.resolve(results); } - return ky - .get(PIECE_REQUESTS_API, { - searchParams: buildPieceRequestsSearchParams(pieces), - }) - .json() - .then(({ circulationRequests }) => circulationRequests); + const pieceChunks = chunk(pieces, CHUNK_SIZE); + const pieceChunksGroups = chunk(pieceChunks, MAX_PARALLEL_REQUESTS); + + for (const group of pieceChunksGroups) { + const responses = await Promise.all( + group.map(async (_chunk) => { + return ky.get( + PIECE_REQUESTS_API, + { searchParams: buildPieceRequestsSearchParams(_chunk) }, + ) + .json() + .then(({ circulationRequests }) => circulationRequests); + }), + ); + + results.push(...responses); + } + + return results.flat(); }; diff --git a/src/common/hooks/usePiecesRequests/usePiecesRequests.js b/src/common/hooks/usePiecesRequests/usePiecesRequests.js index 6f3e4e14..af768957 100644 --- a/src/common/hooks/usePiecesRequests/usePiecesRequests.js +++ b/src/common/hooks/usePiecesRequests/usePiecesRequests.js @@ -1,3 +1,4 @@ +import chunk from 'lodash/chunk'; import { useQuery } from 'react-query'; import { @@ -5,7 +6,11 @@ import { useOkapiKy, } from '@folio/stripes/core'; -import { PIECE_REQUESTS_API } from '../../constants'; +import { + CHUNK_SIZE, + MAX_PARALLEL_REQUESTS, + PIECE_REQUESTS_API, +} from '../../constants'; import { buildPieceRequestsSearchParams } from '../../utils'; const DEFAULT_DATA = []; @@ -26,10 +31,28 @@ export const usePiecesRequests = (pieces = [], options = {}) => { isLoading, } = useQuery({ queryKey: [namespace, tenantId, pieces], - queryFn: ({ signal }) => { - const searchParams = buildPieceRequestsSearchParams(pieces); + queryFn: async ({ signal }) => { + const chunks = chunk(pieces, CHUNK_SIZE); + const chunkGroups = chunk(chunks, MAX_PARALLEL_REQUESTS); + + const initialAccumulator = { circulationRequests: [], totalRecords: 0 }; + + for (const group of chunkGroups) { + const responses = await Promise.all( + group.map(async (_chunk) => { + const searchParams = buildPieceRequestsSearchParams(_chunk); + + return ky.get(PIECE_REQUESTS_API, { searchParams, signal }).json(); + }), + ); + + responses.forEach((response) => { + initialAccumulator.circulationRequests.push(...response.circulationRequests); + initialAccumulator.totalRecords += response.totalRecords; + }); + } - return ky.get(PIECE_REQUESTS_API, { searchParams, signal }).json(); + return initialAccumulator; }, enabled: enabled && Boolean(pieces.length), ...queryOptions,