2020-11-01 08:01:37 +01:00
|
|
|
/* eslint-disable react/prop-types */
|
2020-10-23 02:16:28 +02:00
|
|
|
import React, { useState, useEffect, useContext } from 'react';
|
2020-10-29 02:36:25 +01:00
|
|
|
import { Table, Row } from "antd";
|
|
|
|
import { formatDistanceToNow } from "date-fns";
|
|
|
|
import { UserOutlined} from "@ant-design/icons";
|
2020-11-01 08:01:37 +01:00
|
|
|
import { SortOrder } from "antd/lib/table/interface";
|
2020-10-28 08:53:24 +01:00
|
|
|
import Chart from "./components/chart";
|
2020-10-29 02:36:25 +01:00
|
|
|
import StatisticItem from "./components/statistic";
|
|
|
|
|
2020-11-06 03:30:14 +01:00
|
|
|
import { ServerStatusContext } from '../utils/server-status-context';
|
2020-10-23 01:18:18 +02:00
|
|
|
|
2020-10-29 02:36:25 +01:00
|
|
|
import {
|
|
|
|
CONNECTED_CLIENTS,
|
2020-11-06 03:30:14 +01:00
|
|
|
VIEWERS_OVER_TIME,
|
2020-10-29 02:36:25 +01:00
|
|
|
fetchData,
|
2020-11-01 08:01:37 +01:00
|
|
|
} from "../utils/apis";
|
2020-10-08 09:17:40 +02:00
|
|
|
|
2020-10-12 04:46:48 +02:00
|
|
|
const FETCH_INTERVAL = 5 * 60 * 1000; // 5 mins
|
|
|
|
|
|
|
|
export default function ViewersOverTime() {
|
2020-11-06 03:30:14 +01:00
|
|
|
const context = useContext(ServerStatusContext);
|
|
|
|
const {
|
|
|
|
broadcastActive,
|
|
|
|
viewerCount,
|
|
|
|
overallPeakViewerCount,
|
|
|
|
sessionPeakViewerCount,
|
|
|
|
} = context || {};
|
2020-10-23 02:16:28 +02:00
|
|
|
|
2020-10-12 04:46:48 +02:00
|
|
|
const [viewerInfo, setViewerInfo] = useState([]);
|
2020-10-29 02:36:25 +01:00
|
|
|
const [clients, setClients] = useState([]);
|
2020-10-08 09:17:40 +02:00
|
|
|
|
|
|
|
const getInfo = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchData(VIEWERS_OVER_TIME);
|
2020-10-12 04:46:48 +02:00
|
|
|
setViewerInfo(result);
|
2020-10-08 09:17:40 +02:00
|
|
|
} catch (error) {
|
2020-10-29 02:36:25 +01:00
|
|
|
console.log("==== error", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await fetchData(CONNECTED_CLIENTS);
|
|
|
|
setClients(result);
|
|
|
|
} catch (error) {
|
|
|
|
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();
|
2020-10-23 02:16:28 +02:00
|
|
|
if (broadcastActive) {
|
|
|
|
getStatusIntervalId = setInterval(getInfo, FETCH_INTERVAL);
|
2020-10-29 02:36:25 +01:00
|
|
|
// returned function will be called on component unmount
|
2020-10-23 02:16:28 +02:00
|
|
|
return () => {
|
|
|
|
clearInterval(getStatusIntervalId);
|
2020-10-29 02:36:25 +01:00
|
|
|
};
|
2020-10-08 09:17:40 +02:00
|
|
|
}
|
2020-10-29 02:36:25 +01:00
|
|
|
|
|
|
|
return () => [];
|
2020-10-08 09:17:40 +02:00
|
|
|
}, []);
|
2020-10-23 02:16:28 +02:00
|
|
|
|
2020-10-29 02:36:25 +01: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) {
|
|
|
|
return "no info";
|
|
|
|
}
|
2020-10-08 09:17:40 +02:00
|
|
|
|
2020-10-29 02:36:25 +01:00
|
|
|
const columns = [
|
|
|
|
{
|
|
|
|
title: "User name",
|
|
|
|
dataIndex: "username",
|
|
|
|
key: "username",
|
|
|
|
render: (username) => username || "-",
|
|
|
|
sorter: (a, b) => a.username - b.username,
|
2020-11-01 08:01:37 +01:00
|
|
|
sortDirections: ["descend", "ascend"] as SortOrder[],
|
2020-10-29 02:36:25 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Messages sent",
|
|
|
|
dataIndex: "messageCount",
|
|
|
|
key: "messageCount",
|
|
|
|
sorter: (a, b) => a.messageCount - b.messageCount,
|
2020-11-01 08:01:37 +01:00
|
|
|
sortDirections: ["descend", "ascend"] as SortOrder[],
|
2020-10-29 02:36:25 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Connected Time",
|
|
|
|
dataIndex: "connectedAt",
|
|
|
|
key: "connectedAt",
|
|
|
|
render: (time) => formatDistanceToNow(new Date(time)),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "User Agent",
|
|
|
|
dataIndex: "userAgent",
|
|
|
|
key: "userAgent",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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>
|
|
|
|
<h2>Current Viewers</h2>
|
2020-10-29 02:36:25 +01:00
|
|
|
<Row gutter={[16, 16]}>
|
|
|
|
<StatisticItem
|
|
|
|
title="Current viewers"
|
2020-11-06 03:30:14 +01:00
|
|
|
value={viewerCount.toString()}
|
2020-10-29 02:36:25 +01:00
|
|
|
prefix={<UserOutlined />}
|
|
|
|
/>
|
|
|
|
<StatisticItem
|
|
|
|
title="Peak viewers this session"
|
2020-11-06 03:30:14 +01:00
|
|
|
value={sessionPeakViewerCount.toString()}
|
|
|
|
prefix={<UserOutlined />}
|
|
|
|
/>
|
|
|
|
<StatisticItem
|
|
|
|
title="Peak viewers overall"
|
|
|
|
value={overallPeakViewerCount.toString()}
|
2020-10-29 02:36:25 +01:00
|
|
|
prefix={<UserOutlined />}
|
|
|
|
/>
|
|
|
|
</Row>
|
2020-10-21 10:19:29 +02:00
|
|
|
<div className="chart-container">
|
2020-10-27 07:53:04 +01:00
|
|
|
<Chart data={viewerInfo} color="#ff84d8" unit="" />
|
2020-10-08 09:17:40 +02:00
|
|
|
</div>
|
2020-11-03 05:49:52 +01:00
|
|
|
<Table dataSource={clients} columns={columns} />;
|
2020-10-08 09:17:40 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|