mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 15:36:18 -05:00
115 lines
3.5 KiB
JavaScript
115 lines
3.5 KiB
JavaScript
|
|
import {Header, Loader, Progress, Statistic, StatisticGroup} from "../Theme";
|
||
|
|
import React from "react";
|
||
|
|
import {humanFileSize, LoadStatistic} from "../Common";
|
||
|
|
import {getStatus} from "../../api";
|
||
|
|
import {ThemeContext} from "../../contexts/contexts";
|
||
|
|
import {Container} from "semantic-ui-react";
|
||
|
|
|
||
|
|
function DriveInfo({used, size, percent, mount}) {
|
||
|
|
let color;
|
||
|
|
if (percent >= 90) {
|
||
|
|
color = 'red';
|
||
|
|
} else if (percent >= 80) {
|
||
|
|
color = 'orange';
|
||
|
|
}
|
||
|
|
return <Progress progress
|
||
|
|
percent={percent}
|
||
|
|
label={`${mount} ${humanFileSize(used)} of ${humanFileSize(size)}`}
|
||
|
|
color={color}
|
||
|
|
key={mount}
|
||
|
|
/>
|
||
|
|
}
|
||
|
|
|
||
|
|
function CPUTemperatureStatistic({value}) {
|
||
|
|
if (!value) {
|
||
|
|
return <Statistic label='Temp C°' value='?'/>
|
||
|
|
}
|
||
|
|
let color;
|
||
|
|
if (value >= 75) {
|
||
|
|
color = 'red';
|
||
|
|
} else if (value >= 50) {
|
||
|
|
color = 'yellow';
|
||
|
|
}
|
||
|
|
return <Statistic label='Temp C°' value={value} color={color}/>
|
||
|
|
}
|
||
|
|
|
||
|
|
export function CPUUsageProgress({value, label}) {
|
||
|
|
if (value === null) {
|
||
|
|
return <Progress progress={0} color='grey' label='Average CPU Usage ERROR' disabled/>
|
||
|
|
}
|
||
|
|
|
||
|
|
let color = 'black';
|
||
|
|
if (value >= 90) {
|
||
|
|
color = 'red';
|
||
|
|
} else if (value >= 70) {
|
||
|
|
color = 'brown';
|
||
|
|
} else if (value >= 50) {
|
||
|
|
color = 'orange';
|
||
|
|
} else if (value >= 30) {
|
||
|
|
color = 'green';
|
||
|
|
}
|
||
|
|
return <Progress percent={value} progress color={color} label={label}/>
|
||
|
|
}
|
||
|
|
|
||
|
|
export class Status extends React.Component {
|
||
|
|
|
||
|
|
constructor(props) {
|
||
|
|
super(props);
|
||
|
|
this.state = {
|
||
|
|
status: null,
|
||
|
|
}
|
||
|
|
this.fetchStatus = this.fetchStatus.bind(this);
|
||
|
|
}
|
||
|
|
|
||
|
|
async componentDidMount() {
|
||
|
|
await this.fetchStatus();
|
||
|
|
this.intervalId = setInterval(this.fetchStatus, 1000 * 5);
|
||
|
|
}
|
||
|
|
|
||
|
|
componentWillUnmount() {
|
||
|
|
clearInterval(this.intervalId);
|
||
|
|
}
|
||
|
|
|
||
|
|
fetchStatus = async () => {
|
||
|
|
try {
|
||
|
|
let status = await getStatus();
|
||
|
|
this.setState({status: status});
|
||
|
|
} catch (e) {
|
||
|
|
console.error(e);
|
||
|
|
this.setState({status: null});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
render() {
|
||
|
|
if (this.state.status) {
|
||
|
|
const {cpu_info, load, drives} = this.state.status;
|
||
|
|
const {minute_1, minute_5, minute_15} = load;
|
||
|
|
const {percent, cores, temperature} = cpu_info;
|
||
|
|
|
||
|
|
return <ThemeContext.Consumer>
|
||
|
|
{({i}) => <>
|
||
|
|
<CPUUsageProgress value={percent} label='CPU Usage'/>
|
||
|
|
|
||
|
|
<CPUTemperatureStatistic value={temperature}/>
|
||
|
|
<Statistic label='Cores' value={cores || '?'}/>
|
||
|
|
<StatisticGroup>
|
||
|
|
<LoadStatistic label='1 Minute Load' value={minute_1} cores={cores}/>
|
||
|
|
<LoadStatistic label='5 Minute Load' value={minute_5} cores={cores}/>
|
||
|
|
<LoadStatistic label='15 Minute Load' value={minute_15} cores={cores}/>
|
||
|
|
</StatisticGroup>
|
||
|
|
|
||
|
|
<Header as='h2'>Drive Usage</Header>
|
||
|
|
{drives.map((drive) => <DriveInfo key={drive['mount']} {...drive}/>)}
|
||
|
|
</>}
|
||
|
|
</ThemeContext.Consumer>
|
||
|
|
}
|
||
|
|
|
||
|
|
return <ThemeContext.Consumer>
|
||
|
|
{({i}) => <Container fluid style={{marginBottom: '5em'}}>
|
||
|
|
<Loader active inline='centered'>Loading system status</Loader>;
|
||
|
|
</Container>
|
||
|
|
}
|
||
|
|
</ThemeContext.Consumer>
|
||
|
|
}
|
||
|
|
}
|