0
owncast/web/pages/hardware-info.tsx

119 lines
3.1 KiB
TypeScript
Raw Normal View History

2020-11-01 00:01:37 -07:00
/* eslint-disable no-array-constructor */
2020-11-07 23:00:02 -08:00
import { BulbOutlined, LaptopOutlined, SaveOutlined } from "@ant-design/icons";
2020-11-01 00:01:37 -07:00
import { Row } from "antd";
2020-11-07 23:00:02 -08:00
import React, { useEffect, useState } from 'react';
import { fetchData, FETCH_INTERVAL, HARDWARE_STATS } from '../utils/apis';
2020-10-28 00:53:24 -07:00
import Chart from './components/chart';
import StatisticItem from "./components/statistic";
2020-10-08 00:17:40 -07:00
2020-11-01 00:01:37 -07:00
interface TimedValue {
time: Date,
value: Number
}
2020-10-08 00:17:40 -07:00
export default function HardwareInfo() {
2020-10-28 00:53:24 -07:00
const [hardwareStatus, setHardwareStatus] = useState({
2020-11-01 00:01:37 -07:00
cpu: Array<TimedValue>(),
memory: Array<TimedValue>(),
disk: Array<TimedValue>(),
message: "",
2020-10-28 00:53:24 -07:00
});
2020-10-08 00:17:40 -07:00
const getHardwareStatus = async () => {
try {
const result = await fetchData(HARDWARE_STATS);
setHardwareStatus({ ...result });
} catch (error) {
setHardwareStatus({ ...hardwareStatus, message: error.message });
}
};
useEffect(() => {
let getStatusIntervalId = null;
getHardwareStatus();
2020-10-26 23:53:04 -07:00
getStatusIntervalId = setInterval(getHardwareStatus, FETCH_INTERVAL); // runs every 1 min.
2020-10-08 00:17:40 -07:00
// returned function will be called on component unmount
return () => {
clearInterval(getStatusIntervalId);
}
}, []);
2020-10-26 23:53:04 -07:00
if (!hardwareStatus.cpu) {
return null;
}
const currentCPUUsage = hardwareStatus.cpu[hardwareStatus.cpu.length - 1]?.value;
const currentRamUsage =
hardwareStatus.memory[hardwareStatus.memory.length - 1]?.value;
const currentDiskUsage =
hardwareStatus.disk[hardwareStatus.disk.length - 1]?.value;
const series = [
{
name: "CPU",
color: "#FF7700",
data: hardwareStatus.cpu,
},
{
name: "Memory",
color: "#004777",
data: hardwareStatus.memory,
},
{
name: "Disk",
color: "#A9E190",
data: hardwareStatus.disk,
},
];
2020-10-26 23:53:04 -07:00
return (
<div>
<div>
<h2>Hardware Info</h2>
<Row gutter={[16, 16]}>
<StatisticItem
title="CPU"
value={`${currentCPUUsage}`}
prefix={<LaptopOutlined style={{color: '#FF7700' }}/>}
color="#FF7700"
progress
/>
<StatisticItem
title="RAM"
value={`${currentRamUsage}`}
prefix={<BulbOutlined style={{color: '#004777' }} />}
color="#004777"
progress
/>
<StatisticItem
title="Disk"
value={`${currentDiskUsage}`}
prefix={<SaveOutlined style={{color: '#A9E190' }} />}
color="#A9E190"
progress
/>
</Row>
2020-10-26 23:53:04 -07:00
<div className="chart-container">
<Chart dataCollections={series} color="#FF7700" unit="%" />
2020-10-26 23:53:04 -07:00
</div>
</div>
<p>cpu:[], disk: [], memory: []; value = %age.</p>
<p>the times should be the same for each, though milliseconds differ</p>
<div
style={{
border: "1px solid blue",
height: "300px",
width: "100%",
overflow: "auto",
}}
>
{JSON.stringify(hardwareStatus)}
</div>
2020-10-08 00:17:40 -07:00
</div>
2020-10-26 23:53:04 -07:00
);
}