From ef71e71c8f5ab025c6787dda72ec8348a908eb25 Mon Sep 17 00:00:00 2001 From: MVarshini Date: Mon, 16 Dec 2024 14:10:08 +0530 Subject: [PATCH] ISSUE-143 Random Pagination not working --- backend/app/api/v1/endpoints/cpt/cptJobs.py | 2 +- backend/app/api/v1/endpoints/ocp/ocpJobs.py | 2 +- backend/app/api/v1/endpoints/quay/quayJobs.py | 4 +- .../app/api/v1/endpoints/telco/telcoJobs.py | 13 ++++-- frontend/src/actions/commonActions.js | 4 +- frontend/src/actions/paginationActions.js | 44 ++++++++++--------- .../components/organisms/Pagination/index.jsx | 4 ++ frontend/src/reducers/homeReducer.js | 1 - frontend/src/reducers/ocpReducer.js | 1 - frontend/src/reducers/quayReducer.js | 1 - frontend/src/reducers/telcoReducer.js | 1 - 11 files changed, 43 insertions(+), 34 deletions(-) diff --git a/backend/app/api/v1/endpoints/cpt/cptJobs.py b/backend/app/api/v1/endpoints/cpt/cptJobs.py index 3c9839c..c9c6372 100644 --- a/backend/app/api/v1/endpoints/cpt/cptJobs.py +++ b/backend/app/api/v1/endpoints/cpt/cptJobs.py @@ -102,7 +102,7 @@ async def jobs( "endDate": end_date.__str__(), "results": results_df.to_dict("records"), "total": jobsCount, - "offset": offset + size, + "offset": offset + size if size else 0, } if pretty: diff --git a/backend/app/api/v1/endpoints/ocp/ocpJobs.py b/backend/app/api/v1/endpoints/ocp/ocpJobs.py index 5637b29..3eae57f 100644 --- a/backend/app/api/v1/endpoints/ocp/ocpJobs.py +++ b/backend/app/api/v1/endpoints/ocp/ocpJobs.py @@ -69,7 +69,7 @@ async def jobs( "endDate": end_date.__str__(), "results": jobs, "total": results["total"], - "offset": offset + size, + "offset": offset + size if size else 0, } if pretty: diff --git a/backend/app/api/v1/endpoints/quay/quayJobs.py b/backend/app/api/v1/endpoints/quay/quayJobs.py index be05d07..7165c18 100644 --- a/backend/app/api/v1/endpoints/quay/quayJobs.py +++ b/backend/app/api/v1/endpoints/quay/quayJobs.py @@ -69,8 +69,8 @@ async def jobs( "startDate": start_date.__str__(), "endDate": end_date.__str__(), "results": jobs, - "total": 0, - "offset": 0, + "total": results["total"], + "offset": offset + size if size else 0, } if pretty: diff --git a/backend/app/api/v1/endpoints/telco/telcoJobs.py b/backend/app/api/v1/endpoints/telco/telcoJobs.py index c9a2d18..bd83db0 100644 --- a/backend/app/api/v1/endpoints/telco/telcoJobs.py +++ b/backend/app/api/v1/endpoints/telco/telcoJobs.py @@ -1,7 +1,7 @@ import json from fastapi import Response from datetime import datetime, timedelta, date -from fastapi import APIRouter +from fastapi import APIRouter, HTTPException from ...commons.telco import getData from ...commons.example_responses import telco_200_response, response_422 from fastapi.param_functions import Query @@ -50,6 +50,13 @@ async def jobs( ), status_code=422, ) + if offset and not size: + raise HTTPException(400, f"offset {offset} specified without size") + elif not offset and not size: + size = 10000 + offset = 0 + elif not offset: + offset = 0 results = await getData(start_date, end_date, size, offset, "telco.splunk") jobs = [] @@ -60,8 +67,8 @@ async def jobs( "startDate": start_date.__str__(), "endDate": end_date.__str__(), "results": jobs, - "total": 0, - "offset": 0, + "total": results["total"], + "offset": offset + size if size else 0, } if pretty: diff --git a/frontend/src/actions/commonActions.js b/frontend/src/actions/commonActions.js index 0551bd0..ac8a1a4 100644 --- a/frontend/src/actions/commonActions.js +++ b/frontend/src/actions/commonActions.js @@ -203,12 +203,12 @@ export const getSelectedFilter = }; export const getRequestParams = (type) => (dispatch, getState) => { - const { start_date, end_date, size, offset } = getState()[type]; + const { start_date, end_date, perPage, offset } = getState()[type]; const params = { pretty: true, ...(start_date && { start_date }), ...(end_date && { end_date }), - size: size, + size: perPage, offset: offset, }; diff --git a/frontend/src/actions/paginationActions.js b/frontend/src/actions/paginationActions.js index 09416cc..7f2bd08 100644 --- a/frontend/src/actions/paginationActions.js +++ b/frontend/src/actions/paginationActions.js @@ -6,6 +6,7 @@ import { } from "./ocpActions"; import { fetchOCPJobsData, + setCPTOffset, setCPTPage, setCPTPageOptions, sliceCPTTableRows, @@ -47,36 +48,37 @@ const calculateOffset = (pageNumber, itemsPerPage) => { return (pageNumber - 1) * itemsPerPage; }; +const fetchActions = { + ocp: fetchOCPJobs, + quay: fetchQuayJobsData, + telco: fetchTelcoJobsData, + cpt: fetchOCPJobsData, +}; +const offsetActions = { + ocp: setOCPOffset, + quay: setQuayOffset, + telco: setTelcoOffset, + cpt: setCPTOffset, +}; + export const checkTableData = (newPage, currType) => (dispatch, getState) => { const { results, totalJobs, perPage, page } = getState()[currType]; - const fetchActions = { - ocp: fetchOCPJobs, - quay: fetchQuayJobsData, - telco: fetchTelcoJobsData, - }; - const offsetActions = { - ocp: setOCPOffset, - quay: setQuayOffset, - telco: setTelcoOffset, - }; + const hasPageData = results.length >= newPage * perPage; const offset = calculateOffset(newPage, perPage); - if (results.length < totalJobs && !hasPageData) { - if (currType === "cpt") { - const startIdx = (page - 1) * perPage; - const endIdx = startIdx + perPage - 1; + + if (currType === "cpt") { + const startIdx = (page - 1) * perPage; + const endIdx = startIdx + perPage - 1; + if (results.length < totalJobs && !hasPageData) { if (results[startIdx] === undefined || results[endIdx] === undefined) { dispatch(fetchOCPJobsData()); } } else { - dispatch(offsetActions[currType](offset)); - dispatch(fetchActions[currType]()); - } - } else { - if (currType === "cpt") { - const startIdx = (page - 1) * perPage; - const endIdx = startIdx + perPage - 1; dispatch(sliceCPTTableRows(startIdx, endIdx)); } + } else if (results.length < totalJobs) { + dispatch(offsetActions[currType](offset)); + dispatch(fetchActions[currType]()); } }; diff --git a/frontend/src/components/organisms/Pagination/index.jsx b/frontend/src/components/organisms/Pagination/index.jsx index 5b6454b..32373a6 100644 --- a/frontend/src/components/organisms/Pagination/index.jsx +++ b/frontend/src/components/organisms/Pagination/index.jsx @@ -27,6 +27,7 @@ const RenderPagination = (props) => { const onPerPageSelect = useCallback( (_evt, newPerPage, newPage, startIdx, endIdx) => { dispatch(setPageOptions(newPage, newPerPage, props.type)); + dispatch(checkTableData(newPage, props.type)); }, [dispatch, props.type] ); @@ -51,8 +52,11 @@ const RenderPagination = (props) => { perPageOptions={perPageOptions} onSetPage={onSetPage} onPerPageSelect={onPerPageSelect} + onPreviousClick={onNextClick} onNextClick={onNextClick} onPageInput={onNextClick} + onFirstClick={onNextClick} + onLastClick={onNextClick} isCompact={props.type === "cpt" ? true : false} /> ); diff --git a/frontend/src/reducers/homeReducer.js b/frontend/src/reducers/homeReducer.js index a2f477e..17440c9 100644 --- a/frontend/src/reducers/homeReducer.js +++ b/frontend/src/reducers/homeReducer.js @@ -45,7 +45,6 @@ const initialState = { filteredResults: [], page: START_PAGE, perPage: DEFAULT_PER_PAGE, - size: DEFAULT_PER_PAGE, offset: INITAL_OFFSET, totalJobs: 0, summary: {}, diff --git a/frontend/src/reducers/ocpReducer.js b/frontend/src/reducers/ocpReducer.js index 20232ad..d24fd86 100644 --- a/frontend/src/reducers/ocpReducer.js +++ b/frontend/src/reducers/ocpReducer.js @@ -44,7 +44,6 @@ const initialState = { activeSortIndex: null, page: START_PAGE, perPage: DEFAULT_PER_PAGE, - size: DEFAULT_PER_PAGE, offset: INITAL_OFFSET, totalJobs: 0, //tableData: [], diff --git a/frontend/src/reducers/quayReducer.js b/frontend/src/reducers/quayReducer.js index 9e33d16..03b9327 100644 --- a/frontend/src/reducers/quayReducer.js +++ b/frontend/src/reducers/quayReducer.js @@ -64,7 +64,6 @@ const initialState = { graphData: [], page: START_PAGE, perPage: DEFAULT_PER_PAGE, - size: DEFAULT_PER_PAGE, offset: INITAL_OFFSET, totalJobs: 0, summary: {}, diff --git a/frontend/src/reducers/telcoReducer.js b/frontend/src/reducers/telcoReducer.js index 5dd1a14..efe2f24 100644 --- a/frontend/src/reducers/telcoReducer.js +++ b/frontend/src/reducers/telcoReducer.js @@ -61,7 +61,6 @@ const initialState = { graphData: [], page: START_PAGE, perPage: DEFAULT_PER_PAGE, - size: DEFAULT_PER_PAGE, offset: INITAL_OFFSET, totalJobs: 0, summary: {},