diff --git a/src/app/applications/page.tsx b/src/app/applications/page.tsx index b363fba..83489e1 100644 --- a/src/app/applications/page.tsx +++ b/src/app/applications/page.tsx @@ -20,9 +20,10 @@ export default function ApplicationsPage() { }; fetch(); - }, []); - console.log(applications); + const iid = setInterval(fetch, 3000); + return () => clearInterval(iid); + }, []); return ( CPU
- {app.cpu}% + + {app.cpu.toFixed(1)}% +
@@ -88,7 +91,9 @@ export default function ApplicationsPage() {
- {app.memory}% + + {app.memory.toFixed(1)}% +
diff --git a/src/app/containers/page.tsx b/src/app/containers/page.tsx index 5b5e2aa..a548500 100644 --- a/src/app/containers/page.tsx +++ b/src/app/containers/page.tsx @@ -35,7 +35,7 @@ type ContainerSummary = { status: "running" | "stopped"; uptime: string; cpu: number; - memory: string; + memory: number; }; async function summarizeContainer( @@ -56,12 +56,6 @@ async function summarizeContainer( const stats = await getContainerStats(container.Id); - // 5. CPU and memory - const cpu = stats?.cpu_stats?.cpu_usage?.total_usage || 0; - const memory = `${formatBytes( - stats?.memory_stats?.usage || 0 - )} / ${formatBytes(stats?.memory_stats?.max_usage || 0)}`; - return { id: shortId, name, @@ -69,8 +63,8 @@ async function summarizeContainer( node: nodeName, status, uptime, - cpu, - memory, + cpu: stats?.cpu || 0, + memory: stats?.memory || 0, }; } @@ -194,18 +188,18 @@ export default function ContainersPage() { {container.node} - {/* {container.status} */} + {container.status} {container.uptime} - {container.cpu} + {container.cpu.toFixed(1)}% - {container.memory} + {container.memory.toFixed(1)}% @@ -249,7 +243,7 @@ export default function ContainersPage() { {container.name} - {/* {container.status} */} + {container.status}
@@ -271,11 +265,13 @@ export default function ContainersPage() {
CPU -
{container.cpu}
+
{container.cpu.toFixed(1)}%
Memory -
{container.memory}
+
+ {container.memory.toFixed(1)}% +
diff --git a/src/app/monitoring/page.tsx b/src/app/monitoring/page.tsx index baa6c34..dc873c9 100644 --- a/src/app/monitoring/page.tsx +++ b/src/app/monitoring/page.tsx @@ -11,8 +11,8 @@ import { useIsMobile } from "@/hooks/use-mobile"; import RadialChart from "@/components/RadialChart"; import { getLiveNodes, getNodes } from "@/core/node"; import { average } from "@/lib/format"; -import { ContainerStats } from "dockerode"; import { getContainerStats, getDocker } from "@/core/docker"; +import { SimpleStats } from "@/lib/types"; const cpuChartConfig = { cpu: { label: "CPU Usage", color: "var(--chart-1)" }, @@ -43,7 +43,7 @@ export default function MonitoringPage() { ramUsage: number; }>(); const [containerStats, SetContainerStats] = useState< - (ContainerStats & { name: string })[] + (SimpleStats & { name: string })[] >([]); useEffect(() => { @@ -89,7 +89,7 @@ export default function MonitoringPage() { }) ) ) - ).filter((s): s is ContainerStats & { name: string } => s !== undefined) + ).filter((s): s is SimpleStats & { name: string } => s !== undefined) ); console.log(containerStats); @@ -207,31 +207,20 @@ export default function MonitoringPage() {
CPU - - {container?.cpu_stats?.cpu_usage?.total_usage}% - + {container.cpu.toFixed(1)}%
- +
Memory - {container?.memory_stats?.usage}% + {container.memory.toFixed(1)}%
- +
Network - - {container?.networks && - container?.networks[0]?.tx_packets} - +
diff --git a/src/core/docker.ts b/src/core/docker.ts index bf8e6c7..89f4fbd 100644 --- a/src/core/docker.ts +++ b/src/core/docker.ts @@ -1,9 +1,8 @@ "use server"; import axios from "axios"; -import { Docker, Node } from "@/lib/types"; +import { Docker, Node, SimpleStats } from "@/lib/types"; import { verifySession } from "./auth/session"; -import { ContainerStats } from "dockerode"; import { getNodes } from "./node"; export async function getDocker(nodes: Node[]): Promise { @@ -24,7 +23,7 @@ export async function getDocker(nodes: Node[]): Promise { export async function getContainerStats( containerId: string -): Promise { +): Promise { const nodes = await getNodes(); return ( diff --git a/src/core/server/application.ts b/src/core/server/application.ts index ca27bd5..a5c23ea 100644 --- a/src/core/server/application.ts +++ b/src/core/server/application.ts @@ -1,12 +1,13 @@ "use server"; -import Docker from "dockerode"; +import Docker, { Container, ContainerStats } from "dockerode"; import { randomUUID } from "crypto"; import fs from "fs"; import path from "path"; import { exec } from "child_process"; import util from "util"; import { ImageApp } from "@/lib/types"; +import { CPUusage } from "@/lib/server/calc"; const docker = new Docker(); const execAsync = util.promisify(exec); @@ -42,6 +43,9 @@ export async function getApplications(): Promise { if (containerInfo.State === "running") { const stats = await container.stats({ stream: false }); + + totalMem += (stats.memory_stats.usage / stats.memory_stats.limit) * 100; + totalCpu += CPUusage(stats); } } @@ -62,8 +66,8 @@ export async function getApplications(): Promise { image: imageName, containers: relatedContainers.length, replicas: `${running}/${relatedContainers.length}`, - cpu: Math.round(totalCpu), - memory: Math.round(totalMem), + cpu: totalCpu, + memory: totalMem, network: "—", status: running > 0 ? ("running" as const) : ("stopped" as const), ports: [...ports], diff --git a/src/core/server/docker.ts b/src/core/server/docker.ts index b1e9f20..a08c1ad 100644 --- a/src/core/server/docker.ts +++ b/src/core/server/docker.ts @@ -1,6 +1,8 @@ "use server"; -import Docker, { ContainerStats } from "dockerode"; +import { CPUusage } from "@/lib/server/calc"; +import { SimpleStats } from "@/lib/types"; +import Docker from "dockerode"; const docker = new Docker(); @@ -12,8 +14,13 @@ export async function getDocker() { export async function getContainerStats( containerId: string -): Promise { - return await docker.getContainer(containerId).stats(); +): Promise { + const stats = await docker.getContainer(containerId).stats({ stream: false }); + + return { + cpu: CPUusage(stats), + memory: (stats.memory_stats.usage / stats.memory_stats.limit) * 100, + }; } export async function startContainer(containerId: string) { diff --git a/src/lib/format.ts b/src/lib/format.ts index 55d6ae0..7749bec 100644 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -14,4 +14,4 @@ export function average(values: Array, p: (v: T) => number) { return values.length > 0 ? values.reduce((sum, value) => sum + p(value), 0) / values.length : 0; -} \ No newline at end of file +} diff --git a/src/lib/types.ts b/src/lib/types.ts index 163958e..d91f342 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -69,3 +69,8 @@ export interface ImageApp { status: "running" | "stopped"; ports: string[]; } + +export interface SimpleStats { + cpu: number; + memory: number; +}