-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRunCommand.jsx
More file actions
71 lines (61 loc) · 1.8 KB
/
Copy pathRunCommand.jsx
File metadata and controls
71 lines (61 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Box } from '@material-ui/core';
import React, { useEffect, useState } from 'react';
import { Command } from './CommandWidgets';
import LoadChartWidget from './LoadChartWidget';
const uptime_cmd = 'uptime';
const graph_title = 'Load average';
export default function RunCommand(props) {
const [state, setstate] = useState({
labels: [],
data: [[], [], []],
output: '',
});
function extractLoadFromString(str = '') {
let list = str.split(',').map(ele => {
return ele.trim();
});
let one_min = parseFloat(list[2].split(':')[1]);
let five_min = parseFloat(list[3]);
let fifteen_min = parseFloat(list[4]);
return {
y_value: [one_min, five_min, fifteen_min],
original_str: str,
};
}
function updateData(new_data) {
setstate({
data: [[new_data.y_value[0]], [new_data.y_value[1]], [new_data.y_value[2]]],
output: new_data.original_str,
});
}
useEffect(() => {
const callback = () =>
exec(props.command)
.then(result => {
if (props.showGraph) updateData(extractLoadFromString(result[0]));
else
setstate(prevState => ({
...prevState,
output: result[0],
}));
})
.catch(err => console.log(err[1]));
const interval_handle = setInterval(callback, props.interval);
callback();
return () => clearInterval(interval_handle);
}, [props.command]);
return (
<div>
{props.showGraph !== undefined && props.command === uptime_cmd ? (
<Box
style={{
padding: '1.5rem',
}}
>
<LoadChartWidget data={state.data} title={graph_title} />
</Box>
) : null}
<Command command={props.command} output={state.output} children={props.children} />
</div>
);
}