2022-12-29 00:43:22 +01:00
|
|
|
import { useState, useEffect, useMemo, useCallback } from 'react';
|
2023-02-15 02:23:20 +01:00
|
|
|
import { useIntl } from 'react-intl';
|
|
|
|
import { subMinutes, startOfMinute, differenceInMinutes } from 'date-fns';
|
2020-10-10 09:07:08 +02:00
|
|
|
import firstBy from 'thenby';
|
2023-02-15 02:23:20 +01:00
|
|
|
import { GridRow, GridColumn } from 'components/layout/Grid';
|
2020-10-09 08:26:05 +02:00
|
|
|
import Page from 'components/layout/Page';
|
2020-10-11 10:33:26 +02:00
|
|
|
import RealtimeChart from 'components/metrics/RealtimeChart';
|
2023-02-15 02:23:20 +01:00
|
|
|
import RealtimeLog from 'components/pages/realtime/RealtimeLog';
|
|
|
|
import RealtimeHeader from 'components/pages/realtime/RealtimeHeader';
|
2020-10-11 10:33:26 +02:00
|
|
|
import WorldMap from 'components/common/WorldMap';
|
|
|
|
import DataTable from 'components/metrics/DataTable';
|
2023-02-15 02:23:20 +01:00
|
|
|
import RealtimeViews from 'components/pages/realtime/RealtimeViews';
|
2022-12-29 00:43:22 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2020-10-10 09:07:08 +02:00
|
|
|
import useLocale from 'hooks/useLocale';
|
|
|
|
import useCountryNames from 'hooks/useCountryNames';
|
2020-10-11 10:33:26 +02:00
|
|
|
import { percentFilter } from 'lib/filters';
|
2023-02-15 02:23:20 +01:00
|
|
|
import { labels } from 'components/messages';
|
2022-05-06 04:04:28 +02:00
|
|
|
import { SHARE_TOKEN_HEADER, REALTIME_RANGE, REALTIME_INTERVAL } from 'lib/constants';
|
2020-10-11 10:33:26 +02:00
|
|
|
import styles from './RealtimeDashboard.module.css';
|
2023-02-15 02:23:20 +01:00
|
|
|
import StickyHeader from 'components/helpers/StickyHeader';
|
|
|
|
import PageHeader from 'components/layout/PageHeader';
|
|
|
|
import ActiveUsers from 'components/metrics/ActiveUsers';
|
2020-10-09 11:56:15 +02:00
|
|
|
|
2020-10-09 19:48:47 +02:00
|
|
|
function mergeData(state, data, time) {
|
|
|
|
const ids = state.map(({ __id }) => __id);
|
|
|
|
return state
|
|
|
|
.concat(data.filter(({ __id }) => !ids.includes(__id)))
|
2022-10-10 22:42:18 +02:00
|
|
|
.filter(({ createdAt }) => new Date(createdAt).getTime() >= time);
|
2020-10-09 08:26:05 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 21:39:03 +02:00
|
|
|
function filterWebsite(data, id) {
|
2022-10-10 22:42:18 +02:00
|
|
|
return data.filter(({ websiteId }) => websiteId === id);
|
2020-10-09 21:39:03 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 08:26:05 +02:00
|
|
|
export default function RealtimeDashboard() {
|
2023-02-15 02:23:20 +01:00
|
|
|
const { formatMessage } = useIntl();
|
2021-06-30 03:41:34 +02:00
|
|
|
const { locale } = useLocale();
|
2020-10-10 09:07:08 +02:00
|
|
|
const countryNames = useCountryNames(locale);
|
2020-10-09 08:26:05 +02:00
|
|
|
const [data, setData] = useState();
|
2023-02-15 02:23:20 +01:00
|
|
|
const [websiteId, setWebsiteId] = useState();
|
2022-12-29 00:43:22 +01:00
|
|
|
const { get, useQuery } = useApi();
|
2023-02-15 02:23:20 +01:00
|
|
|
const { data: websites, isLoading } = useQuery(['websites:me'], () => get('/me/websites'));
|
|
|
|
|
2022-12-29 00:43:22 +01:00
|
|
|
const { data: updates } = useQuery(
|
|
|
|
['realtime:updates'],
|
2023-02-15 02:23:20 +01:00
|
|
|
() => get('/realtime/update', { startAt: data?.timestamp }),
|
2022-12-29 00:43:22 +01:00
|
|
|
{
|
2023-02-15 02:23:20 +01:00
|
|
|
enabled: !!websiteId,
|
2022-12-29 00:43:22 +01:00
|
|
|
retryInterval: REALTIME_INTERVAL,
|
|
|
|
},
|
|
|
|
);
|
2020-10-09 21:39:03 +02:00
|
|
|
|
2020-10-13 01:31:51 +02:00
|
|
|
const renderCountryName = useCallback(
|
|
|
|
({ x }) => <span className={locale}>{countryNames[x]}</span>,
|
|
|
|
[countryNames],
|
|
|
|
);
|
2020-10-10 09:07:08 +02:00
|
|
|
|
2020-10-09 21:39:03 +02:00
|
|
|
const realtimeData = useMemo(() => {
|
2020-10-10 09:07:08 +02:00
|
|
|
if (data) {
|
|
|
|
const { pageviews, sessions, events } = data;
|
|
|
|
|
2022-11-01 07:42:37 +01:00
|
|
|
if (websiteId) {
|
2023-02-15 02:23:20 +01:00
|
|
|
const { id } = websites.find(n => n.id === websiteId);
|
2020-10-10 09:07:08 +02:00
|
|
|
return {
|
2022-10-25 18:24:20 +02:00
|
|
|
pageviews: filterWebsite(pageviews, id),
|
|
|
|
sessions: filterWebsite(sessions, id),
|
|
|
|
events: filterWebsite(events, id),
|
2020-10-10 09:07:08 +02:00
|
|
|
};
|
|
|
|
}
|
2020-10-09 21:39:03 +02:00
|
|
|
}
|
2020-10-10 09:07:08 +02:00
|
|
|
|
2020-10-09 21:39:03 +02:00
|
|
|
return data;
|
2022-11-01 07:42:37 +01:00
|
|
|
}, [data, websiteId]);
|
2020-10-09 08:26:05 +02:00
|
|
|
|
2023-02-15 02:23:20 +01:00
|
|
|
const count = useMemo(() => {
|
|
|
|
if (data) {
|
|
|
|
const { sessions } = data;
|
|
|
|
return sessions.filter(
|
|
|
|
({ createdAt }) => differenceInMinutes(new Date(), new Date(createdAt)) <= 5,
|
|
|
|
).length;
|
|
|
|
}
|
|
|
|
}, [data, websiteId]);
|
|
|
|
|
2020-10-10 09:07:08 +02:00
|
|
|
const countries = useMemo(() => {
|
|
|
|
if (realtimeData?.sessions) {
|
|
|
|
return percentFilter(
|
|
|
|
realtimeData.sessions
|
|
|
|
.reduce((arr, { country }) => {
|
|
|
|
if (country) {
|
|
|
|
const row = arr.find(({ x }) => x === country);
|
|
|
|
|
|
|
|
if (!row) {
|
|
|
|
arr.push({ x: country, y: 1 });
|
|
|
|
} else {
|
|
|
|
row.y += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}, [])
|
|
|
|
.sort(firstBy('y', -1)),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return [];
|
2020-10-11 07:36:55 +02:00
|
|
|
}, [realtimeData?.sessions]);
|
2020-10-10 09:07:08 +02:00
|
|
|
|
2020-10-11 07:36:55 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (updates) {
|
2020-10-09 19:48:47 +02:00
|
|
|
const { pageviews, sessions, events, timestamp } = updates;
|
2020-10-10 09:07:08 +02:00
|
|
|
const time = subMinutes(startOfMinute(new Date()), REALTIME_RANGE).getTime();
|
|
|
|
|
2020-10-09 08:26:05 +02:00
|
|
|
setData(state => ({
|
2020-10-10 09:07:08 +02:00
|
|
|
...state,
|
|
|
|
pageviews: mergeData(state.pageviews, pageviews, time),
|
|
|
|
sessions: mergeData(state.sessions, sessions, time),
|
|
|
|
events: mergeData(state.events, events, time),
|
2020-10-09 19:48:47 +02:00
|
|
|
timestamp,
|
2020-10-09 08:26:05 +02:00
|
|
|
}));
|
|
|
|
}
|
2020-10-11 07:36:55 +02:00
|
|
|
}, [updates]);
|
2020-10-09 08:26:05 +02:00
|
|
|
|
2020-10-09 00:02:48 +02:00
|
|
|
return (
|
2023-02-15 02:23:20 +01:00
|
|
|
<Page loading={isLoading || !websites}>
|
|
|
|
<PageHeader title={formatMessage(labels.realtime)}>
|
|
|
|
<ActiveUsers value={count} />
|
|
|
|
</PageHeader>
|
|
|
|
<StickyHeader stickyClassName={styles.sticky}>
|
|
|
|
<RealtimeHeader
|
|
|
|
websites={websites}
|
|
|
|
websiteId={websiteId}
|
|
|
|
data={{ ...realtimeData, countries }}
|
|
|
|
onSelect={setWebsiteId}
|
|
|
|
/>
|
|
|
|
</StickyHeader>
|
2020-10-10 02:58:27 +02:00
|
|
|
<div className={styles.chart}>
|
2022-10-25 18:24:20 +02:00
|
|
|
<RealtimeChart data={realtimeData} unit="minute" records={REALTIME_RANGE} />
|
2020-10-10 02:58:27 +02:00
|
|
|
</div>
|
2023-02-15 02:23:20 +01:00
|
|
|
<GridRow>
|
|
|
|
<GridColumn xs={12} sm={12} md={12} lg={4} xl={4}>
|
2023-02-12 07:10:14 +01:00
|
|
|
<RealtimeViews websiteId={websiteId} data={realtimeData} websites={websites} />
|
2023-02-15 02:23:20 +01:00
|
|
|
</GridColumn>
|
|
|
|
<GridColumn xs={12} sm={12} md={12} lg={8} xl={8}>
|
2023-02-12 07:10:14 +01:00
|
|
|
<RealtimeLog websiteId={websiteId} data={realtimeData} websites={websites} />
|
2023-02-15 02:23:20 +01:00
|
|
|
</GridColumn>
|
|
|
|
</GridRow>
|
|
|
|
<GridRow>
|
|
|
|
<GridColumn xs={12} lg={4}>
|
2023-02-12 07:10:14 +01:00
|
|
|
<DataTable
|
2023-02-15 02:23:20 +01:00
|
|
|
title={formatMessage(labels.countries)}
|
|
|
|
metric={formatMessage(labels.visitors)}
|
2023-02-12 07:10:14 +01:00
|
|
|
data={countries}
|
|
|
|
renderLabel={renderCountryName}
|
|
|
|
/>
|
2023-02-15 02:23:20 +01:00
|
|
|
</GridColumn>
|
|
|
|
<GridColumn xs={12} lg={8}>
|
2023-02-12 07:10:14 +01:00
|
|
|
<WorldMap data={countries} />
|
2023-02-15 02:23:20 +01:00
|
|
|
</GridColumn>
|
|
|
|
</GridRow>
|
2020-10-09 00:02:48 +02:00
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|