Ensured requests when loading visits are made in parallel for big dataset

This commit is contained in:
Alejandro Celaya 2019-03-04 19:21:46 +01:00
parent 7e27ceb885
commit 1bc406b0d9
4 changed files with 60 additions and 4 deletions

View file

@ -53,10 +53,12 @@ const ShortUrlVisits = ({ processStatsFromVisits }) => class ShortUrlVisits exte
const { shortUrlVisits, shortUrlDetail } = this.props; const { shortUrlVisits, shortUrlDetail } = this.props;
const renderVisitsContent = () => { const renderVisitsContent = () => {
const { visits, loading, error } = shortUrlVisits; const { visits, loading, loadingLarge, error } = shortUrlVisits;
if (loading) { 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) { if (error) {

View file

@ -1,9 +1,11 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { flatten, range, splitEvery } from 'ramda';
/* eslint-disable padding-line-between-statements, newline-after-var */ /* 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_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_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 = '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 */ /* eslint-enable padding-line-between-statements, newline-after-var */
export const shortUrlVisitsType = PropTypes.shape({ export const shortUrlVisitsType = PropTypes.shape({
@ -15,6 +17,7 @@ export const shortUrlVisitsType = PropTypes.shape({
const initialState = { const initialState = {
visits: [], visits: [],
loading: false, loading: false,
loadingLarge: false,
error: false, error: false,
}; };
@ -24,19 +27,27 @@ export default function reducer(state = initialState, action) {
return { return {
...state, ...state,
loading: true, loading: true,
loadingLarge: false,
}; };
case GET_SHORT_URL_VISITS_ERROR: case GET_SHORT_URL_VISITS_ERROR:
return { return {
...state, ...state,
loading: false, loading: false,
loadingLarge: false,
error: true, error: true,
}; };
case GET_SHORT_URL_VISITS: case GET_SHORT_URL_VISITS:
return { return {
visits: action.visits, visits: action.visits,
loading: false, loading: false,
loadingLarge: false,
error: false, error: false,
}; };
case GET_SHORT_URL_VISITS_LARGE:
return {
...state,
loadingLarge: true,
};
default: default:
return state; return state;
} }
@ -58,9 +69,36 @@ export const getShortUrlVisits = (buildShlinkApiClient) => (shortCode, dates) =>
return data; 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 { try {
const visits = await loadVisits(); const visits = await loadVisits();

View file

@ -43,7 +43,7 @@ describe('<ShortUrlVisits />', () => {
} }
}); });
it('Renders a preloader when visits are loading', () => { it('renders a preloader when visits are loading', () => {
const wrapper = createComponent({ loading: true }); const wrapper = createComponent({ loading: true });
const loadingMessage = wrapper.find(MutedMessage); const loadingMessage = wrapper.find(MutedMessage);
@ -51,6 +51,14 @@ describe('<ShortUrlVisits />', () => {
expect(loadingMessage.html()).toContain('Loading...'); expect(loadingMessage.html()).toContain('Loading...');
}); });
it('renders a warning when loading large amounts of visits', () => {
const wrapper = createComponent({ loading: true, loadingLarge: true });
const loadingMessage = wrapper.find(MutedMessage);
expect(loadingMessage).toHaveLength(1);
expect(loadingMessage.html()).toContain('This is going to take a while... :S');
});
it('renders an error message when visits could not be loaded', () => { it('renders an error message when visits could not be loaded', () => {
const wrapper = createComponent({ loading: false, error: true }); const wrapper = createComponent({ loading: false, error: true });
const errorMessage = wrapper.find(Card); const errorMessage = wrapper.find(Card);

View file

@ -4,6 +4,7 @@ import reducer, {
GET_SHORT_URL_VISITS_START, GET_SHORT_URL_VISITS_START,
GET_SHORT_URL_VISITS_ERROR, GET_SHORT_URL_VISITS_ERROR,
GET_SHORT_URL_VISITS, GET_SHORT_URL_VISITS,
GET_SHORT_URL_VISITS_LARGE,
} from '../../../src/visits/reducers/shortUrlVisits'; } from '../../../src/visits/reducers/shortUrlVisits';
describe('shortUrlVisitsReducer', () => { describe('shortUrlVisitsReducer', () => {
@ -15,6 +16,13 @@ describe('shortUrlVisitsReducer', () => {
expect(loading).toEqual(true); expect(loading).toEqual(true);
}); });
it('returns loadingLarge on GET_SHORT_URL_VISITS_LARGE', () => {
const state = reducer({ loadingLarge: false }, { type: GET_SHORT_URL_VISITS_LARGE });
const { loadingLarge } = state;
expect(loadingLarge).toEqual(true);
});
it('stops loading and returns error on GET_SHORT_URL_VISITS_ERROR', () => { it('stops loading and returns error on GET_SHORT_URL_VISITS_ERROR', () => {
const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_VISITS_ERROR }); const state = reducer({ loading: true, error: false }, { type: GET_SHORT_URL_VISITS_ERROR });
const { loading, error } = state; const { loading, error } = state;