2020-10-08 09:17:40 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-10-12 04:46:48 +02:00
|
|
|
import { VIEWERS_OVER_TIME, fetchData } 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() {
|
|
|
|
const [viewerInfo, setViewerInfo] = useState([]);
|
2020-10-08 09:17:40 +02:00
|
|
|
|
|
|
|
const getInfo = async () => {
|
|
|
|
try {
|
|
|
|
const result = await fetchData(VIEWERS_OVER_TIME);
|
|
|
|
console.log("viewers result", result)
|
|
|
|
|
2020-10-12 04:46:48 +02:00
|
|
|
setViewerInfo(result);
|
2020-10-08 09:17:40 +02:00
|
|
|
|
|
|
|
} catch (error) {
|
2020-10-12 04:46:48 +02:00
|
|
|
console.log("==== error", error)
|
|
|
|
// setViewerInfo({ ...viewerInfo, message: error.message });
|
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 04:46:48 +02:00
|
|
|
|
|
|
|
const formattedData = viewerInfo.map(viewer => ({
|
|
|
|
x: (new Date(viewer.time)).toLocaleTimeString(),
|
|
|
|
y: viewer.value,
|
|
|
|
}));
|
|
|
|
|
2020-10-08 09:17:40 +02:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h2>Viewers over time</h2>
|
2020-10-12 04:46:48 +02:00
|
|
|
<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 04:46:48 +02:00
|
|
|
{JSON.stringify(formattedData)}
|
2020-10-08 09:17:40 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|