2021-03-28 21:56:16 +03:00
|
|
|
import { isEmpty, propEq, values } from 'ramda';
|
2021-12-22 22:23:26 +03:00
|
|
|
import { useState, useEffect, useMemo, FC, useRef } from 'react';
|
2022-02-13 22:20:20 +03:00
|
|
|
import { Button, Progress, Row } from 'reactstrap';
|
2020-09-05 09:49:18 +03:00
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
2021-03-14 20:12:10 +03:00
|
|
|
import { faCalendarAlt, faMapMarkedAlt, faList, faChartPie, faFileDownload } from '@fortawesome/free-solid-svg-icons';
|
2020-12-12 22:45:23 +03:00
|
|
|
import { IconDefinition } from '@fortawesome/fontawesome-common-types';
|
2022-02-13 22:20:20 +03:00
|
|
|
import { Route, Routes, Navigate } from 'react-router-dom';
|
2021-03-28 16:57:22 +03:00
|
|
|
import classNames from 'classnames';
|
2020-12-15 00:58:15 +03:00
|
|
|
import { DateRangeSelector } from '../utils/dates/DateRangeSelector';
|
2020-09-05 09:49:18 +03:00
|
|
|
import Message from '../utils/Message';
|
2020-12-15 20:40:36 +03:00
|
|
|
import { DateInterval, DateRange, intervalToDateRange } from '../utils/dates/types';
|
2020-12-21 19:54:05 +03:00
|
|
|
import { Result } from '../utils/Result';
|
2020-12-22 01:41:50 +03:00
|
|
|
import { ShlinkApiError } from '../api/ShlinkApiError';
|
2021-03-06 12:56:49 +03:00
|
|
|
import { Settings } from '../settings/reducers/settings';
|
2021-06-13 12:07:32 +03:00
|
|
|
import { SelectedServer } from '../servers/data';
|
2021-06-22 22:01:20 +03:00
|
|
|
import { supportsBotVisits } from '../utils/helpers/features';
|
2021-11-01 13:18:09 +03:00
|
|
|
import { prettify } from '../utils/helpers/numbers';
|
2022-02-14 21:58:20 +03:00
|
|
|
import { NavPillItem, NavPills } from '../utils/NavPills';
|
2021-09-18 20:07:50 +03:00
|
|
|
import LineChartCard from './charts/LineChartCard';
|
2020-09-05 09:49:18 +03:00
|
|
|
import VisitsTable from './VisitsTable';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { NormalizedOrphanVisit, NormalizedVisit, VisitsFilter, VisitsInfo, VisitsParams } from './types';
|
2020-09-05 09:49:18 +03:00
|
|
|
import OpenMapModalBtn from './helpers/OpenMapModalBtn';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { normalizeVisits, processStatsFromVisits } from './services/VisitsParser';
|
2021-06-30 03:36:13 +03:00
|
|
|
import { VisitsFilterDropdown } from './helpers/VisitsFilterDropdown';
|
2021-06-30 04:23:45 +03:00
|
|
|
import { HighlightableProps, highlightedVisitsToStats } from './types/helpers';
|
2021-09-18 20:05:28 +03:00
|
|
|
import { DoughnutChartCard } from './charts/DoughnutChartCard';
|
|
|
|
import { SortableBarChartCard } from './charts/SortableBarChartCard';
|
2020-09-05 09:49:18 +03:00
|
|
|
|
|
|
|
export interface VisitsStatsProps {
|
2021-12-23 12:51:13 +03:00
|
|
|
getVisits: (params: VisitsParams, doIntervalFallback?: boolean) => void;
|
2020-09-05 09:49:18 +03:00
|
|
|
visitsInfo: VisitsInfo;
|
2021-03-06 12:56:49 +03:00
|
|
|
settings: Settings;
|
2021-06-13 12:07:32 +03:00
|
|
|
selectedServer: SelectedServer;
|
2020-09-05 09:49:18 +03:00
|
|
|
cancelGetVisits: () => void;
|
2020-12-20 21:28:09 +03:00
|
|
|
domain?: string;
|
2021-03-14 14:49:12 +03:00
|
|
|
exportCsv: (visits: NormalizedVisit[]) => void;
|
2021-03-28 16:57:22 +03:00
|
|
|
isOrphanVisits?: boolean;
|
2020-09-05 09:49:18 +03:00
|
|
|
}
|
|
|
|
|
2020-12-21 11:22:13 +03:00
|
|
|
interface VisitsNavLinkProps {
|
|
|
|
title: string;
|
|
|
|
subPath: string;
|
|
|
|
icon: IconDefinition;
|
|
|
|
}
|
|
|
|
|
2020-12-12 22:45:23 +03:00
|
|
|
type Section = 'byTime' | 'byContext' | 'byLocation' | 'list';
|
|
|
|
|
2020-12-21 11:22:13 +03:00
|
|
|
const sections: Record<Section, VisitsNavLinkProps> = {
|
2022-02-06 22:07:18 +03:00
|
|
|
byTime: { title: 'By time', subPath: 'by-time', icon: faCalendarAlt },
|
|
|
|
byContext: { title: 'By context', subPath: 'by-context', icon: faChartPie },
|
|
|
|
byLocation: { title: 'By location', subPath: 'by-location', icon: faMapMarkedAlt },
|
|
|
|
list: { title: 'List', subPath: 'list', icon: faList },
|
2020-12-12 22:45:23 +03:00
|
|
|
};
|
2020-09-05 09:49:18 +03:00
|
|
|
|
|
|
|
let selectedBar: string | undefined;
|
|
|
|
|
2021-06-13 12:07:32 +03:00
|
|
|
const VisitsStats: FC<VisitsStatsProps> = ({
|
|
|
|
children,
|
|
|
|
visitsInfo,
|
|
|
|
getVisits,
|
|
|
|
cancelGetVisits,
|
|
|
|
domain,
|
|
|
|
settings,
|
|
|
|
exportCsv,
|
|
|
|
selectedServer,
|
|
|
|
isOrphanVisits = false,
|
|
|
|
}) => {
|
2021-12-22 22:23:26 +03:00
|
|
|
const { visits, loading, loadingLarge, error, errorData, progress, fallbackInterval } = visitsInfo;
|
|
|
|
const [ initialInterval, setInitialInterval ] = useState<DateInterval>(
|
|
|
|
fallbackInterval ?? settings.visits?.defaultInterval ?? 'last30Days',
|
|
|
|
);
|
2020-12-15 20:40:36 +03:00
|
|
|
const [ dateRange, setDateRange ] = useState<DateRange>(intervalToDateRange(initialInterval));
|
2020-09-05 09:49:18 +03:00
|
|
|
const [ highlightedVisits, setHighlightedVisits ] = useState<NormalizedVisit[]>([]);
|
|
|
|
const [ highlightedLabel, setHighlightedLabel ] = useState<string | undefined>();
|
2021-06-22 21:34:28 +03:00
|
|
|
const [ visitsFilter, setVisitsFilter ] = useState<VisitsFilter>({});
|
2021-06-22 22:01:20 +03:00
|
|
|
const botsSupported = supportsBotVisits(selectedServer);
|
2021-12-22 22:23:26 +03:00
|
|
|
const isFirstLoad = useRef(true);
|
2020-09-05 09:49:18 +03:00
|
|
|
|
2020-12-20 21:28:09 +03:00
|
|
|
const buildSectionUrl = (subPath?: string) => {
|
|
|
|
const query = domain ? `?domain=${domain}` : '';
|
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
return !subPath ? `${query}` : `${subPath}${query}`;
|
2020-12-20 21:28:09 +03:00
|
|
|
};
|
2021-06-30 04:23:45 +03:00
|
|
|
const normalizedVisits = useMemo(() => normalizeVisits(visits), [ visits ]);
|
2021-03-28 21:56:16 +03:00
|
|
|
const { os, browsers, referrers, countries, cities, citiesForMap, visitedUrls } = useMemo(
|
2020-09-05 09:49:18 +03:00
|
|
|
() => processStatsFromVisits(normalizedVisits),
|
|
|
|
[ normalizedVisits ],
|
|
|
|
);
|
|
|
|
const mapLocations = values(citiesForMap);
|
|
|
|
|
|
|
|
const setSelectedVisits = (selectedVisits: NormalizedVisit[]) => {
|
|
|
|
selectedBar = undefined;
|
|
|
|
setHighlightedVisits(selectedVisits);
|
|
|
|
};
|
2021-03-28 21:56:16 +03:00
|
|
|
const highlightVisitsForProp = (prop: HighlightableProps<NormalizedOrphanVisit>) => (value: string) => {
|
2020-09-05 09:49:18 +03:00
|
|
|
const newSelectedBar = `${prop}_${value}`;
|
|
|
|
|
|
|
|
if (selectedBar === newSelectedBar) {
|
|
|
|
setHighlightedVisits([]);
|
|
|
|
setHighlightedLabel(undefined);
|
|
|
|
selectedBar = undefined;
|
|
|
|
} else {
|
2021-03-28 21:56:16 +03:00
|
|
|
setHighlightedVisits((normalizedVisits as NormalizedOrphanVisit[]).filter(propEq(prop, value)));
|
2020-09-05 09:49:18 +03:00
|
|
|
setHighlightedLabel(value);
|
|
|
|
selectedBar = newSelectedBar;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-15 20:40:36 +03:00
|
|
|
useEffect(() => cancelGetVisits, []);
|
2020-09-05 09:49:18 +03:00
|
|
|
useEffect(() => {
|
2021-12-22 22:23:26 +03:00
|
|
|
getVisits({ dateRange, filter: visitsFilter }, isFirstLoad.current);
|
|
|
|
isFirstLoad.current = false;
|
2021-06-30 04:23:45 +03:00
|
|
|
}, [ dateRange, visitsFilter ]);
|
2021-12-22 22:23:26 +03:00
|
|
|
useEffect(() => {
|
|
|
|
fallbackInterval && setInitialInterval(fallbackInterval);
|
|
|
|
}, [ fallbackInterval ]);
|
2020-09-05 09:49:18 +03:00
|
|
|
|
|
|
|
const renderVisitsContent = () => {
|
|
|
|
if (loadingLarge) {
|
|
|
|
return (
|
|
|
|
<Message loading>
|
|
|
|
This is going to take a while... :S
|
|
|
|
<Progress value={progress} striped={progress === 100} className="mt-3" />
|
|
|
|
</Message>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
return <Message loading />;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
2020-12-22 01:41:50 +03:00
|
|
|
return (
|
|
|
|
<Result type="error">
|
|
|
|
<ShlinkApiError errorData={errorData} fallbackMessage="An error occurred while loading visits :(" />
|
|
|
|
</Result>
|
|
|
|
);
|
2020-09-05 09:49:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmpty(visits)) {
|
|
|
|
return <Message>There are no visits matching current filter :(</Message>;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-12-12 22:45:23 +03:00
|
|
|
<>
|
2022-02-14 21:58:20 +03:00
|
|
|
<NavPills>
|
|
|
|
{Object.values(sections).map(({ title, icon, subPath }, index) => (
|
|
|
|
<NavPillItem key={index} to={buildSectionUrl(subPath)} replace>
|
|
|
|
<FontAwesomeIcon icon={icon} />
|
|
|
|
<span className="ml-2 d-none d-sm-inline">{title}</span>
|
|
|
|
</NavPillItem>
|
|
|
|
))}
|
|
|
|
</NavPills>
|
2020-12-30 21:48:02 +03:00
|
|
|
<Row>
|
2022-02-06 22:07:18 +03:00
|
|
|
<Routes>
|
|
|
|
<Route
|
|
|
|
path={sections.byTime.subPath}
|
|
|
|
element={(
|
|
|
|
<div className="col-12 mt-3">
|
|
|
|
<LineChartCard
|
|
|
|
title="Visits during time"
|
|
|
|
visits={normalizedVisits}
|
|
|
|
highlightedVisits={highlightedVisits}
|
2021-03-28 21:56:16 +03:00
|
|
|
highlightedLabel={highlightedLabel}
|
2022-02-06 22:07:18 +03:00
|
|
|
setSelectedVisits={setSelectedVisits}
|
2021-03-28 16:57:22 +03:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-02-06 22:07:18 +03:00
|
|
|
/>
|
2020-12-20 21:28:09 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
<Route
|
|
|
|
path={sections.byContext.subPath}
|
|
|
|
element={(
|
|
|
|
<>
|
|
|
|
<div className={classNames('mt-3 col-lg-6', { 'col-xl-4': !isOrphanVisits })}>
|
|
|
|
<DoughnutChartCard title="Operating systems" stats={os} />
|
|
|
|
</div>
|
|
|
|
<div className={classNames('mt-3 col-lg-6', { 'col-xl-4': !isOrphanVisits })}>
|
|
|
|
<DoughnutChartCard title="Browsers" stats={browsers} />
|
|
|
|
</div>
|
|
|
|
<div className={classNames('mt-3', { 'col-xl-4': !isOrphanVisits, 'col-lg-6': isOrphanVisits })}>
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Referrers"
|
|
|
|
stats={referrers}
|
|
|
|
withPagination={false}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'referer')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Referrer name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('referer')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{isOrphanVisits && (
|
|
|
|
<div className="mt-3 col-lg-6">
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Visited URLs"
|
|
|
|
stats={visitedUrls}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'visitedUrl')}
|
|
|
|
sortingItems={{
|
|
|
|
visitedUrl: 'Visited URL',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('visitedUrl')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
2020-12-20 21:28:09 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
<Route
|
|
|
|
path={sections.byLocation.subPath}
|
|
|
|
element={(
|
|
|
|
<>
|
|
|
|
<div className="col-lg-6 mt-3">
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Countries"
|
|
|
|
stats={countries}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'country')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'Country name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('country')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="col-lg-6 mt-3">
|
|
|
|
<SortableBarChartCard
|
|
|
|
title="Cities"
|
|
|
|
stats={cities}
|
|
|
|
highlightedStats={highlightedVisitsToStats(highlightedVisits, 'city')}
|
|
|
|
highlightedLabel={highlightedLabel}
|
|
|
|
extraHeaderContent={(activeCities: string[]) =>
|
|
|
|
mapLocations.length > 0 &&
|
|
|
|
<OpenMapModalBtn modalTitle="Cities" locations={mapLocations} activeCities={activeCities} />
|
|
|
|
}
|
|
|
|
sortingItems={{
|
|
|
|
name: 'City name',
|
|
|
|
amount: 'Visits amount',
|
|
|
|
}}
|
|
|
|
onClick={highlightVisitsForProp('city')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Route
|
|
|
|
path={sections.list.subPath}
|
|
|
|
element={(
|
|
|
|
<div className="col-12">
|
|
|
|
<VisitsTable
|
|
|
|
visits={normalizedVisits}
|
|
|
|
selectedVisits={highlightedVisits}
|
|
|
|
setSelectedVisits={setSelectedVisits}
|
|
|
|
isOrphanVisits={isOrphanVisits}
|
|
|
|
selectedServer={selectedServer}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
2020-12-20 21:28:09 +03:00
|
|
|
|
2022-02-06 22:07:18 +03:00
|
|
|
<Route path="*" element={<Navigate replace to={buildSectionUrl(sections.byTime.subPath)} />} />
|
|
|
|
</Routes>
|
2020-12-30 21:48:02 +03:00
|
|
|
</Row>
|
2020-12-12 22:45:23 +03:00
|
|
|
</>
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2020-11-14 00:44:26 +03:00
|
|
|
<>
|
2020-09-05 09:49:18 +03:00
|
|
|
{children}
|
|
|
|
|
2021-03-29 22:08:48 +03:00
|
|
|
<section className="mt-3">
|
2020-09-05 09:49:18 +03:00
|
|
|
<div className="row flex-md-row-reverse">
|
|
|
|
<div className="col-lg-7 col-xl-6">
|
2021-03-28 18:45:47 +03:00
|
|
|
<div className="d-md-flex">
|
|
|
|
<div className="flex-fill">
|
|
|
|
<DateRangeSelector
|
2021-12-22 22:23:26 +03:00
|
|
|
updatable
|
2021-03-28 18:45:47 +03:00
|
|
|
disabled={loading}
|
|
|
|
initialDateRange={initialInterval}
|
|
|
|
defaultText="All visits"
|
|
|
|
onDatesChange={setDateRange}
|
|
|
|
/>
|
|
|
|
</div>
|
2021-06-22 21:34:28 +03:00
|
|
|
<VisitsFilterDropdown
|
|
|
|
className="ml-0 ml-md-2 mt-3 mt-md-0"
|
|
|
|
isOrphanVisits={isOrphanVisits}
|
2021-06-22 22:01:20 +03:00
|
|
|
botsSupported={botsSupported}
|
2021-06-22 21:34:28 +03:00
|
|
|
selected={visitsFilter}
|
|
|
|
onChange={setVisitsFilter}
|
|
|
|
/>
|
2021-03-28 18:45:47 +03:00
|
|
|
</div>
|
2020-09-05 09:49:18 +03:00
|
|
|
</div>
|
2020-12-12 22:45:23 +03:00
|
|
|
{visits.length > 0 && (
|
2021-03-29 22:08:48 +03:00
|
|
|
<div className="col-lg-5 col-xl-6 mt-3 mt-lg-0">
|
2021-03-14 15:30:50 +03:00
|
|
|
<div className="d-flex">
|
|
|
|
<Button
|
|
|
|
outline
|
|
|
|
disabled={highlightedVisits.length === 0}
|
|
|
|
className="btn-md-block mr-2"
|
|
|
|
onClick={() => setSelectedVisits([])}
|
|
|
|
>
|
2021-11-01 13:18:09 +03:00
|
|
|
Clear selection {highlightedVisits.length > 0 && <>({prettify(highlightedVisits.length)})</>}
|
2021-03-14 15:30:50 +03:00
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
outline
|
|
|
|
color="primary"
|
|
|
|
className="btn-md-block"
|
|
|
|
onClick={() => exportCsv(normalizedVisits)}
|
|
|
|
>
|
2021-11-01 13:18:09 +03:00
|
|
|
<FontAwesomeIcon icon={faFileDownload} /> Export ({prettify(normalizedVisits.length)})
|
2021-03-14 15:30:50 +03:00
|
|
|
</Button>
|
|
|
|
</div>
|
2020-12-12 22:45:23 +03:00
|
|
|
</div>
|
|
|
|
)}
|
2020-09-05 09:49:18 +03:00
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2021-03-29 22:08:48 +03:00
|
|
|
<section className="mt-3">
|
2020-09-05 09:49:18 +03:00
|
|
|
{renderVisitsContent()}
|
|
|
|
</section>
|
2020-11-14 00:44:26 +03:00
|
|
|
</>
|
2020-09-05 09:49:18 +03:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default VisitsStats;
|