owncast/web/pages/viewer-info.tsx

162 lines
3.9 KiB
TypeScript
Raw Normal View History

2020-10-23 03:16:28 +03:00
import React, { useState, useEffect, useContext } from 'react';
2020-10-28 10:53:24 +03:00
import { timeFormat } from "d3-time-format";
import { Table, Row } from "antd";
import { formatDistanceToNow } from "date-fns";
import { UserOutlined} from "@ant-design/icons";
2020-10-28 10:53:24 +03:00
import Chart from "./components/chart";
import StatisticItem from "./components/statistic";
2020-10-23 03:16:28 +03:00
import { BroadcastStatusContext } from './utils/broadcast-status-context';
import {
CONNECTED_CLIENTS,
STREAM_STATUS, VIEWERS_OVER_TIME,
fetchData,
} from "./utils/apis";
2020-10-08 10:17:40 +03:00
const FETCH_INTERVAL = 5 * 60 * 1000; // 5 mins
export default function ViewersOverTime() {
2020-10-23 03:16:28 +03:00
const context = useContext(BroadcastStatusContext);
const { broadcastActive } = context || {};
const [viewerInfo, setViewerInfo] = useState([]);
const [clients, setClients] = useState([]);
const [stats, setStats] = useState(null);
2020-10-08 10:17:40 +03:00
const getInfo = async () => {
try {
const result = await fetchData(VIEWERS_OVER_TIME);
setViewerInfo(result);
2020-10-08 10:17:40 +03:00
} catch (error) {
console.log("==== error", error);
}
try {
const result = await fetchData(CONNECTED_CLIENTS);
console.log("result", result);
setClients(result);
} catch (error) {
console.log("==== error", error);
2020-10-08 10:17:40 +03:00
}
try {
const result = await fetchData(STREAM_STATUS);
setStats(result);
} catch (error) {
console.log(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-10-23 03:16:28 +03:00
if (broadcastActive) {
getStatusIntervalId = setInterval(getInfo, FETCH_INTERVAL);
// returned function will be called on component unmount
2020-10-23 03:16:28 +03:00
return () => {
clearInterval(getStatusIntervalId);
};
2020-10-08 10:17:40 +03:00
}
return () => [];
2020-10-08 10:17:40 +03:00
}, []);
2020-10-23 03:16:28 +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) {
return "no info";
}
2020-10-08 10:17:40 +03:00
const columns = [
{
title: "User name",
dataIndex: "username",
key: "username",
render: (username) => username || "-",
sorter: (a, b) => a.username - b.username,
sortDirections: ["descend", "ascend"],
},
{
title: "Messages sent",
dataIndex: "messageCount",
key: "messageCount",
sorter: (a, b) => a.messageCount - b.messageCount,
sortDirections: ["descend", "ascend"],
},
{
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}`,
},
];
const timeFormatter = (tick) => {
return timeFormat("%H:%M")(new Date(tick));
};
2020-10-12 07:46:07 +03:00
2020-10-21 11:19:29 +03:00
const CustomizedTooltip = (props) => {
const { active, payload, label } = props;
if (active) {
const numViewers = payload && payload[0] && payload[0].value;
const time = timeFormatter(label);
const message = `${numViewers} viewer(s) at ${time}`;
return (
<div className="custom-tooltip">
<p className="label">{message}</p>
</div>
);
}
return null;
};
/*
geo data looks like this
"geo": {
"countryCode": "US",
"regionName": "California",
"timeZone": "America/Los_Angeles"
}
*/
2020-10-08 10:17:40 +03:00
return (
2020-10-21 11:19:29 +03:00
<div>
<h2>Current Viewers</h2>
<Row gutter={[16, 16]}>
<StatisticItem
title="Current viewers"
value={stats?.viewerCount ?? ""}
prefix={<UserOutlined />}
/>
<StatisticItem
title="Peak viewers this session"
value={stats?.sessionMaxViewerCount ?? ""}
prefix={<UserOutlined />}
/>
</Row>
2020-10-21 11:19:29 +03:00
<div className="chart-container">
2020-10-27 09:53:04 +03:00
<Chart data={viewerInfo} color="#ff84d8" unit="" />
<div>
<Table dataSource={clients} columns={columns} />;
</div>
2020-10-08 10:17:40 +03:00
</div>
</div>
);
}