umami/components/metrics/RealtimeLog.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-10-09 11:56:15 +02:00
import React, { useMemo } from 'react';
2020-10-10 02:58:27 +02:00
import { FormattedMessage } from 'react-intl';
2020-10-09 13:21:59 +02:00
import { FixedSizeList } from 'react-window';
2020-10-10 02:58:27 +02:00
import classNames from 'classnames';
2020-10-09 11:56:15 +02:00
import firstBy from 'thenby';
import { format } from 'date-fns';
2020-10-09 13:21:59 +02:00
import Icon from 'components/common/Icon';
import Table, { TableRow } from 'components/common/Table';
2020-10-09 21:39:03 +02:00
import Tag from 'components/common/Tag';
2020-10-09 13:21:59 +02:00
import useLocale from 'hooks/useLocale';
import useCountryNames from 'hooks/useCountryNames';
import { BROWSERS } from 'lib/constants';
import Bolt from 'assets/bolt.svg';
import Visitor from 'assets/visitor.svg';
import Eye from 'assets/eye.svg';
2020-10-09 11:56:15 +02:00
import styles from './RealtimeLog.module.css';
2020-10-10 02:58:27 +02:00
const TYPE_PAGEVIEW = 0;
const TYPE_SESSION = 1;
const TYPE_EVENT = 2;
const TYPE_ICONS = {
[TYPE_PAGEVIEW]: <Eye />,
[TYPE_SESSION]: <Visitor />,
[TYPE_EVENT]: <Bolt />,
};
2020-10-09 11:56:15 +02:00
export default function RealtimeLog({ data, websites }) {
const [locale] = useLocale();
const countryNames = useCountryNames(locale);
const logs = useMemo(() => {
const { pageviews, sessions, events } = data;
return [...pageviews, ...sessions, ...events].sort(firstBy('created_at', -1));
}, [data]);
const columns = [
{
key: 'time',
2020-10-10 02:58:27 +02:00
className: classNames(styles.time, 'col-3 col-lg-1'),
render: ({ created_at }) => format(new Date(created_at), 'h:mm:ss'),
2020-10-09 11:56:15 +02:00
},
{
key: 'website',
2020-10-10 02:58:27 +02:00
className: classNames(styles.website, 'col-9 col-lg-2'),
2020-10-09 11:56:15 +02:00
render: getWebsite,
},
{
2020-10-10 02:58:27 +02:00
key: 'detail',
className: classNames(styles.detail, 'col-12 col-lg-9'),
2020-10-09 13:21:59 +02:00
render: row => (
<>
2020-10-10 02:58:27 +02:00
<Icon className={styles.icon} icon={getIcon(row)} />
{getDetail(row)}
2020-10-09 13:21:59 +02:00
</>
),
2020-10-09 11:56:15 +02:00
},
];
function getType({ view_id, session_id, event_id }) {
if (event_id) {
2020-10-10 02:58:27 +02:00
return TYPE_EVENT;
2020-10-09 11:56:15 +02:00
}
if (view_id) {
2020-10-10 02:58:27 +02:00
return TYPE_PAGEVIEW;
2020-10-09 11:56:15 +02:00
}
if (session_id) {
2020-10-10 02:58:27 +02:00
return TYPE_SESSION;
2020-10-09 13:21:59 +02:00
}
return null;
}
2020-10-10 02:58:27 +02:00
function getIcon(row) {
return TYPE_ICONS[getType(row)];
2020-10-09 11:56:15 +02:00
}
function getWebsite({ website_id }) {
return websites.find(n => n.website_id === website_id)?.name;
}
2020-10-10 02:58:27 +02:00
function getDetail({
2020-10-09 11:56:15 +02:00
event_type,
event_value,
view_id,
session_id,
url,
browser,
os,
country,
device,
}) {
if (event_type) {
2020-10-09 13:21:59 +02:00
return (
<div>
2020-10-09 21:39:03 +02:00
<Tag>{event_type}</Tag> {event_value}
2020-10-09 13:21:59 +02:00
</div>
);
2020-10-09 11:56:15 +02:00
}
if (view_id) {
return url;
}
if (session_id) {
return (
<FormattedMessage
id="message.log.visitor"
defaultMessage="Visitor from {country} using {browser} on {os} {device}"
2020-10-09 11:56:15 +02:00
values={{ country: countryNames[country], browser: BROWSERS[browser], os, device }}
/>
);
}
}
2020-10-09 13:21:59 +02:00
const Row = ({ index, style }) => {
return (
<div style={style}>
<TableRow key={index} columns={columns} row={logs[index]} />
</div>
);
};
2020-10-09 11:56:15 +02:00
return (
<div className={styles.log}>
2020-10-10 05:37:24 +02:00
<div className={styles.header}>
<FormattedMessage id="label.realtime-logs" defaultMessage="Realtime logs" />
</div>
2020-10-10 02:58:27 +02:00
<Table
className={styles.table}
bodyClassName={styles.body}
columns={columns}
rows={logs}
showHeader={false}
>
2020-10-10 05:37:24 +02:00
<FixedSizeList height={400} itemCount={logs.length} itemSize={46}>
2020-10-09 13:21:59 +02:00
{Row}
</FixedSizeList>
</Table>
2020-10-09 11:56:15 +02:00
</div>
);
}