Skip to content

Modern Docker Management, Designed for Everyone

20.6 KB
Raw Blame
<script lang="ts">
	import { onMount } from 'svelte';

	import type { ActionButton } from '#lib/components/action-button-group/types.js';
	import { ArcaneButton } from '#lib/components/arcane-button/index.js';
	import { TabBar, type TabItem } from '#lib/components/tab-bar/index.js';
	import * as Alert from '#lib/components/ui/alert/index.js';
	import * as Collapsible from '#lib/components/ui/collapsible/index.js';
	import * as Table from '#lib/components/ui/table/index.js';
	import * as Tabs from '#lib/components/ui/tabs/index.js';
	import { useUrlTab } from '#lib/hooks/use-url-tab.svelte.js';
	import {
		ActivityIcon,
		AlertTriangleIcon,
		CpuIcon,
		MemoryStickIcon,
		ClockIcon,
		ConnectionIcon,
		DownloadIcon,
		EnvironmentsIcon,
		ArrowDownIcon,
		LayersIcon
	} from '#lib/icons/index.js';
	import { SettingsPageLayout } from '#lib/layouts/index.js';
	import type { SettingsStatCard } from '#lib/layouts/types.js';
	import { m } from '#lib/paraglide/messages.js';
	import type { Diagnostics, GoroutineLeakReport, PprofProfile } from '#lib/types/diagnostics.js';
	import { cn } from '#lib/utils.js';
	import { formatTime } from '#lib/utils/formatting.js';
	import { tryCatch } from '#lib/utils/try-catch.js';
	import { createDiagnosticsWebSocket, type DiagnosticsWebSocket } from '#lib/utils/ws.js';

	import DiagnosticActorPanel from './components/diagnostic-actor-panel.svelte';
	import DiagnosticGoroutinePanel from './components/diagnostic-goroutine-panel.svelte';
	import DiagnosticLeakPanel from './components/diagnostic-leak-panel.svelte';
	import DiagnosticLogPanel from './components/diagnostic-log-panel.svelte';

	type DiagnosticsTab = 'overview' | 'connections' | 'actors' | 'goroutines' | 'logs' | 'profiling';

	let diag = $state<Diagnostics | null>(null);
	let paused = $state(false);
	let error = $state<string | null>(null);

	let ws = $state.raw<DiagnosticsWebSocket | null>(null);

	const wsKindLabels: Record<string, string> = {
		project_logs: m.diagnostics_ws_project_logs(),
		container_logs: m.diagnostics_ws_container_logs(),
		container_stats: m.diagnostics_ws_container_stats(),
		container_exec: m.diagnostics_ws_kind_terminal(),
		system_stats: m.diagnostics_ws_system_stats(),
		service_logs: m.diagnostics_ws_service_logs(),
		diagnostics: m.diagnostics()
	};

	function fmtBytes(n: number): string {
		if (!n) return '0 B';
		if (n < 1024) return `${n} B`;
		const units = ['KB', 'MB', 'GB', 'TB'];
		let v = n;
		let i = -1;
		while (v >= 1024 && i < units.length - 1) {
			v /= 1024;
			i++;
		}
		return `${v.toFixed(1)} ${units[i]}`;
	}

	function fmtUptime(s: number): string {
		if (s < 60) return `${s}s`;
		const d = Math.floor(s / 86400);
		const h = Math.floor((s % 86400) / 3600);
		const mins = Math.floor((s % 3600) / 60);
		if (d > 0) return `${d}d ${h}h`;
		if (h > 0) return `${h}h ${mins}m`;
		return `${mins}m ${s % 60}s`;
	}

	function fmtNum(n: number): string {
		return n?.toLocaleString() ?? '0';
	}

	function fmtMs(ns: number): string {
		const ms = ns / 1e6;
		return ms < 1 ? `${(ns / 1e3).toFixed(0)}µs` : `${ms.toFixed(2)}ms`;
	}

	function leakedGoroutineValue(d: Diagnostics): string {
		if (d.runtime.leakScannedAt || d.runtime.leakedGoroutines > 0) {
			return fmtNum(d.runtime.leakedGoroutines);
		}
		return m.diagnostics_leaks_not_scanned();
	}

	const totalConnections = $derived.by(() => {
		const s = diag?.websocket?.snapshot;
		if (!s) return 0;
		return s.projectLogsActive + s.containerLogsActive + s.containerStats + s.containerExec + s.systemStats + s.serviceLogsActive;
	});

	const wsCounts = $derived.by(() => {
		const s = diag?.websocket?.snapshot;
		if (!s) return [];
		return [
			{ label: m.diagnostics_ws_project_logs(), value: s.projectLogsActive },
			{ label: m.diagnostics_ws_container_logs(), value: s.containerLogsActive },
			{ label: m.diagnostics_ws_container_stats(), value: s.containerStats },
			{ label: m.diagnostics_ws_terminals(), value: s.containerExec },
			{ label: m.diagnostics_ws_system_stats(), value: s.systemStats },
			{ label: m.diagnostics_ws_service_logs(), value: s.serviceLogsActive },
			{ label: m.diagnostics(), value: s.diagnosticsActive }
		];
	});

	const diagnosticsTabItems = $derived.by(
		() =>
			[
				{ value: 'overview', label: m.common_overview(), icon: CpuIcon },
				{
					value: 'connections',
					label: m.diagnostics_tab_connections(),
					icon: ConnectionIcon,
					badge: totalConnections || undefined
				},
				{ value: 'actors', label: m.diagnostics_tab_actors(), icon: EnvironmentsIcon },
				{ value: 'goroutines', label: m.goroutines(), icon: LayersIcon },
				{ value: 'logs', label: m.common_logs(), icon: ActivityIcon },
				{
					value: 'profiling',
					label: m.diagnostics_tab_profiling(),
					icon: DownloadIcon,
					badge: diag?.runtime.leakedGoroutines || undefined
				}
			] satisfies TabItem[]
	);

	const urlTab = useUrlTab<DiagnosticsTab>({
		validTabs: () => diagnosticsTabItems.map((tab) => tab.value as DiagnosticsTab),
		defaultTab: () => 'overview'
	});
	const activeTab = $derived(urlTab.value);

	const heapBar = $derived.by(() => {
		const mem = diag?.memory;
		if (!mem || !mem.heapSys) return null;
		const released = mem.heapReleased;
		const idle = Math.max(0, mem.heapIdle - released);
		const inuse = mem.heapInuse;
		const total = mem.heapSys || inuse + idle + released || 1;
		return {
			inuse: (inuse / total) * 100,
			idle: (idle / total) * 100,
			released: (released / total) * 100
		};
	});

	const maxPause = $derived.by(() => {
		const p = diag?.gc?.recentPausesNs ?? [];
		return p.length ? Math.max(...p) : 0;
	});

	const headerStats = $derived.by((): SettingsStatCard[] => {
		if (!diag) return [];
		return [
			{
				title: m.goroutines(),
				value: fmtNum(diag.runtime.goroutines),
				icon: ActivityIcon,
				iconColor: diag.runtime.leakedGoroutines > 0 ? 'text-rose' : 'text-sky'
			},
			{
				title: m.diagnostics_stat_heap_alloc(),
				value: fmtBytes(diag.memory.heapAlloc),
				icon: MemoryStickIcon,
				iconColor: 'text-primary'
			},
			{
				title: m.common_uptime(),
				value: fmtUptime(diag.runtime.uptimeSeconds),
				icon: ClockIcon,
				iconColor: 'text-teal'
			}
		];
	});

	const actionButtons = $derived.by((): ActionButton[] => [
		{
			id: 'pause',
			action: paused ? 'unpause' : 'pause',
			label: paused ? m.diagnostics_resume() : m.common_pause(),
			placement: 'secondary',
			onclick: togglePause
		},
		{
			id: 'refresh',
			action: 'refresh',
			placement: 'secondary',
			iconOnly: true,
			label: m.common_refresh(),
			onclick: refresh
		}
	]);

	function applySnapshot(d: Diagnostics) {
		if (paused) return;
		diag = d;
		error = null;
	}

	// Pausing only freezes the view so on-demand commands keep working.
	function togglePause() {
		paused = !paused;
		if (!paused) refresh();
	}

	function refresh() {
		if (!ws?.refresh()) error = m.disconnected();
	}

	let dumpOpen = $state<{ goroutine: boolean; heap: boolean }>({ goroutine: false, heap: false });
	let dumpText = $state<{ goroutine: string; heap: string }>({ goroutine: '', heap: '' });
	let dumpLoading = $state<{ goroutine: boolean; heap: boolean }>({ goroutine: false, heap: false });

	async function loadDump(name: 'goroutine' | 'heap') {
		dumpLoading[name] = true;
		try {
			const operationResult = await tryCatch(
				ws ? ws.request({ type: 'dump', name }) : Promise.reject(new Error(m.disconnected()))
			);
			if (operationResult.error !== null) {
				const e = operationResult.error;

				dumpText[name] = e instanceof Error ? e.message : m.diagnostics_error_dump();
			} else {
				dumpText[name] = operationResult.data.text ?? '';
			}
		} finally {
			dumpLoading[name] = false;
		}
	}

	function onDumpToggle(name: 'goroutine' | 'heap', open: boolean) {
		dumpOpen[name] = open;
		if (open && !dumpText[name] && !dumpLoading[name]) loadDump(name);
	}

	const dumps = [
		{ id: 'goroutine' as const, label: m.diagnostics_dump_goroutine() },
		{ id: 'heap' as const, label: m.diagnostics_dump_heap() }
	];

	const profiles: { id: PprofProfile; label: string }[] = [
		{ id: 'heap', label: m.diagnostics_profile_heap() },
		{ id: 'goroutine', label: m.diagnostics_profile_goroutine() },
		{ id: 'goroutineleak', label: m.diagnostics_profile_goroutineleak() },
		{ id: 'allocs', label: m.diagnostics_profile_allocs() },
		{ id: 'block', label: m.diagnostics_profile_block() },
		{ id: 'mutex', label: m.diagnostics_profile_mutex() },
		{ id: 'threadcreate', label: m.diagnostics_profile_threadcreate() },
		{ id: 'profile', label: m.diagnostics_profile_cpu() },
		{ id: 'trace', label: m.diagnostics_profile_trace() }
	];

	function onLeakScanned(report: GoroutineLeakReport) {
		if (!diag) return;
		diag.runtime.leakedGoroutines = report.count;
		diag.runtime.leakScannedAt = report.scannedAt;
	}

	let downloading = $state<string | null>(null);

	async function download(p: PprofProfile) {
		downloading = p;
		try {
			const operationResult = await tryCatch(
				(async () => {
					if (!ws) throw new Error(m.disconnected());
					await ws.downloadProfile(p);
				})()
			);
			if (operationResult.error !== null) {
				const e = operationResult.error;

				error = e instanceof Error ? e.message : m.diagnostics_error_download();
			}
		} finally {
			downloading = null;
		}
	}

	onMount(() => {
		ws = createDiagnosticsWebSocket({
			onSnapshot: applySnapshot
		});
		ws.connect();
		return () => {
			ws?.close();
			ws = null;
		};
	});
