Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
feat: use websockets for dashboard stats (#496)
Kyle Mendell98709250eea1
diff --git a/backend/internal/api/environment_handler.go b/backend/internal/api/environment_handler.goindex 34d42b96c..43d432245 100644--- a/backend/internal/api/environment_handler.go+++ b/backend/internal/api/environment_handler.go@@ -137,6 +137,8 @@ func NewEnvironmentHandler(apiGroup.GET("/:id/updater/history", handler.UpdaterHistory)apiGroup.POST("/:id/agent/pair", handler.PairAgent)++ apiGroup.GET("/:id/stats/ws", handler.GetStatsWS)}}@@ -1098,3 +1100,80 @@ func (h *EnvironmentHandler) GetContainerLogsWS(c *gin.Context) {_ = wsutil.ProxyHTTP(c.Writer, c.Request, u.String(), hdr)}++func (h *EnvironmentHandler) GetStatsWS(c *gin.Context) {+ envID := c.Param("id")++ if envID == LOCAL_DOCKER_ENVIRONMENT_ID {+ u := &url.URL{}++ xfp := strings.TrimSpace(c.GetHeader("X-Forwarded-Proto"))+ if xfp != "" {+ if idx := strings.Index(xfp, ","); idx != -1 {+ xfp = xfp[:idx]+ }+ if strings.HasPrefix(strings.ToLower(xfp), "https") {+ u.Scheme = "wss"+ } else {+ u.Scheme = "ws"+ }+ } else {++ if c.Request.TLS != nil {+ u.Scheme = "wss"+ } else {+ u.Scheme = "ws"+ }+ }++ u.Host = c.Request.Host+ u.Path = "/api/system/stats/ws"+ u.RawQuery = c.Request.URL.RawQuery++ hdr := http.Header{}+ // Forward auth if present (or promote cookie)+ if auth := c.GetHeader("Authorization"); auth != "" {+ hdr.Set("Authorization", auth)+ } else if cookieToken, err := c.Cookie("token"); err == nil && cookieToken != "" {+ hdr.Set("Authorization", "Bearer "+cookieToken)+ }++ _ = wsutil.ProxyHTTP(c.Writer, c.Request, u.String(), hdr)+ return+ }++ environment, err := h.environmentService.GetEnvironmentByID(c.Request.Context(), envID)+ if err != nil || environment == nil || !environment.Enabled {+ c.JSON(http.StatusNotFound, gin.H{"success": false, "data": gin.H{"error": "Environment not found or disabled"}})+ return+ }++ u, err := url.Parse(strings.TrimRight(environment.ApiUrl, "/"))+ if err != nil {+ c.JSON(http.StatusBadRequest, gin.H{"success": false, "data": gin.H{"error": "Invalid environment URL"}})+ return+ }++ // websocket scheme+ if u.Scheme == "https" {+ u.Scheme = "wss"+ } else {+ u.Scheme = "ws"+ }+ u.Path = path.Join(u.Path, "/api/environments/"+LOCAL_DOCKER_ENVIRONMENT_ID+"/stats/ws")+ u.RawQuery = c.Request.URL.RawQuery++ hdr := http.Header{}+ // Forward auth if present+ if auth := c.GetHeader("Authorization"); auth != "" {+ hdr.Set("Authorization", auth)+ } else if cookieToken, err := c.Cookie("token"); err == nil && cookieToken != "" {+ hdr.Set("Authorization", "Bearer "+cookieToken)+ }+ // Agent token+ if environment.AccessToken != nil && *environment.AccessToken != "" {+ hdr.Set("X-Arcane-Agent-Token", *environment.AccessToken)+ }++ _ = wsutil.ProxyHTTP(c.Writer, c.Request, u.String(), hdr)+}diff --git a/backend/internal/api/system_handler.go b/backend/internal/api/system_handler.goindex 1a2755a95..6bde34910 100644--- a/backend/internal/api/system_handler.go+++ b/backend/internal/api/system_handler.go@@ -9,6 +9,7 @@ import ("github.com/docker/docker/api/types/container""github.com/docker/docker/api/types/image""github.com/gin-gonic/gin"+ "github.com/gorilla/websocket""github.com/ofkm/arcane-backend/internal/dto""github.com/ofkm/arcane-backend/internal/middleware""github.com/ofkm/arcane-backend/internal/services"@@ -18,6 +19,10 @@ import ("github.com/shirou/gopsutil/v3/mem")+var sysWsUpgrader = websocket.Upgrader{+ CheckOrigin: func(r *http.Request) bool { return true },+}+type SystemHandler struct {dockerService *services.DockerClientServicesystemService *services.SystemService@@ -29,9 +34,8 @@ func NewSystemHandler(group *gin.RouterGroup, dockerService *services.DockerClieapiGroup := group.Group("/system")apiGroup.Use(authMiddleware.WithAdminNotRequired().Add()){- apiGroup.GET("/stats", handler.GetStats)+ apiGroup.GET("/stats/ws", handler.GetStatsWS)apiGroup.GET("/docker/info", handler.GetDockerInfo)-apiGroup.POST("/prune", handler.PruneAll)apiGroup.POST("/containers/start-all", handler.StartAllContainers)apiGroup.POST("/containers/start-stopped", handler.StartAllStoppedContainers)@@ -51,72 +55,6 @@ type SystemStats struct {Hostname string `json:"hostname,omitempty"`}-func (h *SystemHandler) GetStats(c *gin.Context) {- cpuPercent, err := cpu.Percent(time.Second, false)- var cpuUsage float64- if err != nil || len(cpuPercent) == 0 {- cpuUsage = 0- } else {- cpuUsage = cpuPercent[0]- }-- cpuCount, err := cpu.Counts(true)- if err != nil {- cpuCount = runtime.NumCPU()- }-- memInfo, err := mem.VirtualMemory()- var memoryUsage, memoryTotal uint64- if err != nil {- memoryUsage = 0- memoryTotal = 0- } else {- memoryUsage = memInfo.Used- memoryTotal = memInfo.Total- }-- diskInfo, err := disk.Usage("/")- var diskUsage, diskTotal uint64- if err != nil {- diskUsage = 0- diskTotal = 0- } else {- diskUsage = diskInfo.Used- diskTotal = diskInfo.Total- }-- hostInfo, err := host.Info()- var hostname string- if err == nil {- hostname = hostInfo.Hostname- }-- stats := SystemStats{- CPUUsage: cpuUsage,- MemoryUsage: memoryUsage,- MemoryTotal: memoryTotal,- DiskUsage: diskUsage,- DiskTotal: diskTotal,- CPUCount: cpuCount,- Architecture: runtime.GOARCH,- Platform: runtime.GOOS,- Hostname: hostname,- }-- c.JSON(http.StatusOK, gin.H{- "success": true,- "cpuUsage": stats.CPUUsage,- "memoryUsage": stats.MemoryUsage,- "memoryTotal": stats.MemoryTotal,- "diskUsage": stats.DiskUsage,- "diskTotal": stats.DiskTotal,- "cpuCount": stats.CPUCount,- "architecture": stats.Architecture,- "platform": stats.Platform,- "hostname": stats.Hostname,- })-}-func (h *SystemHandler) GetDockerInfo(c *gin.Context) {ctx := c.Request.Context()@@ -325,3 +263,89 @@ func (h *SystemHandler) StopAllContainers(c *gin.Context) {"data": result,})}++func (h *SystemHandler) GetStatsWS(c *gin.Context) {+ conn, err := sysWsUpgrader.Upgrade(c.Writer, c.Request, nil)+ if err != nil {+ return+ }+ defer conn.Close()++ ticker := time.NewTicker(2 * time.Second)+ defer ticker.Stop()++ sendStats := func() error {+ cpuPercent, err := cpu.Percent(time.Second, false)+ var cpuUsage float64+ if err != nil || len(cpuPercent) == 0 {+ cpuUsage = 0+ } else {+ cpuUsage = cpuPercent[0]+ }++ cpuCount, err := cpu.Counts(true)+ if err != nil {+ cpuCount = runtime.NumCPU()+ }++ memInfo, err := mem.VirtualMemory()+ var memoryUsage, memoryTotal uint64+ if err != nil {+ memoryUsage = 0+ memoryTotal = 0+ } else {+ memoryUsage = memInfo.Used+ memoryTotal = memInfo.Total+ }++ diskInfo, err := disk.Usage("/")+ var diskUsage, diskTotal uint64+ if err != nil {+ diskUsage = 0+ diskTotal = 0+ } else {+ diskUsage = diskInfo.Used+ diskTotal = diskInfo.Total+ }++ hostInfo, err := host.Info()+ var hostname string+ if err == nil {+ hostname = hostInfo.Hostname+ }++ stats := SystemStats{+ CPUUsage: cpuUsage,+ MemoryUsage: memoryUsage,+ MemoryTotal: memoryTotal,+ DiskUsage: diskUsage,+ DiskTotal: diskTotal,+ CPUCount: cpuCount,+ Architecture: runtime.GOARCH,+ Platform: runtime.GOOS,+ Hostname: hostname,+ }++ // write as JSON text message+ _ = conn.SetWriteDeadline(time.Now().Add(10 * time.Second))+ return conn.WriteJSON(stats)+ }++ // initial send+ if err := sendStats(); err != nil {+ slog.Debug("failed to send initial system stats websocket", "err", err)+ return+ }++ for {+ select {+ case <-c.Request.Context().Done():+ return+ case <-ticker.C:+ if err := sendStats(); err != nil {+ slog.Debug("failed to send system stats websocket", "err", err)+ return+ }+ }+ }+}diff --git a/frontend/src/lib/components/log-viewer.svelte b/frontend/src/lib/components/log-viewer.svelteindex e9a45fd45..24a2d4165 100644--- a/frontend/src/lib/components/log-viewer.svelte+++ b/frontend/src/lib/components/log-viewer.svelte@@ -4,6 +4,7 @@import { get } from 'svelte/store';import { environmentStore } from '$lib/stores/environment.store';import { m } from '$lib/paraglide/messages';+ import { ReconnectingWebSocket } from '$lib/utils/ws';interface LogEntry {timestamp: string;@@ -46,7 +47,7 @@let isStreaming = $state(false);let error: string | null = $state(null);let eventSource: EventSource | null = null;- let ws: WebSocket | null = null;+ let wsClient: ReconnectingWebSocket<string> | null = null;let currentStreamKey: string | null = null;function streamKey() {return type === 'stack' ? (stackId ? `stack:${stackId}` : null) : containerId ? `ctr:${containerId}` : null;@@ -106,35 +107,39 @@}async function startWebSocketStream() {- const url = await buildLogWsEndpoint();- ws = new WebSocket(url);-- ws.onopen = () => {- if (dev) console.log(m.log_viewer_connected({ type: humanType }));- error = null;- isStreaming = true;- };-- ws.onmessage = (evt) => {- const data = typeof evt.data === 'string' ? evt.data : '';- if (!data) return;- for (const line of data.split('\n')) {- if (!line.trim()) continue;- handleIncomingLine(line);- }- };-- ws.onerror = (evt) => {- console.error('WebSocket log stream error:', evt);- error = m.log_stream_connection_lost({ type: humanType });- };+ wsClient = new ReconnectingWebSocket<string>({+ buildUrl: async () => {+ return await buildLogWsEndpoint();+ },+ parseMessage: (evt) => {+ return typeof evt.data === 'string' ? evt.data : '';+ },+ onOpen: () => {+ if (dev) console.log(m.log_viewer_connected({ type: humanType }));+ error = null;+ isStreaming = true;+ },+ onMessage: (payload) => {+ if (!payload) return;+ for (const line of payload.split('\n')) {+ if (!line.trim()) continue;+ handleIncomingLine(line);+ }+ },+ onError: (e) => {+ console.error('WebSocket log stream error:', e);+ error = m.log_stream_connection_lost({ type: humanType });+ },+ onClose: () => {+ isStreaming = false;+ if (!error) {+ error = m.log_stream_closed_by_server({ type: humanType });+ }+ },+ maxBackoff: 10000+ });- ws.onclose = () => {- isStreaming = false;- if (!error) {- error = m.log_stream_closed_by_server({ type: humanType });- }- };+ await wsClient.connect();}function handleIncomingLine(raw: string) {@@ -165,17 +170,11 @@eventSource.close();eventSource = null;}- if (ws) {+ if (wsClient) {try {- ws.onerror = null;- ws.onclose = null;- if (ws.readyState === WebSocket.CONNECTING) {- ws.addEventListener('open', () => ws?.close(), { once: true });- } else {- ws.close();- }+ wsClient.close();} catch {}- ws = null;+ wsClient = null;}isStreaming = false;onStop?.();diff --git a/frontend/src/lib/services/api/system-api-service.ts b/frontend/src/lib/services/api/system-api-service.tsindex e1049f2eb..1bc30780d 100644--- a/frontend/src/lib/services/api/system-api-service.ts+++ b/frontend/src/lib/services/api/system-api-service.ts@@ -29,10 +29,6 @@ export default class SystemAPIService extends BaseAPIService {return this.handleResponse(this.api.get('/system/docker/info'));}- async getStats() {- return this.handleResponse(this.api.get('/system/stats'));- }-async testConnection(host?: string) {const params = host ? { host } : {};return this.handleResponse(this.api.get('/system/docker/test', { params }));diff --git a/frontend/src/lib/utils/ws.ts b/frontend/src/lib/utils/ws.tsnew file mode 100644index 000000000..503780ce3--- /dev/null+++ b/frontend/src/lib/utils/ws.ts@@ -0,0 +1,156 @@+import type { SystemStats } from '$lib/types/system-stats.type';++export interface ReconnectWSOptions<T> {+ // builds the websocket URL (can be async)+ buildUrl: () => string | Promise<string>;+ // parse an incoming MessageEvent into T (default: JSON.parse for string payloads)+ parseMessage?: (evt: MessageEvent) => T;+ onMessage?: (msg: T) => void;+ onOpen?: () => void;+ onClose?: () => void;+ onError?: (err: Event | Error) => void;+ maxBackoff?: number;+ // optional: whether to autoConnect immediately+ autoConnect?: boolean;+}++export class ReconnectingWebSocket<T = unknown> {+ private ws: WebSocket | null = null;+ private closed = true;+ private attempt = 0;+ private readonly maxBackoff: number;+ private opts: ReconnectWSOptions<T>;+ private connecting = false;++ constructor(opts: ReconnectWSOptions<T>) {+ this.opts = opts;+ this.maxBackoff = opts.maxBackoff ?? 30000;+ if (opts.autoConnect) this.connect();+ }++ async connect() {+ this.closed = false;+ this.attempt = 0;+ await this.connectOnce();+ }++ async connectOnce() {+ if (this.closed || this.connecting) return;+ this.connecting = true;+ let url: string;+ try {+ url = await this.opts.buildUrl();+ } catch (err) {+ this.connecting = false;+ this.scheduleReconnect();+ this.opts.onError?.(err as Error);+ return;+ }++ try {+ this.ws = new WebSocket(url);+ } catch (err) {+ this.connecting = false;+ this.scheduleReconnect();+ this.opts.onError?.(err as Error);+ return;+ }++ this.ws.onopen = () => {+ this.attempt = 0;+ this.connecting = false;+ this.opts.onOpen?.();+ };++ this.ws.onmessage = (evt) => {+ try {+ const parser =+ this.opts.parseMessage ??+ ((e: MessageEvent) => {+ // default: try JSON.parse for string payloads, else return raw data+ if (typeof e.data === 'string') return JSON.parse(e.data) as unknown as T;+ return e.data as unknown as T;+ });+ const msg = parser(evt);+ this.opts.onMessage?.(msg);+ } catch (err) {+ this.opts.onError?.(err as Error);+ }+ };++ this.ws.onerror = (e) => {+ this.opts.onError?.(e);+ };++ this.ws.onclose = () => {+ this.opts.onClose?.();+ this.ws = null;+ if (!this.closed) this.scheduleReconnect();+ };+ }++ private scheduleReconnect() {+ this.attempt++;+ const backoff = Math.min(1000 * Math.pow(1.5, this.attempt), this.maxBackoff);+ setTimeout(() => {+ if (!this.closed) {+ // ignore connectOnce race if already connecting+ this.connectOnce();+ }+ }, backoff);+ }++ send(payload: string | ArrayBuffer | Blob) {+ try {+ if (this.ws && this.ws.readyState === WebSocket.OPEN) {+ this.ws.send(payload as any);+ return true;+ }+ } catch (err) {+ this.opts.onError?.(err as Error);+ }+ return false;+ }++ close() {+ this.closed = true;+ this.attempt = 0;+ try {+ this.ws?.close();+ } catch {+ // ignore+ }+ this.ws = null;+ this.connecting = false;+ }++ isConnected() {+ return !!this.ws && this.ws.readyState === WebSocket.OPEN;+ }+}++/* small convenience wrapper for the dashboard stats (keeps existing shape) */+export function createStatsWebSocket(opts: {+ getEnvId: () => string;+ onMessage: (data: SystemStats) => void;+ onOpen?: () => void;+ onClose?: () => void;+ onError?: (err: Event | Error) => void;+ maxBackoff?: number;+}) {+ const buildUrl = () => {+ const envId = opts.getEnvId() || '0';+ const protocol = location.protocol === 'https:' ? 'wss' : 'ws';+ return `${protocol}://${location.host}/api/environments/${envId}/stats/ws`;+ };++ return new ReconnectingWebSocket<SystemStats>({+ buildUrl,+ parseMessage: (evt) => JSON.parse(evt.data as string) as SystemStats,+ onMessage: opts.onMessage,+ onOpen: opts.onOpen,+ onClose: opts.onClose,+ onError: opts.onError,+ maxBackoff: opts.maxBackoff+ });+}diff --git a/frontend/src/routes/dashboard/+page.svelte b/frontend/src/routes/dashboard/+page.svelteindex c46b633fb..35684d526 100644--- a/frontend/src/routes/dashboard/+page.svelte+++ b/frontend/src/routes/dashboard/+page.svelte@@ -11,6 +11,10 @@import { systemAPI, settingsAPI, environmentAPI } from '$lib/services/api';import { openConfirmDialog } from '$lib/components/confirm-dialog';import { onMount } from 'svelte';+ import { get } from 'svelte/store';+ import { environmentStore } from '$lib/stores/environment.store';+ import { createStatsWebSocket } from '$lib/utils/ws';+ import type { ReconnectingWebSocket } from '$lib/utils/ws';import MeterMetric from '$lib/components/meter-metric.svelte';import QuickActions from '$lib/components/quick-actions.svelte';import DockerDetailsCards from '$lib/components/docker-details-cards.svelte';@@ -18,6 +22,7 @@import DashboardContainerTable from './dash-container-table.svelte';import DashboardImageTable from './dash-image-table.svelte';import { m } from '$lib/paraglide/messages';+ import { invalidateAll } from '$app/navigation';let { data } = $props();let containers = $state(data.containers);@@ -45,7 +50,7 @@});let liveSystemStats = $state(null as SystemStats | null);- let statsInterval: NodeJS.Timeout | null = null;+ let statsWSClient: ReconnectingWebSocket<SystemStats> | null = null;let hasInitialStatsLoaded = $state(false);let historicalData = $state({@@ -93,41 +98,6 @@}}- async function fetchLiveSystemStats() {- if (!hasInitialStatsLoaded) {- isLoading.loadingStats = true;- }-- try {- const response = await systemAPI.getStats();-- let stats: SystemStats | null = null;-- if (response && typeof response === 'object') {- if ('success' in response && response.success && 'data' in response) {- stats = response.data as SystemStats;- } else if ('cpuUsage' in response) {- stats = response as SystemStats;- }- }-- if (stats) {- liveSystemStats = stats;- dashboardStates.systemStats = stats;- addToHistoricalData(stats);- } else {- console.warn('Invalid system stats response format:', response);- }- } catch (error) {- console.error('Failed to fetch live system stats:', error);- } finally {- if (!hasInitialStatsLoaded) {- isLoading.loadingStats = false;- hasInitialStatsLoaded = true;- }- }- }-let imageRequestOptions = $state(data.imageRequestOptions);async function refreshData() {@@ -146,7 +116,7 @@if (dockerInfoResult.status === 'fulfilled' && !dockerInfoResult.value.error) {dashboardStates.dockerInfo = dockerInfoResult.value.data;- dockerInfo = dockerInfoResult.value.data; // keep meters/cards in sync+ dockerInfo = dockerInfoResult.value.data;}isLoading.loadingDockerInfo = false;@@ -165,29 +135,48 @@containerStatusCounts = statusCountsResult.value.data;}- await fetchLiveSystemStats();+ await invalidateAll();isLoading.refreshing = false;}onMount(() => {let mounted = true;- fetchLiveSystemStats();-- if (!statsInterval) {- statsInterval = setInterval(() => {- if (mounted) {- fetchLiveSystemStats();+ (async () => {+ await environmentStore.ready;++ const getEnvId = () => {+ const env = get(environmentStore.selected);+ return env ? env.id : '0';+ };++ statsWSClient = createStatsWebSocket({+ getEnvId,+ onOpen: () => {+ if (!hasInitialStatsLoaded) {+ isLoading.loadingStats = true;+ }+ },+ onMessage: (data) => {+ if (!mounted) return;+ liveSystemStats = data;+ dashboardStates.systemStats = data;+ addToHistoricalData(data);+ hasInitialStatsLoaded = true;+ isLoading.loadingStats = false;+ },+ onError: (e) => {+ console.error('Stats websocket error:', e);}- }, 5000);- }+ });++ statsWSClient.connect();+ })();return () => {mounted = false;- if (statsInterval) {- clearInterval(statsInterval);- statsInterval = null;- }+ statsWSClient?.close();+ statsWSClient = null;};});