owncast/web/pages/admin/viewer-info.tsx

150 lines
4.7 KiB
TypeScript
Raw Normal View History

2020-10-23 03:16:28 +03:00
import React, { useState, useEffect, useContext } from 'react';
import { Row, Col, Typography, Menu, Dropdown, Spin, Alert } from 'antd';
import { DownOutlined, UserOutlined } from '@ant-design/icons';
import { getUnixTime, sub } from 'date-fns';
reafctor: normalize component formatting (#2082) * refactor: move/rename BanUserButton file * refactor: move/rename Chart file * refactor: update generic component filenames to PascalCase * refactor: update config component filenames to PascalCase * refactor: update AdminLayout component filename to PascalCase * refactor: update/move VideoJS component * chore(eslint): disable bad react/require-default-props rule * refactor: normalize ActionButton component * refactor: normalize ActionButtonRow component * refactor: normalize FollowButton component * refactor: normalize NotifyButton component * refactor: normalize ChatActionMessage component * refactor: normalize ChatContainer component * refactor: normalize ChatJoinMessage component * refactor: normalize ChatModerationActionMenu component * refactor: normalize ChatModerationDetailsModal component * refactor: normalize ChatModeratorNotification component * refactor: normalize ChatSocialMessage component * refactor: normalize ChatSystemMessage component * refactor: normalize ChatTextField component * refactor: normalize ChatUserBadge component * refactor: normalize ChatUserMessage component * refactor: normalize ContentHeader component * refactor: normalize OwncastLogo component * refactor: normalize UserDropdown component * chore(eslint): modify react/function-component-definition rule * refactor: normalize CodecSelector component * refactor: update a bunch of functional components using eslint * refactor: update a bunch of functional components using eslint, pt2 * refactor: update a bunch of functional components using eslint, pt3 * refactor: replace all component->component default imports with named imports * refactor: replace all component-stories->component default imports with named imports * refactor: remove default exports from most components * chore(eslint): add eslint config files for the components and pages dirs * fix: use-before-define error in ChatContainer * Fix ChatContainer import * Only process .tsx files in Next builds Co-authored-by: Gabe Kangas <gabek@real-ity.com>
2022-09-07 10:00:28 +03:00
import { Chart } from '../../components/Chart';
import { StatisticItem } from '../../components/StatisticItem';
import { ViewerTable } from '../../components/ViewerTable';
import { ServerStatusContext } from '../../utils/server-status-context';
import { VIEWERS_OVER_TIME, ACTIVE_VIEWER_DETAILS, fetchData } from '../../utils/apis';
2020-10-08 10:17:40 +03:00
const FETCH_INTERVAL = 60 * 1000; // 1 min
export default function ViewersOverTime() {
const context = useContext(ServerStatusContext);
const { online, broadcaster, viewerCount, overallPeakViewerCount, sessionPeakViewerCount } =
context || {};
let streamStart;
if (broadcaster && broadcaster.time) {
streamStart = new Date(broadcaster.time);
}
const times = [
{ title: 'Current stream', start: streamStart },
{ title: 'Last 12 hours', start: sub(new Date(), { hours: 12 }) },
{ title: 'Last 24 hours', start: sub(new Date(), { hours: 24 }) },
{ title: 'Last 7 days', start: sub(new Date(), { days: 7 }) },
{ title: 'Last 30 days', start: sub(new Date(), { days: 30 }) },
{ title: 'Last 3 months', start: sub(new Date(), { months: 3 }) },
{ title: 'Last 6 months', start: sub(new Date(), { months: 6 }) },
];
2020-10-23 03:16:28 +03:00
const [loadingChart, setLoadingChart] = useState(true);
const [viewerInfo, setViewerInfo] = useState([]);
const [viewerDetails, setViewerDetails] = useState([]);
const [timeWindowStart, setTimeWindowStart] = useState(times[1]);
2020-10-08 10:17:40 +03:00
const getInfo = async () => {
try {
const url = `${VIEWERS_OVER_TIME}?windowStart=${getUnixTime(timeWindowStart.start)}`;
const result = await fetchData(url);
setViewerInfo(result);
setLoadingChart(false);
2020-10-08 10:17:40 +03:00
} catch (error) {
2021-01-31 12:38:20 +03:00
console.log('==== error', error);
}
try {
const result = await fetchData(ACTIVE_VIEWER_DETAILS);
setViewerDetails(result);
} catch (error) {
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();
if (online) {
2020-10-23 03:16:28 +03:00
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 () => [];
}, [online, timeWindowStart]);
2020-10-23 03:16:28 +03:00
const onTimeWindowSelect = ({ key }) => {
setTimeWindowStart(times[key]);
};
const menu = (
<Menu>
{online && streamStart && (
<Menu.Item key="0" onClick={onTimeWindowSelect}>
{times[0].title}
</Menu.Item>
)}
{times.slice(1).map((time, index) => (
// The array is hard coded, so it's safe to use the index as a key.
// eslint-disable-next-line react/no-array-index-key
<Menu.Item key={index + 1} onClick={onTimeWindowSelect}>
{time.title}
</Menu.Item>
))}
</Menu>
);
2020-10-08 10:17:40 +03:00
return (
<>
<Typography.Title>Viewer Info</Typography.Title>
<br />
2020-11-24 08:13:30 +03:00
<Row gutter={[16, 16]} justify="space-around">
{online && (
<Col span={8} md={8}>
<StatisticItem
title="Current viewers"
value={viewerCount.toString()}
prefix={<UserOutlined />}
/>
</Col>
)}
<Col md={online ? 8 : 12}>
<StatisticItem
title={online ? 'Max viewers this stream' : 'Max viewers last stream'}
value={sessionPeakViewerCount.toString()}
prefix={<UserOutlined />}
/>
</Col>
<Col md={online ? 8 : 12}>
<StatisticItem
title="All-time max viewers"
value={overallPeakViewerCount.toString()}
prefix={<UserOutlined />}
/>
</Col>
</Row>
{!viewerInfo.length && (
<Alert
style={{ marginTop: '10px' }}
banner
message="Please wait"
description="No viewer data has been collected yet."
type="info"
/>
)}
2021-02-14 12:30:42 +03:00
<Spin spinning={!viewerInfo.length || loadingChart}>
<Dropdown overlay={menu} trigger={['click']}>
<button
type="button"
style={{ float: 'right', background: 'transparent', border: 'unset' }}
>
{timeWindowStart.title} <DownOutlined />
</button>
</Dropdown>
{viewerInfo.length > 0 && (
<Chart title="Viewers" data={viewerInfo} color="#2087E2" unit="" />
)}
<ViewerTable data={viewerDetails} />
</Spin>
</>
2020-10-08 10:17:40 +03:00
);
}