umami/components/pages/RealtimeDashboard.js

82 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-10-09 11:56:15 +02:00
import React, { useState, useEffect } from 'react';
2020-10-09 08:26:05 +02:00
import { FormattedMessage } from 'react-intl';
2020-10-09 11:56:15 +02:00
import { subMinutes, startOfMinute } from 'date-fns';
2020-10-09 08:26:05 +02:00
import Page from 'components/layout/Page';
import PageHeader from 'components/layout/PageHeader';
import DropDown from 'components/common/DropDown';
import useFetch from 'hooks/useFetch';
2020-10-09 11:56:15 +02:00
import RealtimeChart from '../metrics/RealtimeChart';
import RealtimeLog from '../metrics/RealtimeLog';
const REALTIME_RANGE = 30;
const REALTIME_INTERVAL = 5000;
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
}
export default function RealtimeDashboard() {
const [data, setData] = useState();
const [website, setWebsite] = useState();
2020-10-09 08:26:05 +02:00
const { data: init, loading } = useFetch('/api/realtime', { type: 'init' });
const { data: updates } = useFetch(
'/api/realtime',
{ type: 'update', start_at: data?.timestamp },
2020-10-09 10:04:06 +02:00
{
disabled: !init?.token || !data,
2020-10-09 11:56:15 +02:00
interval: REALTIME_INTERVAL,
2020-10-09 10:04:06 +02:00
headers: { 'x-umami-token': init?.token },
},
2020-10-09 08:26:05 +02:00
);
useEffect(() => {
if (init && !data) {
setData(init.data);
} else if (updates) {
const { pageviews, sessions, events, timestamp } = updates;
2020-10-09 11:56:15 +02:00
const minTime = subMinutes(startOfMinute(new Date()), REALTIME_RANGE).getTime();
2020-10-09 08:26:05 +02:00
setData(state => ({
pageviews: mergeData(state.pageviews, pageviews, minTime),
sessions: mergeData(state.sessions, sessions, minTime),
events: mergeData(state.events, events, minTime),
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;
}
2020-10-09 08:26:05 +02:00
const { websites } = init;
const options = [
{ label: <FormattedMessage id="label.all-websites" defaultMessage="All websites" />, value: 0 },
].concat(websites.map(({ name, website_id }) => ({ label: name, value: website_id })));
const selectedValue = options.find(({ value }) => value === website?.website_id)?.value || 0;
function handleSelect(value) {
2020-10-09 08:26:05 +02:00
setWebsite(websites.find(({ website_id }) => website_id === value));
}
return (
<Page>
<PageHeader>
2020-10-09 08:26:05 +02:00
<div>
<FormattedMessage id="label.realtime" defaultMessage="Realtime" />
</div>
<DropDown value={selectedValue} options={options} onChange={handleSelect} />
</PageHeader>
2020-10-09 11:56:15 +02:00
<RealtimeChart websiteId={website?.website_id} data={data} unit="minute" records={30} />
<div className="row">
<div className="col-12">
<RealtimeLog data={data} websites={websites} />
</div>
</div>
</Page>
);
}