umami/components/pages/RealtimeDashboard.js

186 lines
5.6 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect, useMemo, useCallback } from 'react';
2020-10-09 11:56:15 +02:00
import { subMinutes, startOfMinute } from 'date-fns';
import firstBy from 'thenby';
import { percentFilter } from 'lib/filters';
2020-10-09 08:26:05 +02:00
import Page from 'components/layout/Page';
2020-10-10 05:37:24 +02:00
import GridLayout, { GridRow, GridColumn } from 'components/layout/GridLayout';
2020-10-09 11:56:15 +02:00
import RealtimeChart from '../metrics/RealtimeChart';
import RealtimeLog from '../metrics/RealtimeLog';
2020-10-10 02:58:27 +02:00
import styles from './RealtimeDashboard.module.css';
import RealtimeHeader from '../metrics/RealtimeHeader';
2020-10-10 05:37:24 +02:00
import useFetch from 'hooks/useFetch';
import WorldMap from '../common/WorldMap';
import DataTable from '../metrics/DataTable';
import useLocale from 'hooks/useLocale';
import useCountryNames from 'hooks/useCountryNames';
import { FormattedMessage } from 'react-intl';
2020-10-09 11:56:15 +02:00
const REALTIME_RANGE = 30;
2020-10-10 10:16:28 +02:00
const REALTIME_INTERVAL = 55000;
2020-10-09 08:26:05 +02:00
function mergeData(state, data, time) {
const ids = state.map(({ __id }) => __id);
return state
.concat(data.filter(({ __id }) => !ids.includes(__id)))
.filter(({ created_at }) => new Date(created_at).getTime() >= time);
2020-10-09 08:26:05 +02:00
}
2020-10-09 21:39:03 +02:00
function filterWebsite(data, id) {
return data.filter(({ website_id }) => website_id === id);
}
2020-10-09 08:26:05 +02:00
export default function RealtimeDashboard() {
const [locale] = useLocale();
const countryNames = useCountryNames(locale);
2020-10-09 08:26:05 +02:00
const [data, setData] = useState();
2020-10-10 02:58:27 +02:00
const [websiteId, setWebsiteId] = useState(0);
2020-10-09 21:39:03 +02:00
const { data: init, loading } = useFetch('/api/realtime', { params: { type: 'init' } });
const { data: updates } = useFetch('/api/realtime', {
params: { type: 'update', start_at: data?.timestamp },
2020-10-10 02:58:27 +02:00
disabled: !init?.websites?.length || !data,
2020-10-09 21:39:03 +02:00
interval: REALTIME_INTERVAL,
headers: { 'x-umami-token': init?.token },
});
const renderCountryName = useCallback(({ x }) => countryNames[x], []);
2020-10-09 21:39:03 +02:00
const realtimeData = useMemo(() => {
if (data) {
const { pageviews, sessions, events } = data;
if (websiteId) {
return {
pageviews: filterWebsite(pageviews, websiteId),
sessions: filterWebsite(sessions, websiteId),
events: filterWebsite(events, websiteId),
};
}
2020-10-09 21:39:03 +02:00
}
2020-10-09 21:39:03 +02:00
return data;
2020-10-10 02:58:27 +02:00
}, [data, websiteId]);
2020-10-09 08:26:05 +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 [];
}, [realtimeData]);
const referrers = useMemo(() => {
if (realtimeData?.pageviews) {
return percentFilter(
realtimeData.pageviews
.reduce((arr, { referrer }) => {
if (referrer?.startsWith('http')) {
const { hostname } = new URL(referrer);
const row = arr.find(({ x }) => x === hostname);
if (!row) {
arr.push({ x: hostname, y: 1 });
} else {
row.y += 1;
}
}
return arr;
}, [])
.sort(firstBy('y', -1)),
);
}
return [];
}, [realtimeData]);
2020-10-09 08:26:05 +02:00
useEffect(() => {
if (init && !data) {
const { websites, data } = init;
const domains = init.websites.map(({ domain }) => domain);
setData({ websites, domains, ...data });
2020-10-09 08:26:05 +02:00
} else if (updates) {
const { pageviews, sessions, events, timestamp } = updates;
const time = subMinutes(startOfMinute(new Date()), REALTIME_RANGE).getTime();
2020-10-09 08:26:05 +02:00
setData(state => ({
...state,
pageviews: mergeData(state.pageviews, pageviews, time),
sessions: mergeData(state.sessions, sessions, time),
events: mergeData(state.events, events, time),
timestamp,
2020-10-09 08:26:05 +02:00
}));
}
2020-10-09 11:56:15 +02:00
}, [init, updates]);
2020-10-09 08:26:05 +02:00
if (!init || loading || !data) {
return null;
}
const { websites } = data;
//console.log({realtimeData, countries});
2020-10-09 08:26:05 +02:00
return (
<Page>
2020-10-10 02:58:27 +02:00
<RealtimeHeader
websites={websites}
websiteId={websiteId}
data={{ ...realtimeData, countries }}
2020-10-10 02:58:27 +02:00
onSelect={setWebsiteId}
2020-10-09 21:39:03 +02:00
/>
2020-10-10 02:58:27 +02:00
<div className={styles.chart}>
<RealtimeChart
websiteId={websiteId}
data={realtimeData}
unit="minute"
records={REALTIME_RANGE}
/>
</div>
2020-10-10 05:37:24 +02:00
<GridLayout>
<GridRow>
<GridColumn xs={12} lg={4}>
<DataTable
title={<FormattedMessage id="metrics.referrers" defaultMessage="Referrers" />}
metric={<FormattedMessage id="metrics.views" defaultMessage="Views" />}
data={referrers}
2020-10-10 10:16:28 +02:00
height={400}
animate={false}
/>
2020-10-10 05:37:24 +02:00
</GridColumn>
2020-10-10 10:16:28 +02:00
<GridColumn xs={12} lg={8}>
<RealtimeLog data={realtimeData} websites={websites} />
</GridColumn>
2020-10-10 05:37:24 +02:00
</GridRow>
<GridRow>
<GridColumn xs={12} lg={4}>
<DataTable
title={<FormattedMessage id="metrics.countries" defaultMessage="Countries" />}
metric={<FormattedMessage id="metrics.visitors" defaultMessage="Visitors" />}
data={countries}
renderLabel={renderCountryName}
2020-10-10 10:16:28 +02:00
height={500}
animate={false}
/>
2020-10-10 05:37:24 +02:00
</GridColumn>
<GridColumn xs={12} lg={8}>
<WorldMap data={countries} />
2020-10-10 05:37:24 +02:00
</GridColumn>
</GridRow>
</GridLayout>
</Page>
);
}