mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 23:27:12 +01:00
RealtimeLog component.
This commit is contained in:
parent
db9b238585
commit
b682e41aff
6 changed files with 163 additions and 123 deletions
|
|
@ -1,80 +1,42 @@
|
|||
import React from 'react';
|
||||
import { useIntl } from 'react-intl';
|
||||
import tinycolor from 'tinycolor2';
|
||||
import BarChart from './BarChart';
|
||||
import useTheme from 'hooks/useTheme';
|
||||
import { THEME_COLORS } from 'lib/constants';
|
||||
import React, { useMemo } from 'react';
|
||||
import { format, parseISO, startOfMinute, subMinutes } from 'date-fns';
|
||||
import PageviewsChart from './PageviewsChart';
|
||||
import { getDateArray } from 'lib/date';
|
||||
|
||||
export default function RealtimeChart({ websiteId, data, unit, records, className, loading }) {
|
||||
const intl = useIntl();
|
||||
const [theme] = useTheme();
|
||||
const primaryColor = tinycolor(THEME_COLORS[theme].primary);
|
||||
const colors = {
|
||||
views: {
|
||||
background: primaryColor.setAlpha(0.4).toRgbString(),
|
||||
border: primaryColor.setAlpha(0.5).toRgbString(),
|
||||
},
|
||||
visitors: {
|
||||
background: primaryColor.setAlpha(0.6).toRgbString(),
|
||||
border: primaryColor.setAlpha(0.7).toRgbString(),
|
||||
},
|
||||
};
|
||||
function mapData(data) {
|
||||
let last = 0;
|
||||
const arr = [];
|
||||
|
||||
const handleUpdate = chart => {
|
||||
const {
|
||||
data: { datasets },
|
||||
} = chart;
|
||||
data.reduce((obj, val) => {
|
||||
const { created_at } = val;
|
||||
const t = startOfMinute(parseISO(created_at));
|
||||
if (t.getTime() > last) {
|
||||
obj = { t: format(t, 'yyyy-LL-dd HH:mm:00'), y: 1 };
|
||||
arr.push(obj);
|
||||
last = t;
|
||||
} else {
|
||||
obj.y += 1;
|
||||
}
|
||||
return obj;
|
||||
}, {});
|
||||
|
||||
datasets[0].data = data.uniques;
|
||||
datasets[0].label = intl.formatMessage({
|
||||
id: 'metrics.unique-visitors',
|
||||
defaultMessage: 'Unique visitors',
|
||||
});
|
||||
datasets[1].data = data.pageviews;
|
||||
datasets[1].label = intl.formatMessage({
|
||||
id: 'metrics.page-views',
|
||||
defaultMessage: 'Page views',
|
||||
});
|
||||
|
||||
chart.update();
|
||||
};
|
||||
|
||||
if (!data) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<BarChart
|
||||
className={className}
|
||||
chartId={`realtime-${websiteId}`}
|
||||
datasets={[
|
||||
{
|
||||
label: intl.formatMessage({
|
||||
id: 'metrics.unique-visitors',
|
||||
defaultMessage: 'Unique visitors',
|
||||
}),
|
||||
data: data.uniques,
|
||||
lineTension: 0,
|
||||
backgroundColor: colors.visitors.background,
|
||||
borderColor: colors.visitors.border,
|
||||
borderWidth: 1,
|
||||
},
|
||||
{
|
||||
label: intl.formatMessage({
|
||||
id: 'metrics.page-views',
|
||||
defaultMessage: 'Page views',
|
||||
}),
|
||||
data: data.pageviews,
|
||||
lineTension: 0,
|
||||
backgroundColor: colors.views.background,
|
||||
borderColor: colors.views.border,
|
||||
borderWidth: 1,
|
||||
},
|
||||
]}
|
||||
unit={unit}
|
||||
records={records}
|
||||
onUpdate={handleUpdate}
|
||||
loading={loading}
|
||||
/>
|
||||
);
|
||||
return arr;
|
||||
}
|
||||
|
||||
export default function RealtimeChart({ data, ...props }) {
|
||||
const chartData = useMemo(() => {
|
||||
if (data) {
|
||||
const endDate = startOfMinute(new Date());
|
||||
const startDate = subMinutes(endDate, 30);
|
||||
const unit = 'minute';
|
||||
|
||||
return {
|
||||
pageviews: getDateArray(mapData(data.pageviews), startDate, endDate, unit),
|
||||
sessions: getDateArray(mapData(data.sessions), startDate, endDate, unit),
|
||||
};
|
||||
}
|
||||
return { pageviews: [], sessions: [] };
|
||||
}, [data]);
|
||||
|
||||
return <PageviewsChart {...props} data={chartData} />;
|
||||
}
|
||||
|
|
|
|||
95
components/metrics/RealtimeLog.js
Normal file
95
components/metrics/RealtimeLog.js
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
import React, { useMemo } from 'react';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import firstBy from 'thenby';
|
||||
import { format } from 'date-fns';
|
||||
import Table from 'components/common/Table';
|
||||
import styles from './RealtimeLog.module.css';
|
||||
import useLocale from '../../hooks/useLocale';
|
||||
import useCountryNames from '../../hooks/useCountryNames';
|
||||
import { BROWSERS } from '../../lib/constants';
|
||||
|
||||
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',
|
||||
label: <FormattedMessage id="label.type" defaultMessage="Time" />,
|
||||
className: 'col',
|
||||
render: ({ created_at }) => format(new Date(created_at), 'H:mm:ss'),
|
||||
},
|
||||
{
|
||||
key: 'website',
|
||||
label: <FormattedMessage id="label.website" defaultMessage="Website" />,
|
||||
className: 'col',
|
||||
render: getWebsite,
|
||||
},
|
||||
{
|
||||
key: 'type',
|
||||
label: <FormattedMessage id="label.type" defaultMessage="Type" />,
|
||||
className: 'col',
|
||||
render: getType,
|
||||
},
|
||||
{
|
||||
key: 'type',
|
||||
className: 'col',
|
||||
render: getDescription,
|
||||
},
|
||||
];
|
||||
|
||||
function getType({ view_id, session_id, event_id }) {
|
||||
if (event_id) {
|
||||
return <FormattedMessage id="label.event" defaultMessage="Event" />;
|
||||
}
|
||||
if (view_id) {
|
||||
return <FormattedMessage id="label.pageview" defaultMessage="Pageview" />;
|
||||
}
|
||||
if (session_id) {
|
||||
return <FormattedMessage id="label.visitor" defaultMessage="Visitor" />;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getWebsite({ website_id }) {
|
||||
return websites.find(n => n.website_id === website_id)?.name;
|
||||
}
|
||||
|
||||
function getDescription({
|
||||
event_type,
|
||||
event_value,
|
||||
view_id,
|
||||
session_id,
|
||||
url,
|
||||
browser,
|
||||
os,
|
||||
country,
|
||||
device,
|
||||
}) {
|
||||
if (event_type) {
|
||||
return `${event_type}:${event_value}`;
|
||||
}
|
||||
if (view_id) {
|
||||
return url;
|
||||
}
|
||||
if (session_id) {
|
||||
return (
|
||||
<FormattedMessage
|
||||
id="message.log.visitor"
|
||||
defaultMessage="A visitor from {country} using {browser} on {os} {device}"
|
||||
values={{ country: countryNames[country], browser: BROWSERS[browser], os, device }}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={styles.log}>
|
||||
<Table className={styles.table} columns={columns} rows={logs} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
8
components/metrics/RealtimeLog.module.css
Normal file
8
components/metrics/RealtimeLog.module.css
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
.table {
|
||||
font-size: var(--font-size-small);
|
||||
}
|
||||
|
||||
.row {
|
||||
display: flex;
|
||||
border-bottom: 1px solid var(--gray300);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue