mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-13 02:53:47 +00:00
Ensured requests when loading visits are made in parallel for big dataset
This commit is contained in:
@@ -53,10 +53,12 @@ const ShortUrlVisits = ({ processStatsFromVisits }) => class ShortUrlVisits exte
|
||||
const { shortUrlVisits, shortUrlDetail } = this.props;
|
||||
|
||||
const renderVisitsContent = () => {
|
||||
const { visits, loading, error } = shortUrlVisits;
|
||||
const { visits, loading, loadingLarge, error } = shortUrlVisits;
|
||||
|
||||
if (loading) {
|
||||
return <MutedMessage><FontAwesomeIcon icon={preloader} spin /> Loading...</MutedMessage>;
|
||||
const message = loadingLarge ? 'This is going to take a while... :S' : 'Loading...';
|
||||
|
||||
return <MutedMessage><FontAwesomeIcon icon={preloader} spin /> {message}</MutedMessage>;
|
||||
}
|
||||
|
||||
if (error) {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import PropTypes from 'prop-types';
|
||||
import { flatten, range, splitEvery } from 'ramda';
|
||||
|
||||
/* eslint-disable padding-line-between-statements, newline-after-var */
|
||||
export const GET_SHORT_URL_VISITS_START = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_START';
|
||||
export const GET_SHORT_URL_VISITS_ERROR = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_ERROR';
|
||||
export const GET_SHORT_URL_VISITS = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS';
|
||||
export const GET_SHORT_URL_VISITS_LARGE = 'shlink/shortUrlVisits/GET_SHORT_URL_VISITS_LARGE';
|
||||
/* eslint-enable padding-line-between-statements, newline-after-var */
|
||||
|
||||
export const shortUrlVisitsType = PropTypes.shape({
|
||||
@@ -15,6 +17,7 @@ export const shortUrlVisitsType = PropTypes.shape({
|
||||
const initialState = {
|
||||
visits: [],
|
||||
loading: false,
|
||||
loadingLarge: false,
|
||||
error: false,
|
||||
};
|
||||
|
||||
@@ -24,19 +27,27 @@ export default function reducer(state = initialState, action) {
|
||||
return {
|
||||
...state,
|
||||
loading: true,
|
||||
loadingLarge: false,
|
||||
};
|
||||
case GET_SHORT_URL_VISITS_ERROR:
|
||||
return {
|
||||
...state,
|
||||
loading: false,
|
||||
loadingLarge: false,
|
||||
error: true,
|
||||
};
|
||||
case GET_SHORT_URL_VISITS:
|
||||
return {
|
||||
visits: action.visits,
|
||||
loading: false,
|
||||
loadingLarge: false,
|
||||
error: false,
|
||||
};
|
||||
case GET_SHORT_URL_VISITS_LARGE:
|
||||
return {
|
||||
...state,
|
||||
loadingLarge: true,
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
@@ -58,9 +69,36 @@ export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) =>
|
||||
return data;
|
||||
}
|
||||
|
||||
return data.concat(await loadVisits(page + 1));
|
||||
// If there are more pages, make requests in blocks of 4
|
||||
const parallelRequestsCount = 4;
|
||||
const parallelStartingPage = 2;
|
||||
const pagesRange = range(parallelStartingPage, pagination.pagesCount + 1);
|
||||
const pagesBlocks = splitEvery(parallelRequestsCount, pagesRange);
|
||||
|
||||
if (pagination.pagesCount - 1 > parallelRequestsCount) {
|
||||
dispatch({ type: GET_SHORT_URL_VISITS_LARGE });
|
||||
}
|
||||
|
||||
return data.concat(await loadPagesBlocks(pagesBlocks));
|
||||
};
|
||||
|
||||
const loadPagesBlocks = async (pagesBlocks, index = 0) => {
|
||||
const data = await loadVisitsInParallel(pagesBlocks[index]);
|
||||
|
||||
if (index < pagesBlocks.length - 1) {
|
||||
return data.concat(await loadPagesBlocks(pagesBlocks, index + 1));
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
const loadVisitsInParallel = (pages) =>
|
||||
Promise.all(pages.map(async (page) => {
|
||||
const { data } = await getShortUrlVisits(shortCode, { ...dates, page, itemsPerPage });
|
||||
|
||||
return data;
|
||||
})).then(flatten);
|
||||
|
||||
try {
|
||||
const visits = await loadVisits();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user