owncast/web/pages/viewer-info.tsx

120 lines
3.4 KiB
TypeScript
Raw Normal View History

2020-10-23 02:16:28 +02:00
import React, { useState, useEffect, useContext } from 'react';
2021-01-31 10:38:20 +01:00
import { Table, Row } from 'antd';
import { formatDistanceToNow } from 'date-fns';
import { UserOutlined } from '@ant-design/icons';
import { SortOrder } from 'antd/lib/table/interface';
import Chart from './components/chart';
import StatisticItem from './components/statistic';
import { ServerStatusContext } from '../utils/server-status-context';
2021-01-31 10:38:20 +01:00
import { CONNECTED_CLIENTS, VIEWERS_OVER_TIME, fetchData } from '../utils/apis';
2020-10-08 09:17:40 +02:00
const FETCH_INTERVAL = 60 * 1000; // 1 min
export default function ViewersOverTime() {
const context = useContext(ServerStatusContext);
2021-01-31 10:38:20 +01:00
const { online, viewerCount, overallPeakViewerCount, sessionPeakViewerCount } = context || {};
2020-10-23 02:16:28 +02:00
const [viewerInfo, setViewerInfo] = useState([]);
const [clients, setClients] = useState([]);
2020-10-08 09:17:40 +02:00
const getInfo = async () => {
try {
const result = await fetchData(VIEWERS_OVER_TIME);
setViewerInfo(result);
2020-10-08 09:17:40 +02:00
} catch (error) {
2021-01-31 10:38:20 +01:00
console.log('==== error', error);
}
try {
const result = await fetchData(CONNECTED_CLIENTS);
setClients(result);
} catch (error) {
2021-01-31 10:38:20 +01:00
console.log('==== error', error);
2020-10-08 09:17:40 +02:00
}
};
2020-10-23 02:16:28 +02:00
2020-10-08 09:17:40 +02:00
useEffect(() => {
let getStatusIntervalId = null;
getInfo();
if (online) {
2020-10-23 02:16:28 +02:00
getStatusIntervalId = setInterval(getInfo, FETCH_INTERVAL);
// returned function will be called on component unmount
2020-10-23 02:16:28 +02:00
return () => {
clearInterval(getStatusIntervalId);
};
2020-10-08 09:17:40 +02:00
}
return () => [];
}, [online]);
2020-10-23 02:16:28 +02:00
// todo - check to see if broadcast active has changed. if so, start polling.
2020-10-23 02:16:28 +02:00
if (!viewerInfo.length) {
2021-01-31 10:38:20 +01:00
return 'no info';
2020-10-23 02:16:28 +02:00
}
2020-10-08 09:17:40 +02:00
const columns = [
{
2021-01-31 10:38:20 +01:00
title: 'User name',
dataIndex: 'username',
key: 'username',
render: username => username || '-',
sorter: (a, b) => a.username - b.username,
2021-01-31 10:38:20 +01:00
sortDirections: ['descend', 'ascend'] as SortOrder[],
},
{
2021-01-31 10:38:20 +01:00
title: 'Messages sent',
dataIndex: 'messageCount',
key: 'messageCount',
sorter: (a, b) => a.messageCount - b.messageCount,
2021-01-31 10:38:20 +01:00
sortDirections: ['descend', 'ascend'] as SortOrder[],
},
{
2021-01-31 10:38:20 +01:00
title: 'Connected Time',
dataIndex: 'connectedAt',
key: 'connectedAt',
render: time => formatDistanceToNow(new Date(time)),
2020-12-29 10:13:39 +01:00
sorter: (a, b) => new Date(a.connectedAt).getTime() - new Date(b.connectedAt).getTime(),
2021-01-31 10:38:20 +01:00
sortDirections: ['descend', 'ascend'] as SortOrder[],
},
{
2021-01-31 10:38:20 +01:00
title: 'User Agent',
dataIndex: 'userAgent',
key: 'userAgent',
},
{
2021-01-31 10:38:20 +01:00
title: 'Location',
dataIndex: 'geo',
key: 'geo',
render: geo => (geo ? `${geo.regionName}, ${geo.countryCode}` : '-'),
},
];
2020-10-08 09:17:40 +02:00
return (
2020-10-21 10:19:29 +02:00
<div>
2020-11-24 06:13:30 +01:00
<Row gutter={[16, 16]} justify="space-around">
<StatisticItem
title="Current viewers"
value={viewerCount.toString()}
prefix={<UserOutlined />}
/>
<StatisticItem
title="Peak viewers this session"
value={sessionPeakViewerCount.toString()}
prefix={<UserOutlined />}
/>
<StatisticItem
title="Peak viewers overall"
value={overallPeakViewerCount.toString()}
prefix={<UserOutlined />}
/>
</Row>
2020-11-29 03:14:08 +01:00
<Chart title="Viewers" data={viewerInfo} color="#2087E2" unit="" />
2021-01-31 10:38:20 +01:00
<Table dataSource={clients} columns={columns} rowKey={row => row.clientID} />
2020-10-08 09:17:40 +02:00
</div>
);
}