2020-05-10 20:02:58 +03:00
|
|
|
import { flatten, prop, range, splitEvery } from 'ramda';
|
2020-08-28 19:33:37 +03:00
|
|
|
import { Action, Dispatch } from 'redux';
|
2021-12-23 12:38:02 +03:00
|
|
|
import { ShlinkPaginator, ShlinkVisits, ShlinkVisitsParams } from '../../api/types';
|
2021-08-21 18:53:06 +03:00
|
|
|
import { Visit } from '../types';
|
2020-12-22 11:24:33 +03:00
|
|
|
import { parseApiError } from '../../api/utils';
|
2021-08-21 18:53:06 +03:00
|
|
|
import { ApiErrorAction } from '../../api/types/actions';
|
2021-12-23 12:38:02 +03:00
|
|
|
import { dateToMatchingInterval } from '../../utils/dates/types';
|
2020-05-10 20:02:58 +03:00
|
|
|
|
|
|
|
const ITEMS_PER_PAGE = 5000;
|
2020-05-11 20:32:42 +03:00
|
|
|
const PARALLEL_REQUESTS_COUNT = 4;
|
|
|
|
const PARALLEL_STARTING_PAGE = 2;
|
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
const isLastPage = ({ currentPage, pagesCount }: ShlinkPaginator): boolean => currentPage >= pagesCount;
|
|
|
|
const calcProgress = (total: number, current: number): number => current * 100 / total;
|
|
|
|
|
|
|
|
type VisitsLoader = (page: number, itemsPerPage: number) => Promise<ShlinkVisits>;
|
2021-12-23 12:38:02 +03:00
|
|
|
type LastVisitLoader = () => Promise<Visit | undefined>;
|
2020-08-28 19:33:37 +03:00
|
|
|
interface ActionMap {
|
|
|
|
start: string;
|
|
|
|
large: string;
|
|
|
|
finish: string;
|
|
|
|
error: string;
|
|
|
|
progress: string;
|
2021-12-23 12:38:02 +03:00
|
|
|
fallbackToInterval: string;
|
2020-08-28 19:33:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getVisitsWithLoader = async <T extends Action<string> & { visits: Visit[] }>(
|
|
|
|
visitsLoader: VisitsLoader,
|
2021-12-23 12:38:02 +03:00
|
|
|
lastVisitLoader: LastVisitLoader,
|
2020-08-28 19:33:37 +03:00
|
|
|
extraFinishActionData: Partial<T>,
|
|
|
|
actionMap: ActionMap,
|
|
|
|
dispatch: Dispatch,
|
2020-09-19 11:50:49 +03:00
|
|
|
shouldCancel: () => boolean,
|
2020-08-28 19:33:37 +03:00
|
|
|
) => {
|
2020-05-10 20:02:58 +03:00
|
|
|
dispatch({ type: actionMap.start });
|
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
const loadVisitsInParallel = async (pages: number[]): Promise<Visit[]> =>
|
|
|
|
Promise.all(pages.map(async (page) => visitsLoader(page, ITEMS_PER_PAGE).then(prop('data')))).then(flatten);
|
2020-05-10 20:02:58 +03:00
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
const loadPagesBlocks = async (pagesBlocks: number[][], index = 0): Promise<Visit[]> => {
|
2020-09-19 11:50:49 +03:00
|
|
|
if (shouldCancel()) {
|
2020-05-10 20:02:58 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = await loadVisitsInParallel(pagesBlocks[index]);
|
|
|
|
|
2020-05-11 20:32:42 +03:00
|
|
|
dispatch({ type: actionMap.progress, progress: calcProgress(pagesBlocks.length, index + PARALLEL_STARTING_PAGE) });
|
|
|
|
|
2020-05-10 20:02:58 +03:00
|
|
|
if (index < pagesBlocks.length - 1) {
|
|
|
|
return data.concat(await loadPagesBlocks(pagesBlocks, index + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
2020-08-28 19:33:37 +03:00
|
|
|
const loadVisits = async (page = 1) => {
|
|
|
|
const { pagination, data } = await visitsLoader(page, ITEMS_PER_PAGE);
|
|
|
|
|
|
|
|
// If pagination was not returned, then this is an old shlink version. Just return data
|
|
|
|
if (!pagination || isLastPage(pagination)) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are more pages, make requests in blocks of 4
|
|
|
|
const pagesRange = range(PARALLEL_STARTING_PAGE, pagination.pagesCount + 1);
|
|
|
|
const pagesBlocks = splitEvery(PARALLEL_REQUESTS_COUNT, pagesRange);
|
|
|
|
|
|
|
|
if (pagination.pagesCount - 1 > PARALLEL_REQUESTS_COUNT) {
|
|
|
|
dispatch({ type: actionMap.large });
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.concat(await loadPagesBlocks(pagesBlocks));
|
|
|
|
};
|
2020-05-10 20:02:58 +03:00
|
|
|
|
|
|
|
try {
|
2021-12-23 12:38:02 +03:00
|
|
|
const [ visits, lastVisit ] = await Promise.all([ loadVisits(), lastVisitLoader() ]);
|
2020-05-10 20:02:58 +03:00
|
|
|
|
2021-12-23 12:38:02 +03:00
|
|
|
dispatch(
|
|
|
|
!visits.length && lastVisit
|
|
|
|
? { type: actionMap.fallbackToInterval, fallbackInterval: dateToMatchingInterval(lastVisit.date) }
|
|
|
|
: { ...extraFinishActionData, visits, type: actionMap.finish },
|
|
|
|
);
|
2021-10-31 14:38:42 +03:00
|
|
|
} catch (e: any) {
|
2021-08-21 18:53:06 +03:00
|
|
|
dispatch<ApiErrorAction>({ type: actionMap.error, errorData: parseApiError(e) });
|
2020-05-10 20:02:58 +03:00
|
|
|
}
|
|
|
|
};
|
2021-12-23 12:38:02 +03:00
|
|
|
|
|
|
|
export const lastVisitLoaderForLoader = (
|
2021-12-23 12:51:13 +03:00
|
|
|
doIntervalFallback: boolean,
|
2021-12-23 12:38:02 +03:00
|
|
|
loader: (params: ShlinkVisitsParams) => Promise<ShlinkVisits>,
|
|
|
|
): LastVisitLoader => {
|
2021-12-23 12:51:13 +03:00
|
|
|
if (!doIntervalFallback) {
|
2021-12-23 12:38:02 +03:00
|
|
|
return async () => Promise.resolve(undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
return async () => loader({ page: 1, itemsPerPage: 1 }).then((result) => result.data[0]);
|
|
|
|
};
|