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

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2020-10-08 09:17:40 +02:00
import React, { useState, useEffect } from 'react';
2020-10-12 06:46:07 +02:00
import {timeFormat} from 'd3-time-format';
import { LineChart, XAxis, YAxis, Line, Tooltip } from 'recharts';
import { VIEWERS_OVER_TIME, fetchData } from '../utils/apis';
2020-10-08 09:17:40 +02:00
const FETCH_INTERVAL = 5 * 60 * 1000; // 5 mins
export default function ViewersOverTime() {
const [viewerInfo, setViewerInfo] = 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) {
console.log("==== error", error)
2020-10-08 09:17:40 +02:00
}
};
useEffect(() => {
let getStatusIntervalId = null;
getInfo();
getStatusIntervalId = setInterval(getInfo, FETCH_INTERVAL);
// returned function will be called on component unmount
return () => {
clearInterval(getStatusIntervalId);
}
}, []);
2020-10-12 06:46:07 +02:00
const timeFormatter = (tick) => {return timeFormat('%H:%M:%S')(new Date(tick));};
2020-10-08 09:17:40 +02:00
return (
2020-10-12 06:46:07 +02:00
<div style={{backgroundColor: '#333'}}>
2020-10-08 09:17:40 +02:00
<h2>Viewers over time</h2>
<p>Time on X axis, # Viewer on Y</p>
2020-10-08 09:17:40 +02:00
<div style={{border: '1px solid red', height: '300px', width: '100%', overflow:'auto'}}>
2020-10-12 06:46:07 +02:00
{JSON.stringify(viewerInfo)}
2020-10-08 09:17:40 +02:00
</div>
2020-10-12 06:46:07 +02:00
<LineChart width={800} height={400} data={viewerInfo}>
<XAxis dataKey="time" tickFormatter={timeFormatter}/>
<YAxis dataKey="value"/>
<Tooltip cursor={{ stroke: 'red', strokeWidth: 2 }} />
<Line type="monotone" dataKey="value" stroke="#ff84d8" dot={{ stroke: 'red', strokeWidth: 2 }} />
</LineChart>
2020-10-08 09:17:40 +02:00
</div>
);
}