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