Display allocated ressources to running VMs
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Pierre HUBERT 2024-11-02 16:36:24 +01:00
parent eec6bbb598
commit 91fe291341

View File

@ -7,6 +7,7 @@ import {
TableBody, TableBody,
TableCell, TableCell,
TableContainer, TableContainer,
TableFooter,
TableHead, TableHead,
TableRow, TableRow,
Tooltip, Tooltip,
@ -14,7 +15,7 @@ import {
import { filesize } from "filesize"; import { filesize } from "filesize";
import React from "react"; import React from "react";
import { useNavigate } from "react-router-dom"; import { useNavigate } from "react-router-dom";
import { VMApi, VMInfo } from "../api/VMApi"; import { VMApi, VMInfo, VMState } from "../api/VMApi";
import { AsyncWidget } from "../widgets/AsyncWidget"; import { AsyncWidget } from "../widgets/AsyncWidget";
import { RouterLink } from "../widgets/RouterLink"; import { RouterLink } from "../widgets/RouterLink";
import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer"; import { VirtWebRouteContainer } from "../widgets/VirtWebRouteContainer";
@ -64,6 +65,22 @@ function VMListWidget(p: {
}): React.ReactElement { }): React.ReactElement {
const navigate = useNavigate(); const navigate = useNavigate();
const [runningVMs, setRunningVMs] = React.useState<Set<string>>(new Set());
const updateVMState = (v: VMInfo, s: VMState) => {
const running = s !== "Shutoff";
if (runningVMs.has(v.name) === running) {
return;
}
if (running) runningVMs.add(v.name);
else runningVMs.delete(v.name);
setRunningVMs(new Set([...runningVMs]));
};
console.log(runningVMs);
return ( return (
<TableContainer component={Paper}> <TableContainer component={Paper}>
<Table> <Table>
@ -72,6 +89,7 @@ function VMListWidget(p: {
<TableCell>Name</TableCell> <TableCell>Name</TableCell>
<TableCell>Description</TableCell> <TableCell>Description</TableCell>
<TableCell>Memory</TableCell> <TableCell>Memory</TableCell>
<TableCell>vCPU</TableCell>
<TableCell>Status</TableCell> <TableCell>Status</TableCell>
<TableCell>Actions</TableCell> <TableCell>Actions</TableCell>
</TableRow> </TableRow>
@ -88,9 +106,13 @@ function VMListWidget(p: {
{row.name} {row.name}
</TableCell> </TableCell>
<TableCell>{row.description ?? ""}</TableCell> <TableCell>{row.description ?? ""}</TableCell>
<TableCell>{filesize(row.memory * 1000 * 1000)}</TableCell> <TableCell>{vmMemoryToHuman(row.memory)}</TableCell>
<TableCell>{row.number_vcpu}</TableCell>
<TableCell> <TableCell>
<VMStatusWidget vm={row} /> <VMStatusWidget
vm={row}
onChange={(s) => updateVMState(row, s)}
/>
</TableCell> </TableCell>
<TableCell> <TableCell>
<Tooltip title="View this VM"> <Tooltip title="View this VM">
@ -104,7 +126,35 @@ function VMListWidget(p: {
</TableRow> </TableRow>
))} ))}
</TableBody> </TableBody>
<TableFooter>
<TableRow>
<TableCell></TableCell>
<TableCell></TableCell>
<TableCell>
{vmMemoryToHuman(
p.list
.filter((v) => runningVMs.has(v.name))
.reduce((s, v) => s + v.memory, 0)
)}
{" / "}
{vmMemoryToHuman(p.list.reduce((s, v) => s + v.memory, 0))}
</TableCell>
<TableCell>
{p.list
.filter((v) => runningVMs.has(v.name))
.reduce((s, v) => s + v.number_vcpu, 0)}
{" / "}
{p.list.reduce((s, v) => s + v.number_vcpu, 0)}
</TableCell>
<TableCell></TableCell>
<TableCell></TableCell>
</TableRow>
</TableFooter>
</Table> </Table>
</TableContainer> </TableContainer>
); );
} }
function vmMemoryToHuman(size: number): string {
return filesize(size * 1000 * 1000);
}