Skip to content

Modern Docker Management, Designed for Everyone

feat: system usage meters

Kyle Mendell64dde28ef34d
diff --git a/package-lock.json b/package-lock.json
index 6f3cff920..b4c604427 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -50,7 +50,7 @@
"@types/dockerode-compose": "^1.4.1",
"@types/js-yaml": "^4.0.9",
"@types/proper-lockfile": "^4.1.4",
- "bits-ui": "^2.2.0",
+ "bits-ui": "^2.4.1",
"clsx": "^2.1.1",
"eslint": "^9.27.0",
"eslint-plugin-svelte": "^3.8.1",
@@ -3425,9 +3425,9 @@
}
},
"node_modules/bits-ui": {
- "version": "2.2.0",
- "resolved": "https://registry.npmjs.org/bits-ui/-/bits-ui-2.2.0.tgz",
- "integrity": "sha512-Jo3PIWpMMAeT4rs5f3X3S5Qdu1MWyjz3YV5DhTJRtLI3UZn8A5YkZyHbIaPsAkKIxjLMNAqAa2FAMfDJ8DdXjw==",
+ "version": "2.4.1",
+ "resolved": "https://registry.npmjs.org/bits-ui/-/bits-ui-2.4.1.tgz",
+ "integrity": "sha512-Z0qZkPgtxP5dkEOyZqCK2PQ1oFXsD0AlfFMtsrMLJqaYtjUBoQGosHVKEH03M9q4G8kxVnG74Zb0F9pS0X4+6w==",
"dev": true,
"license": "MIT",
"dependencies": {
diff --git a/package.json b/package.json
index 25654411f..e4de26740 100644
--- a/package.json
+++ b/package.json
@@ -34,7 +34,7 @@
"@types/dockerode-compose": "^1.4.1",
"@types/js-yaml": "^4.0.9",
"@types/proper-lockfile": "^4.1.4",
- "bits-ui": "^2.2.0",
+ "bits-ui": "^2.4.1",
"clsx": "^2.1.1",
"eslint": "^9.27.0",
"eslint-plugin-svelte": "^3.8.1",
@@ -69,8 +69,8 @@
"date-fns": "^4.1.0",
"dockerode": "^4.0.6",
"dockerode-compose": "^1.4.0",
- "drizzle-kit": "^0.31.1",
"dotenv": "^16.5.0",
+ "drizzle-kit": "^0.31.1",
"drizzle-orm": "^0.44.1",
"js-yaml": "^4.1.0",
"nanoid": "^5.1.5",
diff --git a/src/lib/components/meter.svelte b/src/lib/components/meter.svelte
new file mode 100644
index 000000000..f1d39a3d0
--- /dev/null
+++ b/src/lib/components/meter.svelte
@@ -0,0 +1,81 @@
+<script lang="ts">
+ import { Meter, useId } from "bits-ui";
+ import { cn } from "$lib/utils";
+ import type { ComponentProps } from "svelte";
+
+ let {
+ max = 100,
+ value = 0,
+ min = 0,
+ label,
+ valueLabel,
+ showLabel = true,
+ showValueLabel = true,
+ variant = "default",
+ size = "default",
+ class: className = "",
+ ...restProps
+ }: ComponentProps<typeof Meter.Root> & {
+ label?: string;
+ valueLabel?: string;
+ showLabel?: boolean;
+ showValueLabel?: boolean;
+ variant?: "default" | "success" | "warning" | "destructive";
+ size?: "sm" | "default" | "lg";
+ class?: string;
+ } = $props();
+
+ const labelId = useId();
+
+ const variantStyles = {
+ default: "bg-accent",
+ success: "bg-green-500",
+ warning: "bg-amber-500",
+ destructive: "bg-destructive",
+ };
+
+ const sizeStyles = {
+ sm: "h-1.5",
+ default: "h-2.5",
+ lg: "h-4",
+ };
+
+ const percentage = $derived(Math.round(((value - min) / (max - min)) * 100));
+</script>
+
+{#if showLabel && (label || valueLabel)}
+ <div class="flex items-center justify-between text-sm mb-2">
+ {#if showLabel && label}
+ <span id={labelId} class="font-medium text-foreground">{label}</span>
+ {/if}
+ {#if showValueLabel && valueLabel}
+ <span class="text-muted-foreground">{valueLabel}</span>
+ {/if}
+ </div>
+{/if}
+
+<div
+ class={cn(
+ "relative w-full bg-secondary rounded-full overflow-hidden",
+ sizeStyles[size],
+ className
+ )}
+>
+ <Meter.Root
+ aria-labelledby={label ? labelId : undefined}
+ aria-valuetext={valueLabel}
+ {value}
+ {min}
+ {max}
+ class="w-full h-full"
+ {...restProps}
+ >
+ <div
+ class={cn(
+ "h-full rounded-full transition-all duration-300 ease-in-out",
+ variantStyles[variant]
+ )}
+ style="width: {percentage}%"
+ ></div>
+ </Meter.Root>
+</div>
diff --git a/src/lib/icons/docker-icon.svelte b/src/lib/icons/docker-icon.svelte
new file mode 100644
index 000000000..cc3624d63
--- /dev/null
+++ b/src/lib/icons/docker-icon.svelte
@@ -0,0 +1,14 @@
+<script lang="ts">
+ let {
+ class: className
+ }: {
+ class?: string;
+ } = $props();
+</script>
+
+<svg fill="currentColor" width="24" height="24" viewBox="0 0 24 24" role="img" xmlns="http://www.w3.org/2000/svg" class={className}>
+ <title>Docker icon</title>
+ <path
+ d="M13.983 11.078h2.119a.186.186 0 00.186-.185V9.006a.186.186 0 00-.186-.186h-2.119a.185.185 0 00-.185.185v1.888c0 .102.083.185.185.185m-2.954-5.43h2.118a.186.186 0 00.186-.186V3.574a.186.186 0 00-.186-.185h-2.118a.185.185 0 00-.185.185v1.888c0 .102.082.185.185.185m0 2.716h2.118a.187.187 0 00.186-.186V6.29a.186.186 0 00-.186-.185h-2.118a.185.185 0 00-.185.185v1.887c0 .102.082.185.185.186m-2.93 0h2.12a.186.186 0 00.184-.186V6.29a.185.185 0 00-.185-.185H8.1a.185.185 0 00-.185.185v1.887c0 .102.083.185.185.186m-2.964 0h2.119a.186.186 0 00.185-.186V6.29a.185.185 0 00-.185-.185H5.136a.186.186 0 00-.186.185v1.887c0 .102.084.185.186.186m5.893 2.715h2.118a.186.186 0 00.186-.185V9.006a.186.186 0 00-.186-.186h-2.118a.185.185 0 00-.185.185v1.888c0 .102.082.185.185.185m-2.93 0h2.12a.185.185 0 00.184-.185V9.006a.185.185 0 00-.184-.186h-2.12a.185.185 0 00-.184.185v1.888c0 .102.083.185.185.185m-2.964 0h2.119a.185.185 0 00.185-.185V9.006a.185.185 0 00-.184-.186h-2.12a.186.186 0 00-.186.186v1.887c0 .102.084.185.186.185m-2.92 0h2.12a.185.185 0 00.184-.185V9.006a.185.185 0 00-.184-.186h-2.12a.185.185 0 00-.184.185v1.888c0 .102.082.185.185.185M23.763 9.89c-.065-.051-.672-.51-1.954-.51-.338.001-.676.03-1.01.087-.248-1.7-1.653-2.53-1.716-2.566l-.344-.199-.226.327c-.284.438-.49.922-.612 1.43-.23.97-.09 1.882.403 2.661-.595.332-1.55.413-1.744.42H.751a.751.751 0 00-.75.748 11.376 11.376 0 00.692 4.062c.545 1.428 1.355 2.48 2.41 3.124 1.18.723 3.1 1.137 5.275 1.137.983.003 1.963-.086 2.93-.266a12.248 12.248 0 003.823-1.389c.98-.567 1.86-1.288 2.61-2.136 1.252-1.418 1.998-2.997 2.553-4.4h.221c1.372 0 2.215-.549 2.68-1.009.309-.293.55-.65.707-1.046l.098-.288Z"
+ />
+</svg>
diff --git a/src/lib/icons/github-icon.svelte b/src/lib/icons/github-icon.svelte
new file mode 100644
index 000000000..e3dbd3ce6
--- /dev/null
+++ b/src/lib/icons/github-icon.svelte
@@ -0,0 +1,15 @@
+<script lang="ts">
+ let {
+ class: className
+ }: {
+ class?: string;
+ } = $props();
+</script>
+
+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class={className}>
+ <title>GitHub</title>
+ <path
+ d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"
+ fill="currentColor"
+ />
+</svg>
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte
index 5ae31febd..64ec16396 100644
--- a/src/routes/+page.svelte
+++ b/src/routes/+page.svelte
@@ -3,7 +3,7 @@
import * as Card from '$lib/components/ui/card/index.js';
import { Button } from '$lib/components/ui/button/index.js';
import UniversalTable from '$lib/components/universal-table.svelte';
- import { AlertCircle, Box, HardDrive, Cpu, MemoryStick, ArrowRight, PlayCircle, StopCircle, Trash2, Settings, RefreshCw, Loader2, Monitor } from '@lucide/svelte';
+ import { AlertCircle, Box, HardDrive, Cpu, MemoryStick, ArrowRight, PlayCircle, StopCircle, Trash2, Settings, RefreshCw, Loader2, Monitor, Server } from '@lucide/svelte';
import * as Alert from '$lib/components/ui/alert/index.js';
import { Progress } from '$lib/components/ui/progress/index.js';
import { capitalizeFirstLetter, truncateString, shortId, parseStatusTime } from '$lib/utils/string.utils';
@@ -27,6 +27,9 @@
import ImageAPIService from '$lib/services/api/image-api-service';
import type { PruneType } from '$lib/types/actions.type';
import DropdownCard from '$lib/components/dropdown-card.svelte';
+ import Meter from '$lib/components/meter.svelte';
+ import DockerIcon from '$lib/icons/docker-icon.svelte';
+ import GitHubIcon from '$lib/icons/github-icon.svelte';
let { data }: { data: PageData & { containers: ContainerInfo[] } } = $props();
@@ -53,6 +56,7 @@
const runningContainers = $derived(dashboardStates.containers?.filter((c: ContainerInfo) => c.State === 'running').length ?? 0);
const stoppedContainers = $derived(dashboardStates.containers?.filter((c: ContainerInfo) => c.State === 'exited').length ?? 0);
const totalImageSize = $derived(dashboardStates.images?.reduce((sum, image) => sum + (image.Size || 0), 0) ?? 0);
+ const containerUsagePercent = $derived(dashboardStates.containers?.length ? (runningContainers / dashboardStates.containers.length) * 100 : 0);
function getContainerDisplayName(container: ContainerInfo): string {
if (container.Names && container.Names.length > 0) {
@@ -68,9 +72,36 @@
dashboardStates.settings = data.settings;
dashboardStates.error = data.error;
});
+ // Add server stats state
+ let serverStats = $state<{
+ cpuUsage: number;
+ memoryUsage: number;
+ memoryTotal: number;
+ } | null>(null);
- onMount(async () => {
- await loadTopImagesMaturity();
+ // Add server stats fetching
+ async function fetchServerStats() {
+ try {
+ const response = await fetch('/api/system/stats');
+ if (response.ok) {
+ serverStats = await response.json();
+ }
+ } catch (error) {
+ console.error('Failed to fetch server stats:', error);
+ }
+ }
+
+ // Fetch server stats on mount and refresh
+ onMount(() => {
+ // Run async operations without blocking the mount
+ (async () => {
+ await loadTopImagesMaturity();
+ await fetchServerStats();
+ })();
+
+ // Set up periodic updates for server stats
+ const interval = setInterval(fetchServerStats, 5000); // Update every 5 seconds
+ return () => clearInterval(interval);
});
async function refreshData() {
@@ -78,6 +109,7 @@
isLoading.refreshing = true;
try {
await invalidateAll();
+ await fetchServerStats(); // Also refresh server stats
} catch (err) {
console.error('Error during dashboard refresh:', err);
dashboardStates.error = 'Failed to refresh dashboard data.';
@@ -203,102 +235,117 @@
<section>
<DropdownCard id="system-overview" title="System Overview" description="Hardware and Docker engine information" icon={Monitor} defaultExpanded={true}>
- <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
- <Card.Root class="overflow-hidden border-l-4 border-l-green-500">
+ <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
+ <!-- Containers & Docker Info Card -->
+ <Card.Root class="overflow-hidden">
<Card.Content class="p-6">
- <div class="flex justify-between items-start">
- <div>
- <p class="text-sm font-medium text-muted-foreground">Running Containers</p>
- <div class="mt-1">
+ <div class="flex items-center justify-between mb-4">
+ <div class="flex items-center gap-3">
+ <div class="bg-green-500/10 p-2.5 rounded-lg">
+ <Box class="text-green-500 size-5" />
+ </div>
+ <div>
+ <p class="text-sm font-medium text-muted-foreground">Containers</p>
<p class="text-2xl font-bold">
{runningContainers}
- <span class="text-xs font-normal text-muted-foreground ml-1">/ {dashboardStates.containers?.length || 0}</span>
+ <span class="text-sm font-normal text-muted-foreground">/ {dashboardStates.containers?.length || 0}</span>
</p>
</div>
</div>
- <div class="bg-green-500/10 p-2 rounded-full">
- <Box class="text-green-500 size-5" />
- </div>
</div>
+
{#if dashboardStates.containers?.length}
- <Progress value={(runningContainers / (dashboardStates.containers.length || 1)) * 100} class="mt-4 h-2" />
+ <div class="mb-6">
+ <Meter label="Active Containers" valueLabel="{runningContainers} running" value={containerUsagePercent} max={100} variant={containerUsagePercent > 80 ? 'warning' : 'default'} size="sm" />
+ </div>
{/if}
+
+ <!-- Docker Engine Info -->
+ <div class="pt-4 border-t space-y-3">
+ <div class="flex items-center gap-2">
+ <DockerIcon class="text-muted-foreground size-4" />
+ <p class="text-sm font-medium text-muted-foreground">Docker Engine</p>
+ </div>
+ <div class="grid grid-cols-2 gap-3 text-xs">
+ <div>
+ <p class="text-muted-foreground">Version</p>
+ <p class="font-medium">{dashboardStates.dockerInfo?.ServerVersion || 'Unknown'}</p>
+ </div>
+ <div>
+ <p class="text-muted-foreground">OS</p>
+ <p class="font-medium">{dashboardStates.dockerInfo?.OperatingSystem?.split(' ')[0] || 'Unknown'}</p>
+ </div>
+ </div>
+ </div>
</Card.Content>
</Card.Root>
- <Card.Root class="overflow-hidden border-l-4 border-l-blue-500">
+ <!-- Images & Storage Card -->
+ <Card.Root class="overflow-hidden">
<Card.Content class="p-6">
- <div class="flex justify-between items-start">
- <div>
- <p class="text-sm font-medium text-muted-foreground">Images</p>
- <p class="text-2xl font-bold mt-1">{dashboardStates.dockerInfo?.Images || 0}</p>
- </div>
- <div class="bg-blue-500/10 p-2 rounded-full">
- <HardDrive class="text-blue-500 size-5" />
+ <div class="flex items-center justify-between mb-4">
+ <div class="flex items-center gap-3">
+ <div class="bg-blue-500/10 p-2.5 rounded-lg">
+ <HardDrive class="text-blue-500 size-5" />
+ </div>
+ <div>
+ <p class="text-sm font-medium text-muted-foreground">Images</p>
+ <p class="text-2xl font-bold">{dashboardStates.dockerInfo?.Images || 0}</p>
+ </div>
</div>
</div>
+
{#if totalImageSize > 0}
- <div class="mt-4 text-xs text-muted-foreground">
- Total size: {formatBytes(totalImageSize)}
+ <div class="mb-4">
+ <Meter label="Storage Usage" valueLabel={formatBytes(totalImageSize)} value={totalImageSize} max={dashboardStates.dockerInfo?.MemTotal || totalImageSize} variant={totalImageSize > (dashboardStates.dockerInfo?.MemTotal || 0) * 0.1 ? 'warning' : 'default'} size="sm" />
</div>
+ <p class="text-xs text-muted-foreground">
+ Total: {formatBytes(totalImageSize)}
+ </p>
{:else if dashboardStates.dockerInfo?.Images === 0}
- <div class="mt-4 text-xs text-muted-foreground">No images stored</div>
+ <div class="text-center py-6">
+ <p class="text-sm text-muted-foreground">No images stored</p>
+ </div>
+ {:else}
+ <div class="text-center py-6">
+ <p class="text-sm text-muted-foreground">Loading...</p>
+ </div>
{/if}
</Card.Content>
</Card.Root>
- <Card.Root class="overflow-hidden border-l-4 border-l-purple-500">
+ <!-- Hardware & Performance Card -->
+ <Card.Root class="overflow-hidden">
<Card.Content class="p-6">
- <div class="flex justify-between items-start">
- <div>
- <p class="text-sm font-medium text-muted-foreground">Hardware Resources</p>
- <div class="grid grid-cols-2 gap-x-4 mt-2">
- <div>
- <p class="text-xs text-muted-foreground">CPU</p>
- <p class="text-lg font-semibold">{dashboardStates.dockerInfo?.NCPU || 'N/A'}</p>
- </div>
- <div>
- <p class="text-xs text-muted-foreground">Memory</p>
- <p class="text-lg font-semibold">{dashboardStates.dockerInfo?.MemTotal ? formatBytes(dashboardStates.dockerInfo.MemTotal, 0) : 'N/A'}</p>
+ <div class="flex items-center justify-between mb-4">
+ <div class="flex items-center gap-3">
+ <div class="bg-purple-500/10 p-2.5 rounded-lg">
+ <Cpu class="text-purple-500 size-5" />
+ </div>
+ <div>
+ <p class="text-sm font-medium text-muted-foreground">Hardware</p>
+ <div class="flex items-center gap-4 text-xs text-muted-foreground mt-1">
+ <span>{dashboardStates.dockerInfo?.NCPU || 'N/A'} cores</span>
+ <span>{dashboardStates.dockerInfo?.MemTotal ? formatBytes(dashboardStates.dockerInfo.MemTotal, 0) : 'N/A'}</span>
</div>
</div>
</div>
- <div class="bg-purple-500/10 p-2 rounded-full">
- <Cpu class="text-purple-500 size-5" />
- </div>
</div>
- <div class="mt-4 text-xs text-muted-foreground">
- {dashboardStates.dockerInfo?.Architecture || 'Unknown architecture'}
- </div>
- </Card.Content>
- </Card.Root>
- <Card.Root class="overflow-hidden border-l-4 border-l-amber-500">
- <Card.Content class="p-6">
- <div class="flex justify-between items-start">
- <div>
- <p class="text-sm font-medium text-muted-foreground">Docker Engine</p>
- <p class="text-xl font-semibold mt-1">
- {dashboardStates.dockerInfo?.ServerVersion || 'Unknown version'}
- </p>
- </div>
- <div class="bg-amber-500/10 p-2 rounded-full">
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="text-amber-500 size-5">
- <path d="M8 17v2a3 3 0 0 1-3 3H3"></path><path d="M12 17h10"></path><path d="M16 7H3a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h7"></path>
- <circle cx="20" cy="9" r="2"></circle>
- </svg>
- </div>
- </div>
- <div class="mt-4 grid grid-cols-2 gap-2">
- <div>
- <p class="text-xs font-medium text-muted-foreground">Operating System</p>
- <p class="text-sm">{dashboardStates.dockerInfo?.OperatingSystem || 'Unknown OS'}</p>
+ {#if serverStats}
+ <div class="space-y-4">
+ <Meter label="CPU Usage" valueLabel="{serverStats.cpuUsage.toFixed(1)}%" value={serverStats.cpuUsage} max={100} variant={serverStats.cpuUsage > 80 ? 'destructive' : serverStats.cpuUsage > 60 ? 'warning' : 'default'} size="sm" />
+
+ <Meter label="Memory Usage" valueLabel="{((serverStats.memoryUsage / serverStats.memoryTotal) * 100).toFixed(1)}%" value={(serverStats.memoryUsage / serverStats.memoryTotal) * 100} max={100} variant={(serverStats.memoryUsage / serverStats.memoryTotal) * 100 > 80 ? 'destructive' : (serverStats.memoryUsage / serverStats.memoryTotal) * 100 > 60 ? 'warning' : 'default'} size="sm" />
</div>
- <div>
- <p class="text-xs font-medium text-muted-foreground">Kernel Version</p>
- <p class="text-sm">{dashboardStates.dockerInfo?.KernelVersion || 'Unknown'}</p>
+ <p class="text-xs text-muted-foreground mt-3">
+ {dashboardStates.dockerInfo?.Architecture || 'Unknown arch'}
+ </p>
+ {:else}
+ <div class="text-center py-6">
+ <p class="text-sm text-muted-foreground">Loading stats...</p>
</div>
- </div>
+ {/if}
</Card.Content>
</Card.Root>
</div>
@@ -329,7 +376,7 @@
{/if}
</div>
<span class="text-base font-medium text-center">Stop All Running</span>
- <span class="text-sm text-muted-foreground mt-1">{runningContainers} containers</span>
+ <span class="text-sm text-muted-foreground mt-1">{runningContainers}</span>
</button>
<button
@@ -511,12 +558,7 @@
</a>
<span class="mx-2">•</span>
<a href="https://github.com/ofkm/arcane" target="_blank" rel="noopener noreferrer" class="hover:text-foreground transition-colors" title="GitHub">
- <svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="fill-current size-4">
- <title>GitHub</title>
- <path
- d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"
- />
- </svg>
+ <GitHubIcon class="fill-current size-4" />
<span class="sr-only">GitHub</span>
</a>
</div>
diff --git a/src/routes/api/system/stats/+server.ts b/src/routes/api/system/stats/+server.ts
new file mode 100644
index 000000000..21f5eb525
--- /dev/null
+++ b/src/routes/api/system/stats/+server.ts
@@ -0,0 +1,165 @@
+import { json } from '@sveltejs/kit';
+import type { RequestHandler } from './$types';
+import { ApiErrorCode, type ApiErrorResponse } from '$lib/types/errors.type';
+import { tryCatch } from '$lib/utils/try-catch';
+import { promises as fs } from 'fs';
+import { platform } from 'os';
+
+interface SystemStats {
+ cpuUsage: number;
+ memoryUsage: number;
+ memoryTotal: number;
+}
+
+async function getLinuxCpuUsage(): Promise<number> {
+ try {
+ const stat1 = await fs.readFile('/proc/stat', 'utf8');
+ const line1 = stat1.split('\n')[0];
+ const times1 = line1.split(/\s+/).slice(1, 8).map(Number);
+ const idle1 = times1[3] + times1[4];
+ const total1 = times1.reduce((a, b) => a + b, 0);
+
+ // Wait 1 second for accurate measurement
+ await new Promise((resolve) => setTimeout(resolve, 1000));
+
+ const stat2 = await fs.readFile('/proc/stat', 'utf8');
+ const line2 = stat2.split('\n')[0];
+ const times2 = line2.split(/\s+/).slice(1, 8).map(Number);
+ const idle2 = times2[3] + times2[4];
+ const total2 = times2.reduce((a, b) => a + b, 0);
+
+ const idleDiff = idle2 - idle1;
+ const totalDiff = total2 - total1;
+
+ const cpuUsage = totalDiff > 0 ? (1 - idleDiff / totalDiff) * 100 : 0;
+ return Math.max(0, Math.min(100, cpuUsage));
+ } catch (error) {
+ console.error('Error reading CPU stats from /proc/stat:', error);
+ return 0;
+ }
+}
+
+async function getLinuxMemoryStats(): Promise<{ usage: number; total: number }> {
+ try {
+ const meminfo = await fs.readFile('/proc/meminfo', 'utf8');
+ const lines = meminfo.split('\n');
+
+ const getValue = (key: string): number => {
+ const line = lines.find((l) => l.startsWith(key));
+ if (!line) return 0;
+ const match = line.match(/(\d+)/);
+ return match ? parseInt(match[1]) * 1024 : 0; // Convert KB to bytes
+ };
+
+ const memTotal = getValue('MemTotal:');
+ const memFree = getValue('MemFree:');
+ const buffers = getValue('Buffers:');
+ const cached = getValue('Cached:');
+ const sReclaimable = getValue('SReclaimable:');
+
+ // Calculate actual memory usage (excluding buffers and cache)
+ const memUsed = memTotal - memFree - buffers - cached - sReclaimable;
+
+ return {
+ usage: Math.max(0, memUsed),
+ total: memTotal
+ };
+ } catch (error) {
+ console.error('Error reading memory stats from /proc/meminfo:', error);
+ return { usage: 0, total: 0 };
+ }
+}
+
+async function getMacOSStats(): Promise<SystemStats> {
+ try {
+ const { exec } = await import('child_process');
+ const { promisify } = await import('util');
+ const execAsync = promisify(exec);
+
+ // Get CPU usage
+ const { stdout: cpuOutput } = await execAsync('top -l 1 -n 0 | grep "CPU usage"');
+ const cpuMatch = cpuOutput.match(/(\d+\.\d+)% user/);
+ const cpuUsage = cpuMatch ? parseFloat(cpuMatch[1]) : 0;
+
+ // Get memory stats
+ const { stdout: memOutput } = await execAsync('vm_stat');
+ const pageSize = 4096; // macOS page size is typically 4KB
+
+ const getMemValue = (key: string): number => {
+ const match = memOutput.match(new RegExp(`${key}:\\s+(\\d+)`));
+ return match ? parseInt(match[1]) * pageSize : 0;
+ };
+
+ const pagesWired = getMemValue('Pages wired down');
+ const pagesActive = getMemValue('Pages active');
+ const pagesInactive = getMemValue('Pages inactive');
+ const pagesSpeculative = getMemValue('Pages speculative');
+ const pagesCompressed = getMemValue('Pages stored in compressor');
+
+ // Get total memory
+ const { stdout: hwOutput } = await execAsync('sysctl hw.memsize');
+ const memTotalMatch = hwOutput.match(/hw\.memsize: (\d+)/);
+ const memTotal = memTotalMatch ? parseInt(memTotalMatch[1]) : 0;
+
+ const memUsed = pagesWired + pagesActive + pagesInactive + pagesSpeculative + pagesCompressed;
+
+ return {
+ cpuUsage: Math.max(0, Math.min(100, cpuUsage)),
+ memoryUsage: memUsed,
+ memoryTotal: memTotal
+ };
+ } catch (error) {
+ console.error('Error getting macOS system stats:', error);
+ return { cpuUsage: 0, memoryUsage: 0, memoryTotal: 0 };
+ }
+}
+
+async function getSystemStats(): Promise<SystemStats> {
+ const os = platform();
+
+ if (os === 'linux') {
+ const [cpuUsage, memStats] = await Promise.all([getLinuxCpuUsage(), getLinuxMemoryStats()]);
+
+ return {
+ cpuUsage,
+ memoryUsage: memStats.usage,
+ memoryTotal: memStats.total
+ };
+ } else if (os === 'darwin') {
+ return getMacOSStats();
+ } else {
+ // Fallback for other operating systems
+ console.warn(`System stats not supported for platform: ${os}`);
+ return {
+ cpuUsage: 0,
+ memoryUsage: 0,
+ memoryTotal: 0
+ };
+ }
+}
+
+export const GET: RequestHandler = async () => {
+ console.log('API: GET /api/system/stats - Fetching system statistics');
+
+ const result = await tryCatch(getSystemStats());
+
+ if (result.error) {
+ console.error('API Error (getSystemStats):', result.error);
+ const response: ApiErrorResponse = {
+ success: false,
+ error: result.error.message || 'Failed to get system statistics.',
+ code: ApiErrorCode.INTERNAL_SERVER_ERROR,
+ details: result.error
+ };
+ return json(response, { status: 500 });
+ }
+
+ const stats = result.data;
+
+ console.log(`API: System stats retrieved - CPU: ${stats.cpuUsage.toFixed(1)}%, Memory: ${((stats.memoryUsage / stats.memoryTotal) * 100).toFixed(1)}%`);
+
+ return json({
+ success: true,
+ ...stats
+ });
+};
diff --git a/src/routes/containers/[id]/+page.svelte b/src/routes/containers/[id]/+page.svelte
index e619d9942..d41642aea 100644
--- a/src/routes/containers/[id]/+page.svelte
+++ b/src/routes/containers/[id]/+page.svelte
@@ -13,6 +13,7 @@
import StatusBadge from '$lib/components/badges/status-badge.svelte';
import { onDestroy } from 'svelte';
import LogViewer from '$lib/components/LogViewer.svelte';
+ import Meter from '$lib/components/meter.svelte';
// Define the network config interface to match what Docker actually returns
interface NetworkConfig {
@@ -342,31 +343,11 @@
<Card.Header class="pb-4">
<Card.Title>Quick Stats</Card.Title>
</Card.Header>
- <Card.Content class="space-y-4">
+ <Card.Content class="space-y-6">
{#if stats && container.State?.Running}
- <div class="space-y-3">
- <div class="flex justify-between items-center">
- <span class="text-muted-foreground flex items-center gap-2">
- <Cpu class="size-4" /> CPU
- </span>
- <span class="font-medium">{cpuUsagePercent.toFixed(1)}%</span>
- </div>
- <div class="w-full bg-secondary rounded-full h-2">
- <div class="bg-primary rounded-full h-2 transition-all duration-300" style="width: {Math.min(cpuUsagePercent, 100)}%"></div>
- </div>
- </div>
+ <Meter label="CPU Usage" valueLabel="{cpuUsagePercent.toFixed(1)}%" value={cpuUsagePercent} max={100} variant={cpuUsagePercent > 80 ? 'destructive' : cpuUsagePercent > 60 ? 'warning' : 'default'} />
- <div class="space-y-3">
- <div class="flex justify-between items-center">
- <span class="text-muted-foreground flex items-center gap-2">
- <MemoryStick class="size-4" /> Memory
- </span>
- <span class="font-medium">{memoryUsageFormatted}</span>
- </div>
- <div class="w-full bg-secondary rounded-full h-2">
- <div class="bg-primary rounded-full h-2 transition-all duration-300" style="width: {memoryUsagePercent.toFixed(2)}%"></div>
- </div>
- </div>
+ <Meter label="Memory Usage" valueLabel="{memoryUsageFormatted} / {memoryLimitFormatted}" value={memoryUsagePercent} max={100} variant={memoryUsagePercent > 80 ? 'destructive' : memoryUsagePercent > 60 ? 'warning' : 'default'} />
{:else}
<div class="text-muted-foreground text-center py-8">
{container.State?.Running ? 'Loading stats...' : 'Container not running'}
@@ -431,29 +412,9 @@
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<!-- CPU and Memory -->
<div class="space-y-6">
- <div>
- <div class="flex justify-between items-center mb-3">
- <span class="text-muted-foreground flex items-center gap-2">
- <Cpu class="size-4" /> CPU Usage
- </span>
- <span class="font-medium">{cpuUsagePercent.toFixed(2)}%</span>
- </div>
- <div class="w-full bg-secondary rounded-full h-3">
- <div class="bg-blue-500 rounded-full h-3 transition-all duration-300" style="width: {Math.min(cpuUsagePercent, 100)}%"></div>
- </div>
- </div>
+ <Meter label="CPU Usage" valueLabel="{cpuUsagePercent.toFixed(2)}%" value={cpuUsagePercent} max={100} variant={cpuUsagePercent > 80 ? 'destructive' : cpuUsagePercent > 60 ? 'warning' : 'default'} size="lg" />
- <div>
- <div class="flex justify-between items-center mb-3">
- <span class="text-muted-foreground flex items-center gap-2">
- <MemoryStick class="size-4" /> Memory Usage
- </span>
- <span class="font-medium">{memoryUsageFormatted} / {memoryLimitFormatted}</span>
- </div>
- <div class="w-full bg-secondary rounded-full h-3">
- <div class="bg-green-500 rounded-full h-3 transition-all duration-300" style="width: {memoryUsagePercent.toFixed(2)}%"></div>
- </div>
- </div>
+ <Meter label="Memory Usage" valueLabel="{memoryUsageFormatted} / {memoryLimitFormatted} ({memoryUsagePercent.toFixed(1)}%)" value={memoryUsagePercent} max={100} variant={memoryUsagePercent > 80 ? 'destructive' : memoryUsagePercent > 60 ? 'warning' : 'default'} size="lg" />
</div>
<!-- Network I/O -->