Skip to content

Commit

Permalink
feat(RHINENG): limit -1 is removed from template feature codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
mkholjuraev committed Feb 15, 2024
1 parent ab9b4d5 commit 3f65144
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import PatchSetWizard from '../../SmartComponents/PatchSetWizard/PatchSetWizard'
import UnassignSystemsModal from '../../SmartComponents/Modals/UnassignSystemsModal';
import AssignSystemsModal from '../../SmartComponents/Modals/AssignSystemsModal';

const PatchSetWrapper = ({ patchSetState, setPatchSetState }) => {
const PatchSetWrapper = ({ patchSetState, setPatchSetState, totalItems }) => {
return (<>
{(patchSetState.isUnassignSystemsModalOpen) && <UnassignSystemsModal
unassignSystemsModalState={patchSetState}
setUnassignSystemsModalOpen={setPatchSetState}
systemsIDs={patchSetState.systemsIDs}
totalItems={totalItems}
/>}
<AssignSystemsModal
patchSetState={patchSetState}
setPatchSetState={setPatchSetState}
systemsIDs={patchSetState.systemsIDs}
totalItems={totalItems}
/>
{(patchSetState.isPatchSetWizardOpen) &&
<PatchSetWizard systemsIDs={patchSetState.systemsIDs} setBaselineState={setPatchSetState} />}
Expand All @@ -24,6 +26,7 @@ const PatchSetWrapper = ({ patchSetState, setPatchSetState }) => {

PatchSetWrapper.propTypes = {
patchSetState: propTypes.object,
setPatchSetState: propTypes.func
setPatchSetState: propTypes.func,
totalItems: propTypes.number
};
export default PatchSetWrapper;
17 changes: 12 additions & 5 deletions src/SmartComponents/Modals/AssignSystemsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { addNotification } from '@redhat-cloud-services/frontend-components-noti
import { patchSetAssignSystemsNotifications } from '../PatchSet/PatchSetAssets';
import { filterSelectedActiveSystemIDs } from '../../Utilities/Helpers';
import { filterSatelliteManagedSystems } from './Helpers';
import { useFetchBatched } from '../../Utilities/hooks';
import isEmpty from 'lodash/isEmpty';

const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {
const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl, totalItems }) => {
const dispatch = useDispatch();

const { systemsIDs, isAssignSystemsModalOpen } = patchSetState;
Expand All @@ -20,6 +22,7 @@ const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {

const [systemsNotManagedBySatellite, setSystemsNotManagedBySatellite] = useState([]);
const [systemsLoading, setSystemsLoading] = useState(true);
const { fetchBatched } = useFetchBatched();

const closeModal = () => {
setPatchSetState({
Expand Down Expand Up @@ -66,10 +69,13 @@ const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {
};

useEffect(() => {
if (systemsIDs) {
if (systemsIDs && !isEmpty(systemsIDs)) {
setSystemsLoading(true);

filterSatelliteManagedSystems(Object.keys(systemsIDs)).then(result => {
filterSatelliteManagedSystems(
Object.keys(systemsIDs),
fetchBatched,
totalItems
).then(result => {
setSystemsNotManagedBySatellite(result);
setSystemsLoading(false);
});
Expand Down Expand Up @@ -143,7 +149,8 @@ const AssignSystemsModal = ({ patchSetState = {}, setPatchSetState, intl }) => {
AssignSystemsModal.propTypes = {
intl: propTypes.any,
setPatchSetState: propTypes.func,
patchSetState: propTypes.object
patchSetState: propTypes.object,
totalItems: propTypes.number
};

export default injectIntl(AssignSystemsModal);
43 changes: 26 additions & 17 deletions src/SmartComponents/Modals/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,37 @@ import React from 'react';
import { GridItem } from '@patternfly/react-core';

import messages from '../../Messages';
import { fetchIDs, fetchSystems } from '../../Utilities/api';
import { fetchIDs } from '../../Utilities/api';

export const filterSystemsWithoutSets = (systemsIDs) => {
return fetchSystems({
limit: -1, 'filter[baseline_name]': 'neq:',
filter: { stale: [true, false] }
}).then((allSystemsWithPatchSet) => {
return systemsIDs.filter(systemID =>
allSystemsWithPatchSet?.data?.some(system => system.id === systemID)
const filterChosenSystems = (urlFilter, systemsIDs, fetchBatched, totalItems) => {
return fetchBatched(
(filter) => fetchIDs(
'/ids/systems',
filter
),
{
...urlFilter,
filter: { stale: [true, false] }
},
totalItems,
100
).then((systemsNotManagedBySatellite) => {
const aggregatedResult = systemsNotManagedBySatellite.flatMap(({ data }) => data);
return systemsIDs.filter(systemID =>{
return aggregatedResult?.some(system => system.id === systemID);
}
);
});
};

export const filterSatelliteManagedSystems = (systemsIDs) => {
return fetchIDs('/ids/systems', {
limit: -1, 'filter[satellite_managed]': 'false',
filter: { stale: [true, false] }
}).then((systemsNotManagedBySatellite) => {
return systemsIDs.filter(systemID =>
systemsNotManagedBySatellite?.data?.some(system => system.id === systemID)
);
});
export const filterSystemsWithoutSets = (systemsIDs, fetchBatched, totalItems) => {
const urlFilter = { 'filter[baseline_name]': 'neq:' };
return filterChosenSystems(urlFilter, systemsIDs, fetchBatched, totalItems);
};

export const filterSatelliteManagedSystems = (systemsIDs, fetchBatched, totalItems) => {
const urlFilter = { 'filter[satellite_managed]': 'false' };
return filterChosenSystems(urlFilter, systemsIDs, fetchBatched, totalItems);
};

export const renderUnassignModalMessages = (bodyMessage, systemsCount, intl) => (<GridItem>
Expand Down
14 changes: 11 additions & 3 deletions src/SmartComponents/Modals/UnassignSystemsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import { injectIntl } from 'react-intl';
import messages from '../../Messages';
import { useUnassignSystemsHook } from './useUnassignSystemsHook';
import { renderUnassignModalMessages, filterSystemsWithoutSets } from './Helpers';
import { useFetchBatched } from '../../Utilities/hooks';

const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSystemsModalOpen, intl }) => {
const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSystemsModalOpen, intl, totalItems }) => {
const { systemsIDs, isUnassignSystemsModalOpen } = unassignSystemsModalState;
const [systemsWithPatchSet, setSystemWithPatchSet] = useState([]);
const [systemsLoading, setSystemsLoading] = useState(true);
const { fetchBatched } = useFetchBatched();

const handleModalToggle = (shouldRefresh) => {
setUnassignSystemsModalOpen({
Expand All @@ -29,7 +31,12 @@ const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSyste
useEffect(() => {
setSystemsLoading(true);

filterSystemsWithoutSets(systemsIDs).then(result => {
filterSystemsWithoutSets(
systemsIDs,
fetchBatched,
totalItems
)
.then(result => {
setSystemWithPatchSet(result);
setSystemsLoading(false);
});
Expand Down Expand Up @@ -81,6 +88,7 @@ const UnassignSystemsModal = ({ unassignSystemsModalState = {}, setUnassignSyste
UnassignSystemsModal.propTypes = {
intl: propTypes.any,
setUnassignSystemsModalOpen: propTypes.func,
unassignSystemsModalState: propTypes.object
unassignSystemsModalState: propTypes.object,
totalItems: propTypes.number
};
export default injectIntl(UnassignSystemsModal);
4 changes: 2 additions & 2 deletions src/SmartComponents/Systems/SystemsListAssets.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ export const useActivateRemediationModal = (setRemediationIssues, setRemediation

fetchBatched(
(__, pagination) => fetchApplicableSystemAdvisoriesApi({ ...filter, ...pagination }),
totalCount,
filter
filter,
totalCount
).then(response => {
const advisories = response.flatMap(({ data }) => data);
const remediationIssues = remediationProvider(
Expand Down
3 changes: 2 additions & 1 deletion src/SmartComponents/Systems/SystemsTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const SystemsTable = ({
{
endpoint: ID_API_ENDPOINTS.systems,
queryParams,
selectionDispatcher: systemSelectAction
selectionDispatcher: systemSelectAction,
totalItems
}
);

Expand Down
4 changes: 2 additions & 2 deletions src/Utilities/RemediationPairs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chunk from 'lodash/chunk';

const REQUEST_CHUNK_SIZE = 1000;
const BATCH_REQUEST_SIZE = 5;
const REQUEST_CHUNK_SIZE = 100;
const BATCH_REQUEST_SIZE = 10;
const REQUEST_INTERVAL = 15000; //15 seconds. AKAMAI allowed life for 5 API calls

const fetchDataCallback = (endpoint, authToken) => (input) => {
Expand Down
17 changes: 14 additions & 3 deletions src/Utilities/hooks/useFetchBatched.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@ export const useFetchBatched = () => {

return {
isLoading,
fetchBatched: (fetchFunction, total, filter, batchSize = 50) => {
fetchBatched: async (fetchFunction, filter, total, batchSize = 50) => {
if (!total) {
total = await fetchFunction({ limit: 1 }).then(
response => response?.meta?.total_items || 0
);
}

const pages = Math.ceil(total / batchSize) || 1;

const results = resolve(
[...new Array(pages)].map(
// eslint-disable-next-line camelcase
(_, pageIdx) => () =>
fetchFunction(filter, { offset: pageIdx + 1, limit: batchSize })
(_, pageIdx) => () => {
return fetchFunction({
...filter,
offset: pageIdx * batchSize,
limit: batchSize
});
}
)
);

Expand Down
24 changes: 16 additions & 8 deletions src/Utilities/hooks/useOnSelect.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useDispatch } from 'react-redux';
import { fetchIDs } from '../api';
import { toggleAllSelectedAction } from '../../store/Actions/Actions';
import { isObject } from '../Helpers';
import { useFetchBatched } from './useFetchBatched';

export const ID_API_ENDPOINTS = {
advisories: '/ids/advisories',
Expand All @@ -17,15 +18,21 @@ export const ID_API_ENDPOINTS = {

const useFetchAllIDs = (
endpoint,
apiResponseTransformer
) =>
useCallback((queryParams) =>
fetchIDs(endpoint, { ...queryParams, limit: -1 })
apiResponseTransformer,
totalItems
) => {
const { fetchBatched } = useFetchBatched();
return useCallback((queryParams) =>
fetchBatched(
(__, pagination) => fetchIDs(endpoint, { ...queryParams, ...pagination }),
totalItems,
queryParams
)
.then(response =>
apiResponseTransformer ? apiResponseTransformer(response) : response
),
[]
);
[totalItems, endpoint]);
};

const useCreateSelectedRow = (transformKey, constructFilename) =>
useCallback((rows, toSelect = []) => {
Expand Down Expand Up @@ -103,11 +110,12 @@ export const useOnSelect = (rawData, selectedRows, config) => {
transformKey,
apiResponseTransformer,
//TODO: get rid of this custom selector
customSelector
customSelector,
totalItems
} = config;

const dispatch = useDispatch();
const fetchIDs = useFetchAllIDs(endpoint, apiResponseTransformer);
const fetchIDs = useFetchAllIDs(endpoint, apiResponseTransformer, totalItems);
const createSelectedRow = useCreateSelectedRow(transformKey, constructFilename);

const toggleAllSystemsSelected = (flagState) => {
Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/hooks/usePatchSetState.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const usePatchSetState = (selectedRows) => {
isUnassignSystemsModalOpen: false,
isAssignSystemsModalOpen: false,
shouldRefresh: false,
systemsIDs: []
systemsIDs: {}
});

const openPatchSetAssignWizard = (systemID) => {
Expand Down

0 comments on commit 3f65144

Please sign in to comment.