mirror of
https://github.com/CyberL1/dlinux-dashboard.git
synced 2025-01-22 01:19:18 -05:00
feat(frontend): move stats to a component, fix CPU usage
This commit is contained in:
parent
9fa7f57a14
commit
8d5a57614e
36
frontend/src/components/ContainerStats.tsx
Normal file
36
frontend/src/components/ContainerStats.tsx
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import { useEffect, useState } from "react";
|
||||||
|
import { ContainerStats as ContainerStatsType } from "dockerode";
|
||||||
|
import { Typography } from "@mui/material";
|
||||||
|
|
||||||
|
export default function ContainerStats({ id }: { id: string }) {
|
||||||
|
const [stats, setStats] = useState<ContainerStatsType>();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const sse = new EventSource(`/api/containers/${id}/stats`);
|
||||||
|
|
||||||
|
sse.onmessage = ({ data }) => {
|
||||||
|
const parsed = JSON.parse(data);
|
||||||
|
setStats(parsed);
|
||||||
|
};
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
sse.close();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
if (!stats) {
|
||||||
|
return "Fetching container stats...";
|
||||||
|
}
|
||||||
|
|
||||||
|
const CPUPercentage =
|
||||||
|
((stats.cpu_stats.cpu_usage.total_usage -
|
||||||
|
stats.precpu_stats.cpu_usage.total_usage) /
|
||||||
|
(stats.cpu_stats.system_cpu_usage -
|
||||||
|
stats.precpu_stats.system_cpu_usage)) *
|
||||||
|
stats.cpu_stats.online_cpus *
|
||||||
|
100;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Typography>CPU Usage: {(CPUPercentage || 0).toFixed(2) + "%"}</Typography>
|
||||||
|
);
|
||||||
|
}
|
@ -1,8 +1,8 @@
|
|||||||
import { useLoaderData, useRevalidator } from "react-router";
|
import { useLoaderData, useRevalidator } from "react-router";
|
||||||
import { Container } from "../../types";
|
import { Container } from "../../types";
|
||||||
import { Button, ButtonGroup, Paper, Typography } from "@mui/material";
|
import { Button, ButtonGroup, Paper, Typography } from "@mui/material";
|
||||||
import { useEffect, useState } from "react";
|
import { useState } from "react";
|
||||||
import { ContainerStats } from "dockerode";
|
import ContainerStats from "../../components/ContainerStats";
|
||||||
|
|
||||||
interface Params {
|
interface Params {
|
||||||
name: string;
|
name: string;
|
||||||
@ -22,24 +22,6 @@ export default function ContainerPage() {
|
|||||||
return "Container not found";
|
return "Container not found";
|
||||||
}
|
}
|
||||||
|
|
||||||
const [stats, setStats] = useState<ContainerStats>();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const statsSource = new EventSource(
|
|
||||||
`/api/containers/${container.name}/stats`,
|
|
||||||
);
|
|
||||||
|
|
||||||
statsSource.onmessage = ({ data }) => {
|
|
||||||
const parsed = JSON.parse(data);
|
|
||||||
setStats(parsed);
|
|
||||||
};
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
statsSource.close();
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
console.log("stats", stats);
|
|
||||||
const revalidator = useRevalidator();
|
const revalidator = useRevalidator();
|
||||||
const [isPowerStateLocked, setPowerStateLocked] = useState<boolean>();
|
const [isPowerStateLocked, setPowerStateLocked] = useState<boolean>();
|
||||||
|
|
||||||
@ -69,7 +51,7 @@ export default function ContainerPage() {
|
|||||||
</Button>
|
</Button>
|
||||||
</ButtonGroup>
|
</ButtonGroup>
|
||||||
</Paper>
|
</Paper>
|
||||||
CPU Usage: {stats?.cpu_stats.cpu_usage.total_usage}
|
<ContainerStats id={container.id} />
|
||||||
</Paper>
|
</Paper>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user