</script>

{#snippet statusList(rows: { label: string; value: string | number }[])}
	<dl class="grid grid-cols-content-action gap-x-6 gap-y-2.5 text-sm">
		{#each rows as r (r.label)}
			<dt class="text-muted-foreground">{r.label}</dt>
			<dd class="text-right font-medium tabular-nums">{r.value}</dd>
		{/each}
	</dl>
{/snippet}

{#snippet sectionHeading(title: string, description?: string)}
	<div class="space-y-0.5 px-1">
		<h3 class="text-sm font-semibold">{title}</h3>
		{#if description}
			<p class="text-xs text-muted-foreground">{description}</p>
		{/if}
	</div>
{/snippet}

<SettingsPageLayout
	title={m.diagnostics()}
	description={m.diagnostics_description()}
	icon={ActivityIcon}
	pageType="management"
	statCards={headerStats}
	{actionButtons}
>
	{#snippet mainContent()}
		{#if error}
			<div class="mb-6 rounded-lg border border-destructive/40 bg-destructive/10 px-3 py-2 text-sm text-destructive">
				{error}
			</div>
		{/if}

		{#if diag}
			<Tabs.Root value={activeTab} class="w-full">
				<TabBar items={diagnosticsTabItems} value={activeTab} onValueChange={urlTab.select} />

				<Tabs.Content value="overview" class="mt-6">
					<div class="space-y-8">
						{#if diag.runtime.leakedGoroutines > 0}
							<Alert.Root variant="destructive">
								<AlertTriangleIcon class="size-4" />
								<Alert.Title>{m.diagnostics_leaks_count({ count: diag.runtime.leakedGoroutines })}</Alert.Title>
								<Alert.Description>
									<div class="flex flex-wrap items-center gap-3">
										{m.diagnostics_leaks_hint()}
										<ArcaneButton
											action="base"
											tone="outline"
											size="sm"
											customLabel={m.diagnostics_tab_profiling()}
											onclick={() => urlTab.select('profiling')}
										/>
									</div>
								</Alert.Description>
							</Alert.Root>
						{/if}

						<section class="space-y-6">
							{@render sectionHeading(m.diagnostics_section_system())}
							<div class="grid gap-10 lg:grid-cols-2">
								<div class="space-y-3">
									<h4 class="text-xs font-semibold tracking-wider text-muted-foreground uppercase">
										{m.diagnostics_section_runtime()}
									</h4>
									{@render statusList([
										{ label: m.diagnostics_runtime_go_version(), value: diag.runtime.goVersion },
										{ label: m.platform(), value: `${diag.runtime.os}/${diag.runtime.arch}` },
										{ label: m.diagnostics_runtime_gomaxprocs(), value: diag.runtime.gomaxprocs },
										{ label: m.diagnostics_runtime_num_cpu(), value: diag.runtime.numCpu },
										{ label: m.goroutines(), value: fmtNum(diag.runtime.goroutines) },
										{ label: m.diagnostics_runtime_leaked_goroutines(), value: leakedGoroutineValue(diag) },
										{ label: m.diagnostics_runtime_ws_workers(), value: fmtNum(diag.runtime.wsWorkerGoroutines) },
										{ label: m.diagnostics_runtime_cgo_calls(), value: fmtNum(diag.runtime.numCgoCall) },
										{ label: m.common_uptime(), value: fmtUptime(diag.runtime.uptimeSeconds) }
									])}
								</div>
								<div class="space-y-3">
									<h4 class="text-xs font-semibold tracking-wider text-muted-foreground uppercase">
										{m.diagnostics_section_memory()}
									</h4>
									{#if heapBar}
										<div class="flex h-2 overflow-hidden rounded-full bg-muted/40">
											<div class="w-(--seg) bg-primary" style="--seg: {heapBar.inuse}%" title={m.diagnostics_mem_in_use()}></div>
											<div class="w-(--seg) bg-primary/40" style="--seg: {heapBar.idle}%" title={m.idle()}></div>
											<div
												class="w-(--seg) bg-muted-foreground/40"
												style="--seg: {heapBar.released}%"
												title={m.diagnostics_mem_released()}
											></div>
										</div>
										<div class="flex flex-wrap gap-x-4 gap-y-1 text-2xs text-muted-foreground">
											<span>
												<span class="mr-1 inline-block size-2 rounded-full bg-primary"></span>
												{m.diagnostics_mem_in_use()}
												{fmtBytes(diag.memory.heapInuse)}
											</span>
											<span>
												<span class="mr-1 inline-block size-2 rounded-full bg-primary/40"></span>
												{m.idle()}
												{fmtBytes(diag.memory.heapIdle - diag.memory.heapReleased)}
											</span>
											<span>
												<span class="mr-1 inline-block size-2 rounded-full bg-muted-foreground/40"></span>
												{m.diagnostics_mem_released()}
												{fmtBytes(diag.memory.heapReleased)}
											</span>
										</div>
									{/if}
									{@render statusList([
										{ label: m.diagnostics_mem_heap_alloc(), value: fmtBytes(diag.memory.heapAlloc) },
										{ label: m.diagnostics_mem_heap_sys(), value: fmtBytes(diag.memory.heapSys) },
										{ label: m.diagnostics_mem_heap_objects(), value: fmtNum(diag.memory.heapObjects) },
										{ label: m.diagnostics_mem_stack_in_use(), value: fmtBytes(diag.memory.stackInuse) },
										{ label: m.diagnostics_mem_total_alloc(), value: fmtBytes(diag.memory.totalAlloc) },
										{ label: m.diagnostics_mem_sys_total(), value: fmtBytes(diag.memory.sys) },
										{ label: m.diagnostics_mem_next_gc(), value: fmtBytes(diag.memory.nextGc) },
										{
											label: m.diagnostics_mem_gc_cpu_fraction(),
											value: `${(diag.memory.gcCpuFraction * 100).toFixed(3)}%`
										}
									])}
								</div>
							</div>
						</section>

						<section class="space-y-4 border-t border-border/50 pt-8">
							{@render sectionHeading(m.diagnostics_section_gc())}
							<div class="grid items-start gap-10 lg:grid-cols-2">
								{@render statusList([
									{ label: m.diagnostics_gc_total_cycles(), value: fmtNum(diag.gc.numGc) },
									{ label: m.diagnostics_gc_forced_cycles(), value: fmtNum(diag.memory.numForcedGc) },
									{ label: m.diagnostics_gc_total_pause(), value: fmtMs(diag.gc.pauseTotalNs) },
									{ label: m.diagnostics_gc_last(), value: diag.gc.lastGc ? formatTime(diag.gc.lastGc) : '—' }
								])}
								{#if diag.gc.recentPausesNs?.length}
									<div>
										<div class="mb-1 text-2xs text-muted-foreground">{m.diagnostics_gc_recent_pauses()}</div>
										<div class="flex h-12 items-end gap-0.5">
											<!-- Pause durations can repeat; bars render the current snapshot without local state. -->
											{#each diag.gc.recentPausesNs as p}
												<div
													class="h-(--bar) flex-1 rounded-sm bg-warning/70"
													style="--bar: {maxPause ? Math.max(4, (p / maxPause) * 100) : 4}%"
													title={fmtMs(p)}
												></div>
											{/each}
										</div>
									</div>
								{/if}
							</div>
						</section>
					</div>
				</Tabs.Content>

				<Tabs.Content value="connections" class="mt-6">
					<div class="space-y-4">
						{@render sectionHeading(m.diagnostics_section_connections({ count: diag.websocket.connections?.length ?? 0 }))}
						<div class="grid grid-cols-2 gap-x-6 gap-y-3 sm:grid-cols-4 lg:grid-cols-7">
							{#each wsCounts as c (c.label)}
								<div>
									<div class="text-xs text-muted-foreground">{c.label}</div>
									<div class="text-lg font-semibold tabular-nums">{fmtNum(c.value)}</div>
								</div>
							{/each}
						</div>
						{#if diag.websocket.connections?.length}
							<div class="overflow-x-auto">
								<Table.Root>
									<Table.Header>
										<Table.Row>
											<Table.Head>{m.diagnostics_conn_kind()}</Table.Head>
											<Table.Head>{m.resource()}</Table.Head>
											<Table.Head>{m.diagnostics_conn_client_ip()}</Table.Head>
											<Table.Head>{m.common_user()}</Table.Head>
											<Table.Head>{m.diagnostics_conn_since()}</Table.Head>
										</Table.Row>
									</Table.Header>
									<Table.Body>
										{#each diag.websocket.connections as c (c.id)}
											<Table.Row>
												<Table.Cell>{wsKindLabels[c.kind] ?? c.kind}</Table.Cell>
												<Table.Cell><span class="font-mono text-xs text-muted-foreground">{c.resourceId || '—'}</span></Table.Cell
												>
												<Table.Cell><span class="font-mono text-xs text-muted-foreground">{c.clientIp || '—'}</span></Table.Cell>
												<Table.Cell><span class="text-xs text-muted-foreground">{c.userId || '—'}</span></Table.Cell>
												<Table.Cell>
													<span class="text-xs text-muted-foreground tabular-nums">
														{c.startedAt ? formatTime(c.startedAt) : '—'}
													</span>
												</Table.Cell>
											</Table.Row>
										{/each}
									</Table.Body>
								</Table.Root>
							</div>
						{:else}
							<p class="py-8 text-sm text-muted-foreground">{m.diagnostics_connections_empty()}</p>
						{/if}
					</div>
				</Tabs.Content>

				<Tabs.Content value="actors" class="mt-6">
					<DiagnosticActorPanel />
				</Tabs.Content>

				<Tabs.Content value="goroutines" class="mt-6">
					<DiagnosticGoroutinePanel />
				</Tabs.Content>

				<Tabs.Content value="logs" class="mt-6">
					<DiagnosticLogPanel height="min(70vh, 640px)" />
				</Tabs.Content>

				<Tabs.Content value="profiling" class="mt-6">
					<div class="space-y-8">
						<section class="space-y-4">
							{@render sectionHeading(m.diagnostics_section_leaks())}
							<DiagnosticLeakPanel
								leakedGoroutines={diag.runtime.leakedGoroutines}
								leakScannedAt={diag.runtime.leakScannedAt}
								socket={ws}
								onscanned={onLeakScanned}
							/>
						</section>

						<section class="space-y-4 border-t border-border/50 pt-8">
							{@render sectionHeading(m.diagnostics_section_dumps())}
							<div class="space-y-2">
								{#each dumps as d (d.id)}
									<Collapsible.Root open={dumpOpen[d.id]} onOpenChange={(o: boolean) => onDumpToggle(d.id, o)}>
										<Collapsible.Trigger>
											{#snippet child({ props })}
												<button
													{...props}
													type="button"
													class="flex w-full items-center justify-between rounded-lg border border-border/60 px-3 py-2 text-sm font-medium hover:bg-muted/30"
												>
													{d.label}
													<ArrowDownIcon class={cn('size-4 transition-transform', dumpOpen[d.id] && '-rotate-180')} />
												</button>
											{/snippet}
										</Collapsible.Trigger>
										<Collapsible.Content>
											<pre
												class="mt-1 max-h-96 overflow-auto rounded-lg border border-border/60 bg-background p-3 font-mono text-2xs leading-relaxed">{dumpLoading[
													d.id
												]
													? m.common_loading()
													: dumpText[d.id] || m.diagnostics_dump_empty()}</pre>
										</Collapsible.Content>
									</Collapsible.Root>
								{/each}
							</div>
						</section>

						<section class="space-y-4 border-t border-border/50 pt-8">
							{@render sectionHeading(m.diagnostics_section_profiles(), m.diagnostics_profiles_hint())}
							<ul class="divide-y divide-border/40">
								{#each profiles as p (p.id)}
									<li class="flex items-center justify-between gap-4 py-2.5">
										<span class="text-sm font-medium">{p.label}</span>
										<ArcaneButton
											action="base"
											tone="outline"
											size="sm"
											icon={DownloadIcon}
											customLabel={m.diagnostics_download()}
											loading={downloading === p.id}
											disabled={downloading !== null}
											onclick={() => download(p.id)}
										/>
									</li>
								{/each}
							</ul>
						</section>
					</div>
				</Tabs.Content>
			</Tabs.Root>
		{:else if !error}
			<div class="py-16 text-center text-sm text-muted-foreground">{m.diagnostics_loading()}</div>
		{/if}
	{/snippet}
</SettingsPageLayout>