Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

feat: AI integration, Docker metrics, RAID display, and infrastructure improvements

rcourtman927ac76bade6
diff --git a/.agent/workflows/dev-environment.md b/.agent/workflows/dev-environment.md
new file mode 100644
index 0000000000..890d17d949
--- /dev/null
+++ b/.agent/workflows/dev-environment.md
@@ -0,0 +1,46 @@
+---
+description: How the development environment works and how to manage it
+---
+
+# Pulse Development Environment
+
+This is a **development-only machine**. Production is never run here.
+
+## Primary Service: `pulse-hot-dev`
+
+The dev environment uses a **single systemd service** that runs both backend and frontend with hot-reloading:
+
+```bash
+# Status check
+systemctl status pulse-hot-dev
+
+# Restart (if needed)
+// turbo
+sudo systemctl restart pulse-hot-dev
+
+# View logs
+journalctl -u pulse-hot-dev -f
+```
+
+### What `pulse-hot-dev` does:
+1. **Go Backend** (`./pulse`): Monitored by `inotifywait` - auto-rebuilds and restarts when `.go` files change
+2. **Vite Frontend**: HMR enabled - browser updates instantly when frontend files change
+
+### Access URLs:
+- **Frontend**: http://192.168.0.123:5173/ (Vite dev server with HMR)
+- **Backend API**: http://192.168.0.123:7655/ (Go server, proxied through Vite)
+
+## DO NOT USE these services in development:
+- `pulse.service` - This is for production (runs pre-built binary without hot-reload)
+- Do NOT create separate `pulse-frontend.service` - the hot-dev script handles everything
+
+## When things don't work:
+
+1. **Frontend not loading at :5173** → Check if `pulse-hot-dev` is running
+2. **Backend changes not reflected** → Check logs: `journalctl -u pulse-hot-dev -f`
+3. **Need full restart** → `sudo systemctl restart pulse-hot-dev`
+
+## Key Files:
+- **Hot-dev script**: `/opt/pulse/scripts/hot-dev.sh`
+- **Systemd service**: `/etc/systemd/system/pulse-hot-dev.service`
+- **Makefile targets**: `make dev` or `make dev-hot`
diff --git a/.gitignore b/.gitignore
index 81f66e9b4f..431a06acdf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -157,4 +157,3 @@ tmp_*.sh
# Local agent directories
scripts/agent/
docs/internal/
-.agent/
diff --git a/cmd/pulse/main.go b/cmd/pulse/main.go
index a299ac95b7..ef3cb0c379 100644
--- a/cmd/pulse/main.go
+++ b/cmd/pulse/main.go
@@ -141,8 +141,15 @@ func runServer() {
}
// Set state getter for WebSocket hub
+ // IMPORTANT: Return StateFrontend (not StateSnapshot) to match broadcast format.
+ // StateSnapshot uses time.Time fields while StateFrontend uses Unix timestamps,
+ // and includes frontend-specific field transformations. Without this conversion,
+ // nodes/hosts would be missing on initial page load but appear after broadcasts.
wsHub.SetStateGetter(func() interface{} {
- return reloadableMonitor.GetState()
+ // GetMonitor().GetState() returns models.StateSnapshot
+ state := reloadableMonitor.GetMonitor().GetState()
+ // Convert to frontend format, matching what BroadcastState does
+ return state.ToFrontend()
})
// Wire up Prometheus metrics for alert lifecycle
diff --git a/frontend-modern/src/App.tsx b/frontend-modern/src/App.tsx
index d7a14ea0ef..ce3f104ccb 100644
--- a/frontend-modern/src/App.tsx
+++ b/frontend-modern/src/App.tsx
@@ -56,6 +56,7 @@ const StorageComponent = lazy(() => import('./components/Storage/Storage'));
const Backups = lazy(() => import('./components/Backups/Backups'));
const Replication = lazy(() => import('./components/Replication/Replication'));
const MailGateway = lazy(() => import('./components/PMG/MailGateway'));
+const CephPage = lazy(() => import('./pages/Ceph'));
const AlertsPage = lazy(() =>
import('./pages/Alerts').then((module) => ({ default: module.Alerts })),
);
@@ -105,18 +106,9 @@ function DockerRoute() {
return <DockerHosts hosts={asDockerHosts() as any} activeAlerts={activeAlerts} />;
}
-// Hosts route component - uses unified resources via useResourcesAsLegacy hook
+// Hosts route component - HostsOverview uses useResourcesAsLegacy directly for proper reactivity
function HostsRoute() {
- const wsContext = useContext(WebSocketContext);
- if (!wsContext) {
- return <div>Loading...</div>;
- }
- const { state } = wsContext;
- const { asHosts } = useResourcesAsLegacy();
-
- return (
- <HostsOverview hosts={asHosts() as any} connectionHealth={state.connectionHealth ?? {}} />
- );
+ return <HostsOverview />;
}
// Helper to detect if an update is actively in progress (not just checking for updates)
@@ -877,6 +869,7 @@ function App() {
<Route path="/proxmox" component={() => <Navigate href="/proxmox/overview" />} />
<Route path="/proxmox/overview" component={DashboardView} />
<Route path="/proxmox/storage" component={StorageComponent} />
+ <Route path="/proxmox/ceph" component={CephPage} />
<Route path="/proxmox/replication" component={Replication} />
<Route path="/proxmox/mail" component={MailGateway} />
<Route path="/proxmox/backups" component={Backups} />
diff --git a/frontend-modern/src/api/__tests__/charts.test.ts b/frontend-modern/src/api/__tests__/charts.test.ts
new file mode 100644
index 0000000000..b50c7dd487
--- /dev/null
+++ b/frontend-modern/src/api/__tests__/charts.test.ts
@@ -0,0 +1,259 @@
+/**
+ * Tests for Charts API types and interface
+ */
+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
+import type { ChartData, ChartsResponse, TimeRange, MetricPoint, ChartStats } from '@/api/charts';
+
+// Note: We test the types and interfaces here since the actual API calls
+// require a running backend. Integration tests should cover the full flow.
+
+describe('Charts API Types', () => {
+ describe('TimeRange', () => {
+ it('supports all expected time range values', () => {
+ const validRanges: TimeRange[] = ['5m', '15m', '30m', '1h', '4h', '12h', '24h', '7d'];
+
+ validRanges.forEach(range => {
+ // This is a compile-time check - if it compiles, the types are correct
+ const r: TimeRange = range;
+ expect(r).toBe(range);
+ });
+ });
+ });
+
+ describe('MetricPoint', () => {
+ it('has required timestamp and value properties', () => {
+ const point: MetricPoint = {
+ timestamp: 1733700000000,
+ value: 45.7,
+ };
+
+ expect(point.timestamp).toBe(1733700000000);
+ expect(point.value).toBe(45.7);
+ });
+ });
+
+ describe('ChartData', () => {
+ it('supports all metric types', () => {
+ const data: ChartData = {
+ cpu: [{ timestamp: 1000, value: 50 }],
+ memory: [{ timestamp: 1000, value: 60 }],
+ disk: [{ timestamp: 1000, value: 70 }],
+ diskread: [{ timestamp: 1000, value: 1000000 }],
+ diskwrite: [{ timestamp: 1000, value: 500000 }],
+ netin: [{ timestamp: 1000, value: 2000000 }],
+ netout: [{ timestamp: 1000, value: 1500000 }],
+ };
+
+ expect(data.cpu).toHaveLength(1);
+ expect(data.memory![0].value).toBe(60);
+ });
+
+ it('allows partial data (all fields optional)', () => {
+ const cpuOnly: ChartData = {
+ cpu: [{ timestamp: 1000, value: 25 }],
+ };
+
+ expect(cpuOnly.cpu).toBeDefined();
+ expect(cpuOnly.memory).toBeUndefined();
+ expect(cpuOnly.disk).toBeUndefined();
+ });
+
+ it('allows empty arrays for metrics', () => {
+ const emptyData: ChartData = {
+ cpu: [],
+ memory: [],
+ };
+
+ expect(emptyData.cpu).toHaveLength(0);
+ });
+ });
+
+ describe('ChartsResponse', () => {
+ it('includes all required fields', () => {
+ const response: ChartsResponse = {
+ data: {},
+ nodeData: {},
+ storageData: {},
+ timestamp: Date.now(),
+ stats: { oldestDataTimestamp: Date.now() - 3600000 },
+ };
+
+ expect(response.data).toBeDefined();
+ expect(response.nodeData).toBeDefined();
+ expect(response.timestamp).toBeGreaterThan(0);
+ });
+
+ it('supports optional dockerData field', () => {
+ const response: ChartsResponse = {
+ data: {},
+ nodeData: {},
+ storageData: {},
+ dockerData: {
+ 'container-abc123': {
+ cpu: [{ timestamp: 1000, value: 10 }],
+ memory: [{ timestamp: 1000, value: 20 }],
+ },
+ },
+ timestamp: Date.now(),
+ stats: { oldestDataTimestamp: Date.now() },
+ };
+
+ expect(response.dockerData).toBeDefined();
+ expect(response.dockerData!['container-abc123'].cpu).toHaveLength(1);
+ });
+
+ it('supports optional dockerHostData field', () => {
+ const response: ChartsResponse = {
+ data: {},
+ nodeData: {},
+ storageData: {},
+ dockerHostData: {
+ 'host-1': {
+ cpu: [{ timestamp: 1000, value: 30 }],
+ memory: [{ timestamp: 1000, value: 40 }],
+ },
+ },
+ timestamp: Date.now(),
+ stats: { oldestDataTimestamp: Date.now() },
+ };
+
+ expect(response.dockerHostData).toBeDefined();
+ expect(response.dockerHostData!['host-1'].cpu![0].value).toBe(30);
+ });
+
+ it('supports optional guestTypes field for VM/container type mapping', () => {
+ const response: ChartsResponse = {
+ data: {
+ 'vm-100': { cpu: [{ timestamp: 1000, value: 50 }] },
+ 'ct-200': { cpu: [{ timestamp: 1000, value: 30 }] },
+ },
+ nodeData: {},
+ storageData: {},
+ guestTypes: {
+ 'vm-100': 'vm',
+ 'ct-200': 'container',
+ },
+ timestamp: Date.now(),
+ stats: { oldestDataTimestamp: Date.now() },
+ };
+
+ expect(response.guestTypes!['vm-100']).toBe('vm');
+ expect(response.guestTypes!['ct-200']).toBe('container');
+ });
+
+ it('contains all data sources for comprehensive monitoring', () => {
+ const comprehensiveResponse: ChartsResponse = {
+ // VM and container metrics (legacy format)
+ data: {
+ 'pve1/qemu/100': {
+ cpu: [{ timestamp: 1000, value: 45 }],
+ memory: [{ timestamp: 1000, value: 55 }],
+ disk: [{ timestamp: 1000, value: 30 }],
+ },
+ 'pve1/lxc/200': {
+ cpu: [{ timestamp: 1000, value: 15 }],
+ },
+ },
+ // Node metrics
+ nodeData: {
+ 'pve1': {
+ cpu: [{ timestamp: 1000, value: 35 }],
+ memory: [{ timestamp: 1000, value: 65 }],
+ },
+ },
+ // Storage metrics
+ storageData: {
+ 'local-zfs': {
+ disk: [{ timestamp: 1000, value: 50 }],
+ },
+ },
+ // Docker container metrics
+ dockerData: {
+ 'abc123': {
+ cpu: [{ timestamp: 1000, value: 5 }],
+ memory: [{ timestamp: 1000, value: 128 }],
+ },
+ },
+ // Docker host metrics
+ dockerHostData: {
+ 'docker-host-1': {
+ cpu: [{ timestamp: 1000, value: 25 }],
+ },
+ },
+ // Guest type mapping
+ guestTypes: {
+ 'pve1/qemu/100': 'vm',
+ 'pve1/lxc/200': 'container',
+ },
+ timestamp: 1733700000000,
+ stats: {
+ oldestDataTimestamp: 1733696400000, // 1 hour ago
+ },
+ };
+
+ expect(Object.keys(comprehensiveResponse.data)).toHaveLength(2);
+ expect(Object.keys(comprehensiveResponse.nodeData)).toHaveLength(1);
+ expect(Object.keys(comprehensiveResponse.dockerData!)).toHaveLength(1);
+ expect(Object.keys(comprehensiveResponse.dockerHostData!)).toHaveLength(1);
+ });
+ });
+
+ describe('ChartStats', () => {
+ it('contains oldest data timestamp for data availability', () => {
+ const stats: ChartStats = {
+ oldestDataTimestamp: 1733696400000,
+ };
+
+ expect(stats.oldestDataTimestamp).toBe(1733696400000);
+ });
+
+ it('can be used to determine available data range', () => {
+ const now = Date.now();
+ const oneHourAgo = now - 3600000;
+
+ const stats: ChartStats = {
+ oldestDataTimestamp: oneHourAgo,
+ };
+
+ const availableRangeMs = now - stats.oldestDataTimestamp;
+ expect(availableRangeMs).toBeCloseTo(3600000, -2); // Allow 100ms tolerance
+ });
+ });
+});
+
+describe('Time Range to Milliseconds Conversion', () => {
+ // This tests the concept matching metricsHistory.ts timeRangeToMs
+ function timeRangeToMs(range: TimeRange): number {
+ switch (range) {
+ case '5m': return 5 * 60 * 1000;
+ case '15m': return 15 * 60 * 1000;
+ case '30m': return 30 * 60 * 1000;
+ case '1h': return 60 * 60 * 1000;
+ case '4h': return 4 * 60 * 60 * 1000;
+ case '12h': return 12 * 60 * 60 * 1000;
+ case '24h': return 24 * 60 * 60 * 1000;
+ case '7d': return 7 * 24 * 60 * 60 * 1000;
+ default: return 60 * 60 * 1000;
+ }
+ }
+
+ const expectedValues: [TimeRange, number][] = [
+ ['5m', 300000],
+ ['15m', 900000],
+ ['30m', 1800000],
+ ['1h', 3600000],
+ ['4h', 14400000],
+ ['12h', 43200000],
+ ['24h', 86400000],
+ ['7d', 604800000],
+ ];
+
+ it.each(expectedValues)('converts %s to %d ms', (range, expectedMs) => {
+ expect(timeRangeToMs(range)).toBe(expectedMs);
+ });
+
+ it('defaults to 1 hour for unknown range', () => {
+ // @ts-expect-error - Testing invalid input
+ expect(timeRangeToMs('invalid')).toBe(3600000);
+ });
+});
diff --git a/frontend-modern/src/api/ai.ts b/frontend-modern/src/api/ai.ts
index de4547e8d4..c8b9122cda 100644
--- a/frontend-modern/src/api/ai.ts
+++ b/frontend-modern/src/api/ai.ts
@@ -32,6 +32,30 @@ export class AIAPI {
}) as Promise<AITestResult>;
}
+ // Start OAuth flow for Claude Pro/Max subscription
+ // Returns the authorization URL to redirect the user to
+ static async startOAuth(): Promise<{ auth_url: string; state: string }> {
+ return apiFetchJSON(`${this.baseUrl}/ai/oauth/start`, {
+ method: 'POST',
+ }) as Promise<{ auth_url: string; state: string }>;
+ }
+
+ // Exchange manually-pasted authorization code for tokens
+ static async exchangeOAuthCode(code: string, state: string): Promise<{ success: boolean; message: string }> {
+ return apiFetchJSON(`${this.baseUrl}/ai/oauth/exchange`, {
+ method: 'POST',
+ body: JSON.stringify({ code, state }),
+ }) as Promise<{ success: boolean; message: string }>;
+ }
+
+ // Disconnect OAuth and clear tokens
+ static async disconnectOAuth(): Promise<{ success: boolean; message: string }> {
+ return apiFetchJSON(`${this.baseUrl}/ai/oauth/disconnect`, {
+ method: 'POST',
+ }) as Promise<{ success: boolean; message: string }>;
+ }
+
+
// Execute an AI prompt
static async execute(request: AIExecuteRequest): Promise<AIExecuteResponse> {
return apiFetchJSON(`${this.baseUrl}/ai/execute`, {
diff --git a/frontend-modern/src/api/charts.ts b/frontend-modern/src/api/charts.ts
index 962d8ab0b5..9a8f23f4dd 100644
--- a/frontend-modern/src/api/charts.ts
+++ b/frontend-modern/src/api/charts.ts
@@ -31,6 +31,8 @@ export interface ChartsResponse {
data: Record<string, ChartData>; // VM/Container data keyed by ID
nodeData: Record<string, ChartData>; // Node data keyed by ID
storageData: Record<string, ChartData>; // Storage data keyed by ID
+ dockerData?: Record<string, ChartData>; // Docker container data keyed by container ID
+ dockerHostData?: Record<string, ChartData>; // Docker host data keyed by host ID
guestTypes?: Record<string, 'vm' | 'container'>; // Maps guest ID to type
timestamp: number;
stats: ChartStats;
diff --git a/frontend-modern/src/components/Dashboard/EnhancedCPUBar.tsx b/frontend-modern/src/components/Dashboard/EnhancedCPUBar.tsx
index 9785abd11d..3606625a19 100644
--- a/frontend-modern/src/components/Dashboard/EnhancedCPUBar.tsx
+++ b/frontend-modern/src/components/Dashboard/EnhancedCPUBar.tsx
@@ -117,7 +117,7 @@ export function EnhancedCPUBar(props: EnhancedCPUBarProps) {
}
>
{/* Sparkline mode - full width, flex centered like stacked bars */}
- <div class="metric-text w-full h-4 flex items-center justify-center">
+ <div class="metric-text w-full h-4 flex items-center justify-center min-w-0 overflow-hidden">
<Sparkline
data={metricHistory()}
metric="cpu"
diff --git a/frontend-modern/src/components/Dashboard/MetricBar.tsx b/frontend-modern/src/components/Dashboard/MetricBar.tsx
index bd53bfaacb..d6d0a0da76 100644
--- a/frontend-modern/src/components/Dashboard/MetricBar.tsx
+++ b/frontend-modern/src/components/Dashboard/MetricBar.tsx
@@ -124,7 +124,7 @@ export function MetricBar(props: MetricBarProps) {
}
>
{/* Sparkline mode - full width, flex centered like stacked bars */}
- <div class="metric-text w-full h-4 flex items-center justify-center">
+ <div class="metric-text w-full h-4 flex items-center justify-center min-w-0 overflow-hidden">
<Sparkline
data={metricHistory()}
metric={sparklineMetric()}
diff --git a/frontend-modern/src/components/Dashboard/StackedMemoryBar.tsx b/frontend-modern/src/components/Dashboard/StackedMemoryBar.tsx
index 788a556e36..1593d5f5d5 100644
--- a/frontend-modern/src/components/Dashboard/StackedMemoryBar.tsx
+++ b/frontend-modern/src/components/Dashboard/StackedMemoryBar.tsx
@@ -251,7 +251,7 @@ export function StackedMemoryBar(props: StackedMemoryBarProps) {
}
>
{/* Sparkline mode - full width, flex centered like stacked bars */}
- <div class="metric-text w-full h-4 flex items-center justify-center">
+ <div class="metric-text w-full h-4 flex items-center justify-center min-w-0 overflow-hidden">
<Sparkline
data={metricHistory()}
metric="memory"
diff --git a/frontend-modern/src/components/Hosts/HostsOverview.tsx b/frontend-modern/src/components/Hosts/HostsOverview.tsx
index c1954d7edb..dfd6da0ae7 100644
--- a/frontend-modern/src/components/Hosts/HostsOverview.tsx
+++ b/frontend-modern/src/components/Hosts/HostsOverview.tsx
@@ -2,7 +2,7 @@ import type { Component } from 'solid-js';
import { For, Show, createMemo, createSignal, onMount, onCleanup } from 'solid-js';
import { Portal } from 'solid-js/web';
import { useNavigate } from '@solidjs/router';
-import type { Host } from '@/types/api';
+import type { Host, HostRAIDArray } from '@/types/api';
import { formatBytes, formatRelativeTime, formatUptime } from '@/utils/format';
import { Card } from '@/components/shared/Card';
import { EmptyState } from '@/components/shared/EmptyState';
@@ -18,6 +18,7 @@ import { useBreakpoint, type ColumnPriority } from '@/hooks/useBreakpoint';
import { useColumnVisibility } from '@/hooks/useColumnVisibility';
import { aiChatStore } from '@/stores/aiChat';
import { STORAGE_KEYS } from '@/utils/localStorage';
+import { useResourcesAsLegacy } from '@/hooks/useResources';
// Column definition for hosts table
export interface HostColumnDef {
@@ -262,14 +263,214 @@ function HostTemperatureCell(props: { sensors: Record<string, number> | null | u
);
}
-type SortKey = 'name' | 'platform' | 'cpu' | 'memory' | 'disk' | 'uptime';
+// RAID status cell with rich tooltip showing array details
+interface HostRAIDStatusCellProps {
+ raid: HostRAIDArray[] | undefined;
+}
+
+function HostRAIDStatusCell(props: HostRAIDStatusCellProps) {
+ const [showTooltip, setShowTooltip] = createSignal(false);
+ const [tooltipPos, setTooltipPos] = createSignal({ x: 0, y: 0 });
+
+ const hasArrays = () => props.raid && props.raid.length > 0;
+
+ // Analyze overall status
+ const status = createMemo(() => {
+ if (!props.raid || props.raid.length === 0) {
+ return { type: 'none' as const, label: '-', color: 'text-gray-400' };
+ }
+
+ let hasDegraded = false;
+ let hasRebuilding = false;
+ let maxRebuildPercent = 0;
+
+ for (const array of props.raid) {
+ const state = array.state.toLowerCase();
+ if (state.includes('degraded') || array.failedDevices > 0) {
+ hasDegraded = true;
+ }
+ if (state.includes('recover') || state.includes('resync') || array.rebuildPercent > 0) {
+ hasRebuilding = true;
+ maxRebuildPercent = Math.max(maxRebuildPercent, array.rebuildPercent);
+ }
+ }
+
+ if (hasDegraded) {
+ return { type: 'degraded' as const, label: 'Degraded', color: 'text-red-600 dark:text-red-400' };
+ }
+ if (hasRebuilding) {
+ return {
+ type: 'rebuilding' as const,
+ label: `${Math.round(maxRebuildPercent)}%`,
+ color: 'text-amber-600 dark:text-amber-400'
+ };
+ }
+ return { type: 'ok' as const, label: 'OK', color: 'text-green-600 dark:text-green-400' };
+ });
+
+ const handleMouseEnter = (e: MouseEvent) => {
+ const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
+ setTooltipPos({ x: rect.left + rect.width / 2, y: rect.top });
+ setShowTooltip(true);
+ };
+
+ const handleMouseLeave = () => {
+ setShowTooltip(false);
+ };
+
+ // Get state color for individual devices
+ const getDeviceStateColor = (state: string) => {
+ const s = state.toLowerCase();
+ if (s.includes('active') || s.includes('sync')) return 'text-green-400';
+ if (s.includes('spare')) return 'text-blue-400';
+ if (s.includes('faulty') || s.includes('removed')) return 'text-red-400';
+ if (s.includes('rebuilding')) return 'text-amber-400';
+ return 'text-gray-400';
+ };
+
+ // Get array state color
+ const getArrayStateColor = (array: HostRAIDArray) => {
+ const state = array.state.toLowerCase();
+ if (state.includes('degraded') || array.failedDevices > 0) return 'text-red-400';
+ if (state.includes('recover') || state.includes('resync') || array.rebuildPercent > 0) return 'text-amber-400';
+ if (state.includes('clean') || state.includes('active')) return 'text-green-400';
+ return 'text-gray-400';
+ };
+
+ return (
+ <>
+ <span
+ class={`inline-flex items-center gap-1 text-xs whitespace-nowrap ${status().color} ${hasArrays() ? 'cursor-help' : ''}`}
+ onMouseEnter={handleMouseEnter}
+ onMouseLeave={handleMouseLeave}
+ >
+ <Show when={hasArrays()} fallback={<span class="text-gray-400">—</span>}>
+ {/* Status icon */}
+ <Show when={status().type === 'ok'}>
+ <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
+ <path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75L11.25 15 15 9.75M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
+ </svg>
+ </Show>
+ <Show when={status().type === 'degraded'}>
+ <svg class="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
+ <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
+ </svg>
+ </Show>
+ <Show when={status().type === 'rebuilding'}>
+ <svg class="w-3 h-3 animate-spin" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
+ <path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0l3.181 3.183a8.25 8.25 0 0013.803-3.7M4.031 9.865a8.25 8.25 0 0113.803-3.7l3.181 3.182m0-4.991v4.99" />
+ </svg>
+ </Show>
+ <span class="text-[10px] font-medium">
+ {props.raid!.length > 1 ? `${props.raid!.length} ` : ''}{status().label}
+ </span>
+ </Show>
+ </span>
-interface HostsOverviewProps {
- hosts: Host[];
- connectionHealth: Record<string, boolean>;
+ <Show when={showTooltip() && hasArrays()}>
+ <Portal mount={document.body}>
+ <div
+ class="fixed z-[9999] pointer-events-none"
+ style={{
+ left: `${tooltipPos().x}px`,
+ top: `${tooltipPos().y - 8}px`,
+ transform: 'translate(-50%, -100%)',
+ }}
+ >
+ <div class="bg-gray-900 dark:bg-gray-800 text-white text-[10px] rounded-md shadow-lg px-2.5 py-2 min-w-[200px] max-w-[320px] border border-gray-700">
+ <div class="font-medium mb-1.5 text-gray-300 border-b border-gray-700 pb-1 flex items-center gap-1.5">
+ <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5">
+ <path stroke-linecap="round" stroke-linejoin="round" d="M5.25 14.25h13.5m-13.5 0a3 3 0 01-3-3m3 3a3 3 0 100 6h13.5a3 3 0 100-6m-16.5-3a3 3 0 013-3h13.5a3 3 0 013 3m-19.5 0a4.5 4.5 0 01.9-2.7L5.737 5.1a3.375 3.375 0 012.7-1.35h7.126c1.062 0 2.062.5 2.7 1.35l2.587 3.45a4.5 4.5 0 01.9 2.7m0 0a3 3 0 01-3 3m0 3h.008v.008h-.008v-.008zm0-6h.008v.008h-.008v-.008zm-3 6h.008v.008h-.008v-.008zm0-6h.008v.008h-.008v-.008z" />
+ </svg>
+ RAID Arrays ({props.raid!.length})
+ </div>
+
+ <div class="space-y-2">
+ <For each={props.raid}>
+ {(array) => (
+ <div class="border-b border-gray-700/50 pb-1.5 last:border-0 last:pb-0">
+ {/* Array header */}
+ <div class="flex items-center justify-between gap-2 mb-1">
+ <div class="flex items-center gap-1.5">
+ <span class="font-mono text-blue-400">{array.device}</span>
+ <span class="text-gray-500 uppercase text-[9px]">{array.level}</span>
+ </div>
+ <span class={`font-medium capitalize ${getArrayStateColor(array)}`}>
+ {array.state}
+ </span>
+ </div>
+
+ {/* Array name if present */}
+ <Show when={array.name}>
+ <div class="text-[9px] text-gray-500 mb-1">{array.name}</div>
+ </Show>
+
+ {/* Device counts */}
+ <div class="flex flex-wrap gap-x-2 gap-y-0.5 text-[9px] text-gray-400 mb-1">
+ <span>Active: <span class="text-green-400">{array.activeDevices}</span></span>
+ <span>Working: <span class="text-gray-200">{array.workingDevices}</span></span>
+ <Show when={array.spareDevices > 0}>
+ <span>Spare: <span class="text-blue-400">{array.spareDevices}</span></span>
+ </Show>
+ <Show when={array.failedDevices > 0}>
+ <span>Failed: <span class="text-red-400">{array.failedDevices}</span></span>
+ </Show>
+ </div>
+
+ {/* Rebuild progress */}
+ <Show when={array.rebuildPercent > 0}>
+ <div class="mb-1">
+ <div class="flex items-center justify-between text-[9px] mb-0.5">
+ <span class="text-amber-400">Rebuilding</span>
+ <span class="text-gray-300">{array.rebuildPercent.toFixed(1)}%</span>
+ </div>
+ <div class="h-1 bg-gray-700 rounded-full overflow-hidden">
+ <div
+ class="h-full bg-amber-500 transition-all duration-300"
+ style={{ width: `${array.rebuildPercent}%` }}
+ />
+ </div>
+ <Show when={array.rebuildSpeed}>
+ <div class="text-[9px] text-gray-500 mt-0.5">
+ Speed: {array.rebuildSpeed}
+ </div>
+ </Show>
+ </div>
+ </Show>
+
+ {/* Individual devices */}
+ <Show when={array.devices && array.devices.length > 0}>
+ <div class="flex flex-wrap gap-1 mt-1">
+ <For each={array.devices}>
+ {(dev) => (
+ <span
+ class={`font-mono text-[9px] px-1 py-0.5 rounded bg-gray-800 ${getDeviceStateColor(dev.state)}`}
+ title={`${dev.device} - ${dev.state}`}
+ >
+ {dev.device.replace('/dev/', '')}
+ </span>
+ )}
+ </For>
+ </div>
+ </Show>
+ </div>
+ )}
+ </For>
+ </div>
+ </div>
+ </div>
+ </Portal>
+ </Show>
+ </>
+ );
}
-export const HostsOverview: Component<HostsOverviewProps> = (props) => {
+type SortKey = 'name' | 'platform' | 'cpu' | 'memory' | 'disk' | 'uptime';
+
+// eslint-disable-next-line @typescript-eslint/no-empty-interface
+interface HostsOverviewProps { }
+
+export const HostsOverview: Component<HostsOverviewProps> = () => {
const navigate = useNavigate();
const wsContext = useWebSocket();
const [search, setSearch] = createSignal('');
@@ -278,6 +479,10 @@ export const HostsOverview: Component<HostsOverviewProps> = (props) => {
const [sortDirection, setSortDirection] = createSignal<'asc' | 'desc'>('asc');
const { isMobile } = useBreakpoint();
+ // Use the hook directly to ensure reactivity is maintained
+ // This fixes the issue where props.hosts would not update when the underlying data changes
+ const { asHosts } = useResourcesAsLegacy();
+
// Column visibility management
const columnVisibility = useColumnVisibility(
STORAGE_KEYS.HOSTS_HIDDEN_COLUMNS,
@@ -331,16 +536,19 @@ export const HostsOverview: Component<HostsOverviewProps> = (props) => {
const reconnecting = () => wsContext.reconnecting();
const reconnect = () => wsContext.reconnect();
+ // Access asHosts() directly inside the memo to maintain reactivity
+ const hosts = () => asHosts() as Host[];
+
const isInitialLoading = createMemo(() => {
- return !connected() && !reconnecting() && props.hosts.length === 0;
+ return !connected() && !reconnecting() && hosts().length === 0;
});
const sortedHosts = createMemo(() => {
- const hosts = [...props.hosts];
+ const hostList = [...hosts()];
const key = sortKey();
const direction = sortDirection();
- return hosts.sort((a, b) => {
+ return hostList.sort((a: Host, b: Host) => {
let comparison = 0;
switch (key) {
case 'name':
@@ -356,8 +564,8 @@ export const HostsOverview: Component<HostsOverviewProps> = (props) => {
comparison = (a.memory?.usage ?? 0) - (b.memory?.usage ?? 0);
break;
case 'disk': {
- const aDisk = a.disks?.reduce((sum, d) => sum + (d.usage ?? 0), 0) ?? 0;
- const bDisk = b.disks?.reduce((sum, d) => sum + (d.usage ?? 0), 0) ?? 0;
+ const aDisk = a.disks?.reduce((sum: number, d: { usage?: number }) => sum + (d.usage ?? 0), 0) ?? 0;
+ const bDisk = b.disks?.reduce((sum: number, d: { usage?: number }) => sum + (d.usage ?? 0), 0) ?? 0;
comparison = aDisk - bDisk;
break;
}
@@ -410,27 +618,6 @@ export const HostsOverview: Component<HostsOverviewProps> = (props) => {
- const getRaidStatus = (host: Host): { status: string; color: string } => {
- if (!host.raid || host.raid.length === 0) return { status: '-', color: 'text-gray-400' };
-
- let hasDegraded = false;
- let hasRebuilding = false;
-
- for (const array of host.raid) {
- const state = array.state.toLowerCase();
- if (state.includes('degraded') || array.failedDevices > 0) {
- hasDegraded = true;
- }
- if (state.includes('recover') || state.includes('resync') || array.rebuildPercent > 0) {
- hasRebuilding = true;
- }
- }
-
- if (hasDegraded) return { status: 'Degraded', color: 'text-red-600 dark:text-red-400' };
- if (hasRebuilding) return { status: 'Rebuild', color: 'text-amber-600 dark:text-amber-400' };
- return { status: 'OK', color: 'text-green-600 dark:text-green-400' };
- };
-
const thClass = "px-2 py-1 text-center text-[11px] sm:text-xs font-medium uppercase tracking-wider cursor-pointer hover:bg-gray-200 dark:hover:bg-gray-600 whitespace-nowrap";
return (
@@ -604,13 +791,13 @@ export const HostsOverview: Component<HostsOverviewProps> = (props) => {
<th class={thClass}>Kernel</th>
</Show>
<Show when={isColVisible('raid')}>
- <th class={thClass}>RAID</th>
+ <th class={thClass} title="Linux Software RAID (mdadm) Status">RAID</th>
</Show>
</tr>
</thead>
<tbody class="divide-y divide-gray-200 dark:divide-gray-700">
<For each={filteredHosts()}>
- {(host) => <HostRow host={host} isColVisible={isColVisible} isMobile={isMobile} getDiskStats={getDiskStats} getRaidStatus={getRaidStatus} />}
+ {(host) => <HostRow host={host} isColVisible={isColVisible} isMobile={isMobile} getDiskStats={getDiskStats} />}
</For>
</tbody>
</table>
@@ -661,8 +848,6 @@ interface HostRowProps {
isMobile: () => boolean;
getDiskStats: (host: Host) => { percent: number; used: number; total: number };
-
- getRaidStatus: (host: Host) => { status: string; color: string };
}
const HostRow: Component<HostRowProps> = (props) => {
@@ -709,7 +894,6 @@ const HostRow: Component<HostRowProps> = (props) => {
const cpuPercent = host.cpuUsage ?? 0;
const memPercent = host.memory?.usage ?? 0;
const diskStats = props.getDiskStats(host);
- const raidStatus = props.getRaidStatus(host);
const rowClass = () => {
const base = 'transition-all duration-200';
@@ -909,9 +1093,7 @@ const HostRow: Component<HostRowProps> = (props) => {
<Show when={props.isColVisible('raid')}>
<td class="px-2 py-1 align-middle">
<div class="flex justify-center">
- <span class={`text-[10px] font-medium ${raidStatus.color}`}>
- {raidStatus.status}
- </span>
+ <HostRAIDStatusCell raid={host.raid} />
</div>
</td>
</Show>
diff --git a/frontend-modern/src/components/Hosts/__tests__/RAIDStatus.test.ts b/frontend-modern/src/components/Hosts/__tests__/RAIDStatus.test.ts
new file mode 100644
index 0000000000..b7a9f6da7a
--- /dev/null
+++ b/frontend-modern/src/components/Hosts/__tests__/RAIDStatus.test.ts
@@ -0,0 +1,478 @@
+/**
+ * Tests for RAID status display logic
+ *
+ * These tests cover the RAID status analysis and display logic used in HostsOverview.
+ */
+import { describe, expect, it } from 'vitest';
+
+// Mock types matching HostRAIDArray from api.ts
+interface HostRAIDDevice {
+ device: string;
+ state: string;
+ slot: number;
+}
+
+interface HostRAIDArray {
+ device: string;
+ name?: string;
+ level: string;
+ state: string;
+ totalDevices: number;
+ activeDevices: number;
+ workingDevices: number;
+ failedDevices: number;
+ spareDevices: number;
+ uuid?: string;
+ devices: HostRAIDDevice[];
+ rebuildPercent: number;
+ rebuildSpeed?: string;
+}
+
+// Status analysis logic matching HostRAIDStatusCell
+type RAIDStatusType = 'none' | 'ok' | 'degraded' | 'rebuilding';
+
+interface RAIDStatus {
+ type: RAIDStatusType;
+ label: string;
+ color: string;
+}
+
+function analyzeRAIDStatus(raid: HostRAIDArray[] | undefined): RAIDStatus {
+ if (!raid || raid.length === 0) {
+ return { type: 'none', label: '-', color: 'text-gray-400' };
+ }
+
+ let hasDegraded = false;
+ let hasRebuilding = false;
+ let maxRebuildPercent = 0;
+
+ for (const array of raid) {
+ const state = array.state.toLowerCase();
+ if (state.includes('degraded') || array.failedDevices > 0) {
+ hasDegraded = true;
+ }
+ if (state.includes('recover') || state.includes('resync') || array.rebuildPercent > 0) {
+ hasRebuilding = true;
+ maxRebuildPercent = Math.max(maxRebuildPercent, array.rebuildPercent);
+ }
+ }
+
+ if (hasDegraded) {
+ return { type: 'degraded', label: 'Degraded', color: 'text-red-600 dark:text-red-400' };
+ }
+ if (hasRebuilding) {
+ return {
+ type: 'rebuilding',
+ label: `${Math.round(maxRebuildPercent)}%`,
+ color: 'text-amber-600 dark:text-amber-400'
+ };
+ }
+ return { type: 'ok', label: 'OK', color: 'text-green-600 dark:text-green-400' };
+}
+
+function getDeviceStateColor(state: string): string {
+ const s = state.toLowerCase();
+ if (s.includes('active') || s.includes('sync')) return 'text-green-400';
+ if (s.includes('spare')) return 'text-blue-400';
+ if (s.includes('faulty') || s.includes('removed')) return 'text-red-400';
+ if (s.includes('rebuilding')) return 'text-amber-400';
+ return 'text-gray-400';
+}
+
+function getArrayStateColor(array: HostRAIDArray): string {
+ const state = array.state.toLowerCase();
+ if (state.includes('degraded') || array.failedDevices > 0) return 'text-red-400';
+ if (state.includes('recover') || state.includes('resync') || array.rebuildPercent > 0) return 'text-amber-400';
+ if (state.includes('clean') || state.includes('active')) return 'text-green-400';
+ return 'text-gray-400';
+}
+
+describe('RAID Status Analysis', () => {
+ describe('analyzeRAIDStatus', () => {
+ it('returns none status for undefined raid', () => {
+ const status = analyzeRAIDStatus(undefined);
+ expect(status.type).toBe('none');
+ expect(status.label).toBe('-');
+ });
+
+ it('returns none status for empty raid array', () => {
+ const status = analyzeRAIDStatus([]);
+ expect(status.type).toBe('none');
+ });
+
+ it('returns ok status for healthy RAID1', () => {
+ const raid: HostRAIDArray[] = [{
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean, active',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [
+ { device: 'sda1', state: 'active sync', slot: 0 },
+ { device: 'sdb1', state: 'active sync', slot: 1 },
+ ],
+ rebuildPercent: 0,
+ }];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('ok');
+ expect(status.label).toBe('OK');
+ expect(status.color).toContain('green');
+ });
+
+ it('returns degraded status when array has failed devices', () => {
+ const raid: HostRAIDArray[] = [{
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean, degraded',
+ totalDevices: 2,
+ activeDevices: 1,
+ workingDevices: 1,
+ failedDevices: 1,
+ spareDevices: 0,
+ devices: [
+ { device: 'sda1', state: 'active sync', slot: 0 },
+ { device: 'sdb1', state: 'faulty', slot: 1 },
+ ],
+ rebuildPercent: 0,
+ }];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('degraded');
+ expect(status.label).toBe('Degraded');
+ expect(status.color).toContain('red');
+ });
+
+ it('returns degraded status when state includes degraded', () => {
+ const raid: HostRAIDArray[] = [{
+ device: '/dev/md0',
+ level: 'raid5',
+ state: 'degraded, recovering',
+ totalDevices: 3,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0, // No failed but degraded state
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 50,
+ }];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('degraded');
+ });
+
+ it('returns rebuilding status with percentage', () => {
+ const raid: HostRAIDArray[] = [{
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean, recovering',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 45.5,
+ rebuildSpeed: '100MB/s',
+ }];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('rebuilding');
+ expect(status.label).toBe('46%'); // Rounded
+ expect(status.color).toContain('amber');
+ });
+
+ it('returns max rebuild percentage when multiple arrays rebuilding', () => {
+ const raid: HostRAIDArray[] = [
+ {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'recovering',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 30,
+ },
+ {
+ device: '/dev/md1',
+ level: 'raid1',
+ state: 'recovering',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 75,
+ },
+ ];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('rebuilding');
+ expect(status.label).toBe('75%');
+ });
+
+ it('prioritizes degraded over rebuilding', () => {
+ const raid: HostRAIDArray[] = [
+ {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean, degraded, recovering',
+ totalDevices: 2,
+ activeDevices: 1,
+ workingDevices: 1,
+ failedDevices: 1, // Failed device
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 50, // Also rebuilding
+ },
+ ];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('degraded'); // Degraded takes priority
+ });
+
+ it('handles resync state as rebuilding', () => {
+ const raid: HostRAIDArray[] = [{
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean, resyncing',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 0, // resync state triggers rebuilding even without percent
+ }];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('rebuilding');
+ });
+
+ it('handles multiple healthy arrays', () => {
+ const raid: HostRAIDArray[] = [
+ {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 0,
+ },
+ {
+ device: '/dev/md1',
+ level: 'raid5',
+ state: 'active',
+ totalDevices: 3,
+ activeDevices: 3,
+ workingDevices: 3,
+ failedDevices: 0,
+ spareDevices: 1,
+ devices: [],
+ rebuildPercent: 0,
+ },
+ ];
+
+ const status = analyzeRAIDStatus(raid);
+ expect(status.type).toBe('ok');
+ });
+ });
+
+ describe('getDeviceStateColor', () => {
+ it('returns green for active devices', () => {
+ expect(getDeviceStateColor('active sync')).toContain('green');
+ expect(getDeviceStateColor('Active Sync')).toContain('green');
+ });
+
+ it('returns blue for spare devices', () => {
+ expect(getDeviceStateColor('spare')).toContain('blue');
+ expect(getDeviceStateColor('hot spare')).toContain('blue');
+ });
+
+ it('returns red for faulty devices', () => {
+ expect(getDeviceStateColor('faulty')).toContain('red');
+ expect(getDeviceStateColor('removed')).toContain('red');
+ });
+
+ it('returns amber for rebuilding devices', () => {
+ expect(getDeviceStateColor('rebuilding')).toContain('amber');
+ });
+
+ it('returns gray for unknown states', () => {
+ expect(getDeviceStateColor('')).toContain('gray');
+ expect(getDeviceStateColor('unknown')).toContain('gray');
+ });
+ });
+
+ describe('getArrayStateColor', () => {
+ it('returns green for clean/active arrays', () => {
+ const cleanArray: HostRAIDArray = {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 0,
+ };
+ expect(getArrayStateColor(cleanArray)).toContain('green');
+
+ const activeArray: HostRAIDArray = { ...cleanArray, state: 'active' };
+ expect(getArrayStateColor(activeArray)).toContain('green');
+ });
+
+ it('returns red for degraded arrays', () => {
+ const degradedArray: HostRAIDArray = {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'degraded',
+ totalDevices: 2,
+ activeDevices: 1,
+ workingDevices: 1,
+ failedDevices: 1,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 0,
+ };
+ expect(getArrayStateColor(degradedArray)).toContain('red');
+ });
+
+ it('returns red for arrays with failed devices even if state is clean', () => {
+ const failedDeviceArray: HostRAIDArray = {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean', // State says clean but...
+ totalDevices: 2,
+ activeDevices: 1,
+ workingDevices: 1,
+ failedDevices: 1, // Has a failed device
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 0,
+ };
+ expect(getArrayStateColor(failedDeviceArray)).toContain('red');
+ });
+
+ it('returns amber for recovering/resyncing arrays', () => {
+ const recoveringArray: HostRAIDArray = {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'recovering',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 50,
+ };
+ expect(getArrayStateColor(recoveringArray)).toContain('amber');
+
+ const resyncArray: HostRAIDArray = { ...recoveringArray, state: 'resyncing' };
+ expect(getArrayStateColor(resyncArray)).toContain('amber');
+ });
+
+ it('returns amber for arrays with rebuild percent > 0', () => {
+ const rebuildingArray: HostRAIDArray = {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean', // State is clean but...
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 25, // Rebuilding
+ };
+ expect(getArrayStateColor(rebuildingArray)).toContain('amber');
+ });
+ });
+});
+
+describe('RAID Level Support', () => {
+ const raidLevels = ['raid0', 'raid1', 'raid5', 'raid6', 'raid10'];
+
+ it.each(raidLevels)('supports %s level', (level) => {
+ const array: HostRAIDArray = {
+ device: '/dev/md0',
+ level: level,
+ state: 'clean',
+ totalDevices: level === 'raid0' ? 2 : level === 'raid10' ? 4 : 3,
+ activeDevices: level === 'raid0' ? 2 : level === 'raid10' ? 4 : 3,
+ workingDevices: level === 'raid0' ? 2 : level === 'raid10' ? 4 : 3,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [],
+ rebuildPercent: 0,
+ };
+
+ const status = analyzeRAIDStatus([array]);
+ expect(status.type).toBe('ok');
+ });
+});
+
+describe('RAID Device Counts', () => {
+ it('displays correct device counts for RAID1 with spares', () => {
+ const array: HostRAIDArray = {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'clean',
+ totalDevices: 3,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 1,
+ devices: [
+ { device: 'sda1', state: 'active sync', slot: 0 },
+ { device: 'sdb1', state: 'active sync', slot: 1 },
+ { device: 'sdc1', state: 'spare', slot: -1 },
+ ],
+ rebuildPercent: 0,
+ };
+
+ expect(array.activeDevices).toBe(2);
+ expect(array.workingDevices).toBe(2);
+ expect(array.spareDevices).toBe(1);
+ expect(array.failedDevices).toBe(0);
+ expect(array.devices).toHaveLength(3);
+ });
+
+ it('displays degraded state for RAID5 missing one device', () => {
+ const array: HostRAIDArray = {
+ device: '/dev/md0',
+ level: 'raid5',
+ state: 'clean, degraded',
+ totalDevices: 4,
+ activeDevices: 3,
+ workingDevices: 3,
+ failedDevices: 1,
+ spareDevices: 0,
+ devices: [
+ { device: 'sda1', state: 'active sync', slot: 0 },
+ { device: 'sdb1', state: 'active sync', slot: 1 },
+ { device: 'sdc1', state: 'active sync', slot: 2 },
+ { device: 'sdd1', state: 'removed', slot: 3 },
+ ],
+ rebuildPercent: 0,
+ };
+
+ const status = analyzeRAIDStatus([array]);
+ expect(status.type).toBe('degraded');
+ expect(array.failedDevices).toBe(1);
+ });
+});
diff --git a/frontend-modern/src/components/Proxmox/ProxmoxSectionNav.tsx b/frontend-modern/src/components/Proxmox/ProxmoxSectionNav.tsx
index 1e7dc435c1..5b9779d9e4 100644
--- a/frontend-modern/src/components/Proxmox/ProxmoxSectionNav.tsx
+++ b/frontend-modern/src/components/Proxmox/ProxmoxSectionNav.tsx
@@ -3,7 +3,7 @@ import { createMemo, For } from 'solid-js';
import { useNavigate } from '@solidjs/router';
import { useWebSocket } from '@/App';
-type ProxmoxSection = 'overview' | 'storage' | 'replication' | 'backups' | 'mail';
+type ProxmoxSection = 'overview' | 'storage' | 'ceph' | 'replication' | 'backups' | 'mail';
interface ProxmoxSectionNavProps {
current: ProxmoxSection;
@@ -15,41 +15,51 @@ const allSections: Array<{
label: string;
path: string;
}> = [
- {
- id: 'overview',
- label: 'Overview',
- path: '/proxmox/overview',
- },
- {
- id: 'storage',
- label: 'Storage',
- path: '/proxmox/storage',
- },
- {
- id: 'replication',
- label: 'Replication',
- path: '/proxmox/replication',
- },
- {
- id: 'mail',
- label: 'Mail Gateway',
- path: '/proxmox/mail',
- },
- {
- id: 'backups',
- label: 'Backups',
- path: '/proxmox/backups',
- },
-];
+ {
+ id: 'overview',
+ label: 'Overview',
+ path: '/proxmox/overview',
+ },
+ {
+ id: 'storage',
+ label: 'Storage',
+ path: '/proxmox/storage',
+ },
+ {
+ id: 'ceph',
+ label: 'Ceph',
+ path: '/proxmox/ceph',
+ },
+ {
+ id: 'replication',
+ label: 'Replication',
+ path: '/proxmox/replication',
+ },
+ {
+ id: 'mail',
+ label: 'Mail Gateway',
+ path: '/proxmox/mail',
+ },
+ {
+ id: 'backups',
+ label: 'Backups',
+ path: '/proxmox/backups',
+ },
+ ];
export const ProxmoxSectionNav: Component<ProxmoxSectionNavProps> = (props) => {
const navigate = useNavigate();
const { state } = useWebSocket();
// Only show Mail Gateway tab if PMG instances are configured
+ // Only show Ceph tab if Ceph clusters are detected (from agent or Proxmox API)
const sections = createMemo(() => {
const hasPMG = state.pmg && state.pmg.length > 0;
- return allSections.filter((section) => section.id !== 'mail' || hasPMG);
+ const hasCeph = state.cephClusters && state.cephClusters.length > 0;
+ return allSections.filter((section) =>
+ (section.id !== 'mail' || hasPMG) &&
+ (section.id !== 'ceph' || hasCeph)
+ );
});
const baseClasses =
diff --git a/frontend-modern/src/components/Settings/AISettings.tsx b/frontend-modern/src/components/Settings/AISettings.tsx
index ea3b2e565a..54f3431671 100644
--- a/frontend-modern/src/components/Settings/AISettings.tsx
+++ b/frontend-modern/src/components/Settings/AISettings.tsx
@@ -7,7 +7,7 @@ import { formField, labelClass, controlClass, formHelpText } from '@/components/
import { notificationStore } from '@/stores/notifications';
import { logger } from '@/utils/logger';
import { AIAPI } from '@/api/ai';
-import type { AISettings as AISettingsType, AIProvider } from '@/types/ai';
+import type { AISettings as AISettingsType, AIProvider, AuthMethod } from '@/types/ai';
import { PROVIDER_NAMES, PROVIDER_DESCRIPTIONS, DEFAULT_MODELS } from '@/types/ai';
const PROVIDERS: AIProvider[] = ['anthropic', 'openai', 'ollama', 'deepseek'];
@@ -17,6 +17,14 @@ export const AISettings: Component = () => {
const [loading, setLoading] = createSignal(false);
const [saving, setSaving] = createSignal(false);
const [testing, setTesting] = createSignal(false);
+ const [startingOAuth, setStartingOAuth] = createSignal(false);
+ const [disconnectingOAuth, setDisconnectingOAuth] = createSignal(false);
+ const [exchangingCode, setExchangingCode] = createSignal(false);
+
+ // OAuth flow state
+ const [oauthAuthUrl, setOAuthAuthUrl] = createSignal<string | null>(null);
+ const [oauthState, setOAuthState] = createSignal<string | null>(null);
+ const [oauthCode, setOAuthCode] = createSignal('');
const [form, setForm] = createStore({
enabled: false,
@@ -26,6 +34,7 @@ export const AISettings: Component = () => {
baseUrl: '',
clearApiKey: false,
autonomousMode: false,
+ authMethod: 'api_key' as AuthMethod,
});
const resetForm = (data: AISettingsType | null) => {
@@ -38,6 +47,7 @@ export const AISettings: Component = () => {
baseUrl: '',
clearApiKey: false,
autonomousMode: false,
+ authMethod: 'api_key',
});
return;
}
@@ -50,6 +60,7 @@ export const AISettings: Component = () => {
baseUrl: data.base_url || '',
clearApiKey: false,
autonomousMode: data.autonomous_mode || false,
+ authMethod: data.auth_method || 'api_key',
});
};
@@ -71,6 +82,29 @@ export const AISettings: Component = () => {
onMount(() => {
loadSettings();
+
+ // Check for OAuth callback parameters in URL
+ const params = new URLSearchParams(window.location.search);
+ const oauthSuccess = params.get('ai_oauth_success');
+ const oauthError = params.get('ai_oauth_error');
+
+ if (oauthSuccess === 'true') {
+ notificationStore.success('Successfully connected to Claude with your subscription!');
+ // Clean up URL
+ window.history.replaceState({}, '', window.location.pathname);
+ // Reload settings to get updated OAuth status
+ loadSettings();
+ } else if (oauthError) {
+ const errorMessages: Record<string, string> = {
+ 'missing_params': 'OAuth callback missing required parameters',
+ 'invalid_state': 'Invalid OAuth state - please try again',
+ 'token_exchange_failed': 'Failed to complete authentication with Claude',
+ 'save_failed': 'Failed to save OAuth credentials',
+ };
+ notificationStore.error(errorMessages[oauthError] || `OAuth error: ${oauthError}`);
+ // Clean up URL
+ window.history.replaceState({}, '', window.location.pathname);
+ }
});
const handleProviderChange = (provider: AIProvider) => {
@@ -145,8 +179,82 @@ export const AISettings: Component = () => {
}
};
- const needsApiKey = () => form.provider !== 'ollama';
+ const handleStartOAuth = async () => {
+ setStartingOAuth(true);
+ try {
+ const result = await AIAPI.startOAuth();
+ // Store the auth URL and state for the user to visit manually
+ setOAuthAuthUrl(result.auth_url);
+ setOAuthState(result.state);
+ setOAuthCode('');
+ notificationStore.info('Click the link below to sign in, then paste the code back here', 5000);
+ } catch (error) {
+ logger.error('[AISettings] OAuth start failed:', error);
+ const message = error instanceof Error ? error.message : 'Failed to start OAuth flow';
+ notificationStore.error(message);
+ } finally {
+ setStartingOAuth(false);
+ }
+ };
+
+ const handleExchangeCode = async () => {
+ const code = oauthCode().trim();
+ const state = oauthState();
+
+ if (!code || !state) {
+ notificationStore.error('Please enter the authorization code');
+ return;
+ }
+
+ setExchangingCode(true);
+ try {
+ await AIAPI.exchangeOAuthCode(code, state);
+ notificationStore.success('Successfully connected to Claude with your subscription!');
+ // Clear OAuth flow state
+ setOAuthAuthUrl(null);
+ setOAuthState(null);
+ setOAuthCode('');
+ // Reload settings
+ await loadSettings();
+ } catch (error) {
+ logger.error('[AISettings] OAuth code exchange failed:', error);
+ const message = error instanceof Error ? error.message : 'Failed to exchange authorization code';
+ notificationStore.error(message);
+ } finally {
+ setExchangingCode(false);
+ }
+ };
+
+ const handleCancelOAuth = () => {
+ setOAuthAuthUrl(null);
+ setOAuthState(null);
+ setOAuthCode('');
+ };
+
+ const handleDisconnectOAuth = async () => {
+ if (!confirm('Are you sure you want to disconnect your Claude subscription? You will need to provide an API key to continue using AI features.')) {
+ return;
+ }
+
+ setDisconnectingOAuth(true);
+ try {
+ await AIAPI.disconnectOAuth();
+ notificationStore.success('Claude subscription disconnected');
+ // Reload settings
+ await loadSettings();
+ } catch (error) {
+ logger.error('[AISettings] OAuth disconnect failed:', error);
+ const message = error instanceof Error ? error.message : 'Failed to disconnect OAuth';
+ notificationStore.error(message);
+ } finally {
+ setDisconnectingOAuth(false);
+ }
+ };
+
+ const needsApiKey = () => form.provider !== 'ollama' && (form.provider !== 'anthropic' || form.authMethod !== 'oauth');
const showBaseUrl = () => form.provider === 'ollama' || form.provider === 'openai' || form.provider === 'deepseek';
+ const isAnthropicWithOAuth = () => form.provider === 'anthropic' && form.authMethod === 'oauth';
+ const showAuthMethodSelector = () => form.provider === 'anthropic';
return (
<Card
@@ -239,7 +347,163 @@ export const AISettings: Component = () => {
</div>
</div>
- {/* API Key - not shown for Ollama */}
+ {/* Authentication Method - only for Anthropic */}
+ <Show when={showAuthMethodSelector()}>
+ <div class={formField}>
+ <label class={labelClass()}>Authentication Method</label>
+ <div class="grid grid-cols-2 gap-3">
+ <button
+ type="button"
+ class={`p-3 rounded-lg border-2 text-left transition-all ${form.authMethod === 'api_key'
+ ? 'border-purple-500 bg-purple-50 dark:bg-purple-900/30'
+ : 'border-gray-200 dark:border-gray-700 hover:border-gray-300 dark:hover:border-gray-600'
+ }`}
+ onClick={() => setForm('authMethod', 'api_key')}
+ disabled={saving()}
+ >
+ <div class="font-medium text-sm text-gray-900 dark:text-gray-100">
+ API Key
+ </div>
+ <div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
+ Pay per token usage
+ </div>
+ </button>
+ <button
+ type="button"
+ class={`p-3 rounded-lg border-2 text-left transition-all opacity-60 cursor-not-allowed ${form.authMethod === 'oauth'
+ ? 'border-purple-500 bg-purple-50 dark:bg-purple-900/30'
+ : 'border-gray-200 dark:border-gray-700'
+ }`}
+ disabled={true}
+ title="OAuth with Claude Pro/Max subscription is not yet available. Anthropic currently restricts third-party OAuth access."
+ >
+ <div class="font-medium text-sm text-gray-900 dark:text-gray-100 flex items-center gap-1.5">
+ Claude Pro/Max
+ <span class="inline-flex items-center px-1.5 py-0.5 text-[10px] font-semibold bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400 rounded">
+ Unavailable
+ </span>
+ </div>
+ <div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">
+ Use your subscription
+ </div>
+ </button>
+ </div>
+ <p class={formHelpText}>
+ {form.authMethod === 'api_key'
+ ? 'Pay-per-use API billing from console.anthropic.com'
+ : 'Use your Claude Pro ($20/mo) or Max ($100+/mo) subscription'}
+ </p>
+ </div>
+ </Show>
+
+ {/* OAuth Login/Status - shown when Anthropic + OAuth selected */}
+ <Show when={isAnthropicWithOAuth()}>
+ <div class={`${formField} p-4 rounded-lg border ${settings()?.oauth_connected
+ ? 'bg-green-50 dark:bg-green-900/20 border-green-200 dark:border-green-800'
+ : oauthAuthUrl()
+ ? 'bg-amber-50 dark:bg-amber-900/20 border-amber-200 dark:border-amber-800'
+ : 'bg-blue-50 dark:bg-blue-900/20 border-blue-200 dark:border-blue-800'
+ }`}>
+ {/* Connected state */}
+ <Show when={settings()?.oauth_connected}>
+ <div class="flex items-center justify-between">
+ <div>
+ <div class="flex items-center gap-2 text-sm font-medium text-green-800 dark:text-green-200">
+ <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7" />
+ </svg>
+ Connected to Claude with your subscription
+ </div>
+ <p class="text-xs text-green-700 dark:text-green-300 mt-1">
+ AI requests use your Claude Pro/Max subscription limits instead of API billing.
+ </p>
+ </div>
+ <button
+ type="button"
+ class="px-3 py-1.5 text-xs border border-red-300 dark:border-red-700 text-red-700 dark:text-red-300 rounded hover:bg-red-50 dark:hover:bg-red-900/30 disabled:opacity-50"
+ onClick={handleDisconnectOAuth}
+ disabled={disconnectingOAuth() || saving()}
+ >
+ {disconnectingOAuth() ? 'Disconnecting...' : 'Disconnect'}
+ </button>
+ </div>
+ </Show>
+
+ {/* OAuth flow in progress - show URL and code input */}
+ <Show when={!settings()?.oauth_connected && oauthAuthUrl()}>
+ <div class="space-y-4">
+ <div class="text-sm text-amber-800 dark:text-amber-200">
+ <strong>Step 1:</strong> Click the link below to sign in with your Anthropic account:
+ </div>
+ <a
+ href={oauthAuthUrl()!}
+ target="_blank"
+ rel="noopener noreferrer"
+ class="block p-2 bg-white dark:bg-gray-800 rounded border border-amber-300 dark:border-amber-700 text-xs text-blue-600 dark:text-blue-400 hover:underline break-all"
+ >
+ {oauthAuthUrl()}
+ </a>
+ <div class="text-sm text-amber-800 dark:text-amber-200">
+ <strong>Step 2:</strong> After signing in, you'll see a code. Paste it here:
+ </div>
+ <div class="flex gap-2">
+ <input
+ type="text"
+ value={oauthCode()}
+ onInput={(e) => setOAuthCode(e.currentTarget.value)}
+ placeholder="Paste authorization code here..."
+ class="flex-1 px-3 py-2 text-sm border border-amber-300 dark:border-amber-600 rounded-md bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 placeholder-gray-500 dark:placeholder-gray-400"
+ />
+ <button
+ type="button"
+ class="px-4 py-2 bg-gradient-to-r from-purple-600 to-pink-600 text-white text-sm rounded-md hover:from-purple-700 hover:to-pink-700 disabled:opacity-50 disabled:cursor-not-allowed"
+ onClick={handleExchangeCode}
+ disabled={exchangingCode() || !oauthCode().trim()}
+ >
+ {exchangingCode() ? 'Connecting...' : 'Connect'}
+ </button>
+ </div>
+ <button
+ type="button"
+ class="text-xs text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
+ onClick={handleCancelOAuth}
+ >
+ Cancel
+ </button>
+ </div>
+ </Show>
+
+ {/* Initial state - show sign in button */}
+ <Show when={!settings()?.oauth_connected && !oauthAuthUrl()}>
+ <div class="text-center">
+ <div class="text-sm text-blue-800 dark:text-blue-200 mb-3">
+ <strong>Use your Claude subscription</strong>
+ <p class="text-xs mt-1 text-blue-700 dark:text-blue-300">
+ Sign in with your Anthropic account to use your Pro/Max subscription for AI features instead of API billing.
+ </p>
+ </div>
+ <button
+ type="button"
+ class="px-4 py-2 bg-gradient-to-r from-purple-600 to-pink-600 text-white rounded-md hover:from-purple-700 hover:to-pink-700 disabled:opacity-50 disabled:cursor-not-allowed flex items-center gap-2 mx-auto"
+ onClick={handleStartOAuth}
+ disabled={startingOAuth() || saving()}
+ >
+ <Show when={startingOAuth()}>
+ <span class="h-4 w-4 border-2 border-white border-t-transparent rounded-full animate-spin" />
+ </Show>
+ <Show when={!startingOAuth()}>
+ <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 16l-4-4m0 0l4-4m-4 4h14m-5 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h7a3 3 0 013 3v1" />
+ </svg>
+ </Show>
+ {startingOAuth() ? 'Starting...' : 'Sign in with Claude'}
+ </button>
+ </div>
+ </Show>
+ </div>
+ </Show>
+
+ {/* API Key - not shown for Ollama or when using OAuth */}
<Show when={needsApiKey()}>
<div class={formField}>
<div class="flex items-center justify-between">
@@ -382,17 +646,21 @@ export const AISettings: Component = () => {
/>
<span class="text-xs font-medium">
{settings()?.configured
- ? `Ready to use with ${settings()?.model}`
- : needsApiKey()
- ? 'API key required to enable AI features'
- : 'Configure Ollama server URL to enable AI features'}
+ ? settings()?.oauth_connected
+ ? `Ready to use with ${settings()?.model} (via Claude subscription)`
+ : `Ready to use with ${settings()?.model}`
+ : form.authMethod === 'oauth' && form.provider === 'anthropic'
+ ? 'Sign in with your Claude subscription to enable AI features'
+ : needsApiKey()
+ ? 'API key required to enable AI features'
+ : 'Configure Ollama server URL to enable AI features'}
</span>
</div>
</Show>
{/* Actions */}
<div class="flex flex-wrap items-center justify-between gap-3 pt-4">
- <Show when={settings()?.api_key_set}>
+ <Show when={settings()?.api_key_set || settings()?.oauth_connected}>
<button
type="button"
class="px-4 py-2 text-sm border border-purple-300 dark:border-purple-600 text-purple-700 dark:text-purple-300 rounded-md hover:bg-purple-50 dark:hover:bg-purple-900/30 disabled:opacity-50 disabled:cursor-not-allowed"
diff --git a/frontend-modern/src/components/Settings/Settings.tsx b/frontend-modern/src/components/Settings/Settings.tsx
index 39f852a53b..22f186a762 100644
--- a/frontend-modern/src/components/Settings/Settings.tsx
+++ b/frontend-modern/src/components/Settings/Settings.tsx
@@ -463,6 +463,7 @@ const Settings: Component<SettingsProps> = (props) => {
if (path.includes('/settings/system-network')) return 'system-network';
if (path.includes('/settings/system-updates')) return 'system-updates';
if (path.includes('/settings/system-backups')) return 'system-backups';
+ if (path.includes('/settings/system-ai')) return 'system-ai';
if (path.includes('/settings/system')) return 'system-general';
if (path.includes('/settings/api')) return 'api';
if (path.includes('/settings/security-overview')) return 'security-overview';
diff --git a/frontend-modern/src/components/shared/NodeSummaryTable.tsx b/frontend-modern/src/components/shared/NodeSummaryTable.tsx
index 23dccbadc6..6b6febe678 100644
--- a/frontend-modern/src/components/shared/NodeSummaryTable.tsx
+++ b/frontend-modern/src/components/shared/NodeSummaryTable.tsx
@@ -337,9 +337,13 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
return sortDirection() === 'asc' ? '▲' : '▼';
};
- const thClassBase = "px-2 py-0.5 text-[11px] sm:text-xs font-medium uppercase tracking-wider cursor-pointer hover:bg-gray-200 dark:hover:bg-gray-600 whitespace-nowrap";
+ const thClassBase = "px-2 py-1.5 text-[11px] sm:text-xs font-medium uppercase tracking-wider cursor-pointer hover:bg-gray-200 dark:hover:bg-gray-600 whitespace-nowrap";
const thClass = `${thClassBase} text-center`;
+ // Cell class constants for consistency
+ const tdClass = "px-2 py-1.5 align-middle";
+ const metricColumnStyle = { width: "200px", "min-width": "200px", "max-width": "200px" } as const;
+
return (
<Card padding="none" tone="glass" class="mb-4 overflow-hidden">
<div class="overflow-x-auto">
@@ -482,7 +486,7 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
onClick={() => props.onNodeClick(nodeId, isPVEItem ? 'pve' : 'pbs')}
>
{/* Name */}
- <td class={`pr-2 py-0.5 align-middle ${showAlertHighlight() ? 'pl-4' : 'pl-3'}`}>
+ <td class={`pr-2 py-1.5 align-middle ${showAlertHighlight() ? 'pl-4' : 'pl-3'}`}>
<div class="flex items-center gap-1.5">
<StatusDot
variant={statusIndicator().variant}
@@ -544,7 +548,7 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
</td>
{/* Uptime */}
- <td class="px-2 py-0.5 align-middle">
+ <td class={tdClass}>
<div class="flex justify-center">
<span
class={`text-xs whitespace-nowrap ${isPVEItem && (node?.uptime ?? 0) < 3600
@@ -562,7 +566,7 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
</td>
{/* CPU */}
- <td class="px-2 py-0.5 align-middle" style={{ width: "200px", "min-width": "200px", "max-width": "200px" }}>
+ <td class={tdClass} style={metricColumnStyle}>
<Show when={isMobile()}>
<div class="md:hidden flex justify-center">
<MetricText value={cpuPercentValue} type="cpu" />
@@ -590,7 +594,7 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
</td>
{/* Memory */}
- <td class="px-2 py-0.5 align-middle" style={{ width: "200px", "min-width": "200px", "max-width": "200px" }}>
+ <td class={tdClass} style={metricColumnStyle}>
<Show when={isMobile()}>
<div class="md:hidden flex justify-center">
<MetricText value={memoryPercentValue} type="memory" />
@@ -633,7 +637,7 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
</td>
{/* Disk */}
- <td class="px-2 py-0.5 align-middle" style={{ width: "200px", "min-width": "200px", "max-width": "200px" }}>
+ <td class={tdClass} style={metricColumnStyle}>
<ResponsiveMetricCell
value={diskPercentValue}
type="disk"
@@ -646,7 +650,7 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
{/* Temperature */}
<Show when={hasAnyTemperatureData()}>
- <td class="px-2 py-0.5 align-middle">
+ <td class={tdClass}>
<div class="flex justify-center">
<Show
when={
@@ -698,14 +702,14 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
{/* Dashboard tab: VMs and CTs */}
<Show when={props.currentTab === 'dashboard'}>
- <td class="px-2 py-0.5 align-middle">
+ <td class={tdClass}>
<div class="flex justify-center">
<span class={online ? 'text-xs text-gray-700 dark:text-gray-300' : 'text-xs text-gray-400 dark:text-gray-500'}>
{online ? getCountValue(item, 'vmCount') ?? '-' : '-'}
</span>
</div>
</td>
- <td class="px-2 py-0.5 align-middle">
+ <td class={tdClass}>
<div class="flex justify-center">
<span class={online ? 'text-xs text-gray-700 dark:text-gray-300' : 'text-xs text-gray-400 dark:text-gray-500'}>
{online ? getCountValue(item, 'containerCount') ?? '-' : '-'}
@@ -716,14 +720,14 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
{/* Storage tab: Storage and Disks */}
<Show when={props.currentTab === 'storage'}>
- <td class="px-2 py-0.5 align-middle">
+ <td class={tdClass}>
<div class="flex justify-center">
<span class={online ? 'text-xs text-gray-700 dark:text-gray-300' : 'text-xs text-gray-400 dark:text-gray-500'}>
{online ? getCountValue(item, 'storageCount') ?? '-' : '-'}
</span>
</div>
</td>
- <td class="px-2 py-0.5 align-middle">
+ <td class={tdClass}>
<div class="flex justify-center">
<span class={online ? 'text-xs text-gray-700 dark:text-gray-300' : 'text-xs text-gray-400 dark:text-gray-500'}>
{online ? getCountValue(item, 'diskCount') ?? '-' : '-'}
@@ -734,7 +738,7 @@ export const NodeSummaryTable: Component<NodeSummaryTableProps> = (props) => {
{/* Backups tab: Backups */}
<Show when={props.currentTab === 'backups'}>
- <td class="px-2 py-0.5 align-middle">
+ <td class={tdClass}>
<div class="flex justify-center">
<span class={online ? 'text-xs text-gray-700 dark:text-gray-300' : 'text-xs text-gray-400 dark:text-gray-500'}>
{online ? getCountValue(item, 'backupCount') ?? '-' : '-'}
diff --git a/frontend-modern/src/components/shared/Sparkline.tsx b/frontend-modern/src/components/shared/Sparkline.tsx
index b4d8367be3..f745a3c09f 100644
--- a/frontend-modern/src/components/shared/Sparkline.tsx
+++ b/frontend-modern/src/components/shared/Sparkline.tsx
@@ -95,11 +95,9 @@ export const Sparkline: Component<SparklineProps> = (props) => {
const dpr = window.devicePixelRatio || 1;
canvas.width = w * dpr;
canvas.height = h * dpr;
- // Only set explicit width style if a fixed width was provided
- // Otherwise let CSS handle the width (w-full class)
- if (props.width !== 0) {
- canvas.style.width = `${w}px`;
- }
+ // Always set explicit width style to prevent canvas from expanding beyond container
+ // When width=0, we use the calculated container width
+ canvas.style.width = `${w}px`;
canvas.style.height = `${h}px`;
ctx.scale(dpr, dpr);
@@ -255,12 +253,13 @@ export const Sparkline: Component<SparklineProps> = (props) => {
return (
<>
- <div class="relative block w-full" style={{ height: `${height()}px` }}>
+ <div class="relative block w-full overflow-hidden" style={{ height: `${height()}px`, 'max-width': '100%' }}>
<canvas
ref={canvasRef}
- class="block cursor-crosshair w-full"
+ class="block cursor-crosshair"
style={{
height: `${height()}px`,
+ 'max-width': '100%',
}}
onMouseMove={handleMouseMove}
onMouseLeave={handleMouseLeave}
diff --git a/frontend-modern/src/hooks/__tests__/useResources.test.ts b/frontend-modern/src/hooks/__tests__/useResources.test.ts
new file mode 100644
index 0000000000..f077be5275
--- /dev/null
+++ b/frontend-modern/src/hooks/__tests__/useResources.test.ts
@@ -0,0 +1,486 @@
+/**
+ * Tests for useResources hook functionality
+ *
+ * Note: These tests focus on the logic and type conversions rather than
+ * reactive behavior, which requires a full SolidJS testing environment.
+ */
+import { describe, expect, it } from 'vitest';
+import type { Resource, ResourceStatus } from '@/types/resource';
+
+// Helper to create mock resources for testing conversion logic
+function createMockResource(overrides: Partial<Resource> = {}): Resource {
+ return {
+ id: 'test-resource-1',
+ type: 'vm',
+ name: 'test-vm',
+ displayName: 'Test VM',
+ platformId: 'pve1',
+ platformType: 'proxmox-pve',
+ sourceType: 'api',
+ status: 'running',
+ lastSeen: Date.now(),
+ cpu: { current: 25.5 },
+ memory: { current: 50, total: 4294967296, used: 2147483648 },
+ disk: { current: 30, total: 107374182400, used: 32212254720 },
+ ...overrides,
+ };
+}
+
+describe('useResources - Resource Filtering Logic', () => {
+ describe('byType filtering', () => {
+ const resources: Resource[] = [
+ createMockResource({ id: '1', type: 'vm' }),
+ createMockResource({ id: '2', type: 'vm' }),
+ createMockResource({ id: '3', type: 'container' }),
+ createMockResource({ id: '4', type: 'node' }),
+ createMockResource({ id: '5', type: 'docker-container' }),
+ ];
+
+ it('filters resources by single type', () => {
+ const vms = resources.filter(r => r.type === 'vm');
+ expect(vms).toHaveLength(2);
+ });
+
+ it('returns empty array when no matches', () => {
+ const filtered = resources.filter(r => r.type === 'pbs');
+ expect(filtered).toHaveLength(0);
+ });
+ });
+
+ describe('byPlatform filtering', () => {
+ const resources: Resource[] = [
+ createMockResource({ id: '1', platformType: 'proxmox-pve' }),
+ createMockResource({ id: '2', platformType: 'proxmox-pve' }),
+ createMockResource({ id: '3', platformType: 'docker' }),
+ createMockResource({ id: '4', platformType: 'host-agent' }),
+ ];
+
+ it('filters resources by platform', () => {
+ const pveResources = resources.filter(r => r.platformType === 'proxmox-pve');
+ expect(pveResources).toHaveLength(2);
+ });
+
+ it('filters Docker resources', () => {
+ const dockerResources = resources.filter(r => r.platformType === 'docker');
+ expect(dockerResources).toHaveLength(1);
+ });
+ });
+
+ describe('children filtering', () => {
+ const resources: Resource[] = [
+ createMockResource({ id: 'node-1', type: 'node' }),
+ createMockResource({ id: 'vm-1', type: 'vm', parentId: 'node-1' }),
+ createMockResource({ id: 'vm-2', type: 'vm', parentId: 'node-1' }),
+ createMockResource({ id: 'vm-3', type: 'vm', parentId: 'node-2' }),
+ ];
+
+ it('finds children of a parent', () => {
+ const children = resources.filter(r => r.parentId === 'node-1');
+ expect(children).toHaveLength(2);
+ });
+
+ it('returns empty array for parent with no children', () => {
+ const children = resources.filter(r => r.parentId === 'node-3');
+ expect(children).toHaveLength(0);
+ });
+ });
+
+ describe('complex filtering', () => {
+ const resources: Resource[] = [
+ createMockResource({ id: '1', type: 'vm', status: 'running', platformType: 'proxmox-pve' }),
+ createMockResource({ id: '2', type: 'vm', status: 'stopped', platformType: 'proxmox-pve' }),
+ createMockResource({ id: '3', type: 'container', status: 'running', platformType: 'proxmox-pve' }),
+ createMockResource({ id: '4', type: 'docker-container', status: 'running', platformType: 'docker' }),
+ ];
+
+ it('filters by multiple criteria', () => {
+ const runningPveVms = resources.filter(r =>
+ r.type === 'vm' &&
+ r.status === 'running' &&
+ r.platformType === 'proxmox-pve'
+ );
+ expect(runningPveVms).toHaveLength(1);
+ });
+
+ it('filters by search term in name', () => {
+ const searchTerm = 'test';
+ const matched = resources.filter(r =>
+ r.name.toLowerCase().includes(searchTerm.toLowerCase())
+ );
+ expect(matched).toHaveLength(4);
+ });
+ });
+
+ describe('status counts', () => {
+ const resources: Resource[] = [
+ createMockResource({ id: '1', status: 'running' }),
+ createMockResource({ id: '2', status: 'running' }),
+ createMockResource({ id: '3', status: 'stopped' }),
+ createMockResource({ id: '4', status: 'online' }),
+ createMockResource({ id: '5', status: 'degraded' }),
+ ];
+
+ it('counts resources by status', () => {
+ const counts: Record<ResourceStatus, number> = {
+ online: 0,
+ offline: 0,
+ running: 0,
+ stopped: 0,
+ degraded: 0,
+ paused: 0,
+ unknown: 0,
+ };
+
+ for (const r of resources) {
+ if (r.status in counts) {
+ counts[r.status]++;
+ }
+ }
+
+ expect(counts.running).toBe(2);
+ expect(counts.stopped).toBe(1);
+ expect(counts.online).toBe(1);
+ expect(counts.degraded).toBe(1);
+ });
+ });
+
+ describe('topByCpu sorting', () => {
+ const resources: Resource[] = [
+ createMockResource({ id: '1', name: 'low', cpu: { current: 10 } }),
+ createMockResource({ id: '2', name: 'high', cpu: { current: 90 } }),
+ createMockResource({ id: '3', name: 'medium', cpu: { current: 50 } }),
+ createMockResource({ id: '4', name: 'no-cpu', cpu: undefined }), // Explicitly no CPU data
+ ];
+
+ it('sorts by CPU descending and limits results', () => {
+ const sorted = [...resources]
+ .filter(r => r.cpu && r.cpu.current > 0)
+ .sort((a, b) => (b.cpu?.current ?? 0) - (a.cpu?.current ?? 0))
+ .slice(0, 2);
+
+ expect(sorted).toHaveLength(2);
+ expect(sorted[0].name).toBe('high');
+ expect(sorted[1].name).toBe('medium');
+ });
+
+ it('excludes resources without CPU data', () => {
+ const withCpu = resources.filter(r => r.cpu && r.cpu.current > 0);
+ expect(withCpu).toHaveLength(3);
+ });
+ });
+});
+
+describe('useResourcesAsLegacy - Legacy Format Conversion', () => {
+ describe('VM conversion', () => {
+ it('converts Resource to legacy VM format', () => {
+ const resource = createMockResource({
+ type: 'vm',
+ cpu: { current: 50 },
+ memory: { current: 60, total: 4294967296, used: 2576980378 },
+ disk: { current: 30, total: 107374182400, used: 32212254720 },
+ uptime: 86400,
+ network: { rxBytes: 1000000, txBytes: 500000 },
+ tags: ['web', 'production'],
+ platformData: {
+ vmid: 100,
+ node: 'pve1',
+ instance: 'pve1/qemu/100',
+ cpus: 4,
+ template: false,
+ osName: 'Ubuntu',
+ osVersion: '22.04',
+ },
+ });
+
+ // Simulate the conversion logic from useResourcesAsLegacy
+ const platformData = resource.platformData as Record<string, unknown>;
+ const legacyVm = {
+ id: resource.id,
+ vmid: platformData?.vmid as number ?? parseInt(resource.id.split('-').pop() ?? '0', 10),
+ name: resource.name,
+ node: platformData?.node as string ?? '',
+ instance: platformData?.instance as string ?? resource.platformId,
+ status: resource.status === 'running' ? 'running' : 'stopped',
+ type: 'qemu',
+ cpu: (resource.cpu?.current ?? 0) / 100, // Convert percentage to ratio
+ cpus: platformData?.cpus as number ?? 1,
+ memory: resource.memory ? {
+ total: resource.memory.total ?? 0,
+ used: resource.memory.used ?? 0,
+ free: resource.memory.free ?? 0,
+ usage: resource.memory.current,
+ } : { total: 0, used: 0, free: 0, usage: 0 },
+ uptime: resource.uptime ?? 0,
+ networkIn: resource.network?.rxBytes ?? 0,
+ networkOut: resource.network?.txBytes ?? 0,
+ tags: resource.tags ?? [],
+ };
+
+ expect(legacyVm.vmid).toBe(100);
+ expect(legacyVm.node).toBe('pve1');
+ expect(legacyVm.cpu).toBe(0.5); // 50% -> 0.5 ratio
+ expect(legacyVm.memory.total).toBe(4294967296);
+ expect(legacyVm.uptime).toBe(86400);
+ expect(legacyVm.networkIn).toBe(1000000);
+ expect(legacyVm.tags).toEqual(['web', 'production']);
+ });
+
+ it('handles missing platformData gracefully', () => {
+ const resource = createMockResource({
+ type: 'vm',
+ platformData: undefined,
+ });
+
+ const platformData = resource.platformData as Record<string, unknown> | undefined;
+ const vmid = platformData?.vmid as number ?? 0;
+ const node = platformData?.node as string ?? '';
+
+ expect(vmid).toBe(0);
+ expect(node).toBe('');
+ });
+ });
+
+ describe('Container conversion', () => {
+ it('converts Resource to legacy Container format', () => {
+ const resource = createMockResource({
+ type: 'container',
+ platformData: {
+ vmid: 200,
+ node: 'pve1',
+ },
+ });
+
+ const platformData = resource.platformData as Record<string, unknown>;
+ const legacyContainer = {
+ id: resource.id,
+ vmid: platformData?.vmid as number,
+ name: resource.name,
+ type: 'lxc',
+ status: resource.status === 'running' ? 'running' : 'stopped',
+ };
+
+ expect(legacyContainer.vmid).toBe(200);
+ expect(legacyContainer.type).toBe('lxc');
+ });
+ });
+
+ describe('Node conversion', () => {
+ it('converts Resource to legacy Node format with temperature', () => {
+ const now = Date.now();
+ const resource = createMockResource({
+ type: 'node',
+ name: 'pve1',
+ status: 'online',
+ temperature: 45.5,
+ lastSeen: now,
+ cpu: { current: 35 },
+ platformData: {
+ host: '192.168.1.10',
+ loadAverage: [1.5, 1.2, 0.9],
+ kernelVersion: '6.1.0-pve',
+ pveVersion: '8.0.3',
+ },
+ });
+
+ // Simulate temperature conversion
+ let temperature = undefined;
+ if (resource.temperature !== undefined && resource.temperature !== null && resource.temperature > 0) {
+ temperature = {
+ cpuPackage: resource.temperature,
+ cpuMax: resource.temperature,
+ available: true,
+ hasCPU: true,
+ hasGPU: false,
+ hasNVMe: false,
+ lastUpdate: new Date(resource.lastSeen).toISOString(),
+ };
+ }
+
+ expect(temperature).toBeDefined();
+ expect(temperature!.cpuPackage).toBe(45.5);
+ expect(temperature!.available).toBe(true);
+ });
+
+ it('handles node without temperature', () => {
+ const resource = createMockResource({
+ type: 'node',
+ temperature: undefined,
+ });
+
+ let temperature = undefined;
+ if (resource.temperature !== undefined && resource.temperature !== null && resource.temperature > 0) {
+ temperature = { cpuPackage: resource.temperature };
+ }
+
+ expect(temperature).toBeUndefined();
+ });
+
+ it('converts CPU from percentage to ratio', () => {
+ const resource = createMockResource({
+ type: 'node',
+ cpu: { current: 75 },
+ });
+
+ const legacyCpu = (resource.cpu?.current ?? 0) / 100;
+ expect(legacyCpu).toBe(0.75);
+ });
+ });
+
+ describe('DockerHost conversion', () => {
+ it('converts Resource to legacy DockerHost format with containers', () => {
+ const dockerHost = createMockResource({
+ id: 'docker-host-1',
+ type: 'docker-host',
+ name: 'docker-server',
+ platformType: 'docker',
+ cpu: { current: 25 },
+ memory: { current: 50, total: 8589934592, used: 4294967296 },
+ platformData: {
+ agentId: 'agent-123',
+ runtime: 'docker',
+ runtimeVersion: '24.0.0',
+ dockerVersion: '24.0.0',
+ cpus: 8,
+ },
+ });
+
+ const dockerContainers: Resource[] = [
+ createMockResource({
+ id: 'docker-host-1/container-abc',
+ type: 'docker-container',
+ name: 'nginx',
+ parentId: 'docker-host-1',
+ status: 'running',
+ cpu: { current: 5 },
+ memory: { current: 10, total: 134217728, used: 13421772 },
+ platformData: {
+ image: 'nginx:latest',
+ health: 'healthy',
+ },
+ }),
+ ];
+
+ // Simulate container ID extraction for sparklines
+ const originalContainerId = dockerContainers[0].id.includes('/')
+ ? dockerContainers[0].id.split('/').pop()!
+ : dockerContainers[0].id;
+
+ expect(originalContainerId).toBe('container-abc');
+
+ const legacyHost = {
+ id: dockerHost.id,
+ agentId: (dockerHost.platformData as any).agentId ?? dockerHost.id,
+ hostname: dockerHost.identity?.hostname ?? dockerHost.name,
+ cpuUsagePercent: dockerHost.cpu?.current,
+ containers: dockerContainers.filter(c => c.parentId === dockerHost.id),
+ };
+
+ expect(legacyHost.agentId).toBe('agent-123');
+ expect(legacyHost.containers).toHaveLength(1);
+ });
+ });
+
+ describe('Host conversion', () => {
+ it('converts Resource to legacy Host format with sensors', () => {
+ const resource = createMockResource({
+ type: 'host',
+ name: 'server1',
+ platformType: 'host-agent',
+ status: 'online',
+ platformData: {
+ platform: 'linux',
+ osName: 'Ubuntu',
+ osVersion: '22.04 LTS',
+ kernelVersion: '6.2.0',
+ architecture: 'x86_64',
+ cpuCount: 16,
+ loadAverage: [2.5, 1.8, 1.2],
+ sensors: {
+ temperatureCelsius: { 'CPU Package': 55.0, 'nvme0': 42.0 },
+ fanRpm: { 'CPU Fan': 1200 },
+ },
+ raid: [
+ {
+ device: '/dev/md0',
+ level: 'raid1',
+ state: 'active',
+ totalDevices: 2,
+ activeDevices: 2,
+ workingDevices: 2,
+ failedDevices: 0,
+ spareDevices: 0,
+ devices: [
+ { device: 'sda1', state: 'active', slot: 0 },
+ { device: 'sdb1', state: 'active', slot: 1 },
+ ],
+ rebuildPercent: 0,
+ },
+ ],
+ },
+ });
+
+ const platformData = resource.platformData as Record<string, unknown>;
+
+ expect(platformData.sensors).toBeDefined();
+ expect((platformData.sensors as any).temperatureCelsius['CPU Package']).toBe(55.0);
+ expect(platformData.raid).toHaveLength(1);
+ expect((platformData.raid as any[])[0].level).toBe('raid1');
+ });
+
+ it('maps interfaces correctly to networkInterfaces', () => {
+ const resource = createMockResource({
+ type: 'host',
+ platformData: {
+ interfaces: [
+ { name: 'eth0', mac: '00:11:22:33:44:55', addresses: ['192.168.1.10'] },
+ { name: 'docker0', mac: '02:42:00:00:00:01', addresses: ['172.17.0.1'] },
+ ],
+ },
+ });
+
+ const platformData = resource.platformData as Record<string, unknown>;
+ const interfaces = platformData.interfaces as any[];
+
+ expect(interfaces).toHaveLength(2);
+ expect(interfaces[0].name).toBe('eth0');
+ expect(interfaces[1].addresses).toContain('172.17.0.1');
+ });
+ });
+});
+
+describe('Fallback Logic', () => {
+ describe('hasUnifiedResources check', () => {
+ it('returns true when resources array has items', () => {
+ const resources: Resource[] = [createMockResource()];
+ expect(resources.length > 0).toBe(true);
+ });
+
+ it('returns false when resources array is empty', () => {
+ const resources: Resource[] = [];
+ expect(resources.length > 0).toBe(false);
+ });
+ });
+
+ describe('legacy array fallback', () => {
+ // This tests the concept - actual implementation uses WebSocket store
+ it('uses legacy array when unified resources not available', () => {
+ const unifiedResources: Resource[] = [];
+ const legacyVms = [{ id: 'vm-1', name: 'Legacy VM' }];
+
+ const hasUnifiedResources = unifiedResources.length > 0;
+ const result = hasUnifiedResources ? unifiedResources : legacyVms;
+
+ expect(result).toEqual(legacyVms);
+ });
+
+ it('uses unified resources when available', () => {
+ const unifiedResources: Resource[] = [createMockResource({ id: 'unified-1' })];
+ const legacyVms = [{ id: 'vm-1', name: 'Legacy VM' }];
+
+ const hasUnifiedResources = unifiedResources.length > 0;
+ const result = hasUnifiedResources ? unifiedResources : legacyVms;
+
+ expect(result).toEqual(unifiedResources);
+ });
+ });
+});
diff --git a/frontend-modern/src/hooks/useResources.ts b/frontend-modern/src/hooks/useResources.ts
index 4ca7c46e41..f096899701 100644
--- a/frontend-modern/src/hooks/useResources.ts
+++ b/frontend-modern/src/hooks/useResources.ts
@@ -225,12 +225,29 @@ export function useResources(): UseResourcesReturn {
/**
* Helper hook for resource-to-legacy-type conversion.
* This helps during migration when components still expect legacy types.
+ *
+ * IMPORTANT: This hook includes fallback logic for initial page loads.
+ * The unified resources array is populated by monitor broadcasts, but the
+ * initial WebSocket state may not include it. In that case, we fall back
+ * to the legacy arrays (nodes, vms, containers, etc.) from the WebSocket state.
*/
export function useResourcesAsLegacy() {
const { resources, byType } = useResources();
+ // Get the WebSocket store for legacy array fallback
+ const wsStore = getGlobalWebSocketStore();
+
+ // Check if we have unified resources (populated after first broadcast)
+ const hasUnifiedResources = createMemo(() => resources().length > 0);
+
// Convert resources to legacy VM format
+ // Falls back to legacy state.vms array when unified resources aren't yet populated
const asVMs = createMemo(() => {
+ // If we don't have unified resources yet, use legacy arrays directly
+ if (!hasUnifiedResources()) {
+ return wsStore.state.vms ?? [];
+ }
+
return byType('vm').map(r => {
const platformData = r.platformData as Record<string, unknown> | undefined;
return {
@@ -255,6 +272,16 @@ export function useResourcesAsLegacy() {
free: r.disk.free ?? 0,
usage: r.disk.current,
} : { total: 0, used: 0, free: 0, usage: 0 },
+ // IP and OS fields - crucial for the Dashboard columns
+ ipAddresses: (platformData?.ipAddresses as string[] | undefined) ?? (r.identity?.ips as string[] | undefined),
+ osName: platformData?.osName as string | undefined,
+ osVersion: platformData?.osVersion as string | undefined,
+ agentVersion: platformData?.agentVersion as string | undefined,
+ networkInterfaces: platformData?.networkInterfaces as Array<{
+ name: string;
+ mac?: string;
+ addresses?: string[];
+ }> | undefined,
networkIn: r.network?.rxBytes ?? 0,
networkOut: r.network?.txBytes ?? 0,
diskRead: platformData?.diskRead as number ?? 0,
@@ -270,7 +297,13 @@ export function useResourcesAsLegacy() {
});
// Convert resources to legacy Container format
+ // Falls back to legacy state.containers array when unified resources aren't yet populated
const asContainers = createMemo(() => {
+ // If we don't have unified resources yet, use legacy arrays directly
+ if (!hasUnifiedResources()) {
+ return wsStore.state.containers ?? [];
+ }
+
return byType('container').map(r => {
const platformData = r.platformData as Record<string, unknown> | undefined;
return {
@@ -295,6 +328,15 @@ export function useResourcesAsLegacy() {
free: r.disk.free ?? 0,
usage: r.disk.current,
} : { total: 0, used: 0, free: 0, usage: 0 },
+ // IP and OS fields - crucial for the Dashboard columns
+ ipAddresses: (platformData?.ipAddresses as string[] | undefined) ?? (r.identity?.ips as string[] | undefined),
+ osName: platformData?.osName as string | undefined,
+ osVersion: platformData?.osVersion as string | undefined,
+ networkInterfaces: platformData?.networkInterfaces as Array<{
+ name: string;
+ mac?: string;
+ addresses?: string[];
+ }> | undefined,
networkIn: r.network?.rxBytes ?? 0,
networkOut: r.network?.txBytes ?? 0,
diskRead: platformData?.diskRead as number ?? 0,
@@ -310,10 +352,25 @@ export function useResourcesAsLegacy() {
});
// Convert resources to legacy Host format
+ // Falls back to legacy state.hosts array when unified resources aren't yet populated
const asHosts = createMemo(() => {
- return byType('host').map(r => {
- // Extract platform-specific data if available
- const platformData = r.platformData as Record<string, unknown> | undefined;
+ // If we don't have unified resources yet, use legacy arrays directly
+ if (!hasUnifiedResources()) {
+ return wsStore.state.hosts ?? [];
+ }
+
+ return byType('host').map((r) => {
+ // Extract platform-specific data - unwrap SolidJS Proxy objects into plain JS objects
+ const platformData = r.platformData ? JSON.parse(JSON.stringify(r.platformData)) as Record<string, unknown> : undefined;
+
+ // Interfaces from platformData
+ const interfaces = platformData?.interfaces as Array<{
+ name: string;
+ mac?: string;
+ addresses?: string[];
+ rxBytes?: number;
+ txBytes?: number;
+ }> | undefined;
return {
id: r.id,
@@ -347,13 +404,8 @@ export function useResourcesAsLegacy() {
readBytes?: number;
writeBytes?: number;
}> | undefined,
- networkInterfaces: platformData?.networkInterfaces as Array<{
- name: string;
- mac?: string;
- addresses?: string[];
- rxBytes?: number;
- txBytes?: number;
- }> | undefined,
+ // Map backend 'interfaces' to frontend 'networkInterfaces'
+ networkInterfaces: interfaces,
sensors: platformData?.sensors as {
temperatureCelsius?: Record<string, number>;
fanRpm?: Record<string, number>;
@@ -378,15 +430,40 @@ export function useResourcesAsLegacy() {
agentVersion: platformData?.agentVersion as string | undefined,
tokenId: platformData?.tokenId as string | undefined,
tokenName: platformData?.tokenName as string | undefined,
- tags: r.tags,
+ tags: r.tags ? [...r.tags] : undefined,
};
});
});
// Convert resources to legacy Node format
+ // Falls back to legacy state.nodes array when unified resources aren't yet populated
const asNodes = createMemo(() => {
+ // If we don't have unified resources yet, use legacy arrays directly
+ if (!hasUnifiedResources()) {
+ // Return legacy nodes array as-is (it's already in the right format)
+ return wsStore.state.nodes ?? [];
+ }
+
return byType('node').map(r => {
- const platformData = r.platformData as Record<string, unknown> | undefined;
+ // Unwrap SolidJS Proxy objects into plain JS objects
+ const platformData = r.platformData ? JSON.parse(JSON.stringify(r.platformData)) as Record<string, unknown> : undefined;
+
+ // Build temperature object from unified resource
+ // The unified resource has temperature as a simple number,
+ // but legacy components expect the full Temperature struct
+ let temperature = undefined;
+ if (r.temperature !== undefined && r.temperature !== null && r.temperature > 0) {
+ temperature = {
+ cpuPackage: r.temperature,
+ cpuMax: r.temperature,
+ available: true,
+ hasCPU: true,
+ hasGPU: false,
+ hasNVMe: false,
+ lastUpdate: new Date(r.lastSeen).toISOString(),
+ };
+ }
+
return {
id: r.id,
name: r.name,
@@ -395,7 +472,7 @@ export function useResourcesAsLegacy() {
host: platformData?.host as string ?? '',
status: r.status,
type: 'node',
- cpu: r.cpu?.current ?? 0,
+ cpu: (r.cpu?.current ?? 0) / 100, // Convert from percentage to ratio for legacy components
memory: r.memory ? {
total: r.memory.total ?? 0,
used: r.memory.used ?? 0,
@@ -413,7 +490,7 @@ export function useResourcesAsLegacy() {
kernelVersion: platformData?.kernelVersion as string ?? '',
pveVersion: platformData?.pveVersion as string ?? '',
cpuInfo: platformData?.cpuInfo ?? { model: '', cores: 0, sockets: 0, mhz: '' },
- temperature: platformData?.temperature,
+ temperature,
lastSeen: new Date(r.lastSeen).toISOString(),
connectionHealth: platformData?.connectionHealth as string ?? 'unknown',
isClusterMember: platformData?.isClusterMember as boolean | undefined,
@@ -423,20 +500,30 @@ export function useResourcesAsLegacy() {
});
// Convert resources to legacy DockerHost format (including nested containers)
+ // Falls back to legacy state.dockerHosts array when unified resources aren't yet populated
const asDockerHosts = createMemo(() => {
+ // If we don't have unified resources yet, use legacy arrays directly
+ if (!hasUnifiedResources()) {
+ return wsStore.state.dockerHosts ?? [];
+ }
+
const dockerHostResources = byType('docker-host');
const dockerContainerResources = byType('docker-container');
return dockerHostResources.map(h => {
- const platformData = h.platformData as Record<string, unknown> | undefined;
+ // Unwrap SolidJS Proxy objects into plain JS objects
+ const platformData = h.platformData ? JSON.parse(JSON.stringify(h.platformData)) as Record<string, unknown> : undefined;
// Find containers belonging to this host
const hostContainers = dockerContainerResources
.filter(c => c.parentId === h.id)
.map(c => {
- const cPlatform = c.platformData as Record<string, unknown> | undefined;
+ const cPlatform = c.platformData ? JSON.parse(JSON.stringify(c.platformData)) as Record<string, unknown> : undefined;
+ // Extract original container ID from compound resource ID (hostID/containerID)
+ // This is needed for sparkline metrics to match the sampler which uses original IDs
+ const originalContainerId = c.id.includes('/') ? c.id.split('/').pop()! : c.id;
return {
- id: c.id,
+ id: originalContainerId,
name: c.name,
image: cPlatform?.image as string ?? '',
state: c.status === 'running' ? 'running' : 'exited',
@@ -455,6 +542,10 @@ export function useResourcesAsLegacy() {
ports: cPlatform?.ports,
labels: cPlatform?.labels as Record<string, string> | undefined,
networks: cPlatform?.networks,
+ mounts: cPlatform?.mounts,
+ blockIo: cPlatform?.blockIo,
+ writableLayerBytes: cPlatform?.writableLayerBytes as number | undefined,
+ rootFilesystemBytes: cPlatform?.rootFilesystemBytes as number | undefined,
};
});
@@ -483,7 +574,8 @@ export function useResourcesAsLegacy() {
usage: h.memory.current,
} : undefined,
disks: platformData?.disks,
- networkInterfaces: platformData?.networkInterfaces,
+ // Map backend 'interfaces' to frontend 'networkInterfaces'
+ networkInterfaces: platformData?.interfaces,
status: h.status === 'online' || h.status === 'running' ? 'online' : h.status,
lastSeen: h.lastSeen,
intervalSeconds: platformData?.intervalSeconds as number ?? 30,
diff --git a/frontend-modern/src/pages/Ceph.tsx b/frontend-modern/src/pages/Ceph.tsx
new file mode 100644
index 0000000000..baffdfac55
--- /dev/null
+++ b/frontend-modern/src/pages/Ceph.tsx
@@ -0,0 +1,366 @@
+import { Component, For, Show, createMemo } from 'solid-js';
+import { useWebSocket } from '@/App';
+import { ProxmoxSectionNav } from '@/components/Proxmox/ProxmoxSectionNav';
+import type { CephCluster, CephPool, CephServiceStatus } from '@/types/api';
+
+// Format bytes to human readable
+const formatBytes = (bytes: number): string => {
+ if (bytes === 0) return '0 B';
+ const k = 1024;
+ const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
+};
+
+// Get health color classes
+const getHealthClasses = (health: string) => {
+ const h = health?.toUpperCase() || '';
+ if (h === 'OK' || h === 'HEALTH_OK') {
+ return {
+ bg: 'bg-green-100 dark:bg-green-900/30',
+ text: 'text-green-700 dark:text-green-400',
+ border: 'border-green-200 dark:border-green-800',
+ dot: 'bg-green-500',
+ };
+ }
+ if (h === 'WARN' || h === 'HEALTH_WARN' || h === 'WARNING') {
+ return {
+ bg: 'bg-yellow-100 dark:bg-yellow-900/30',
+ text: 'text-yellow-700 dark:text-yellow-400',
+ border: 'border-yellow-200 dark:border-yellow-800',
+ dot: 'bg-yellow-500',
+ };
+ }
+ if (h === 'ERR' || h === 'HEALTH_ERR' || h === 'ERROR' || h === 'CRITICAL') {
+ return {
+ bg: 'bg-red-100 dark:bg-red-900/30',
+ text: 'text-red-700 dark:text-red-400',
+ border: 'border-red-200 dark:border-red-800',
+ dot: 'bg-red-500',
+ };
+ }
+ return {
+ bg: 'bg-gray-100 dark:bg-gray-800',
+ text: 'text-gray-700 dark:text-gray-400',
+ border: 'border-gray-200 dark:border-gray-700',
+ dot: 'bg-gray-500',
+ };
+};
+
+// Cluster Overview Card
+const ClusterCard: Component<{ cluster: CephCluster }> = (props) => {
+ const healthClasses = createMemo(() => getHealthClasses(props.cluster.health));
+
+ const displayHealth = createMemo(() => {
+ const h = props.cluster.health?.toUpperCase() || 'UNKNOWN';
+ return h.replace('HEALTH_', '');
+ });
+
+ return (
+ <div class={`rounded-xl border ${healthClasses().border} ${healthClasses().bg} p-5 shadow-sm`}>
+ <div class="flex items-start justify-between mb-4">
+ <div>
+ <h3 class="text-lg font-semibold text-gray-900 dark:text-gray-100">
+ {props.cluster.name || 'Ceph Cluster'}
+ </h3>
+ <Show when={props.cluster.fsid}>
+ <p class="text-xs text-gray-500 dark:text-gray-400 font-mono mt-0.5">
+ {props.cluster.fsid}
+ </p>
+ </Show>
+ </div>
+ <div class={`flex items-center gap-2 px-3 py-1.5 rounded-full ${healthClasses().bg} ${healthClasses().text} font-semibold text-sm`}>
+ <span class={`w-2 h-2 rounded-full ${healthClasses().dot} animate-pulse`} />
+ {displayHealth()}
+ </div>
+ </div>
+
+ <Show when={props.cluster.healthMessage}>
+ <p class={`text-sm ${healthClasses().text} mb-4 italic`}>
+ {props.cluster.healthMessage}
+ </p>
+ </Show>
+
+ {/* Capacity Bar */}
+ <div class="mb-4">
+ <div class="flex justify-between text-sm mb-1">
+ <span class="text-gray-600 dark:text-gray-400">Capacity</span>
+ <span class="font-medium text-gray-900 dark:text-gray-100">
+ {props.cluster.usagePercent?.toFixed(1) || 0}% used
+ </span>
+ </div>
+ <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2.5 overflow-hidden">
+ <div
+ class={`h-full rounded-full transition-all duration-500 ${(props.cluster.usagePercent || 0) > 85
+ ? 'bg-red-500'
+ : (props.cluster.usagePercent || 0) > 70
+ ? 'bg-yellow-500'
+ : 'bg-blue-500'
+ }`}
+ style={{ width: `${Math.min(props.cluster.usagePercent || 0, 100)}%` }}
+ />
+ </div>
+ <div class="flex justify-between text-xs text-gray-500 dark:text-gray-400 mt-1">
+ <span>{formatBytes(props.cluster.usedBytes || 0)} used</span>
+ <span>{formatBytes(props.cluster.totalBytes || 0)} total</span>
+ </div>
+ </div>
+
+ {/* Service Stats Grid */}
+ <div class="grid grid-cols-3 gap-3">
+ <div class="text-center p-2 bg-white/50 dark:bg-gray-800/50 rounded-lg">
+ <div class="text-2xl font-bold text-blue-600 dark:text-blue-400">
+ {props.cluster.numMons || 0}
+ </div>
+ <div class="text-xs text-gray-500 dark:text-gray-400">Monitors</div>
+ </div>
+ <div class="text-center p-2 bg-white/50 dark:bg-gray-800/50 rounded-lg">
+ <div class="text-2xl font-bold text-purple-600 dark:text-purple-400">
+ {props.cluster.numMgrs || 0}
+ </div>
+ <div class="text-xs text-gray-500 dark:text-gray-400">Managers</div>
+ </div>
+ <div class="text-center p-2 bg-white/50 dark:bg-gray-800/50 rounded-lg">
+ <div class="text-2xl font-bold text-green-600 dark:text-green-400">
+ <span class={props.cluster.numOsdsUp !== props.cluster.numOsds ? 'text-yellow-600 dark:text-yellow-400' : ''}>
+ {props.cluster.numOsdsUp || 0}
+ </span>
+ <span class="text-gray-400 dark:text-gray-500 text-lg">/{props.cluster.numOsds || 0}</span>
+ </div>
+ <div class="text-xs text-gray-500 dark:text-gray-400">OSDs Up</div>
+ </div>
+ </div>
+
+ {/* PG Count */}
+ <Show when={props.cluster.numPGs}>
+ <div class="mt-3 text-center text-sm text-gray-500 dark:text-gray-400">
+ <span class="font-medium text-gray-700 dark:text-gray-300">{props.cluster.numPGs?.toLocaleString()}</span>
+ {' '}Placement Groups
+ </div>
+ </Show>
+ </div>
+ );
+};
+
+// Pool Table
+const PoolsTable: Component<{ pools: CephPool[] }> = (props) => {
+ return (
+ <div class="overflow-hidden rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm">
+ <div class="px-4 py-3 border-b border-gray-200 dark:border-gray-700">
+ <h3 class="font-semibold text-gray-900 dark:text-gray-100">
+ Pools ({props.pools.length})
+ </h3>
+ </div>
+ <div class="overflow-x-auto">
+ <table class="w-full text-sm">
+ <thead class="bg-gray-50 dark:bg-gray-900/50">
+ <tr>
+ <th class="px-4 py-2 text-left text-gray-600 dark:text-gray-400 font-medium">Name</th>
+ <th class="px-4 py-2 text-right text-gray-600 dark:text-gray-400 font-medium">Used</th>
+ <th class="px-4 py-2 text-right text-gray-600 dark:text-gray-400 font-medium">Available</th>
+ <th class="px-4 py-2 text-right text-gray-600 dark:text-gray-400 font-medium">Objects</th>
+ <th class="px-4 py-2 text-center text-gray-600 dark:text-gray-400 font-medium">Usage</th>
+ </tr>
+ </thead>
+ <tbody class="divide-y divide-gray-100 dark:divide-gray-700">
+ <For each={props.pools}>
+ {(pool) => (
+ <tr class="hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors">
+ <td class="px-4 py-3 font-medium text-gray-900 dark:text-gray-100">
+ {pool.name}
+ </td>
+ <td class="px-4 py-3 text-right text-gray-700 dark:text-gray-300">
+ {formatBytes(pool.storedBytes || 0)}
+ </td>
+ <td class="px-4 py-3 text-right text-gray-700 dark:text-gray-300">
+ {formatBytes(pool.availableBytes || 0)}
+ </td>
+ <td class="px-4 py-3 text-right text-gray-700 dark:text-gray-300">
+ {(pool.objects || 0).toLocaleString()}
+ </td>
+ <td class="px-4 py-3">
+ <div class="flex items-center gap-2 justify-center">
+ <div class="w-16 h-2 bg-gray-200 dark:bg-gray-600 rounded-full overflow-hidden">
+ <div
+ class={`h-full rounded-full ${(pool.percentUsed || 0) > 85
+ ? 'bg-red-500'
+ : (pool.percentUsed || 0) > 70
+ ? 'bg-yellow-500'
+ : 'bg-blue-500'
+ }`}
+ style={{ width: `${Math.min(pool.percentUsed || 0, 100)}%` }}
+ />
+ </div>
+ <span class="text-xs text-gray-600 dark:text-gray-400 w-12 text-right">
+ {(pool.percentUsed || 0).toFixed(1)}%
+ </span>
+ </div>
+ </td>
+ </tr>
+ )}
+ </For>
+ </tbody>
+ </table>
+ </div>
+ </div>
+ );
+};
+
+// Services Status
+const ServicesStatus: Component<{ services: CephServiceStatus[] }> = (props) => {
+ const getServiceIcon = (type: string) => {
+ switch (type.toLowerCase()) {
+ case 'mon':
+ return '🔵';
+ case 'mgr':
+ return '🟣';
+ case 'osd':
+ return '🟢';
+ case 'mds':
+ return '🟡';
+ case 'rgw':
+ return '🟠';
+ default:
+ return '⚪';
+ }
+ };
+
+ return (
+ <div class="rounded-xl border border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800 shadow-sm p-4">
+ <h3 class="font-semibold text-gray-900 dark:text-gray-100 mb-3">Services</h3>
+ <div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-5 gap-3">
+ <For each={props.services}>
+ {(service) => {
+ const allRunning = service.running === service.total;
+ return (
+ <div
+ class={`p-3 rounded-lg border ${allRunning
+ ? 'bg-green-50 dark:bg-green-900/20 border-green-200 dark:border-green-800'
+ : service.running > 0
+ ? 'bg-yellow-50 dark:bg-yellow-900/20 border-yellow-200 dark:border-yellow-800'
+ : 'bg-red-50 dark:bg-red-900/20 border-red-200 dark:border-red-800'
+ }`}
+ >
+ <div class="flex items-center gap-2 mb-1">
+ <span class="text-lg">{getServiceIcon(service.type)}</span>
+ <span class="font-medium text-gray-900 dark:text-gray-100 uppercase text-sm">
+ {service.type}
+ </span>
+ </div>
+ <div class={`text-lg font-bold ${allRunning
+ ? 'text-green-700 dark:text-green-400'
+ : service.running > 0
+ ? 'text-yellow-700 dark:text-yellow-400'
+ : 'text-red-700 dark:text-red-400'
+ }`}>
+ {service.running}/{service.total}
+ </div>
+ <Show when={service.message}>
+ <div class="text-xs text-gray-500 dark:text-gray-400 mt-1 truncate" title={service.message}>
+ {service.message}
+ </div>
+ </Show>
+ </div>
+ );
+ }}
+ </For>
+ </div>
+ </div>
+ );
+};
+
+// Main Ceph Page
+const Ceph: Component = () => {
+ const { state } = useWebSocket();
+
+ const clusters = createMemo(() => state.cephClusters || []);
+ const hasClusters = createMemo(() => clusters().length > 0);
+
+ // Aggregate all pools from all clusters
+ const allPools = createMemo(() => {
+ const pools: CephPool[] = [];
+ for (const cluster of clusters()) {
+ if (cluster.pools) {
+ pools.push(...cluster.pools);
+ }
+ }
+ return pools;
+ });
+
+ // Aggregate services from all clusters
+ const allServices = createMemo(() => {
+ const serviceMap = new Map<string, { running: number; total: number }>();
+ for (const cluster of clusters()) {
+ if (cluster.services) {
+ for (const svc of cluster.services) {
+ const existing = serviceMap.get(svc.type) || { running: 0, total: 0 };
+ serviceMap.set(svc.type, {
+ running: existing.running + svc.running,
+ total: existing.total + svc.total,
+ });
+ }
+ }
+ }
+ return Array.from(serviceMap.entries()).map(([type, counts]) => ({
+ type,
+ running: counts.running,
+ total: counts.total,
+ }));
+ });
+
+ return (
+ <div class="flex flex-col gap-4 sm:gap-5">
+ {/* Header */}
+ <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
+ <div>
+ <h1 class="text-xl sm:text-2xl font-bold text-gray-900 dark:text-gray-100">
+ Ceph Storage
+ </h1>
+ <p class="text-sm text-gray-500 dark:text-gray-400 mt-0.5">
+ Distributed storage cluster status and pools
+ </p>
+ </div>
+ <ProxmoxSectionNav current="ceph" />
+ </div>
+
+ <Show
+ when={hasClusters()}
+ fallback={
+ <div class="flex flex-col items-center justify-center py-12 text-center">
+ <div class="w-16 h-16 mb-4 rounded-full bg-gray-100 dark:bg-gray-800 flex items-center justify-center">
+ <svg class="w-8 h-8 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 13V6a2 2 0 00-2-2H6a2 2 0 00-2 2v7m16 0v5a2 2 0 01-2 2H6a2 2 0 01-2-2v-5m16 0h-2.586a1 1 0 00-.707.293l-2.414 2.414a1 1 0 01-.707.293h-3.172a1 1 0 01-.707-.293l-2.414-2.414A1 1 0 006.586 13H4" />
+ </svg>
+ </div>
+ <h3 class="text-lg font-medium text-gray-900 dark:text-gray-100 mb-1">
+ No Ceph Clusters Detected
+ </h3>
+ <p class="text-sm text-gray-500 dark:text-gray-400 max-w-md">
+ Ceph cluster data will appear here when detected via the Pulse agent on your Proxmox nodes.
+ Install the agent on a node with Ceph configured.
+ </p>
+ </div>
+ }
+ >
+ {/* Cluster Cards */}
+ <div class="grid gap-4 md:grid-cols-2 xl:grid-cols-3">
+ <For each={clusters()}>
+ {(cluster) => <ClusterCard cluster={cluster} />}
+ </For>
+ </div>
+
+ {/* Services Overview */}
+ <Show when={allServices().length > 0}>
+ <ServicesStatus services={allServices() as CephServiceStatus[]} />
+ </Show>
+
+ {/* Pools Table */}
+ <Show when={allPools().length > 0}>
+ <PoolsTable pools={allPools()} />
+ </Show>
+ </Show>
+ </div>
+ );
+};
+
+export default Ceph;
diff --git a/frontend-modern/src/stores/__tests__/metricsHistory.test.ts b/frontend-modern/src/stores/__tests__/metricsHistory.test.ts
new file mode 100644
index 0000000000..15790b5491
--- /dev/null
+++ b/frontend-modern/src/stores/__tests__/metricsHistory.test.ts
@@ -0,0 +1,409 @@
+/**
+ * Tests for metricsHistory store functionality
+ *
+ * These tests focus on the ring buffer operations and metric recording logic.
+ */
+import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest';
+
+// We can't directly test the module due to side effects (localStorage, window),
+// so we test the core logic patterns used in the implementation.
+
+describe('Ring Buffer Logic', () => {
+ // Implementation of ring buffer matching metricsHistory.ts
+ interface MetricSnapshot {
+ timestamp: number;
+ cpu: number;
+ memory: number;
+ disk: number;
+ }
+
+ interface RingBuffer {
+ buffer: (MetricSnapshot | undefined)[];
+ head: number;
+ size: number;
+ }
+
+ const MAX_POINTS = 100;
+
+ function createRingBuffer(): RingBuffer {
+ return {
+ buffer: new Array(MAX_POINTS),
+ head: 0,
+ size: 0,
+ };
+ }
+
+ function pushToRingBuffer(ring: RingBuffer, snapshot: MetricSnapshot): void {
+ const index = (ring.head + ring.size) % MAX_POINTS;
+ ring.buffer[index] = snapshot;
+
+ if (ring.size < MAX_POINTS) {
+ ring.size++;
+ } else {
+ ring.head = (ring.head + 1) % MAX_POINTS;
+ }
+ }
+
+ function getRingBufferData(ring: RingBuffer, cutoffTime: number): MetricSnapshot[] {
+ const result: MetricSnapshot[] = [];
+
+ for (let i = 0; i < ring.size; i++) {
+ const index = (ring.head + i) % MAX_POINTS;
+ const snapshot = ring.buffer[index];
+
+ if (snapshot && snapshot.timestamp >= cutoffTime) {
+ result.push(snapshot);
+ }
+ }
+
+ return result;
+ }
+
+ describe('createRingBuffer', () => {
+ it('creates an empty ring buffer', () => {
+ const ring = createRingBuffer();
+ expect(ring.head).toBe(0);
+ expect(ring.size).toBe(0);
+ expect(ring.buffer.length).toBe(MAX_POINTS);
+ });
+ });
+
+ describe('pushToRingBuffer', () => {
+ it('adds snapshots to the buffer', () => {
+ const ring = createRingBuffer();
+ const snapshot: MetricSnapshot = {
+ timestamp: Date.now(),
+ cpu: 50,
+ memory: 60,
+ disk: 70,
+ };
+
+ pushToRingBuffer(ring, snapshot);
+
+ expect(ring.size).toBe(1);
+ expect(ring.buffer[0]).toEqual(snapshot);
+ });
+
+ it('increments size correctly', () => {
+ const ring = createRingBuffer();
+
+ for (let i = 0; i < 10; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: Date.now() + i,
+ cpu: i,
+ memory: i,
+ disk: i,
+ });
+ }
+
+ expect(ring.size).toBe(10);
+ });
+
+ it('wraps around and overwrites oldest when full', () => {
+ const ring = createRingBuffer();
+
+ // Fill the buffer
+ for (let i = 0; i < MAX_POINTS; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: 1000 + i,
+ cpu: i,
+ memory: i,
+ disk: i,
+ });
+ }
+
+ expect(ring.size).toBe(MAX_POINTS);
+ expect(ring.head).toBe(0);
+
+ // Add one more - should overwrite first entry
+ pushToRingBuffer(ring, {
+ timestamp: 2000,
+ cpu: 999,
+ memory: 999,
+ disk: 999,
+ });
+
+ expect(ring.size).toBe(MAX_POINTS);
+ expect(ring.head).toBe(1); // Head moved forward
+ expect(ring.buffer[0]!.cpu).toBe(999); // New entry at position 0
+ });
+
+ it('maintains O(1) insertion performance', () => {
+ const ring = createRingBuffer();
+ const iterations = 10000;
+ const start = performance.now();
+
+ for (let i = 0; i < iterations; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: Date.now(),
+ cpu: Math.random() * 100,
+ memory: Math.random() * 100,
+ disk: Math.random() * 100,
+ });
+ }
+
+ const duration = performance.now() - start;
+ // Should complete quickly (< 100ms for 10k insertions)
+ expect(duration).toBeLessThan(100);
+ });
+ });
+
+ describe('getRingBufferData', () => {
+ it('returns all data when no cutoff', () => {
+ const ring = createRingBuffer();
+
+ for (let i = 0; i < 5; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: 1000 + i,
+ cpu: i * 10,
+ memory: i * 10,
+ disk: i * 10,
+ });
+ }
+
+ const data = getRingBufferData(ring, 0);
+ expect(data).toHaveLength(5);
+ });
+
+ it('returns data in chronological order', () => {
+ const ring = createRingBuffer();
+
+ for (let i = 0; i < 5; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: 1000 + i,
+ cpu: i,
+ memory: i,
+ disk: i,
+ });
+ }
+
+ const data = getRingBufferData(ring, 0);
+ expect(data[0].timestamp).toBe(1000);
+ expect(data[4].timestamp).toBe(1004);
+ });
+
+ it('filters by cutoff time', () => {
+ const ring = createRingBuffer();
+
+ for (let i = 0; i < 10; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: 1000 + i,
+ cpu: i,
+ memory: i,
+ disk: i,
+ });
+ }
+
+ const data = getRingBufferData(ring, 1005);
+ expect(data).toHaveLength(5); // Only timestamps >= 1005
+ expect(data[0].timestamp).toBe(1005);
+ });
+
+ it('handles wrapped buffer correctly', () => {
+ const ring = createRingBuffer();
+
+ // Fill buffer completely and then some
+ for (let i = 0; i < MAX_POINTS + 20; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: 1000 + i,
+ cpu: i % 100,
+ memory: i % 100,
+ disk: i % 100,
+ });
+ }
+
+ const data = getRingBufferData(ring, 0);
+ expect(data).toHaveLength(MAX_POINTS);
+
+ // First entry should be the 21st one (index 20)
+ expect(data[0].timestamp).toBe(1020);
+ // Last entry should be the most recent
+ expect(data[MAX_POINTS - 1].timestamp).toBe(1000 + MAX_POINTS + 19);
+ });
+
+ it('returns empty array for empty buffer', () => {
+ const ring = createRingBuffer();
+ const data = getRingBufferData(ring, 0);
+ expect(data).toHaveLength(0);
+ });
+
+ it('returns empty array when all data is before cutoff', () => {
+ const ring = createRingBuffer();
+
+ for (let i = 0; i < 5; i++) {
+ pushToRingBuffer(ring, {
+ timestamp: 1000 + i,
+ cpu: i,
+ memory: i,
+ disk: i,
+ });
+ }
+
+ const data = getRingBufferData(ring, 2000);
+ expect(data).toHaveLength(0);
+ });
+ });
+});
+
+describe('Metric Key Generation', () => {
+ // Test metric key pattern matching
+ function buildMetricKey(kind: string, id: string): string {
+ return `${kind}:${id}`;
+ }
+
+ describe('key format', () => {
+ it('creates properly namespaced keys', () => {
+ expect(buildMetricKey('vm', '100')).toBe('vm:100');
+ expect(buildMetricKey('node', 'pve1')).toBe('node:pve1');
+ expect(buildMetricKey('dockerContainer', 'abc123')).toBe('dockerContainer:abc123');
+ });
+
+ it('allows filtering by prefix', () => {
+ const keys = [
+ 'vm:100',
+ 'vm:101',
+ 'node:pve1',
+ 'dockerContainer:abc',
+ ];
+
+ const vmKeys = keys.filter(k => k.startsWith('vm:'));
+ expect(vmKeys).toHaveLength(2);
+
+ const dockerKeys = keys.filter(k => k.startsWith('dockerContainer:'));
+ expect(dockerKeys).toHaveLength(1);
+ });
+ });
+});
+
+describe('Time Range Calculations', () => {
+ type TimeRange = '5m' | '15m' | '30m' | '1h' | '4h' | '12h' | '24h' | '7d';
+
+ function timeRangeToMs(range: TimeRange): number {
+ switch (range) {
+ case '5m': return 5 * 60 * 1000;
+ case '15m': return 15 * 60 * 1000;
+ case '30m': return 30 * 60 * 1000;
+ case '1h': return 60 * 60 * 1000;
+ case '4h': return 4 * 60 * 60 * 1000;
+ case '12h': return 12 * 60 * 60 * 1000;
+ case '24h': return 24 * 60 * 60 * 1000;
+ case '7d': return 7 * 24 * 60 * 60 * 1000;
+ default: return 60 * 60 * 1000;
+ }
+ }
+
+ it('converts all time ranges correctly', () => {
+ expect(timeRangeToMs('5m')).toBe(300000);
+ expect(timeRangeToMs('15m')).toBe(900000);
+ expect(timeRangeToMs('30m')).toBe(1800000);
+ expect(timeRangeToMs('1h')).toBe(3600000);
+ expect(timeRangeToMs('4h')).toBe(14400000);
+ expect(timeRangeToMs('12h')).toBe(43200000);
+ expect(timeRangeToMs('24h')).toBe(86400000);
+ expect(timeRangeToMs('7d')).toBe(604800000);
+ });
+
+ it('calculates correct cutoff times', () => {
+ const now = Date.now();
+ const oneHourAgo = now - timeRangeToMs('1h');
+
+ expect(now - oneHourAgo).toBe(3600000);
+ });
+});
+
+describe('Sample Interval Enforcement', () => {
+ const SAMPLE_INTERVAL_MS = 30 * 1000; // 30 seconds
+
+ function shouldRecordSample(lastSampleTime: number, now: number): boolean {
+ return now - lastSampleTime >= SAMPLE_INTERVAL_MS;
+ }
+
+ it('allows recording when interval has passed', () => {
+ const now = Date.now();
+ const lastSample = now - 31000; // 31 seconds ago
+ expect(shouldRecordSample(lastSample, now)).toBe(true);
+ });
+
+ it('blocks recording when too soon', () => {
+ const now = Date.now();
+ const lastSample = now - 15000; // 15 seconds ago
+ expect(shouldRecordSample(lastSample, now)).toBe(false);
+ });
+
+ it('allows recording exactly at interval', () => {
+ const now = Date.now();
+ const lastSample = now - SAMPLE_INTERVAL_MS;
+ expect(shouldRecordSample(lastSample, now)).toBe(true);
+ });
+
+ it('always allows first sample (lastSample = 0)', () => {
+ const now = Date.now();
+ expect(shouldRecordSample(0, now)).toBe(true);
+ });
+});
+
+describe('Metric Value Rounding', () => {
+ function roundMetric(value: number): number {
+ return Math.round(value * 10) / 10;
+ }
+
+ it('rounds to 1 decimal place', () => {
+ expect(roundMetric(45.123)).toBe(45.1);
+ expect(roundMetric(45.156)).toBe(45.2);
+ expect(roundMetric(45.149)).toBe(45.1);
+ expect(roundMetric(100.0)).toBe(100.0);
+ expect(roundMetric(0.05)).toBe(0.1);
+ expect(roundMetric(0.04)).toBe(0.0);
+ });
+
+ it('handles edge cases', () => {
+ expect(roundMetric(0)).toBe(0);
+ expect(roundMetric(-45.123)).toBe(-45.1);
+ expect(roundMetric(99.95)).toBe(100.0);
+ });
+});
+
+describe('Duplicate Timestamp Prevention', () => {
+ interface MetricSnapshot {
+ timestamp: number;
+ cpu: number;
+ }
+
+ function shouldSkipDuplicate(
+ existingData: MetricSnapshot[],
+ newTimestamp: number,
+ toleranceMs: number = 15000
+ ): boolean {
+ for (const existing of existingData) {
+ if (Math.abs(existing.timestamp - newTimestamp) < toleranceMs) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ it('skips timestamps within tolerance', () => {
+ const existing: MetricSnapshot[] = [
+ { timestamp: 1000000, cpu: 50 },
+ { timestamp: 1030000, cpu: 55 },
+ ];
+
+ // Within 15s of existing
+ expect(shouldSkipDuplicate(existing, 1010000)).toBe(true);
+ expect(shouldSkipDuplicate(existing, 1025000)).toBe(true);
+ });
+
+ it('allows timestamps outside tolerance', () => {
+ const existing: MetricSnapshot[] = [
+ { timestamp: 1000000, cpu: 50 },
+ ];
+
+ // More than 15s from existing
+ expect(shouldSkipDuplicate(existing, 1020000)).toBe(false);
+ });
+
+ it('allows all timestamps when no existing data', () => {
+ const existing: MetricSnapshot[] = [];
+ expect(shouldSkipDuplicate(existing, 1000000)).toBe(false);
+ });
+});
diff --git a/frontend-modern/src/stores/metricsHistory.ts b/frontend-modern/src/stores/metricsHistory.ts
index d36159dd6c..569ca0ec8e 100644
--- a/frontend-modern/src/stores/metricsHistory.ts
+++ b/frontend-modern/src/stores/metricsHistory.ts
@@ -388,6 +388,28 @@ export async function seedFromBackend(range: TimeRange = '1h'): Promise<void> {
}
}
+ // Process Docker containers
+ if (response.dockerData) {
+ for (const [id, chartData] of Object.entries(response.dockerData)) {
+ const resourceKey = buildMetricKey('dockerContainer', id);
+ processChartData(resourceKey, chartData as ChartData);
+ }
+ logger.debug('[MetricsHistory] Processed Docker container data', {
+ count: Object.keys(response.dockerData).length
+ });
+ }
+
+ // Process Docker hosts
+ if (response.dockerHostData) {
+ for (const [id, chartData] of Object.entries(response.dockerHostData)) {
+ const resourceKey = buildMetricKey('dockerHost', id);
+ processChartData(resourceKey, chartData as ChartData);
+ }
+ logger.debug('[MetricsHistory] Processed Docker host data', {
+ count: Object.keys(response.dockerHostData).length
+ });
+ }
+
hasSeededFromBackend = true;
logger.info('[MetricsHistory] Seeded from backend', { seededCount, totalResources: metricsHistoryMap.size });
diff --git a/frontend-modern/src/types/__tests__/resource.test.ts b/frontend-modern/src/types/__tests__/resource.test.ts
new file mode 100644
index 0000000000..b9783f4803
--- /dev/null
+++ b/frontend-modern/src/types/__tests__/resource.test.ts
@@ -0,0 +1,251 @@
+/**
+ * Tests for resource type guards and helper functions
+ */
+import { describe, expect, it } from 'vitest';
+import {
+ isInfrastructure,
+ isWorkload,
+ isStorage,
+ getDisplayName,
+ getCpuPercent,
+ getMemoryPercent,
+ getDiskPercent,
+ type Resource,
+ type ResourceType,
+} from '@/types/resource';
+
+// Helper to create a minimal resource for testing
+function createResource(overrides: Partial<Resource> = {}): Resource {
+ return {
+ id: 'test-1',
+ type: 'vm',
+ name: 'test-resource',
+ displayName: '',
+ platformId: 'platform-1',
+ platformType: 'proxmox-pve',
+ sourceType: 'api',
+ status: 'running',
+ lastSeen: Date.now(),
+ ...overrides,
+ };
+}
+
+describe('Resource Type Guards', () => {
+ describe('isInfrastructure', () => {
+ const infrastructureTypes: ResourceType[] = ['node', 'host', 'docker-host', 'k8s-node', 'truenas'];
+ const nonInfrastructureTypes: ResourceType[] = ['vm', 'container', 'docker-container', 'pod', 'jail', 'storage'];
+
+ it.each(infrastructureTypes)('returns true for %s', (type) => {
+ const resource = createResource({ type });
+ expect(isInfrastructure(resource)).toBe(true);
+ });
+
+ it.each(nonInfrastructureTypes)('returns false for %s', (type) => {
+ const resource = createResource({ type });
+ expect(isInfrastructure(resource)).toBe(false);
+ });
+ });
+
+ describe('isWorkload', () => {
+ const workloadTypes: ResourceType[] = ['vm', 'container', 'docker-container', 'pod', 'jail'];
+ const nonWorkloadTypes: ResourceType[] = ['node', 'host', 'docker-host', 'storage', 'pbs'];
+
+ it.each(workloadTypes)('returns true for %s', (type) => {
+ const resource = createResource({ type });
+ expect(isWorkload(resource)).toBe(true);
+ });
+
+ it.each(nonWorkloadTypes)('returns false for %s', (type) => {
+ const resource = createResource({ type });
+ expect(isWorkload(resource)).toBe(false);
+ });
+ });
+
+ describe('isStorage', () => {
+ const storageTypes: ResourceType[] = ['storage', 'datastore', 'pool', 'dataset'];
+ const nonStorageTypes: ResourceType[] = ['vm', 'node', 'container', 'docker-host'];
+
+ it.each(storageTypes)('returns true for %s', (type) => {
+ const resource = createResource({ type });
+ expect(isStorage(resource)).toBe(true);
+ });
+
+ it.each(nonStorageTypes)('returns false for %s', (type) => {
+ const resource = createResource({ type });
+ expect(isStorage(resource)).toBe(false);
+ });
+ });
+});
+
+describe('Resource Helper Functions', () => {
+ describe('getDisplayName', () => {
+ it('returns displayName when set', () => {
+ const resource = createResource({ name: 'machine-1', displayName: 'Production Server' });
+ expect(getDisplayName(resource)).toBe('Production Server');
+ });
+
+ it('returns name when displayName is empty', () => {
+ const resource = createResource({ name: 'machine-1', displayName: '' });
+ expect(getDisplayName(resource)).toBe('machine-1');
+ });
+
+ it('returns name when displayName is undefined', () => {
+ const resource = createResource({ name: 'machine-1' });
+ // Force displayName to be falsy
+ (resource as any).displayName = undefined;
+ expect(getDisplayName(resource)).toBe('machine-1');
+ });
+ });
+
+ describe('getCpuPercent', () => {
+ it('returns current CPU value when available', () => {
+ const resource = createResource({ cpu: { current: 75.5 } });
+ expect(getCpuPercent(resource)).toBe(75.5);
+ });
+
+ it('returns 0 when cpu is undefined', () => {
+ const resource = createResource({});
+ expect(getCpuPercent(resource)).toBe(0);
+ });
+
+ it('returns 0 when cpu.current is undefined', () => {
+ const resource = createResource({ cpu: {} as any });
+ expect(getCpuPercent(resource)).toBe(0);
+ });
+ });
+
+ describe('getMemoryPercent', () => {
+ it('calculates percentage from used/total when available', () => {
+ const resource = createResource({
+ memory: { current: 0, total: 1000, used: 250 },
+ });
+ expect(getMemoryPercent(resource)).toBe(25);
+ });
+
+ it('returns current when used/total not available', () => {
+ const resource = createResource({
+ memory: { current: 45.5 },
+ });
+ expect(getMemoryPercent(resource)).toBe(45.5);
+ });
+
+ it('returns 0 when memory is undefined', () => {
+ const resource = createResource({});
+ expect(getMemoryPercent(resource)).toBe(0);
+ });
+
+ it('handles zero total gracefully', () => {
+ const resource = createResource({
+ memory: { current: 50, total: 0, used: 0 },
+ });
+ // When total is 0 (falsy), it should fall back to current
+ expect(getMemoryPercent(resource)).toBe(50);
+ });
+ });
+
+ describe('getDiskPercent', () => {
+ it('calculates percentage from used/total when available', () => {
+ const resource = createResource({
+ disk: { current: 0, total: 1000000000, used: 500000000 },
+ });
+ expect(getDiskPercent(resource)).toBe(50);
+ });
+
+ it('returns current when used/total not available', () => {
+ const resource = createResource({
+ disk: { current: 80.2 },
+ });
+ expect(getDiskPercent(resource)).toBe(80.2);
+ });
+
+ it('returns 0 when disk is undefined', () => {
+ const resource = createResource({});
+ expect(getDiskPercent(resource)).toBe(0);
+ });
+ });
+});
+
+describe('Resource Interface', () => {
+ it('allows all valid resource types', () => {
+ const types: ResourceType[] = [
+ 'node', 'host', 'docker-host', 'k8s-node', 'truenas',
+ 'vm', 'container', 'docker-container', 'pod', 'jail',
+ 'docker-service', 'k8s-deployment', 'k8s-service',
+ 'storage', 'datastore', 'pool', 'dataset',
+ 'pbs', 'pmg',
+ ];
+
+ types.forEach(type => {
+ const resource = createResource({ type });
+ expect(resource.type).toBe(type);
+ });
+ });
+
+ it('supports hierarchy with parentId and clusterId', () => {
+ const vm = createResource({
+ type: 'vm',
+ parentId: 'node-1',
+ clusterId: 'pve-cluster-1',
+ });
+
+ expect(vm.parentId).toBe('node-1');
+ expect(vm.clusterId).toBe('pve-cluster-1');
+ });
+
+ it('supports tags and labels', () => {
+ const resource = createResource({
+ tags: ['production', 'web'],
+ labels: { env: 'prod', role: 'frontend' },
+ });
+
+ expect(resource.tags).toEqual(['production', 'web']);
+ expect(resource.labels).toEqual({ env: 'prod', role: 'frontend' });
+ });
+
+ it('supports alerts array', () => {
+ const resource = createResource({
+ alerts: [
+ {
+ id: 'alert-1',
+ type: 'cpu',
+ level: 'warning',
+ message: 'High CPU usage',
+ value: 85,
+ threshold: 80,
+ startTime: Date.now(),
+ },
+ ],
+ });
+
+ expect(resource.alerts).toHaveLength(1);
+ expect(resource.alerts![0].type).toBe('cpu');
+ });
+
+ it('supports identity for deduplication', () => {
+ const resource = createResource({
+ identity: {
+ hostname: 'server-1',
+ machineId: 'abc-123',
+ ips: ['192.168.1.10', '10.0.0.5'],
+ },
+ });
+
+ expect(resource.identity?.hostname).toBe('server-1');
+ expect(resource.identity?.machineId).toBe('abc-123');
+ expect(resource.identity?.ips).toHaveLength(2);
+ });
+
+ it('supports platformData for type-specific data', () => {
+ const dockerContainer = createResource({
+ type: 'docker-container',
+ platformData: {
+ image: 'nginx:latest',
+ health: 'healthy',
+ restartCount: 0,
+ ports: [{ hostPort: 8080, containerPort: 80 }],
+ },
+ });
+
+ expect((dockerContainer.platformData as any).image).toBe('nginx:latest');
+ });
+});
diff --git a/frontend-modern/src/types/ai.ts b/frontend-modern/src/types/ai.ts
index fa602e5f2e..0690a8b069 100644
--- a/frontend-modern/src/types/ai.ts
+++ b/frontend-modern/src/types/ai.ts
@@ -1,6 +1,7 @@
// AI feature types
export type AIProvider = 'anthropic' | 'openai' | 'ollama' | 'deepseek';
+export type AuthMethod = 'api_key' | 'oauth';
export interface AISettings {
enabled: boolean;
@@ -11,6 +12,9 @@ export interface AISettings {
configured: boolean; // true if AI is ready to use
autonomous_mode: boolean; // true if AI can execute commands without approval
custom_context: string; // user-provided infrastructure context
+ // OAuth fields for Claude Pro/Max subscription authentication
+ auth_method: AuthMethod; // "api_key" or "oauth"
+ oauth_connected: boolean; // true if OAuth tokens are configured
}
export interface AISettingsUpdateRequest {
@@ -21,8 +25,10 @@ export interface AISettingsUpdateRequest {
base_url?: string;
autonomous_mode?: boolean;
custom_context?: string; // user-provided infrastructure context
+ auth_method?: AuthMethod; // "api_key" or "oauth"
}
+
export interface AITestResult {
success: boolean;
message: string;
diff --git a/frontend-modern/src/utils/__tests__/format.test.ts b/frontend-modern/src/utils/__tests__/format.test.ts
new file mode 100644
index 0000000000..1d7d887484
--- /dev/null
+++ b/frontend-modern/src/utils/__tests__/format.test.ts
@@ -0,0 +1,315 @@
+/**
+ * Tests for format utility functions
+ */
+import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
+import {
+ formatBytes,
+ formatSpeed,
+ formatPercent,
+ formatNumber,
+ formatUptime,
+ formatAbsoluteTime,
+ formatRelativeTime,
+ getBackupInfo,
+} from '@/utils/format';
+
+describe('formatBytes', () => {
+ it('formats bytes correctly', () => {
+ expect(formatBytes(0)).toBe('0 B');
+ expect(formatBytes(512)).toBe('512.0 B');
+ expect(formatBytes(1024)).toBe('1.0 KB');
+ expect(formatBytes(1536)).toBe('1.5 KB');
+ });
+
+ it('formats kilobytes correctly', () => {
+ expect(formatBytes(1024 * 1024)).toBe('1.0 MB');
+ expect(formatBytes(1536 * 1024)).toBe('1.5 MB');
+ });
+
+ it('formats megabytes correctly', () => {
+ expect(formatBytes(1024 * 1024 * 1024)).toBe('1.0 GB');
+ expect(formatBytes(1.5 * 1024 * 1024 * 1024)).toBe('1.5 GB');
+ });
+
+ it('formats gigabytes correctly', () => {
+ expect(formatBytes(1024 * 1024 * 1024 * 1024)).toBe('1.0 TB');
+ });
+
+ it('handles negative values', () => {
+ expect(formatBytes(-1024)).toBe('0 B');
+ });
+
+ it('handles custom decimal places', () => {
+ expect(formatBytes(1536 * 1024, 2)).toBe('1.50 MB');
+ expect(formatBytes(1536 * 1024, 0)).toBe('2 MB');
+ });
+});
+
+describe('formatSpeed', () => {
+ it('formats speed correctly', () => {
+ expect(formatSpeed(0)).toBe('0 B/s');
+ expect(formatSpeed(1024)).toBe('1 KB/s');
+ expect(formatSpeed(1024 * 1024)).toBe('1 MB/s');
+ });
+
+ it('handles negative values', () => {
+ expect(formatSpeed(-1024)).toBe('0 B/s');
+ });
+});
+
+describe('formatPercent', () => {
+ it('formats percentages correctly', () => {
+ expect(formatPercent(0)).toBe('0%');
+ expect(formatPercent(50)).toBe('50%');
+ expect(formatPercent(100)).toBe('100%');
+ });
+
+ it('rounds to nearest integer', () => {
+ expect(formatPercent(50.4)).toBe('50%');
+ expect(formatPercent(50.5)).toBe('51%');
+ expect(formatPercent(50.6)).toBe('51%');
+ });
+
+ it('handles very small values', () => {
+ expect(formatPercent(0.1)).toBe('0%');
+ expect(formatPercent(0.49)).toBe('0%');
+ });
+
+ it('handles non-finite values', () => {
+ expect(formatPercent(NaN)).toBe('0%');
+ expect(formatPercent(Infinity)).toBe('0%');
+ expect(formatPercent(-Infinity)).toBe('0%');
+ });
+});
+
+describe('formatNumber', () => {
+ it('formats numbers with locale separators', () => {
+ expect(formatNumber(0)).toBe('0');
+ expect(formatNumber(1000)).toMatch(/1[,.]000/); // Locale-dependent
+ });
+
+ it('handles non-finite values', () => {
+ expect(formatNumber(NaN)).toBe('0');
+ expect(formatNumber(Infinity)).toBe('0');
+ });
+});
+
+describe('formatUptime', () => {
+ it('formats seconds correctly', () => {
+ expect(formatUptime(0)).toBe('0s');
+ expect(formatUptime(30)).toBe('0m');
+ });
+
+ it('formats minutes correctly', () => {
+ expect(formatUptime(60)).toBe('1m');
+ expect(formatUptime(120)).toBe('2m');
+ expect(formatUptime(3599)).toBe('59m');
+ });
+
+ it('formats hours correctly', () => {
+ expect(formatUptime(3600)).toBe('1h 0m');
+ expect(formatUptime(7200)).toBe('2h 0m');
+ expect(formatUptime(7230)).toBe('2h 0m'); // 2 hours, 0 minutes, 30 seconds
+ expect(formatUptime(7260)).toBe('2h 1m');
+ });
+
+ it('formats days correctly', () => {
+ expect(formatUptime(86400)).toBe('1d 0h');
+ expect(formatUptime(86400 + 3600)).toBe('1d 1h');
+ expect(formatUptime(86400 * 7 + 3600 * 12)).toBe('7d 12h');
+ });
+
+ it('uses condensed format when requested', () => {
+ expect(formatUptime(86400 + 3600, true)).toBe('1d');
+ expect(formatUptime(3600 + 60, true)).toBe('1h');
+ expect(formatUptime(60, true)).toBe('1m');
+ });
+
+ it('handles negative values', () => {
+ expect(formatUptime(-100)).toBe('0s');
+ });
+});
+
+describe('formatAbsoluteTime', () => {
+ it('returns empty string for falsy input', () => {
+ expect(formatAbsoluteTime(0)).toBe('');
+ });
+
+ it('formats timestamp correctly', () => {
+ // Create a known date: March 15, 2024 at 14:30
+ const date = new Date(2024, 2, 15, 14, 30); // Month is 0-indexed
+ const timestamp = date.getTime();
+ const result = formatAbsoluteTime(timestamp);
+ expect(result).toBe('15 Mar 14:30');
+ });
+
+ it('pads hours and minutes with zeros', () => {
+ const date = new Date(2024, 0, 5, 9, 5); // Jan 5 at 09:05
+ const result = formatAbsoluteTime(date.getTime());
+ expect(result).toBe('5 Jan 09:05');
+ });
+});
+
+describe('formatRelativeTime', () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date('2024-03-15T12:00:00Z'));
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it('returns empty string for falsy input', () => {
+ expect(formatRelativeTime(0)).toBe('');
+ });
+
+ it('formats seconds ago', () => {
+ const now = Date.now();
+ expect(formatRelativeTime(now - 30 * 1000)).toBe('30s ago');
+ expect(formatRelativeTime(now - 1 * 1000)).toBe('1s ago');
+ });
+
+ it('formats minutes ago', () => {
+ const now = Date.now();
+ expect(formatRelativeTime(now - 60 * 1000)).toBe('1 min ago');
+ expect(formatRelativeTime(now - 5 * 60 * 1000)).toBe('5 mins ago');
+ expect(formatRelativeTime(now - 59 * 60 * 1000)).toBe('59 mins ago');
+ });
+
+ it('formats hours ago', () => {
+ const now = Date.now();
+ expect(formatRelativeTime(now - 60 * 60 * 1000)).toBe('1 hour ago');
+ expect(formatRelativeTime(now - 5 * 60 * 60 * 1000)).toBe('5 hours ago');
+ expect(formatRelativeTime(now - 23 * 60 * 60 * 1000)).toBe('23 hours ago');
+ });
+
+ it('formats days ago', () => {
+ const now = Date.now();
+ expect(formatRelativeTime(now - 24 * 60 * 60 * 1000)).toBe('1 day ago');
+ expect(formatRelativeTime(now - 7 * 24 * 60 * 60 * 1000)).toBe('7 days ago');
+ });
+
+ it('formats months ago', () => {
+ const now = Date.now();
+ expect(formatRelativeTime(now - 30 * 24 * 60 * 60 * 1000)).toBe('1 month ago');
+ expect(formatRelativeTime(now - 60 * 24 * 60 * 60 * 1000)).toBe('2 months ago');
+ });
+
+ it('formats years ago', () => {
+ const now = Date.now();
+ expect(formatRelativeTime(now - 365 * 24 * 60 * 60 * 1000)).toBe('1 year ago');
+ expect(formatRelativeTime(now - 2 * 365 * 24 * 60 * 60 * 1000)).toBe('2 years ago');
+ });
+
+ it('handles future timestamps', () => {
+ const now = Date.now();
+ expect(formatRelativeTime(now + 60 * 1000)).toBe('0s ago');
+ });
+});
+
+describe('getBackupInfo', () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.setSystemTime(new Date('2024-03-15T12:00:00Z'));
+ });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it('returns never status for null/undefined input', () => {
+ expect(getBackupInfo(null)).toEqual({
+ status: 'never',
+ ageMs: null,
+ ageFormatted: 'Never',
+ });
+ expect(getBackupInfo(undefined)).toEqual({
+ status: 'never',
+ ageMs: null,
+ ageFormatted: 'Never',
+ });
+ });
+
+ it('returns never status for invalid timestamp', () => {
+ expect(getBackupInfo('invalid')).toEqual({
+ status: 'never',
+ ageMs: null,
+ ageFormatted: 'Never',
+ });
+ expect(getBackupInfo(0)).toEqual({
+ status: 'never',
+ ageMs: null,
+ ageFormatted: 'Never',
+ });
+ });
+
+ it('returns fresh status for backup within 24 hours', () => {
+ const now = Date.now();
+ const oneHourAgo = now - 60 * 60 * 1000;
+ const result = getBackupInfo(oneHourAgo);
+ expect(result.status).toBe('fresh');
+ expect(result.ageMs).toBe(60 * 60 * 1000);
+ expect(result.ageFormatted).toBe('1 hour ago');
+ });
+
+ it('returns stale status for backup between 24-72 hours', () => {
+ const now = Date.now();
+ const twoDaysAgo = now - 48 * 60 * 60 * 1000;
+ const result = getBackupInfo(twoDaysAgo);
+ expect(result.status).toBe('stale');
+ expect(result.ageFormatted).toBe('2 days ago');
+ });
+
+ it('returns critical status for backup older than 72 hours', () => {
+ const now = Date.now();
+ const fourDaysAgo = now - 4 * 24 * 60 * 60 * 1000;
+ const result = getBackupInfo(fourDaysAgo);
+ expect(result.status).toBe('critical');
+ expect(result.ageFormatted).toBe('4 days ago');
+ });
+
+ it('handles ISO date string input', () => {
+ const now = Date.now();
+ const oneHourAgo = new Date(now - 60 * 60 * 1000).toISOString();
+ const result = getBackupInfo(oneHourAgo);
+ expect(result.status).toBe('fresh');
+ });
+
+ it('handles numeric timestamp input', () => {
+ const now = Date.now();
+ const oneHourAgo = now - 60 * 60 * 1000;
+ const result = getBackupInfo(oneHourAgo);
+ expect(result.status).toBe('fresh');
+ });
+
+ describe('threshold boundaries', () => {
+ it('fresh at exactly 24 hours', () => {
+ const now = Date.now();
+ const exactly24Hours = now - 24 * 60 * 60 * 1000;
+ const result = getBackupInfo(exactly24Hours);
+ expect(result.status).toBe('fresh');
+ });
+
+ it('stale just after 24 hours', () => {
+ const now = Date.now();
+ const justOver24Hours = now - (24 * 60 * 60 * 1000 + 1000);
+ const result = getBackupInfo(justOver24Hours);
+ expect(result.status).toBe('stale');
+ });
+
+ it('stale at exactly 72 hours', () => {
+ const now = Date.now();
+ const exactly72Hours = now - 72 * 60 * 60 * 1000;
+ const result = getBackupInfo(exactly72Hours);
+ expect(result.status).toBe('stale');
+ });
+
+ it('critical just after 72 hours', () => {
+ const now = Date.now();
+ const justOver72Hours = now - (72 * 60 * 60 * 1000 + 1000);
+ const result = getBackupInfo(justOver72Hours);
+ expect(result.status).toBe('critical');
+ });
+ });
+});
diff --git a/frontend-modern/src/utils/__tests__/metricsKeys.test.ts b/frontend-modern/src/utils/__tests__/metricsKeys.test.ts
new file mode 100644
index 0000000000..539904fdaa
--- /dev/null
+++ b/frontend-modern/src/utils/__tests__/metricsKeys.test.ts
@@ -0,0 +1,67 @@
+/**
+ * Tests for metricsKeys utility functions
+ */
+import { describe, expect, it } from 'vitest';
+import {
+ buildMetricKey,
+ getMetricKeyPrefix,
+ type MetricResourceKind,
+} from '@/utils/metricsKeys';
+
+describe('metricsKeys', () => {
+ describe('buildMetricKey', () => {
+ it('builds a key for node resources', () => {
+ expect(buildMetricKey('node', 'pve1')).toBe('node:pve1');
+ });
+
+ it('builds a key for VM resources', () => {
+ expect(buildMetricKey('vm', 'vm-100')).toBe('vm:vm-100');
+ });
+
+ it('builds a key for container resources', () => {
+ expect(buildMetricKey('container', 'ct-200')).toBe('container:ct-200');
+ });
+
+ it('builds a key for docker host resources', () => {
+ expect(buildMetricKey('dockerHost', 'docker-host-1')).toBe('dockerHost:docker-host-1');
+ });
+
+ it('builds a key for docker container resources', () => {
+ expect(buildMetricKey('dockerContainer', 'abc123def')).toBe('dockerContainer:abc123def');
+ });
+
+ it('handles IDs with special characters', () => {
+ expect(buildMetricKey('vm', 'pve1/qemu/100')).toBe('vm:pve1/qemu/100');
+ });
+
+ it('handles empty IDs', () => {
+ expect(buildMetricKey('node', '')).toBe('node:');
+ });
+
+ it('handles IDs with colons (should not conflict)', () => {
+ expect(buildMetricKey('container', 'host:123')).toBe('container:host:123');
+ });
+ });
+
+ describe('getMetricKeyPrefix', () => {
+ const kinds: MetricResourceKind[] = ['node', 'vm', 'container', 'dockerHost', 'dockerContainer'];
+
+ it.each(kinds)('returns correct prefix for %s', (kind) => {
+ expect(getMetricKeyPrefix(kind)).toBe(`${kind}:`);
+ });
+
+ it('prefixes can be used to filter keys', () => {
+ const keys = [
+ 'node:pve1',
+ 'node:pve2',
+ 'vm:100',
+ 'dockerContainer:abc123',
+ ];
+
+ const nodePrefix = getMetricKeyPrefix('node');
+ const nodeKeys = keys.filter(k => k.startsWith(nodePrefix));
+
+ expect(nodeKeys).toEqual(['node:pve1', 'node:pve2']);
+ });
+ });
+});
diff --git a/frontend-modern/src/utils/__tests__/status.test.ts b/frontend-modern/src/utils/__tests__/status.test.ts
new file mode 100644
index 0000000000..d9efec1a65
--- /dev/null
+++ b/frontend-modern/src/utils/__tests__/status.test.ts
@@ -0,0 +1,275 @@
+/**
+ * Tests for status utility functions
+ *
+ * These tests verify the status indicator logic for various resource types.
+ */
+import { describe, expect, it } from 'vitest';
+import {
+ isNodeOnline,
+ isGuestRunning,
+ getNodeStatusIndicator,
+ getGuestPowerIndicator,
+ getHostStatusIndicator,
+ getDockerHostStatusIndicator,
+ getDockerContainerStatusIndicator,
+ getDockerServiceStatusIndicator,
+ OFFLINE_HEALTH_STATUSES,
+ DEGRADED_HEALTH_STATUSES,
+ STOPPED_CONTAINER_STATES,
+ ERROR_CONTAINER_STATES,
+} from '@/utils/status';
+
+describe('isNodeOnline', () => {
+ it('returns false for null/undefined', () => {
+ expect(isNodeOnline(null)).toBe(false);
+ expect(isNodeOnline(undefined)).toBe(false);
+ });
+
+ it('returns true for online node with uptime', () => {
+ expect(isNodeOnline({ status: 'online', uptime: 1000 })).toBe(true);
+ });
+
+ it('returns false for offline status', () => {
+ expect(isNodeOnline({ status: 'offline', uptime: 1000 })).toBe(false);
+ });
+
+ it('returns false for zero uptime', () => {
+ expect(isNodeOnline({ status: 'online', uptime: 0 })).toBe(false);
+ });
+
+ it('returns false for negative uptime', () => {
+ expect(isNodeOnline({ status: 'online', uptime: -1 })).toBe(false);
+ });
+
+ it('returns false for error connection health', () => {
+ expect(isNodeOnline({ status: 'online', uptime: 1000, connectionHealth: 'error' })).toBe(false);
+ expect(isNodeOnline({ status: 'online', uptime: 1000, connectionHealth: 'offline' })).toBe(false);
+ });
+});
+
+describe('isGuestRunning', () => {
+ it('returns false for null/undefined', () => {
+ expect(isGuestRunning(null)).toBe(false);
+ expect(isGuestRunning(undefined)).toBe(false);
+ });
+
+ it('returns true for running guest with online parent', () => {
+ expect(isGuestRunning({ status: 'running' }, true)).toBe(true);
+ });
+
+ it('returns false for running guest with offline parent', () => {
+ expect(isGuestRunning({ status: 'running' }, false)).toBe(false);
+ });
+
+ it('returns false for stopped guest', () => {
+ expect(isGuestRunning({ status: 'stopped' }, true)).toBe(false);
+ });
+});
+
+describe('getNodeStatusIndicator', () => {
+ it('returns muted for null/undefined', () => {
+ expect(getNodeStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
+ expect(getNodeStatusIndicator(undefined)).toEqual({ variant: 'muted', label: 'Unknown' });
+ });
+
+ it('returns success for online node', () => {
+ const result = getNodeStatusIndicator({ status: 'online', uptime: 1000 });
+ expect(result.variant).toBe('success');
+ expect(result.label).toBe('Online');
+ });
+
+ it('returns danger for offline node', () => {
+ const result = getNodeStatusIndicator({ status: 'offline', uptime: 0 });
+ expect(result.variant).toBe('danger');
+ });
+
+ it('returns warning for degraded node', () => {
+ const result = getNodeStatusIndicator({ status: 'degraded', uptime: 1000 });
+ expect(result.variant).toBe('warning');
+ });
+});
+
+describe('getGuestPowerIndicator', () => {
+ it('returns muted for null/undefined', () => {
+ expect(getGuestPowerIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
+ });
+
+ it('returns success for running guest', () => {
+ const result = getGuestPowerIndicator({ status: 'running' }, true);
+ expect(result.variant).toBe('success');
+ expect(result.label).toBe('Running');
+ });
+
+ it('returns danger for stopped guest', () => {
+ const result = getGuestPowerIndicator({ status: 'stopped' }, true);
+ expect(result.variant).toBe('danger');
+ expect(result.label).toBe('Stopped');
+ });
+
+ it('returns danger when parent node is offline', () => {
+ const result = getGuestPowerIndicator({ status: 'running' }, false);
+ expect(result.variant).toBe('danger');
+ expect(result.label).toBe('Node offline');
+ });
+});
+
+describe('getHostStatusIndicator', () => {
+ it('returns muted for null/undefined', () => {
+ expect(getHostStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
+ });
+
+ it('returns success for online host', () => {
+ const result = getHostStatusIndicator({ status: 'online' });
+ expect(result.variant).toBe('success');
+ expect(result.label).toBe('Online');
+ });
+
+ it('returns danger for offline host', () => {
+ const result = getHostStatusIndicator({ status: 'offline' });
+ expect(result.variant).toBe('danger');
+ });
+
+ it('returns warning for degraded host', () => {
+ const result = getHostStatusIndicator({ status: 'degraded' });
+ expect(result.variant).toBe('warning');
+ });
+});
+
+describe('getDockerHostStatusIndicator', () => {
+ it('returns muted for null/undefined', () => {
+ expect(getDockerHostStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
+ });
+
+ it('accepts string status directly', () => {
+ const result = getDockerHostStatusIndicator('online');
+ expect(result.variant).toBe('success');
+ });
+
+ it('returns success for healthy Docker host', () => {
+ const result = getDockerHostStatusIndicator({ status: 'healthy' });
+ expect(result.variant).toBe('success');
+ });
+
+ it('returns danger for offline Docker host', () => {
+ const result = getDockerHostStatusIndicator({ status: 'offline' });
+ expect(result.variant).toBe('danger');
+ });
+});
+
+describe('getDockerContainerStatusIndicator', () => {
+ it('returns muted for null/undefined', () => {
+ expect(getDockerContainerStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
+ });
+
+ it('returns success for running healthy container', () => {
+ const result = getDockerContainerStatusIndicator({ state: 'running', health: 'healthy' });
+ expect(result.variant).toBe('success');
+ expect(result.label).toBe('Running');
+ });
+
+ it('returns success for running container without health', () => {
+ const result = getDockerContainerStatusIndicator({ state: 'running' });
+ expect(result.variant).toBe('success');
+ });
+
+ it('returns danger for unhealthy container', () => {
+ const result = getDockerContainerStatusIndicator({ state: 'running', health: 'unhealthy' });
+ expect(result.variant).toBe('danger');
+ expect(result.label).toBe('Unhealthy');
+ });
+
+ it('returns danger for exited container', () => {
+ const result = getDockerContainerStatusIndicator({ state: 'exited' });
+ expect(result.variant).toBe('danger');
+ });
+
+ it('returns danger for dead container', () => {
+ const result = getDockerContainerStatusIndicator({ state: 'dead' });
+ expect(result.variant).toBe('danger');
+ });
+});
+
+describe('getDockerServiceStatusIndicator', () => {
+ it('returns muted for null/undefined', () => {
+ expect(getDockerServiceStatusIndicator(null)).toEqual({ variant: 'muted', label: 'Unknown' });
+ });
+
+ it('returns success when running >= desired', () => {
+ const result = getDockerServiceStatusIndicator({ desiredTasks: 3, runningTasks: 3 });
+ expect(result.variant).toBe('success');
+ expect(result.label).toBe('Healthy');
+ });
+
+ it('returns warning when running < desired but > 0', () => {
+ const result = getDockerServiceStatusIndicator({ desiredTasks: 3, runningTasks: 2 });
+ expect(result.variant).toBe('warning');
+ expect(result.label).toBe('Degraded (2/3)');
+ });
+
+ it('returns danger when running is 0', () => {
+ const result = getDockerServiceStatusIndicator({ desiredTasks: 3, runningTasks: 0 });
+ expect(result.variant).toBe('danger');
+ expect(result.label).toBe('Stopped (0/3)');
+ });
+
+ it('returns muted when desired is 0 and running is 0', () => {
+ const result = getDockerServiceStatusIndicator({ desiredTasks: 0, runningTasks: 0 });
+ expect(result.variant).toBe('muted');
+ expect(result.label).toBe('No tasks');
+ });
+
+ it('returns warning when desired is 0 but running > 0', () => {
+ const result = getDockerServiceStatusIndicator({ desiredTasks: 0, runningTasks: 2 });
+ expect(result.variant).toBe('warning');
+ expect(result.label).toBe('Running 2 tasks');
+ });
+});
+
+describe('Status sets', () => {
+ describe('OFFLINE_HEALTH_STATUSES', () => {
+ it('contains expected offline statuses', () => {
+ expect(OFFLINE_HEALTH_STATUSES.has('offline')).toBe(true);
+ expect(OFFLINE_HEALTH_STATUSES.has('error')).toBe(true);
+ expect(OFFLINE_HEALTH_STATUSES.has('failed')).toBe(true);
+ expect(OFFLINE_HEALTH_STATUSES.has('timeout')).toBe(true);
+ expect(OFFLINE_HEALTH_STATUSES.has('stopped')).toBe(true);
+ });
+
+ it('does not contain online statuses', () => {
+ expect(OFFLINE_HEALTH_STATUSES.has('online')).toBe(false);
+ expect(OFFLINE_HEALTH_STATUSES.has('running')).toBe(false);
+ expect(OFFLINE_HEALTH_STATUSES.has('healthy')).toBe(false);
+ });
+ });
+
+ describe('DEGRADED_HEALTH_STATUSES', () => {
+ it('contains expected degraded statuses', () => {
+ expect(DEGRADED_HEALTH_STATUSES.has('degraded')).toBe(true);
+ expect(DEGRADED_HEALTH_STATUSES.has('warning')).toBe(true);
+ expect(DEGRADED_HEALTH_STATUSES.has('syncing')).toBe(true);
+ expect(DEGRADED_HEALTH_STATUSES.has('pending')).toBe(true);
+ });
+ });
+
+ describe('STOPPED_CONTAINER_STATES', () => {
+ it('contains expected stopped states', () => {
+ expect(STOPPED_CONTAINER_STATES.has('exited')).toBe(true);
+ expect(STOPPED_CONTAINER_STATES.has('stopped')).toBe(true);
+ expect(STOPPED_CONTAINER_STATES.has('created')).toBe(true);
+ expect(STOPPED_CONTAINER_STATES.has('paused')).toBe(true);
+ });
+
+ it('does not contain running state', () => {
+ expect(STOPPED_CONTAINER_STATES.has('running')).toBe(false);
+ });
+ });
+
+ describe('ERROR_CONTAINER_STATES', () => {
+ it('contains expected error states', () => {
+ expect(ERROR_CONTAINER_STATES.has('dead')).toBe(true);
+ expect(ERROR_CONTAINER_STATES.has('oomkilled')).toBe(true);
+ expect(ERROR_CONTAINER_STATES.has('unhealthy')).toBe(true);
+ expect(ERROR_CONTAINER_STATES.has('restarting')).toBe(true);
+ });
+ });
+});
diff --git a/internal/ai/providers/anthropic_oauth.go b/internal/ai/providers/anthropic_oauth.go
new file mode 100644
index 0000000000..8396a8781f
--- /dev/null
+++ b/internal/ai/providers/anthropic_oauth.go
@@ -0,0 +1,611 @@
+package providers
+
+// anthropic_oauth.go - OAuth client for Claude Pro/Max subscription authentication
+//
+// STATUS: DISABLED IN UI - Anthropic restricts OAuth to Claude Code only
+//
+// This implementation follows Claude Code's OAuth flow exactly:
+// 1. Get authorization code from claude.ai/oauth/authorize
+// 2. Exchange code for tokens at console.anthropic.com/v1/oauth/token
+// 3. Try to create API key (works for Team/Enterprise users)
+// 4. Use OAuth tokens directly (for Pro/Max users)
+//
+// However, Anthropic has locked down their OAuth system:
+// - OAuth tokens are server-side validated to only work within Claude Code
+// - The error "This credential is only authorized for use with Claude Code" is returned
+// - Pro/Max users don't have org:create_api_key permission to create API keys
+//
+// This code is kept intact in case Anthropic opens up OAuth for third-party apps in the future.
+// To re-enable, update the frontend AISettings.tsx to un-disable the OAuth button.
+
+import (
+ "bytes"
+ "context"
+ "crypto/rand"
+ "crypto/sha256"
+ "encoding/base64"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+ "net/url"
+ "time"
+
+ "github.com/rs/zerolog/log"
+)
+
+const (
+ // OAuth endpoints - these are the same endpoints Claude Code uses
+ // Authorization happens at claude.ai, token exchange at console.anthropic.com
+ anthropicAuthorizeURL = "https://claude.ai/oauth/authorize"
+ anthropicTokenURL = "https://console.anthropic.com/v1/oauth/token"
+ // API key creation endpoint - OAuth tokens create an API key for actual use
+ anthropicAPIKeyURL = "https://api.anthropic.com/api/oauth/claude_cli/create_api_key"
+ // Claude Code's client ID - this is what enables subscription-based auth
+ claudeCodeClientID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"
+ // Scopes needed for inference with subscription (matching Claude Code's Oo0 array)
+ // org:create_api_key, user:profile, user:inference, user:sessions:claude_code
+ oauthScopes = "org:create_api_key user:profile user:inference user:sessions:claude_code"
+)
+
+// OAuthTokens represents the tokens obtained from OAuth flow
+type OAuthTokens struct {
+ AccessToken string `json:"access_token"`
+ RefreshToken string `json:"refresh_token"`
+ TokenType string `json:"token_type"`
+ ExpiresIn int `json:"expires_in"`
+ Scope string `json:"scope"`
+ ExpiresAt time.Time `json:"-"`
+ // APIKey is created from the access token and used for actual API calls
+ APIKey string `json:"api_key,omitempty"`
+}
+
+// OAuthSession stores the state for an in-progress OAuth flow
+type OAuthSession struct {
+ State string
+ CodeVerifier string
+ RedirectURI string
+ CreatedAt time.Time
+}
+
+// GenerateOAuthSession creates a new OAuth session with PKCE parameters
+func GenerateOAuthSession(redirectURI string) (*OAuthSession, error) {
+ // Generate state (for CSRF protection)
+ stateBytes := make([]byte, 32)
+ if _, err := rand.Read(stateBytes); err != nil {
+ return nil, fmt.Errorf("failed to generate state: %w", err)
+ }
+ state := base64.RawURLEncoding.EncodeToString(stateBytes)
+
+ // Generate code verifier (PKCE)
+ verifierBytes := make([]byte, 32)
+ if _, err := rand.Read(verifierBytes); err != nil {
+ return nil, fmt.Errorf("failed to generate code verifier: %w", err)
+ }
+ codeVerifier := base64.RawURLEncoding.EncodeToString(verifierBytes)
+
+ return &OAuthSession{
+ State: state,
+ CodeVerifier: codeVerifier,
+ RedirectURI: redirectURI,
+ CreatedAt: time.Now(),
+ }, nil
+}
+
+// GetAuthorizationURL returns the URL to redirect the user to for OAuth authorization
+// The user will visit this URL, log in, and get an authorization code to paste back
+func GetAuthorizationURL(session *OAuthSession) string {
+ // Generate code challenge from code verifier (S256)
+ h := sha256.Sum256([]byte(session.CodeVerifier))
+ codeChallenge := base64.RawURLEncoding.EncodeToString(h[:])
+
+ params := url.Values{}
+ // Match Claude Code's exact parameter order and values
+ params.Set("code", "true") // Claude Code adds this
+ params.Set("client_id", claudeCodeClientID)
+ params.Set("response_type", "code")
+ // Use Anthropic's official callback which displays the code for the user to copy
+ params.Set("redirect_uri", "https://console.anthropic.com/oauth/code/callback")
+ params.Set("scope", oauthScopes)
+ params.Set("code_challenge", codeChallenge)
+ params.Set("code_challenge_method", "S256")
+ params.Set("state", session.State)
+
+ return anthropicAuthorizeURL + "?" + params.Encode()
+}
+
+// ExchangeCodeForTokens exchanges an authorization code for access tokens
+func ExchangeCodeForTokens(ctx context.Context, code string, session *OAuthSession) (*OAuthTokens, error) {
+ // Build JSON body (matching Claude Code's exact implementation)
+ // Claude Code uses Content-Type: application/json, NOT form-urlencoded
+ payload := map[string]interface{}{
+ "grant_type": "authorization_code",
+ "code": code,
+ "redirect_uri": "https://console.anthropic.com/oauth/code/callback",
+ "client_id": claudeCodeClientID,
+ "code_verifier": session.CodeVerifier,
+ "state": session.State,
+ }
+
+ jsonBody, err := json.Marshal(payload)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal token request: %w", err)
+ }
+
+ log.Debug().
+ Str("grant_type", "authorization_code").
+ Str("client_id", claudeCodeClientID).
+ Str("code_len", fmt.Sprintf("%d", len(code))).
+ Str("verifier_len", fmt.Sprintf("%d", len(session.CodeVerifier))).
+ Str("state_prefix", session.State[:min(8, len(session.State))]).
+ Str("redirect_uri", "https://console.anthropic.com/oauth/code/callback").
+ Msg("Sending OAuth token exchange request (JSON)")
+
+ req, err := http.NewRequestWithContext(ctx, "POST", anthropicTokenURL, bytes.NewReader(jsonBody))
+ if err != nil {
+ return nil, fmt.Errorf("failed to create token request: %w", err)
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+
+ client := &http.Client{Timeout: 30 * time.Second}
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("token request failed: %w", err)
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read token response: %w", err)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ log.Error().
+ Int("status", resp.StatusCode).
+ Str("body", string(body)).
+ Msg("OAuth token exchange failed")
+ return nil, fmt.Errorf("token exchange failed with status %d: %s", resp.StatusCode, string(body))
+ }
+
+ var tokens OAuthTokens
+ if err := json.Unmarshal(body, &tokens); err != nil {
+ return nil, fmt.Errorf("failed to parse token response: %w", err)
+ }
+
+ // Calculate expiration time
+ if tokens.ExpiresIn > 0 {
+ tokens.ExpiresAt = time.Now().Add(time.Duration(tokens.ExpiresIn) * time.Second)
+ }
+
+ log.Info().
+ Str("token_type", tokens.TokenType).
+ Int("expires_in", tokens.ExpiresIn).
+ Str("scope", tokens.Scope).
+ Msg("Successfully exchanged OAuth code for tokens")
+
+ return &tokens, nil
+}
+
+// CreateAPIKeyFromOAuth uses an OAuth access token to create a real API key
+// This is how Claude Code uses the OAuth flow - the OAuth token creates an API key
+func CreateAPIKeyFromOAuth(ctx context.Context, accessToken string) (string, error) {
+ req, err := http.NewRequestWithContext(ctx, "POST", anthropicAPIKeyURL, nil)
+ if err != nil {
+ return "", fmt.Errorf("failed to create API key request: %w", err)
+ }
+
+ req.Header.Set("Authorization", "Bearer "+accessToken)
+
+ client := &http.Client{Timeout: 30 * time.Second}
+ resp, err := client.Do(req)
+ if err != nil {
+ return "", fmt.Errorf("API key request failed: %w", err)
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return "", fmt.Errorf("failed to read API key response: %w", err)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ log.Error().
+ Int("status", resp.StatusCode).
+ Str("body", string(body)).
+ Msg("Failed to create API key from OAuth token")
+ return "", fmt.Errorf("API key creation failed with status %d: %s", resp.StatusCode, string(body))
+ }
+
+ var result struct {
+ RawKey string `json:"raw_key"`
+ }
+ if err := json.Unmarshal(body, &result); err != nil {
+ return "", fmt.Errorf("failed to parse API key response: %w", err)
+ }
+
+ if result.RawKey == "" {
+ return "", fmt.Errorf("no API key returned from OAuth")
+ }
+
+ log.Info().Msg("Successfully created API key from OAuth token")
+ return result.RawKey, nil
+}
+
+// RefreshAccessToken uses a refresh token to get a new access token
+func RefreshAccessToken(ctx context.Context, refreshToken string) (*OAuthTokens, error) {
+ // Build JSON body (matching Claude Code's implementation)
+ payload := map[string]interface{}{
+ "grant_type": "refresh_token",
+ "refresh_token": refreshToken,
+ "client_id": claudeCodeClientID,
+ "scope": oauthScopes,
+ }
+
+ jsonBody, err := json.Marshal(payload)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal refresh request: %w", err)
+ }
+
+ log.Debug().
+ Str("grant_type", "refresh_token").
+ Str("client_id", claudeCodeClientID).
+ Msg("Sending OAuth token refresh request")
+
+ req, err := http.NewRequestWithContext(ctx, "POST", anthropicTokenURL, bytes.NewReader(jsonBody))
+ if err != nil {
+ return nil, fmt.Errorf("failed to create refresh request: %w", err)
+ }
+
+ req.Header.Set("Content-Type", "application/json")
+
+ client := &http.Client{Timeout: 30 * time.Second}
+ resp, err := client.Do(req)
+ if err != nil {
+ return nil, fmt.Errorf("refresh request failed: %w", err)
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read refresh response: %w", err)
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ log.Warn().
+ Int("status", resp.StatusCode).
+ Str("body", string(body)).
+ Msg("OAuth token refresh failed")
+ return nil, fmt.Errorf("token refresh failed with status %d: %s", resp.StatusCode, string(body))
+ }
+
+ var tokens OAuthTokens
+ if err := json.Unmarshal(body, &tokens); err != nil {
+ return nil, fmt.Errorf("failed to parse refresh response: %w", err)
+ }
+
+ // Keep the original refresh token if a new one wasn't returned
+ if tokens.RefreshToken == "" {
+ tokens.RefreshToken = refreshToken
+ }
+
+ // Calculate expiration time
+ if tokens.ExpiresIn > 0 {
+ tokens.ExpiresAt = time.Now().Add(time.Duration(tokens.ExpiresIn) * time.Second)
+ }
+
+ log.Info().
+ Int("expires_in", tokens.ExpiresIn).
+ Msg("Successfully refreshed OAuth access token")
+
+ return &tokens, nil
+}
+
+// AnthropicOAuthClient is a client that uses OAuth tokens instead of API keys
+type AnthropicOAuthClient struct {
+ accessToken string
+ refreshToken string
+ expiresAt time.Time
+ model string
+ client *http.Client
+ onTokenRefresh func(tokens *OAuthTokens) // Callback when tokens are refreshed
+}
+
+// NewAnthropicOAuthClient creates a new Anthropic client using OAuth tokens
+func NewAnthropicOAuthClient(accessToken, refreshToken string, expiresAt time.Time, model string) *AnthropicOAuthClient {
+ return &AnthropicOAuthClient{
+ accessToken: accessToken,
+ refreshToken: refreshToken,
+ expiresAt: expiresAt,
+ model: model,
+ client: &http.Client{
+ Timeout: 300 * time.Second,
+ },
+ }
+}
+
+// SetTokenRefreshCallback sets a callback that will be called when tokens are refreshed
+func (c *AnthropicOAuthClient) SetTokenRefreshCallback(callback func(tokens *OAuthTokens)) {
+ c.onTokenRefresh = callback
+}
+
+// Name returns the provider name
+func (c *AnthropicOAuthClient) Name() string {
+ return "anthropic-oauth"
+}
+
+// ensureValidToken checks if the token is expired and refreshes if needed
+func (c *AnthropicOAuthClient) ensureValidToken(ctx context.Context) error {
+ // Add some buffer (5 minutes) before expiration
+ if time.Now().Add(5 * time.Minute).Before(c.expiresAt) {
+ return nil // Token is still valid
+ }
+
+ return c.forceRefreshToken(ctx)
+}
+
+// forceRefreshToken forces a token refresh (used on 401 errors)
+func (c *AnthropicOAuthClient) forceRefreshToken(ctx context.Context) error {
+ if c.refreshToken == "" {
+ return fmt.Errorf("access token expired and no refresh token available")
+ }
+
+ log.Info().Msg("OAuth access token expired, refreshing...")
+
+ tokens, err := RefreshAccessToken(ctx, c.refreshToken)
+ if err != nil {
+ return fmt.Errorf("failed to refresh token: %w", err)
+ }
+
+ c.accessToken = tokens.AccessToken
+ if tokens.RefreshToken != "" {
+ c.refreshToken = tokens.RefreshToken
+ }
+ c.expiresAt = tokens.ExpiresAt
+
+ // Notify callback of new tokens
+ if c.onTokenRefresh != nil {
+ c.onTokenRefresh(tokens)
+ }
+
+ return nil
+}
+
+// Chat sends a chat request to the Anthropic API using OAuth bearer token
+func (c *AnthropicOAuthClient) Chat(ctx context.Context, req ChatRequest) (*ChatResponse, error) {
+ // Ensure we have a valid token
+ if err := c.ensureValidToken(ctx); err != nil {
+ return nil, err
+ }
+
+ // Convert messages to Anthropic format (same as regular client)
+ messages := make([]anthropicMessage, 0, len(req.Messages))
+ for _, m := range req.Messages {
+ if m.Role == "system" {
+ continue
+ }
+
+ if m.ToolResult != nil {
+ contentJSON, _ := json.Marshal(m.ToolResult.Content)
+ messages = append(messages, anthropicMessage{
+ Role: "user",
+ Content: []anthropicContent{
+ {
+ Type: "tool_result",
+ ToolUseID: m.ToolResult.ToolUseID,
+ Content: contentJSON,
+ IsError: m.ToolResult.IsError,
+ },
+ },
+ })
+ continue
+ }
+
+ if m.Role == "assistant" && len(m.ToolCalls) > 0 {
+ contentBlocks := make([]anthropicContent, 0)
+ if m.Content != "" {
+ contentBlocks = append(contentBlocks, anthropicContent{
+ Type: "text",
+ Text: m.Content,
+ })
+ }
+ for _, tc := range m.ToolCalls {
+ contentBlocks = append(contentBlocks, anthropicContent{
+ Type: "tool_use",
+ ID: tc.ID,
+ Name: tc.Name,
+ Input: tc.Input,
+ })
+ }
+ messages = append(messages, anthropicMessage{
+ Role: "assistant",
+ Content: contentBlocks,
+ })
+ continue
+ }
+
+ messages = append(messages, anthropicMessage{
+ Role: m.Role,
+ Content: m.Content,
+ })
+ }
+
+ model := req.Model
+ if model == "" {
+ model = c.model
+ }
+
+ maxTokens := req.MaxTokens
+ if maxTokens == 0 {
+ maxTokens = 4096
+ }
+
+ anthropicReq := anthropicRequest{
+ Model: model,
+ Messages: messages,
+ MaxTokens: maxTokens,
+ System: req.System,
+ }
+
+ if req.Temperature > 0 {
+ anthropicReq.Temperature = req.Temperature
+ }
+
+ if len(req.Tools) > 0 {
+ anthropicReq.Tools = make([]anthropicTool, len(req.Tools))
+ for i, t := range req.Tools {
+ if t.Type == "web_search_20250305" {
+ anthropicReq.Tools[i] = anthropicTool{
+ Type: t.Type,
+ Name: t.Name,
+ MaxUses: t.MaxUses,
+ }
+ } else {
+ anthropicReq.Tools[i] = anthropicTool{
+ Name: t.Name,
+ Description: t.Description,
+ InputSchema: t.InputSchema,
+ }
+ }
+ }
+ }
+
+ body, err := json.Marshal(anthropicReq)
+ if err != nil {
+ return nil, fmt.Errorf("failed to marshal request: %w", err)
+ }
+
+ // Use the beta endpoint for subscription-based access
+ apiURL := "https://api.anthropic.com/v1/messages?beta=true"
+
+ var respBody []byte
+ var lastErr error
+
+ for attempt := 0; attempt <= maxRetries; attempt++ {
+ if attempt > 0 {
+ backoff := initialBackoff * time.Duration(1<<(attempt-1))
+ log.Warn().
+ Int("attempt", attempt).
+ Dur("backoff", backoff).
+ Str("last_error", lastErr.Error()).
+ Msg("Retrying Anthropic OAuth API request after transient error")
+
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ case <-time.After(backoff):
+ }
+ }
+
+ httpReq, err := http.NewRequestWithContext(ctx, "POST", apiURL, bytes.NewReader(body))
+ if err != nil {
+ return nil, fmt.Errorf("failed to create request: %w", err)
+ }
+
+ httpReq.Header.Set("Content-Type", "application/json")
+ // OAuth uses Authorization: Bearer instead of x-api-key
+ httpReq.Header.Set("Authorization", "Bearer "+c.accessToken)
+ httpReq.Header.Set("anthropic-version", anthropicAPIVersion)
+ // Required for OAuth authentication - enables subscription-based access
+ httpReq.Header.Set("anthropic-beta", "oauth-2025-04-20")
+ // Claude Code identification headers
+ httpReq.Header.Set("x-app", "cli")
+ httpReq.Header.Set("User-Agent", "claude-code/2.0.60")
+
+ resp, err := c.client.Do(httpReq)
+ if err != nil {
+ lastErr = fmt.Errorf("request failed: %w", err)
+ continue
+ }
+
+ respBody, err = io.ReadAll(resp.Body)
+ resp.Body.Close()
+ if err != nil {
+ lastErr = fmt.Errorf("failed to read response: %w", err)
+ continue
+ }
+
+ // Check for token expiry error (401) - force refresh
+ if resp.StatusCode == 401 && c.refreshToken != "" {
+ log.Info().Msg("Got 401, forcing token refresh...")
+ if err := c.forceRefreshToken(ctx); err == nil {
+ // Token refreshed, retry immediately
+ lastErr = fmt.Errorf("token expired, retried with refreshed token")
+ continue
+ } else {
+ log.Error().Err(err).Msg("Failed to refresh token after 401")
+ }
+ }
+
+ if resp.StatusCode == 429 || resp.StatusCode == 529 || resp.StatusCode >= 500 {
+ var errResp anthropicError
+ errMsg := string(respBody)
+ if err := json.Unmarshal(respBody, &errResp); err == nil && errResp.Error.Message != "" {
+ errMsg = errResp.Error.Message
+ }
+ lastErr = fmt.Errorf("API error (%d): %s", resp.StatusCode, errMsg)
+ continue
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ var errResp anthropicError
+ if err := json.Unmarshal(respBody, &errResp); err == nil && errResp.Error.Message != "" {
+ return nil, fmt.Errorf("API error (%d): %s", resp.StatusCode, errResp.Error.Message)
+ }
+ return nil, fmt.Errorf("API error (%d): %s", resp.StatusCode, string(respBody))
+ }
+
+ lastErr = nil
+ break
+ }
+
+ if lastErr != nil {
+ return nil, fmt.Errorf("failed after %d retries: %w", maxRetries, lastErr)
+ }
+
+ var anthropicResp anthropicResponse
+ if err := json.Unmarshal(respBody, &anthropicResp); err != nil {
+ return nil, fmt.Errorf("failed to parse response: %w", err)
+ }
+
+ var textContent string
+ var toolCalls []ToolCall
+ for _, c := range anthropicResp.Content {
+ switch c.Type {
+ case "text":
+ textContent += c.Text
+ case "tool_use":
+ toolCalls = append(toolCalls, ToolCall{
+ ID: c.ID,
+ Name: c.Name,
+ Input: c.Input,
+ })
+ case "server_tool_use":
+ log.Debug().
+ Str("tool_name", c.Name).
+ Msg("Server tool use detected (handled by Anthropic)")
+ case "web_search_tool_result":
+ log.Debug().Msg("Web search results received")
+ }
+ }
+
+ return &ChatResponse{
+ Content: textContent,
+ Model: anthropicResp.Model,
+ StopReason: anthropicResp.StopReason,
+ ToolCalls: toolCalls,
+ InputTokens: anthropicResp.Usage.InputTokens,
+ OutputTokens: anthropicResp.Usage.OutputTokens,
+ }, nil
+}
+
+// TestConnection validates the OAuth token by making a minimal request
+func (c *AnthropicOAuthClient) TestConnection(ctx context.Context) error {
+ _, err := c.Chat(ctx, ChatRequest{
+ Messages: []Message{
+ {Role: "user", Content: "Hi"},
+ },
+ MaxTokens: 10,
+ })
+ return err
+}
diff --git a/internal/ai/providers/factory.go b/internal/ai/providers/factory.go
index 183a8aaed1..4909da9864 100644
--- a/internal/ai/providers/factory.go
+++ b/internal/ai/providers/factory.go
@@ -18,10 +18,21 @@ func NewFromConfig(cfg *config.AIConfig) (Provider, error) {
switch cfg.Provider {
case config.AIProviderAnthropic:
- if cfg.APIKey == "" {
- return nil, fmt.Errorf("Anthropic API key is required")
+ // If we have an API key (from direct entry or OAuth-created), use regular client
+ if cfg.APIKey != "" {
+ return NewAnthropicClient(cfg.APIKey, cfg.GetModel()), nil
+ }
+ // Pro/Max users without org:create_api_key will use OAuth tokens directly
+ if cfg.IsUsingOAuth() && cfg.OAuthAccessToken != "" {
+ client := NewAnthropicOAuthClient(
+ cfg.OAuthAccessToken,
+ cfg.OAuthRefreshToken,
+ cfg.OAuthExpiresAt,
+ cfg.GetModel(),
+ )
+ return client, nil
}
- return NewAnthropicClient(cfg.APIKey, cfg.GetModel()), nil
+ return nil, fmt.Errorf("Anthropic API key is required (or use OAuth login for Pro/Max subscription)")
case config.AIProviderOpenAI:
if cfg.APIKey == "" {
@@ -43,3 +54,4 @@ func NewFromConfig(cfg *config.AIConfig) (Provider, error) {
return nil, fmt.Errorf("unknown provider: %s", cfg.Provider)
}
}
+
diff --git a/internal/api/ai_handlers.go b/internal/api/ai_handlers.go
index 39aa8dc89d..d78ec8797f 100644
--- a/internal/api/ai_handlers.go
+++ b/internal/api/ai_handlers.go
@@ -3,18 +3,22 @@ package api
import (
"context"
"encoding/json"
+ "fmt"
"io"
"net/http"
"strings"
+ "sync"
"time"
"github.com/rcourtman/pulse-go-rewrite/internal/agentexec"
"github.com/rcourtman/pulse-go-rewrite/internal/ai"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/providers"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
"github.com/rcourtman/pulse-go-rewrite/internal/utils"
"github.com/rs/zerolog/log"
)
+
// AISettingsHandler handles AI settings endpoints
type AISettingsHandler struct {
config *config.Config
@@ -67,6 +71,9 @@ type AISettingsResponse struct {
Configured bool `json:"configured"` // true if AI is ready to use
AutonomousMode bool `json:"autonomous_mode"` // true if AI can execute without approval
CustomContext string `json:"custom_context"` // user-provided infrastructure context
+ // OAuth fields for Claude Pro/Max subscription authentication
+ AuthMethod string `json:"auth_method"` // "api_key" or "oauth"
+ OAuthConnected bool `json:"oauth_connected"` // true if OAuth tokens are configured
}
// AISettingsUpdateRequest is the request body for PUT /api/settings/ai
@@ -78,6 +85,7 @@ type AISettingsUpdateRequest struct {
BaseURL *string `json:"base_url,omitempty"`
AutonomousMode *bool `json:"autonomous_mode,omitempty"`
CustomContext *string `json:"custom_context,omitempty"` // user-provided infrastructure context
+ AuthMethod *string `json:"auth_method,omitempty"` // "api_key" or "oauth"
}
// HandleGetAISettings returns the current AI settings (GET /api/settings/ai)
@@ -98,6 +106,12 @@ func (h *AISettingsHandler) HandleGetAISettings(w http.ResponseWriter, r *http.R
settings = config.NewDefaultAIConfig()
}
+ // Determine auth method string
+ authMethod := string(settings.AuthMethod)
+ if authMethod == "" {
+ authMethod = string(config.AuthMethodAPIKey)
+ }
+
response := AISettingsResponse{
Enabled: settings.Enabled,
Provider: settings.Provider,
@@ -107,6 +121,8 @@ func (h *AISettingsHandler) HandleGetAISettings(w http.ResponseWriter, r *http.R
Configured: settings.IsConfigured(),
AutonomousMode: settings.AutonomousMode,
CustomContext: settings.CustomContext,
+ AuthMethod: authMethod,
+ OAuthConnected: settings.OAuthAccessToken != "",
}
if err := utils.WriteJSONResponse(w, response); err != nil {
@@ -1179,3 +1195,333 @@ func (h *AISettingsHandler) HandleInvestigateAlert(w http.ResponseWriter, r *htt
func (h *AISettingsHandler) SetAlertProvider(ap ai.AlertProvider) {
h.aiService.SetAlertProvider(ap)
}
+
+// oauthSessions stores active OAuth sessions (state -> session)
+// In production, consider using a more robust session store with expiry
+var oauthSessions = make(map[string]*providers.OAuthSession)
+var oauthSessionsMu sync.Mutex
+
+// HandleOAuthStart initiates the OAuth flow for Claude Pro/Max subscription (POST /api/ai/oauth/start)
+// Returns an authorization URL for the user to visit manually
+func (h *AISettingsHandler) HandleOAuthStart(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodGet && r.Method != http.MethodPost {
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+
+ // Generate OAuth session (redirect URI is not used since we use Anthropic's callback)
+ session, err := providers.GenerateOAuthSession("")
+ if err != nil {
+ log.Error().Err(err).Msg("Failed to generate OAuth session")
+ http.Error(w, "Failed to start OAuth flow", http.StatusInternalServerError)
+ return
+ }
+
+ // Store session (with cleanup of old sessions)
+ oauthSessionsMu.Lock()
+ // Clean up sessions older than 15 minutes
+ for state, s := range oauthSessions {
+ if time.Since(s.CreatedAt) > 15*time.Minute {
+ delete(oauthSessions, state)
+ }
+ }
+ oauthSessions[session.State] = session
+ oauthSessionsMu.Unlock()
+
+ // Get authorization URL
+ authURL := providers.GetAuthorizationURL(session)
+
+ log.Info().
+ Str("state", session.State[:8]+"...").
+ Str("verifier_len", fmt.Sprintf("%d", len(session.CodeVerifier))).
+ Str("auth_url", authURL).
+ Msg("Starting Claude OAuth flow - user must visit URL and paste code back")
+
+ // Return the URL for the user to visit
+ response := map[string]string{
+ "auth_url": authURL,
+ "state": session.State,
+ }
+
+ if err := utils.WriteJSONResponse(w, response); err != nil {
+ log.Error().Err(err).Msg("Failed to write OAuth start response")
+ }
+}
+
+// HandleOAuthExchange exchanges a manually-pasted authorization code for tokens (POST /api/ai/oauth/exchange)
+func (h *AISettingsHandler) HandleOAuthExchange(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+
+ // Parse request body
+ var req struct {
+ Code string `json:"code"`
+ State string `json:"state"`
+ }
+ if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
+ http.Error(w, "Invalid request body", http.StatusBadRequest)
+ return
+ }
+
+ if req.Code == "" || req.State == "" {
+ http.Error(w, "Missing code or state", http.StatusBadRequest)
+ return
+ }
+
+ // Trim any whitespace from the code (user might have copied extra spaces)
+ code := strings.TrimSpace(req.Code)
+
+ // Anthropic's callback page displays the code as "code#state"
+ // We need to extract just the code part before the #
+ if idx := strings.Index(code, "#"); idx > 0 {
+ code = code[:idx]
+ }
+
+ log.Debug().
+ Str("code_len", fmt.Sprintf("%d", len(code))).
+ Str("code_prefix", code[:min(20, len(code))]).
+ Str("state_prefix", req.State[:min(8, len(req.State))]).
+ Msg("Processing OAuth code exchange")
+
+ // Look up session
+ oauthSessionsMu.Lock()
+ session, ok := oauthSessions[req.State]
+ if ok {
+ delete(oauthSessions, req.State) // One-time use
+ }
+ oauthSessionsMu.Unlock()
+
+ if !ok {
+ log.Error().Str("state", req.State[:min(8, len(req.State))]+"...").Msg("OAuth exchange with unknown state")
+ http.Error(w, "Invalid or expired session. Please start the OAuth flow again.", http.StatusBadRequest)
+ return
+ }
+
+ // Exchange code for tokens
+ ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
+ defer cancel()
+
+ tokens, err := providers.ExchangeCodeForTokens(ctx, code, session)
+ if err != nil {
+ log.Error().Err(err).Msg("Failed to exchange OAuth code for tokens")
+ http.Error(w, "Failed to exchange authorization code: "+err.Error(), http.StatusBadRequest)
+ return
+ }
+
+ // Try to create an API key from the OAuth access token
+ // Team/Enterprise users get org:create_api_key scope and can create API keys
+ // Pro/Max users don't have this scope and will use OAuth tokens directly
+ apiKey, err := providers.CreateAPIKeyFromOAuth(ctx, tokens.AccessToken)
+ if err != nil {
+ // Check if it's a permission error (Pro/Max users)
+ if strings.Contains(err.Error(), "org:create_api_key") || strings.Contains(err.Error(), "403") {
+ log.Info().Msg("User doesn't have org:create_api_key permission - will use OAuth tokens directly")
+ // This is fine for Pro/Max users - they'll use OAuth tokens
+ } else {
+ log.Error().Err(err).Msg("Failed to create API key from OAuth token")
+ http.Error(w, "Failed to create API key: "+err.Error(), http.StatusBadRequest)
+ return
+ }
+ }
+
+ if apiKey != "" {
+ log.Info().Msg("Successfully created API key from OAuth - using subscription-based billing")
+ }
+
+ // Load existing settings
+ settings, err := h.persistence.LoadAIConfig()
+ if err != nil {
+ log.Error().Err(err).Msg("Failed to load AI settings for OAuth")
+ settings = config.NewDefaultAIConfig()
+ }
+ if settings == nil {
+ settings = config.NewDefaultAIConfig()
+ }
+
+ // Update settings
+ settings.Provider = config.AIProviderAnthropic
+ settings.AuthMethod = config.AuthMethodOAuth
+ settings.OAuthAccessToken = tokens.AccessToken
+ settings.OAuthRefreshToken = tokens.RefreshToken
+ settings.OAuthExpiresAt = tokens.ExpiresAt
+ settings.Enabled = true
+
+ // If we got an API key, use it; otherwise use OAuth tokens directly
+ if apiKey != "" {
+ settings.APIKey = apiKey
+ } else {
+ // Pro/Max users: clear any old API key, will use OAuth client
+ settings.ClearAPIKey()
+ }
+
+ // Save settings
+ if err := h.persistence.SaveAIConfig(*settings); err != nil {
+ log.Error().Err(err).Msg("Failed to save OAuth tokens")
+ http.Error(w, "Failed to save OAuth credentials", http.StatusInternalServerError)
+ return
+ }
+
+ // Reload the AI service with new settings
+ if err := h.aiService.Reload(); err != nil {
+ log.Warn().Err(err).Msg("Failed to reload AI service after OAuth setup")
+ }
+
+ log.Info().Msg("Claude OAuth authentication successful")
+
+ response := map[string]interface{}{
+ "success": true,
+ "message": "Successfully connected to Claude with your subscription",
+ }
+
+ if err := utils.WriteJSONResponse(w, response); err != nil {
+ log.Error().Err(err).Msg("Failed to write OAuth exchange response")
+ }
+}
+
+// HandleOAuthCallback handles the OAuth callback (GET /api/ai/oauth/callback)
+// This is kept for backwards compatibility but mainly serves as a fallback
+func (h *AISettingsHandler) HandleOAuthCallback(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodGet {
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+
+ // Get code and state from query params
+ code := r.URL.Query().Get("code")
+ state := r.URL.Query().Get("state")
+ errParam := r.URL.Query().Get("error")
+ errDesc := r.URL.Query().Get("error_description")
+
+ // Check for OAuth error
+ if errParam != "" {
+ log.Error().
+ Str("error", errParam).
+ Str("description", errDesc).
+ Msg("OAuth authorization failed")
+ // Redirect to settings page with error
+ http.Redirect(w, r, "/settings?ai_oauth_error="+errParam, http.StatusTemporaryRedirect)
+ return
+ }
+
+ if code == "" || state == "" {
+ log.Error().Msg("OAuth callback missing code or state")
+ http.Redirect(w, r, "/settings?ai_oauth_error=missing_params", http.StatusTemporaryRedirect)
+ return
+ }
+
+ // Look up session
+ oauthSessionsMu.Lock()
+ session, ok := oauthSessions[state]
+ if ok {
+ delete(oauthSessions, state) // One-time use
+ }
+ oauthSessionsMu.Unlock()
+
+ if !ok {
+ log.Error().Str("state", state).Msg("OAuth callback with unknown state")
+ http.Redirect(w, r, "/settings?ai_oauth_error=invalid_state", http.StatusTemporaryRedirect)
+ return
+ }
+
+ // Exchange code for tokens
+ ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
+ defer cancel()
+
+ tokens, err := providers.ExchangeCodeForTokens(ctx, code, session)
+ if err != nil {
+ log.Error().Err(err).Msg("Failed to exchange OAuth code for tokens")
+ http.Redirect(w, r, "/settings?ai_oauth_error=token_exchange_failed", http.StatusTemporaryRedirect)
+ return
+ }
+
+ // Load existing settings
+ settings, err := h.persistence.LoadAIConfig()
+ if err != nil {
+ log.Error().Err(err).Msg("Failed to load AI settings for OAuth")
+ settings = config.NewDefaultAIConfig()
+ }
+ if settings == nil {
+ settings = config.NewDefaultAIConfig()
+ }
+
+ // Update settings with OAuth tokens
+ settings.Provider = config.AIProviderAnthropic
+ settings.AuthMethod = config.AuthMethodOAuth
+ settings.OAuthAccessToken = tokens.AccessToken
+ settings.OAuthRefreshToken = tokens.RefreshToken
+ settings.OAuthExpiresAt = tokens.ExpiresAt
+ settings.Enabled = true
+ // Clear API key since we're using OAuth
+ settings.ClearAPIKey()
+
+ // Save settings
+ if err := h.persistence.SaveAIConfig(*settings); err != nil {
+ log.Error().Err(err).Msg("Failed to save OAuth tokens")
+ http.Redirect(w, r, "/settings?ai_oauth_error=save_failed", http.StatusTemporaryRedirect)
+ return
+ }
+
+ // Reload the AI service with new settings
+ if err := h.aiService.Reload(); err != nil {
+ log.Warn().Err(err).Msg("Failed to reload AI service after OAuth setup")
+ }
+
+ log.Info().Msg("Claude OAuth authentication successful")
+
+ // Redirect to settings page with success
+ http.Redirect(w, r, "/settings?ai_oauth_success=true", http.StatusTemporaryRedirect)
+}
+
+// HandleOAuthDisconnect disconnects OAuth and clears tokens (POST /api/ai/oauth/disconnect)
+func (h *AISettingsHandler) HandleOAuthDisconnect(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost {
+ http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+
+ // Require admin authentication
+ if !CheckAuth(h.config, w, r) {
+ return
+ }
+
+ // Load existing settings
+ settings, err := h.persistence.LoadAIConfig()
+ if err != nil {
+ log.Error().Err(err).Msg("Failed to load AI settings for OAuth disconnect")
+ http.Error(w, "Failed to load settings", http.StatusInternalServerError)
+ return
+ }
+ if settings == nil {
+ settings = config.NewDefaultAIConfig()
+ }
+
+ // Clear OAuth tokens
+ settings.ClearOAuthTokens()
+ settings.AuthMethod = config.AuthMethodAPIKey
+
+ // Save settings
+ if err := h.persistence.SaveAIConfig(*settings); err != nil {
+ log.Error().Err(err).Msg("Failed to save settings after OAuth disconnect")
+ http.Error(w, "Failed to save settings", http.StatusInternalServerError)
+ return
+ }
+
+ // Reload the AI service
+ if err := h.aiService.Reload(); err != nil {
+ log.Warn().Err(err).Msg("Failed to reload AI service after OAuth disconnect")
+ }
+
+ log.Info().Msg("Claude OAuth disconnected")
+
+ response := map[string]interface{}{
+ "success": true,
+ "message": "OAuth disconnected successfully",
+ }
+
+ if err := utils.WriteJSONResponse(w, response); err != nil {
+ log.Error().Err(err).Msg("Failed to write OAuth disconnect response")
+ }
+}
+
diff --git a/internal/api/charts_test.go b/internal/api/charts_test.go
new file mode 100644
index 0000000000..465e236163
--- /dev/null
+++ b/internal/api/charts_test.go
@@ -0,0 +1,439 @@
+package api
+
+import (
+ "encoding/json"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+)
+
+// TestChartResponseTypes verifies the ChartResponse struct fields
+func TestChartResponseTypes(t *testing.T) {
+ t.Parallel()
+
+ now := time.Now().Unix() * 1000
+
+ response := ChartResponse{
+ ChartData: map[string]VMChartData{
+ "pve1/qemu/100": {
+ "cpu": []MetricPoint{{Timestamp: now, Value: 45.5}},
+ "memory": []MetricPoint{{Timestamp: now, Value: 60.0}},
+ "disk": []MetricPoint{{Timestamp: now, Value: 30.0}},
+ },
+ },
+ NodeData: map[string]NodeChartData{
+ "pve1": {
+ "cpu": []MetricPoint{{Timestamp: now, Value: 35.0}},
+ "memory": []MetricPoint{{Timestamp: now, Value: 50.0}},
+ },
+ },
+ StorageData: map[string]StorageChartData{
+ "local-zfs": {
+ "disk": []MetricPoint{{Timestamp: now, Value: 25.0}},
+ },
+ },
+ DockerData: map[string]VMChartData{
+ "abc123def456": {
+ "cpu": []MetricPoint{{Timestamp: now, Value: 5.0}},
+ "memory": []MetricPoint{{Timestamp: now, Value: 15.0}},
+ },
+ },
+ DockerHostData: map[string]VMChartData{
+ "docker-host-1": {
+ "cpu": []MetricPoint{{Timestamp: now, Value: 20.0}},
+ "memory": []MetricPoint{{Timestamp: now, Value: 40.0}},
+ },
+ },
+ GuestTypes: map[string]string{
+ "pve1/qemu/100": "vm",
+ "pve1/lxc/200": "container",
+ },
+ Timestamp: now,
+ Stats: ChartStats{
+ OldestDataTimestamp: now - 3600000,
+ },
+ }
+
+ // Verify all fields are properly set
+ if len(response.ChartData) != 1 {
+ t.Errorf("Expected 1 chart data entry, got %d", len(response.ChartData))
+ }
+ if len(response.NodeData) != 1 {
+ t.Errorf("Expected 1 node data entry, got %d", len(response.NodeData))
+ }
+ if len(response.StorageData) != 1 {
+ t.Errorf("Expected 1 storage data entry, got %d", len(response.StorageData))
+ }
+ if len(response.DockerData) != 1 {
+ t.Errorf("Expected 1 docker data entry, got %d", len(response.DockerData))
+ }
+ if len(response.DockerHostData) != 1 {
+ t.Errorf("Expected 1 docker host data entry, got %d", len(response.DockerHostData))
+ }
+ if len(response.GuestTypes) != 2 {
+ t.Errorf("Expected 2 guest type entries, got %d", len(response.GuestTypes))
+ }
+
+ // Verify guest types mapping
+ if response.GuestTypes["pve1/qemu/100"] != "vm" {
+ t.Errorf("Expected guest type 'vm', got '%s'", response.GuestTypes["pve1/qemu/100"])
+ }
+ if response.GuestTypes["pve1/lxc/200"] != "container" {
+ t.Errorf("Expected guest type 'container', got '%s'", response.GuestTypes["pve1/lxc/200"])
+ }
+
+ // Verify docker data metric points
+ cpuPoints := response.DockerData["abc123def456"]["cpu"]
+ if len(cpuPoints) != 1 || cpuPoints[0].Value != 5.0 {
+ t.Errorf("Docker container CPU metric incorrect")
+ }
+}
+
+func TestChartResponseJSONSerialization(t *testing.T) {
+ t.Parallel()
+
+ now := time.Now().Unix() * 1000
+
+ response := ChartResponse{
+ ChartData: map[string]VMChartData{
+ "vm-1": {"cpu": []MetricPoint{{Timestamp: now, Value: 50.0}}},
+ },
+ NodeData: map[string]NodeChartData{},
+ StorageData: map[string]StorageChartData{},
+ DockerData: map[string]VMChartData{
+ "container-abc": {"cpu": []MetricPoint{{Timestamp: now, Value: 10.0}}},
+ },
+ DockerHostData: map[string]VMChartData{
+ "host-1": {"cpu": []MetricPoint{{Timestamp: now, Value: 25.0}}},
+ },
+ GuestTypes: map[string]string{"vm-1": "vm"},
+ Timestamp: now,
+ Stats: ChartStats{OldestDataTimestamp: now - 3600000},
+ }
+
+ // Serialize to JSON
+ data, err := json.Marshal(response)
+ if err != nil {
+ t.Fatalf("Failed to marshal ChartResponse: %v", err)
+ }
+
+ // Deserialize back
+ var decoded ChartResponse
+ if err := json.Unmarshal(data, &decoded); err != nil {
+ t.Fatalf("Failed to unmarshal ChartResponse: %v", err)
+ }
+
+ // Verify dockerData field name in JSON
+ var rawMap map[string]interface{}
+ if err := json.Unmarshal(data, &rawMap); err != nil {
+ t.Fatalf("Failed to unmarshal to raw map: %v", err)
+ }
+
+ // Check that the JSON keys match expected names
+ expectedKeys := []string{"data", "nodeData", "storageData", "dockerData", "dockerHostData", "guestTypes", "timestamp", "stats"}
+ for _, key := range expectedKeys {
+ if _, ok := rawMap[key]; !ok {
+ t.Errorf("Expected JSON key '%s' not found", key)
+ }
+ }
+
+ // Verify dockerData content
+ if dockerData, ok := rawMap["dockerData"].(map[string]interface{}); ok {
+ if _, ok := dockerData["container-abc"]; !ok {
+ t.Errorf("Expected 'container-abc' in dockerData")
+ }
+ } else {
+ t.Errorf("dockerData not found or wrong type")
+ }
+
+ // Verify dockerHostData content
+ if dockerHostData, ok := rawMap["dockerHostData"].(map[string]interface{}); ok {
+ if _, ok := dockerHostData["host-1"]; !ok {
+ t.Errorf("Expected 'host-1' in dockerHostData")
+ }
+ } else {
+ t.Errorf("dockerHostData not found or wrong type")
+ }
+}
+
+func TestMetricPointStructure(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ point MetricPoint
+ wantJSON string
+ }{
+ {
+ name: "positive values",
+ point: MetricPoint{Timestamp: 1733700000000, Value: 45.5},
+ wantJSON: `{"timestamp":1733700000000,"value":45.5}`,
+ },
+ {
+ name: "zero values",
+ point: MetricPoint{Timestamp: 0, Value: 0},
+ wantJSON: `{"timestamp":0,"value":0}`,
+ },
+ {
+ name: "high precision value",
+ point: MetricPoint{Timestamp: 1733700000000, Value: 99.99999},
+ wantJSON: `{"timestamp":1733700000000,"value":99.99999}`,
+ },
+ }
+
+ for _, tt := range tests {
+ tt := tt
+ t.Run(tt.name, func(t *testing.T) {
+ data, err := json.Marshal(tt.point)
+ if err != nil {
+ t.Fatalf("Failed to marshal MetricPoint: %v", err)
+ }
+ if string(data) != tt.wantJSON {
+ t.Errorf("JSON mismatch\ngot: %s\nwant: %s", string(data), tt.wantJSON)
+ }
+ })
+ }
+}
+
+func TestTimeRangeConversion(t *testing.T) {
+ t.Parallel()
+
+ // Test the time range conversion logic used in handleCharts
+ tests := []struct {
+ rangeStr string
+ expectedDur time.Duration
+ }{
+ {"5m", 5 * time.Minute},
+ {"15m", 15 * time.Minute},
+ {"30m", 30 * time.Minute},
+ {"1h", time.Hour},
+ {"4h", 4 * time.Hour},
+ {"12h", 12 * time.Hour},
+ {"24h", 24 * time.Hour},
+ {"7d", 7 * 24 * time.Hour},
+ {"", time.Hour}, // default
+ {"invalid", time.Hour}, // fallback
+ }
+
+ for _, tt := range tests {
+ tt := tt
+ t.Run("range_"+tt.rangeStr, func(t *testing.T) {
+ var duration time.Duration
+ switch tt.rangeStr {
+ case "5m":
+ duration = 5 * time.Minute
+ case "15m":
+ duration = 15 * time.Minute
+ case "30m":
+ duration = 30 * time.Minute
+ case "1h":
+ duration = time.Hour
+ case "4h":
+ duration = 4 * time.Hour
+ case "12h":
+ duration = 12 * time.Hour
+ case "24h":
+ duration = 24 * time.Hour
+ case "7d":
+ duration = 7 * 24 * time.Hour
+ default:
+ duration = time.Hour
+ }
+
+ if duration != tt.expectedDur {
+ t.Errorf("Time range %q: got %v, want %v", tt.rangeStr, duration, tt.expectedDur)
+ }
+ })
+ }
+}
+
+func TestDockerMetricKeyFormat(t *testing.T) {
+ t.Parallel()
+
+ // Test that Docker container metric keys follow the expected format
+ // This mirrors the logic in handleCharts: fmt.Sprintf("docker:%s", container.ID)
+ tests := []struct {
+ containerID string
+ expectedKey string
+ }{
+ {"abc123", "docker:abc123"},
+ {"abc123def456789", "docker:abc123def456789"},
+ {"sha256:abc123", "docker:sha256:abc123"},
+ }
+
+ for _, tt := range tests {
+ tt := tt
+ t.Run(tt.containerID, func(t *testing.T) {
+ key := "docker:" + tt.containerID
+ if key != tt.expectedKey {
+ t.Errorf("Docker metric key: got %q, want %q", key, tt.expectedKey)
+ }
+ })
+ }
+}
+
+func TestDockerHostMetricKeyFormat(t *testing.T) {
+ t.Parallel()
+
+ // Test that Docker host metric keys follow the expected format
+ // This mirrors the logic in handleCharts: fmt.Sprintf("dockerHost:%s", host.ID)
+ tests := []struct {
+ hostID string
+ expectedKey string
+ }{
+ {"docker-host-1", "dockerHost:docker-host-1"},
+ {"my-server", "dockerHost:my-server"},
+ {"192.168.1.10", "dockerHost:192.168.1.10"},
+ }
+
+ for _, tt := range tests {
+ tt := tt
+ t.Run(tt.hostID, func(t *testing.T) {
+ key := "dockerHost:" + tt.hostID
+ if key != tt.expectedKey {
+ t.Errorf("Docker host metric key: got %q, want %q", key, tt.expectedKey)
+ }
+ })
+ }
+}
+
+func TestGuestTypesMapping(t *testing.T) {
+ t.Parallel()
+
+ // Simulate the guest types map building from state
+ vms := []models.VM{
+ {ID: "pve1/qemu/100"},
+ {ID: "pve1/qemu/200"},
+ }
+ containers := []models.Container{
+ {ID: "pve1/lxc/300"},
+ {ID: "pve1/lxc/400"},
+ }
+
+ guestTypes := make(map[string]string)
+ for _, vm := range vms {
+ guestTypes[vm.ID] = "vm"
+ }
+ for _, ct := range containers {
+ guestTypes[ct.ID] = "container"
+ }
+
+ if len(guestTypes) != 4 {
+ t.Errorf("Expected 4 guest types, got %d", len(guestTypes))
+ }
+
+ // Verify VM types
+ for _, vm := range vms {
+ if guestTypes[vm.ID] != "vm" {
+ t.Errorf("Expected 'vm' for %s, got '%s'", vm.ID, guestTypes[vm.ID])
+ }
+ }
+
+ // Verify container types
+ for _, ct := range containers {
+ if guestTypes[ct.ID] != "container" {
+ t.Errorf("Expected 'container' for %s, got '%s'", ct.ID, guestTypes[ct.ID])
+ }
+ }
+}
+
+func TestDockerContainerDiskPercentCalculation(t *testing.T) {
+ t.Parallel()
+
+ // Test disk percentage calculation for Docker containers
+ // Mirrors: float64(container.WritableLayerBytes) / float64(container.RootFilesystemBytes) * 100
+ tests := []struct {
+ name string
+ writableLayerBytes uint64
+ rootFilesystemBytes uint64
+ expectedDiskPercent float64
+ }{
+ {"50% usage", 500, 1000, 50.0},
+ {"100% usage", 1000, 1000, 100.0},
+ {"0% usage", 0, 1000, 0.0},
+ {"zero root filesystem", 100, 0, 0.0}, // Should avoid division by zero
+ {"both zero", 0, 0, 0.0},
+ {"small values", 10, 100, 10.0},
+ }
+
+ for _, tt := range tests {
+ tt := tt
+ t.Run(tt.name, func(t *testing.T) {
+ var diskPercent float64
+ if tt.rootFilesystemBytes > 0 && tt.writableLayerBytes > 0 {
+ diskPercent = float64(tt.writableLayerBytes) / float64(tt.rootFilesystemBytes) * 100
+ if diskPercent > 100 {
+ diskPercent = 100
+ }
+ }
+
+ if diskPercent != tt.expectedDiskPercent {
+ t.Errorf("Disk percent: got %f, want %f", diskPercent, tt.expectedDiskPercent)
+ }
+ })
+ }
+}
+
+func TestChartStatsOldestTimestamp(t *testing.T) {
+ t.Parallel()
+
+ now := time.Now().Unix() * 1000
+ oneHourAgo := now - 3600000 // 1 hour in ms
+ fourHoursAgo := now - 14400000 // 4 hours in ms
+
+ // Simulate finding oldest timestamp
+ timestamps := []int64{now, oneHourAgo, fourHoursAgo, now - 1800000}
+
+ oldestTimestamp := now
+ for _, ts := range timestamps {
+ if ts < oldestTimestamp {
+ oldestTimestamp = ts
+ }
+ }
+
+ if oldestTimestamp != fourHoursAgo {
+ t.Errorf("Oldest timestamp: got %d, want %d", oldestTimestamp, fourHoursAgo)
+ }
+
+ stats := ChartStats{OldestDataTimestamp: oldestTimestamp}
+ if stats.OldestDataTimestamp != fourHoursAgo {
+ t.Errorf("ChartStats.OldestDataTimestamp: got %d, want %d",
+ stats.OldestDataTimestamp, fourHoursAgo)
+ }
+}
+
+func TestEmptyDockerData(t *testing.T) {
+ t.Parallel()
+
+ // Verify that empty DockerData is properly handled in JSON
+ response := ChartResponse{
+ ChartData: map[string]VMChartData{},
+ NodeData: map[string]NodeChartData{},
+ StorageData: map[string]StorageChartData{},
+ DockerData: map[string]VMChartData{},
+ DockerHostData: map[string]VMChartData{},
+ GuestTypes: map[string]string{},
+ Timestamp: time.Now().Unix() * 1000,
+ Stats: ChartStats{OldestDataTimestamp: time.Now().Unix() * 1000},
+ }
+
+ data, err := json.Marshal(response)
+ if err != nil {
+ t.Fatalf("Failed to marshal empty ChartResponse: %v", err)
+ }
+
+ var rawMap map[string]interface{}
+ if err := json.Unmarshal(data, &rawMap); err != nil {
+ t.Fatalf("Failed to unmarshal: %v", err)
+ }
+
+ // Check dockerData is present but empty
+ if dockerData, ok := rawMap["dockerData"].(map[string]interface{}); ok {
+ if len(dockerData) != 0 {
+ t.Errorf("Expected empty dockerData, got %d entries", len(dockerData))
+ }
+ } else {
+ t.Errorf("dockerData should be an empty object, not missing")
+ }
+}
diff --git a/internal/api/router.go b/internal/api/router.go
index c96bfacad6..89674b8419 100644
--- a/internal/api/router.go
+++ b/internal/api/router.go
@@ -1055,6 +1055,12 @@ func (r *Router) setupRoutes() {
r.mux.HandleFunc("/api/ai/knowledge/clear", RequireAuth(r.config, r.aiSettingsHandler.HandleClearGuestKnowledge))
r.mux.HandleFunc("/api/ai/debug/context", RequireAdmin(r.config, r.aiSettingsHandler.HandleDebugContext))
r.mux.HandleFunc("/api/ai/agents", RequireAuth(r.config, r.aiSettingsHandler.HandleGetConnectedAgents))
+ // OAuth endpoints for Claude Pro/Max subscription authentication
+ r.mux.HandleFunc("/api/ai/oauth/start", RequireAdmin(r.config, r.aiSettingsHandler.HandleOAuthStart))
+ r.mux.HandleFunc("/api/ai/oauth/exchange", RequireAdmin(r.config, r.aiSettingsHandler.HandleOAuthExchange)) // Manual code input
+ r.mux.HandleFunc("/api/ai/oauth/callback", r.aiSettingsHandler.HandleOAuthCallback) // Public - receives redirect from Anthropic
+ r.mux.HandleFunc("/api/ai/oauth/disconnect", RequireAdmin(r.config, r.aiSettingsHandler.HandleOAuthDisconnect))
+
// Agent WebSocket for AI command execution
r.mux.HandleFunc("/api/agent/ws", r.handleAgentWebSocket)
@@ -1448,6 +1454,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
"/api/system/proxy-public-key", // Temperature proxy public key for setup script
"/api/temperature-proxy/register", // Temperature proxy registration (called by installer)
"/api/temperature-proxy/authorized-nodes", // Proxy control-plane sync
+ "/api/ai/oauth/callback", // OAuth callback from Anthropic for Claude subscription auth
}
// Also allow static assets without auth (JS, CSS, etc)
@@ -2895,12 +2902,117 @@ func (r *Router) handleCharts(w http.ResponseWriter, req *http.Request) {
guestTypes[ct.ID] = "container"
}
+ // Process Docker containers - get historical data
+ dockerData := make(map[string]VMChartData)
+ for _, host := range state.DockerHosts {
+ for _, container := range host.Containers {
+ if container.ID == "" {
+ continue
+ }
+
+ if dockerData[container.ID] == nil {
+ dockerData[container.ID] = make(VMChartData)
+ }
+
+ // Get historical metrics using the docker: prefix key
+ metricKey := fmt.Sprintf("docker:%s", container.ID)
+ metrics := r.monitor.GetGuestMetrics(metricKey, duration)
+
+ // Convert metric points to API format
+ for metricType, points := range metrics {
+ dockerData[container.ID][metricType] = make([]MetricPoint, len(points))
+ for i, point := range points {
+ ts := point.Timestamp.Unix() * 1000
+ if ts < oldestTimestamp {
+ oldestTimestamp = ts
+ }
+ dockerData[container.ID][metricType][i] = MetricPoint{
+ Timestamp: ts,
+ Value: point.Value,
+ }
+ }
+ }
+
+ // If no historical data, add current value
+ if len(dockerData[container.ID]["cpu"]) == 0 {
+ dockerData[container.ID]["cpu"] = []MetricPoint{
+ {Timestamp: currentTime, Value: container.CPUPercent},
+ }
+ dockerData[container.ID]["memory"] = []MetricPoint{
+ {Timestamp: currentTime, Value: container.MemoryPercent},
+ }
+ // Calculate disk percentage for Docker containers
+ var diskPercent float64
+ if container.RootFilesystemBytes > 0 && container.WritableLayerBytes > 0 {
+ diskPercent = float64(container.WritableLayerBytes) / float64(container.RootFilesystemBytes) * 100
+ if diskPercent > 100 {
+ diskPercent = 100
+ }
+ }
+ dockerData[container.ID]["disk"] = []MetricPoint{
+ {Timestamp: currentTime, Value: diskPercent},
+ }
+ }
+ }
+ }
+
+ // Process Docker hosts - get historical data
+ dockerHostData := make(map[string]VMChartData)
+ for _, host := range state.DockerHosts {
+ if host.ID == "" {
+ continue
+ }
+
+ if dockerHostData[host.ID] == nil {
+ dockerHostData[host.ID] = make(VMChartData)
+ }
+
+ // Get historical metrics using the dockerHost: prefix key
+ metricKey := fmt.Sprintf("dockerHost:%s", host.ID)
+ metrics := r.monitor.GetGuestMetrics(metricKey, duration)
+
+ // Convert metric points to API format
+ for metricType, points := range metrics {
+ dockerHostData[host.ID][metricType] = make([]MetricPoint, len(points))
+ for i, point := range points {
+ ts := point.Timestamp.Unix() * 1000
+ if ts < oldestTimestamp {
+ oldestTimestamp = ts
+ }
+ dockerHostData[host.ID][metricType][i] = MetricPoint{
+ Timestamp: ts,
+ Value: point.Value,
+ }
+ }
+ }
+
+ // If no historical data, add current value
+ if len(dockerHostData[host.ID]["cpu"]) == 0 {
+ dockerHostData[host.ID]["cpu"] = []MetricPoint{
+ {Timestamp: currentTime, Value: host.CPUUsage},
+ }
+ dockerHostData[host.ID]["memory"] = []MetricPoint{
+ {Timestamp: currentTime, Value: host.Memory.Usage},
+ }
+ // Use first disk for host disk percentage
+ var diskPercent float64
+ if len(host.Disks) > 0 {
+ diskPercent = host.Disks[0].Usage
+ }
+ dockerHostData[host.ID]["disk"] = []MetricPoint{
+ {Timestamp: currentTime, Value: diskPercent},
+ }
+ }
+ }
+
response := ChartResponse{
- ChartData: chartData,
- NodeData: nodeData,
- StorageData: storageData,
- GuestTypes: guestTypes,
- Timestamp: currentTime,
+ ChartData: chartData,
+ NodeData: nodeData,
+ StorageData: storageData,
+ DockerData: dockerData,
+ DockerHostData: dockerHostData,
+ GuestTypes: guestTypes,
+ Timestamp: currentTime,
Stats: ChartStats{
OldestDataTimestamp: oldestTimestamp,
},
@@ -2917,6 +3029,7 @@ func (r *Router) handleCharts(w http.ResponseWriter, req *http.Request) {
Int("guests", len(chartData)).
Int("nodes", len(nodeData)).
Int("storage", len(storageData)).
+ Int("dockerContainers", len(dockerData)).
Str("range", timeRange).
Msg("Chart data response sent")
}
diff --git a/internal/api/types.go b/internal/api/types.go
index 3c6cfd4956..8c216489aa 100644
--- a/internal/api/types.go
+++ b/internal/api/types.go
@@ -62,12 +62,14 @@ type StateResponse struct {
// ChartResponse represents chart data
type ChartResponse struct {
- ChartData map[string]VMChartData `json:"data"`
- NodeData map[string]NodeChartData `json:"nodeData"`
- StorageData map[string]StorageChartData `json:"storageData"`
- GuestTypes map[string]string `json:"guestTypes"` // Maps guest ID to type ("vm" or "container")
- Timestamp int64 `json:"timestamp"`
- Stats ChartStats `json:"stats"`
+ ChartData map[string]VMChartData `json:"data"`
+ NodeData map[string]NodeChartData `json:"nodeData"`
+ StorageData map[string]StorageChartData `json:"storageData"`
+ DockerData map[string]VMChartData `json:"dockerData"` // Docker container metrics (keyed by container ID)
+ DockerHostData map[string]VMChartData `json:"dockerHostData"` // Docker host metrics (keyed by host ID)
+ GuestTypes map[string]string `json:"guestTypes"` // Maps guest ID to type ("vm" or "container")
+ Timestamp int64 `json:"timestamp"`
+ Stats ChartStats `json:"stats"`
}
// ChartStats represents chart statistics
diff --git a/internal/ceph/collector.go b/internal/ceph/collector.go
new file mode 100644
index 0000000000..d6398b7e7d
--- /dev/null
+++ b/internal/ceph/collector.go
@@ -0,0 +1,353 @@
+// Package ceph provides functionality for collecting Ceph cluster status
+// directly from the local system using the ceph CLI.
+package ceph
+
+import (
+ "bytes"
+ "context"
+ "encoding/json"
+ "fmt"
+ "os/exec"
+ "strings"
+ "time"
+)
+
+// ClusterStatus represents the complete Ceph cluster status as collected by the agent.
+type ClusterStatus struct {
+ FSID string `json:"fsid"`
+ Health HealthStatus `json:"health"`
+ MonMap MonitorMap `json:"monMap,omitempty"`
+ MgrMap ManagerMap `json:"mgrMap,omitempty"`
+ OSDMap OSDMap `json:"osdMap"`
+ PGMap PGMap `json:"pgMap"`
+ Pools []Pool `json:"pools,omitempty"`
+ Services []ServiceInfo `json:"services,omitempty"`
+ CollectedAt time.Time `json:"collectedAt"`
+}
+
+// HealthStatus represents Ceph cluster health.
+type HealthStatus struct {
+ Status string `json:"status"` // HEALTH_OK, HEALTH_WARN, HEALTH_ERR
+ Checks map[string]Check `json:"checks,omitempty"`
+ Summary []HealthSummary `json:"summary,omitempty"`
+}
+
+// Check represents a health check detail.
+type Check struct {
+ Severity string `json:"severity"`
+ Message string `json:"message,omitempty"`
+ Detail []string `json:"detail,omitempty"`
+}
+
+// HealthSummary represents a health summary message.
+type HealthSummary struct {
+ Severity string `json:"severity"`
+ Message string `json:"message"`
+}
+
+// MonitorMap represents Ceph monitor information.
+type MonitorMap struct {
+ Epoch int `json:"epoch"`
+ NumMons int `json:"numMons"`
+ Monitors []Monitor `json:"monitors,omitempty"`
+}
+
+// Monitor represents a single Ceph monitor.
+type Monitor struct {
+ Name string `json:"name"`
+ Rank int `json:"rank"`
+ Addr string `json:"addr,omitempty"`
+ Status string `json:"status,omitempty"`
+}
+
+// ManagerMap represents Ceph manager information.
+type ManagerMap struct {
+ Available bool `json:"available"`
+ NumMgrs int `json:"numMgrs"`
+ ActiveMgr string `json:"activeMgr,omitempty"`
+ Standbys int `json:"standbys"`
+}
+
+// OSDMap represents OSD status summary.
+type OSDMap struct {
+ Epoch int `json:"epoch"`
+ NumOSDs int `json:"numOsds"`
+ NumUp int `json:"numUp"`
+ NumIn int `json:"numIn"`
+ NumDown int `json:"numDown,omitempty"`
+ NumOut int `json:"numOut,omitempty"`
+}
+
+// PGMap represents placement group statistics.
+type PGMap struct {
+ NumPGs int `json:"numPgs"`
+ BytesTotal uint64 `json:"bytesTotal"`
+ BytesUsed uint64 `json:"bytesUsed"`
+ BytesAvailable uint64 `json:"bytesAvailable"`
+ DataBytes uint64 `json:"dataBytes,omitempty"`
+ UsagePercent float64 `json:"usagePercent"`
+ DegradedRatio float64 `json:"degradedRatio,omitempty"`
+ MisplacedRatio float64 `json:"misplacedRatio,omitempty"`
+ ReadBytesPerSec uint64 `json:"readBytesPerSec,omitempty"`
+ WriteBytesPerSec uint64 `json:"writeBytesPerSec,omitempty"`
+ ReadOpsPerSec uint64 `json:"readOpsPerSec,omitempty"`
+ WriteOpsPerSec uint64 `json:"writeOpsPerSec,omitempty"`
+}
+
+// Pool represents a Ceph pool.
+type Pool struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ BytesUsed uint64 `json:"bytesUsed"`
+ BytesAvailable uint64 `json:"bytesAvailable"`
+ Objects uint64 `json:"objects"`
+ PercentUsed float64 `json:"percentUsed"`
+}
+
+// ServiceInfo represents a Ceph service summary.
+type ServiceInfo struct {
+ Type string `json:"type"` // mon, mgr, osd, mds, rgw
+ Running int `json:"running"`
+ Total int `json:"total"`
+ Daemons []string `json:"daemons,omitempty"`
+}
+
+// IsAvailable checks if the ceph CLI is available on the system.
+func IsAvailable(ctx context.Context) bool {
+ cmd := exec.CommandContext(ctx, "which", "ceph")
+ return cmd.Run() == nil
+}
+
+// Collect gathers Ceph cluster status using the ceph CLI.
+// Returns nil if Ceph is not available or not configured on this host.
+func Collect(ctx context.Context) (*ClusterStatus, error) {
+ // Check if ceph CLI is available
+ if !IsAvailable(ctx) {
+ return nil, nil
+ }
+
+ // Try to get ceph status - this will fail if not a ceph node
+ statusJSON, err := runCephCommand(ctx, "status", "--format", "json")
+ if err != nil {
+ // Not an error - just means this isn't a Ceph node
+ return nil, nil
+ }
+
+ status, err := parseStatus(statusJSON)
+ if err != nil {
+ return nil, fmt.Errorf("parse ceph status: %w", err)
+ }
+
+ // Get pool usage from ceph df
+ dfJSON, err := runCephCommand(ctx, "df", "--format", "json")
+ if err == nil {
+ pools, usagePercent, err := parseDF(dfJSON)
+ if err == nil {
+ status.Pools = pools
+ if status.PGMap.UsagePercent == 0 && usagePercent > 0 {
+ status.PGMap.UsagePercent = usagePercent
+ }
+ }
+ }
+
+ status.CollectedAt = time.Now().UTC()
+ return status, nil
+}
+
+// runCephCommand executes a ceph command and returns the output.
+func runCephCommand(ctx context.Context, args ...string) ([]byte, error) {
+ // Use a reasonable timeout for each command
+ cmdCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
+ defer cancel()
+
+ cmd := exec.CommandContext(cmdCtx, "ceph", args...)
+ var stdout, stderr bytes.Buffer
+ cmd.Stdout = &stdout
+ cmd.Stderr = &stderr
+
+ if err := cmd.Run(); err != nil {
+ return nil, fmt.Errorf("ceph %s failed: %w (stderr: %s)",
+ strings.Join(args, " "), err, stderr.String())
+ }
+
+ return stdout.Bytes(), nil
+}
+
+// parseStatus parses the output of `ceph status --format json`.
+func parseStatus(data []byte) (*ClusterStatus, error) {
+ var raw struct {
+ FSID string `json:"fsid"`
+ Health struct {
+ Status string `json:"status"`
+ Checks map[string]struct {
+ Severity string `json:"severity"`
+ Summary struct {
+ Message string `json:"message"`
+ } `json:"summary"`
+ Detail []struct {
+ Message string `json:"message"`
+ } `json:"detail"`
+ } `json:"checks"`
+ } `json:"health"`
+ MonMap struct {
+ Epoch int `json:"epoch"`
+ Mons []struct {
+ Name string `json:"name"`
+ Rank int `json:"rank"`
+ Addr string `json:"addr"`
+ } `json:"mons"`
+ } `json:"monmap"`
+ MgrMap struct {
+ Available bool `json:"available"`
+ NumActive int `json:"num_active_name,omitempty"`
+ ActiveName string `json:"active_name"`
+ Standbys []struct {
+ Name string `json:"name"`
+ } `json:"standbys"`
+ } `json:"mgrmap"`
+ OSDMap struct {
+ Epoch int `json:"epoch"`
+ NumOSD int `json:"num_osds"`
+ NumUp int `json:"num_up_osds"`
+ NumIn int `json:"num_in_osds"`
+ } `json:"osdmap"`
+ PGMap struct {
+ NumPGs int `json:"num_pgs"`
+ BytesTotal uint64 `json:"bytes_total"`
+ BytesUsed uint64 `json:"bytes_used"`
+ BytesAvail uint64 `json:"bytes_avail"`
+ DataBytes uint64 `json:"data_bytes"`
+ DegradedRatio float64 `json:"degraded_ratio"`
+ MisplacedRatio float64 `json:"misplaced_ratio"`
+ ReadBytesPerSec uint64 `json:"read_bytes_sec"`
+ WriteBytesPerSec uint64 `json:"write_bytes_sec"`
+ ReadOpsPerSec uint64 `json:"read_op_per_sec"`
+ WriteOpsPerSec uint64 `json:"write_op_per_sec"`
+ } `json:"pgmap"`
+ }
+
+ if err := json.Unmarshal(data, &raw); err != nil {
+ return nil, fmt.Errorf("unmarshal: %w", err)
+ }
+
+ status := &ClusterStatus{
+ FSID: raw.FSID,
+ Health: HealthStatus{
+ Status: raw.Health.Status,
+ Checks: make(map[string]Check),
+ },
+ MonMap: MonitorMap{
+ Epoch: raw.MonMap.Epoch,
+ NumMons: len(raw.MonMap.Mons),
+ },
+ MgrMap: ManagerMap{
+ Available: raw.MgrMap.Available,
+ NumMgrs: 1 + len(raw.MgrMap.Standbys),
+ ActiveMgr: raw.MgrMap.ActiveName,
+ Standbys: len(raw.MgrMap.Standbys),
+ },
+ OSDMap: OSDMap{
+ Epoch: raw.OSDMap.Epoch,
+ NumOSDs: raw.OSDMap.NumOSD,
+ NumUp: raw.OSDMap.NumUp,
+ NumIn: raw.OSDMap.NumIn,
+ NumDown: raw.OSDMap.NumOSD - raw.OSDMap.NumUp,
+ NumOut: raw.OSDMap.NumOSD - raw.OSDMap.NumIn,
+ },
+ PGMap: PGMap{
+ NumPGs: raw.PGMap.NumPGs,
+ BytesTotal: raw.PGMap.BytesTotal,
+ BytesUsed: raw.PGMap.BytesUsed,
+ BytesAvailable: raw.PGMap.BytesAvail,
+ DataBytes: raw.PGMap.DataBytes,
+ DegradedRatio: raw.PGMap.DegradedRatio,
+ MisplacedRatio: raw.PGMap.MisplacedRatio,
+ ReadBytesPerSec: raw.PGMap.ReadBytesPerSec,
+ WriteBytesPerSec: raw.PGMap.WriteBytesPerSec,
+ ReadOpsPerSec: raw.PGMap.ReadOpsPerSec,
+ WriteOpsPerSec: raw.PGMap.WriteOpsPerSec,
+ },
+ }
+
+ // Calculate usage percent
+ if raw.PGMap.BytesTotal > 0 {
+ status.PGMap.UsagePercent = float64(raw.PGMap.BytesUsed) / float64(raw.PGMap.BytesTotal) * 100
+ }
+
+ // Parse monitors
+ for _, mon := range raw.MonMap.Mons {
+ status.MonMap.Monitors = append(status.MonMap.Monitors, Monitor{
+ Name: mon.Name,
+ Rank: mon.Rank,
+ Addr: mon.Addr,
+ })
+ }
+
+ // Parse health checks
+ for name, check := range raw.Health.Checks {
+ details := make([]string, 0, len(check.Detail))
+ for _, d := range check.Detail {
+ details = append(details, d.Message)
+ }
+ status.Health.Checks[name] = Check{
+ Severity: check.Severity,
+ Message: check.Summary.Message,
+ Detail: details,
+ }
+ }
+
+ // Build service summary
+ status.Services = []ServiceInfo{
+ {Type: "mon", Running: len(raw.MonMap.Mons), Total: len(raw.MonMap.Mons)},
+ {Type: "mgr", Running: boolToInt(raw.MgrMap.Available), Total: status.MgrMap.NumMgrs},
+ {Type: "osd", Running: raw.OSDMap.NumUp, Total: raw.OSDMap.NumOSD},
+ }
+
+ return status, nil
+}
+
+// parseDF parses the output of `ceph df --format json`.
+func parseDF(data []byte) ([]Pool, float64, error) {
+ var raw struct {
+ Stats struct {
+ TotalBytes uint64 `json:"total_bytes"`
+ TotalUsedBytes uint64 `json:"total_used_bytes"`
+ PercentUsed float64 `json:"percent_used"`
+ } `json:"stats"`
+ Pools []struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ Stats struct {
+ BytesUsed uint64 `json:"bytes_used"`
+ MaxAvail uint64 `json:"max_avail"`
+ Objects uint64 `json:"objects"`
+ PercentUsed float64 `json:"percent_used"`
+ } `json:"stats"`
+ } `json:"pools"`
+ }
+
+ if err := json.Unmarshal(data, &raw); err != nil {
+ return nil, 0, fmt.Errorf("unmarshal df: %w", err)
+ }
+
+ pools := make([]Pool, 0, len(raw.Pools))
+ for _, p := range raw.Pools {
+ pools = append(pools, Pool{
+ ID: p.ID,
+ Name: p.Name,
+ BytesUsed: p.Stats.BytesUsed,
+ BytesAvailable: p.Stats.MaxAvail,
+ Objects: p.Stats.Objects,
+ PercentUsed: p.Stats.PercentUsed * 100, // Convert from 0-1 to 0-100
+ })
+ }
+
+ return pools, raw.Stats.PercentUsed * 100, nil
+}
+
+func boolToInt(b bool) int {
+ if b {
+ return 1
+ }
+ return 0
+}
diff --git a/internal/config/ai.go b/internal/config/ai.go
index 502ebe1feb..40bdf45664 100644
--- a/internal/config/ai.go
+++ b/internal/config/ai.go
@@ -1,15 +1,33 @@
package config
+import "time"
+
+// AuthMethod represents how Anthropic authentication is performed
+type AuthMethod string
+
+const (
+ // AuthMethodAPIKey uses a traditional API key (pay-per-use billing)
+ AuthMethodAPIKey AuthMethod = "api_key"
+ // AuthMethodOAuth uses OAuth tokens (subscription-based, Pro/Max plans)
+ AuthMethodOAuth AuthMethod = "oauth"
+)
+
// AIConfig holds AI feature configuration
// This is stored in ai.enc (encrypted) in the config directory
type AIConfig struct {
Enabled bool `json:"enabled"`
Provider string `json:"provider"` // "anthropic", "openai", "ollama", "deepseek"
- APIKey string `json:"api_key"` // encrypted at rest (not needed for ollama)
+ APIKey string `json:"api_key"` // encrypted at rest (not needed for ollama or oauth)
Model string `json:"model"` // e.g., "claude-opus-4-5-20250514", "gpt-4o", "llama3"
BaseURL string `json:"base_url"` // custom endpoint (required for ollama, optional for openai)
AutonomousMode bool `json:"autonomous_mode"` // when true, AI executes commands without approval
CustomContext string `json:"custom_context"` // user-provided context about their infrastructure
+
+ // OAuth fields for Claude Pro/Max subscription authentication
+ AuthMethod AuthMethod `json:"auth_method,omitempty"` // "api_key" or "oauth" (for anthropic only)
+ OAuthAccessToken string `json:"oauth_access_token,omitempty"` // OAuth access token (encrypted at rest)
+ OAuthRefreshToken string `json:"oauth_refresh_token,omitempty"` // OAuth refresh token (encrypted at rest)
+ OAuthExpiresAt time.Time `json:"oauth_expires_at,omitempty"` // Token expiration time
}
// AIProvider constants
@@ -33,9 +51,10 @@ const (
// NewDefaultAIConfig returns an AIConfig with sensible defaults
func NewDefaultAIConfig() *AIConfig {
return &AIConfig{
- Enabled: false,
- Provider: AIProviderAnthropic,
- Model: DefaultAIModelAnthropic,
+ Enabled: false,
+ Provider: AIProviderAnthropic,
+ Model: DefaultAIModelAnthropic,
+ AuthMethod: AuthMethodAPIKey,
}
}
@@ -46,7 +65,13 @@ func (c *AIConfig) IsConfigured() bool {
}
switch c.Provider {
- case AIProviderAnthropic, AIProviderOpenAI, AIProviderDeepSeek:
+ case AIProviderAnthropic:
+ // Anthropic can use API key OR OAuth
+ if c.AuthMethod == AuthMethodOAuth {
+ return c.OAuthAccessToken != ""
+ }
+ return c.APIKey != ""
+ case AIProviderOpenAI, AIProviderDeepSeek:
return c.APIKey != ""
case AIProviderOllama:
// Ollama doesn't need an API key
@@ -56,6 +81,11 @@ func (c *AIConfig) IsConfigured() bool {
}
}
+// IsUsingOAuth returns true if OAuth authentication is configured for Anthropic
+func (c *AIConfig) IsUsingOAuth() bool {
+ return c.Provider == AIProviderAnthropic && c.AuthMethod == AuthMethodOAuth && c.OAuthAccessToken != ""
+}
+
// GetBaseURL returns the base URL, using defaults where appropriate
func (c *AIConfig) GetBaseURL() string {
if c.BaseURL != "" {
@@ -88,3 +118,15 @@ func (c *AIConfig) GetModel() string {
return ""
}
}
+
+// ClearOAuthTokens clears OAuth tokens (used when switching back to API key auth)
+func (c *AIConfig) ClearOAuthTokens() {
+ c.OAuthAccessToken = ""
+ c.OAuthRefreshToken = ""
+ c.OAuthExpiresAt = time.Time{}
+}
+
+// ClearAPIKey clears the API key (used when switching to OAuth auth)
+func (c *AIConfig) ClearAPIKey() {
+ c.APIKey = ""
+}
diff --git a/internal/hostagent/agent.go b/internal/hostagent/agent.go
index d01160eeb7..6efbd8f46d 100644
--- a/internal/hostagent/agent.go
+++ b/internal/hostagent/agent.go
@@ -14,6 +14,7 @@ import (
"github.com/rcourtman/pulse-go-rewrite/internal/agentupdate"
"github.com/rcourtman/pulse-go-rewrite/internal/buffer"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ceph"
"github.com/rcourtman/pulse-go-rewrite/internal/hostmetrics"
"github.com/rcourtman/pulse-go-rewrite/internal/mdadm"
"github.com/rcourtman/pulse-go-rewrite/internal/sensors"
@@ -318,6 +319,9 @@ func (a *Agent) buildReport(ctx context.Context) (agentshost.Report, error) {
// Collect RAID array data (best effort - don't fail if unavailable)
raidData := a.collectRAIDArrays(collectCtx)
+ // Collect Ceph cluster data (best effort - only on Ceph nodes)
+ cephData := a.collectCephStatus(collectCtx)
+
report := agentshost.Report{
Agent: agentshost.AgentInfo{
ID: a.agentID,
@@ -351,6 +355,7 @@ func (a *Agent) buildReport(ctx context.Context) (agentshost.Report, error) {
Network: append([]agentshost.NetworkInterface(nil), snapshot.Network...),
Sensors: sensorData,
RAID: raidData,
+ Ceph: cephData,
Tags: append([]string(nil), a.cfg.Tags...),
Timestamp: time.Now().UTC(),
}
@@ -482,6 +487,124 @@ func (a *Agent) collectRAIDArrays(ctx context.Context) []agentshost.RAIDArray {
return arrays
}
+// collectCephStatus attempts to collect Ceph cluster status.
+// Returns nil if Ceph is not available or not configured on this host.
+func (a *Agent) collectCephStatus(ctx context.Context) *agentshost.CephCluster {
+ // Only collect on Linux
+ if runtime.GOOS != "linux" {
+ return nil
+ }
+
+ status, err := ceph.Collect(ctx)
+ if err != nil {
+ a.logger.Debug().Err(err).Msg("Failed to collect Ceph status")
+ return nil
+ }
+ if status == nil {
+ return nil
+ }
+
+ // Convert internal ceph types to agent report types
+ result := &agentshost.CephCluster{
+ FSID: status.FSID,
+ Health: agentshost.CephHealth{
+ Status: status.Health.Status,
+ Checks: make(map[string]agentshost.CephCheck),
+ },
+ MonMap: agentshost.CephMonitorMap{
+ Epoch: status.MonMap.Epoch,
+ NumMons: status.MonMap.NumMons,
+ },
+ MgrMap: agentshost.CephManagerMap{
+ Available: status.MgrMap.Available,
+ NumMgrs: status.MgrMap.NumMgrs,
+ ActiveMgr: status.MgrMap.ActiveMgr,
+ Standbys: status.MgrMap.Standbys,
+ },
+ OSDMap: agentshost.CephOSDMap{
+ Epoch: status.OSDMap.Epoch,
+ NumOSDs: status.OSDMap.NumOSDs,
+ NumUp: status.OSDMap.NumUp,
+ NumIn: status.OSDMap.NumIn,
+ NumDown: status.OSDMap.NumDown,
+ NumOut: status.OSDMap.NumOut,
+ },
+ PGMap: agentshost.CephPGMap{
+ NumPGs: status.PGMap.NumPGs,
+ BytesTotal: status.PGMap.BytesTotal,
+ BytesUsed: status.PGMap.BytesUsed,
+ BytesAvailable: status.PGMap.BytesAvailable,
+ DataBytes: status.PGMap.DataBytes,
+ UsagePercent: status.PGMap.UsagePercent,
+ DegradedRatio: status.PGMap.DegradedRatio,
+ MisplacedRatio: status.PGMap.MisplacedRatio,
+ ReadBytesPerSec: status.PGMap.ReadBytesPerSec,
+ WriteBytesPerSec: status.PGMap.WriteBytesPerSec,
+ ReadOpsPerSec: status.PGMap.ReadOpsPerSec,
+ WriteOpsPerSec: status.PGMap.WriteOpsPerSec,
+ },
+ CollectedAt: status.CollectedAt.Format(time.RFC3339),
+ }
+
+ // Convert monitors
+ for _, mon := range status.MonMap.Monitors {
+ result.MonMap.Monitors = append(result.MonMap.Monitors, agentshost.CephMonitor{
+ Name: mon.Name,
+ Rank: mon.Rank,
+ Addr: mon.Addr,
+ Status: mon.Status,
+ })
+ }
+
+ // Convert health checks
+ for name, check := range status.Health.Checks {
+ result.Health.Checks[name] = agentshost.CephCheck{
+ Severity: check.Severity,
+ Message: check.Message,
+ Detail: check.Detail,
+ }
+ }
+
+ // Convert health summary
+ for _, s := range status.Health.Summary {
+ result.Health.Summary = append(result.Health.Summary, agentshost.CephHealthSummary{
+ Severity: s.Severity,
+ Message: s.Message,
+ })
+ }
+
+ // Convert pools
+ for _, pool := range status.Pools {
+ result.Pools = append(result.Pools, agentshost.CephPool{
+ ID: pool.ID,
+ Name: pool.Name,
+ BytesUsed: pool.BytesUsed,
+ BytesAvailable: pool.BytesAvailable,
+ Objects: pool.Objects,
+ PercentUsed: pool.PercentUsed,
+ })
+ }
+
+ // Convert services
+ for _, svc := range status.Services {
+ result.Services = append(result.Services, agentshost.CephService{
+ Type: svc.Type,
+ Running: svc.Running,
+ Total: svc.Total,
+ Daemons: svc.Daemons,
+ })
+ }
+
+ a.logger.Debug().
+ Str("fsid", result.FSID).
+ Str("health", result.Health.Status).
+ Int("osds", result.OSDMap.NumOSDs).
+ Int("pools", len(result.Pools)).
+ Msg("Collected Ceph cluster status")
+
+ return result
+}
+
// runProxmoxSetup performs one-time Proxmox API token setup and node registration.
func (a *Agent) runProxmoxSetup(ctx context.Context) {
a.logger.Info().Msg("Proxmox mode enabled, checking setup...")
diff --git a/internal/models/models.go b/internal/models/models.go
index 2776982551..f12431cb45 100644
--- a/internal/models/models.go
+++ b/internal/models/models.go
@@ -167,6 +167,7 @@ type Host struct {
NetworkInterfaces []HostNetworkInterface `json:"networkInterfaces,omitempty"`
Sensors HostSensorSummary `json:"sensors,omitempty"`
RAID []HostRAIDArray `json:"raid,omitempty"`
+ Ceph *HostCephCluster `json:"ceph,omitempty"`
Status string `json:"status"`
UptimeSeconds int64 `json:"uptimeSeconds,omitempty"`
IntervalSeconds int `json:"intervalSeconds,omitempty"`
@@ -221,6 +222,107 @@ type HostRAIDDevice struct {
Slot int `json:"slot"`
}
+// HostCephCluster represents Ceph cluster status collected directly by the host agent.
+// This is separate from CephCluster which comes from the Proxmox API.
+type HostCephCluster struct {
+ FSID string `json:"fsid"`
+ Health HostCephHealth `json:"health"`
+ MonMap HostCephMonitorMap `json:"monMap,omitempty"`
+ MgrMap HostCephManagerMap `json:"mgrMap,omitempty"`
+ OSDMap HostCephOSDMap `json:"osdMap"`
+ PGMap HostCephPGMap `json:"pgMap"`
+ Pools []HostCephPool `json:"pools,omitempty"`
+ Services []HostCephService `json:"services,omitempty"`
+ CollectedAt time.Time `json:"collectedAt"`
+}
+
+// HostCephHealth represents Ceph cluster health status.
+type HostCephHealth struct {
+ Status string `json:"status"` // HEALTH_OK, HEALTH_WARN, HEALTH_ERR
+ Checks map[string]HostCephCheck `json:"checks,omitempty"`
+ Summary []HostCephHealthSummary `json:"summary,omitempty"`
+}
+
+// HostCephCheck represents a health check detail.
+type HostCephCheck struct {
+ Severity string `json:"severity"`
+ Message string `json:"message,omitempty"`
+ Detail []string `json:"detail,omitempty"`
+}
+
+// HostCephHealthSummary represents a health summary message.
+type HostCephHealthSummary struct {
+ Severity string `json:"severity"`
+ Message string `json:"message"`
+}
+
+// HostCephMonitorMap represents Ceph monitor information.
+type HostCephMonitorMap struct {
+ Epoch int `json:"epoch"`
+ NumMons int `json:"numMons"`
+ Monitors []HostCephMonitor `json:"monitors,omitempty"`
+}
+
+// HostCephMonitor represents a single Ceph monitor.
+type HostCephMonitor struct {
+ Name string `json:"name"`
+ Rank int `json:"rank"`
+ Addr string `json:"addr,omitempty"`
+ Status string `json:"status,omitempty"`
+}
+
+// HostCephManagerMap represents Ceph manager information.
+type HostCephManagerMap struct {
+ Available bool `json:"available"`
+ NumMgrs int `json:"numMgrs"`
+ ActiveMgr string `json:"activeMgr,omitempty"`
+ Standbys int `json:"standbys"`
+}
+
+// HostCephOSDMap represents OSD status summary.
+type HostCephOSDMap struct {
+ Epoch int `json:"epoch"`
+ NumOSDs int `json:"numOsds"`
+ NumUp int `json:"numUp"`
+ NumIn int `json:"numIn"`
+ NumDown int `json:"numDown,omitempty"`
+ NumOut int `json:"numOut,omitempty"`
+}
+
+// HostCephPGMap represents placement group statistics.
+type HostCephPGMap struct {
+ NumPGs int `json:"numPgs"`
+ BytesTotal uint64 `json:"bytesTotal"`
+ BytesUsed uint64 `json:"bytesUsed"`
+ BytesAvailable uint64 `json:"bytesAvailable"`
+ DataBytes uint64 `json:"dataBytes,omitempty"`
+ UsagePercent float64 `json:"usagePercent"`
+ DegradedRatio float64 `json:"degradedRatio,omitempty"`
+ MisplacedRatio float64 `json:"misplacedRatio,omitempty"`
+ ReadBytesPerSec uint64 `json:"readBytesPerSec,omitempty"`
+ WriteBytesPerSec uint64 `json:"writeBytesPerSec,omitempty"`
+ ReadOpsPerSec uint64 `json:"readOpsPerSec,omitempty"`
+ WriteOpsPerSec uint64 `json:"writeOpsPerSec,omitempty"`
+}
+
+// HostCephPool represents a Ceph pool.
+type HostCephPool struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ BytesUsed uint64 `json:"bytesUsed"`
+ BytesAvailable uint64 `json:"bytesAvailable"`
+ Objects uint64 `json:"objects"`
+ PercentUsed float64 `json:"percentUsed"`
+}
+
+// HostCephService represents a Ceph service summary.
+type HostCephService struct {
+ Type string `json:"type"` // mon, mgr, osd, mds, rgw
+ Running int `json:"running"`
+ Total int `json:"total"`
+ Daemons []string `json:"daemons,omitempty"`
+}
+
// DiskIO represents I/O statistics for a block device.
// Counters are cumulative since boot.
type DiskIO struct {
@@ -1523,6 +1625,33 @@ func (s *State) RemoveHost(hostID string) (Host, bool) {
return Host{}, false
}
+// UpsertCephCluster inserts or updates a Ceph cluster in the state.
+// Uses ID (typically the FSID) for matching.
+func (s *State) UpsertCephCluster(cluster CephCluster) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ updated := false
+ for i, existing := range s.CephClusters {
+ if existing.ID == cluster.ID {
+ s.CephClusters[i] = cluster
+ updated = true
+ break
+ }
+ }
+
+ if !updated {
+ s.CephClusters = append(s.CephClusters, cluster)
+ }
+
+ sort.Slice(s.CephClusters, func(i, j int) bool {
+ return s.CephClusters[i].Name < s.CephClusters[j].Name
+ })
+
+ s.LastUpdate = time.Now()
+}
+
+
// SetHostStatus updates the status of a host if present.
func (s *State) SetHostStatus(hostID, status string) bool {
s.mu.Lock()
diff --git a/internal/monitoring/host_agent_temps.go b/internal/monitoring/host_agent_temps.go
index 52c8ac84d0..da7b069faf 100644
--- a/internal/monitoring/host_agent_temps.go
+++ b/internal/monitoring/host_agent_temps.go
@@ -252,6 +252,12 @@ func mergeTemperatureData(hostAgentTemp, proxyTemp *models.Temperature) *models.
result.HasGPU = true
}
+ // Merge NVMe data - prefer host agent if available, fall back to proxy
+ if !hostAgentTemp.HasNVMe && proxyTemp.HasNVMe {
+ result.NVMe = proxyTemp.NVMe
+ result.HasNVMe = true
+ }
+
// Update historical max if current is higher
currentTemp := result.CPUPackage
if currentTemp == 0 && result.CPUMax > 0 {
diff --git a/internal/monitoring/monitor.go b/internal/monitoring/monitor.go
index 3ce4678d1a..5f815e244f 100644
--- a/internal/monitoring/monitor.go
+++ b/internal/monitoring/monitor.go
@@ -162,6 +162,18 @@ func mergeNVMeTempsIntoDisks(disks []models.PhysicalDisk, nodes []models.Node) [
nvmeTempsByNode := make(map[string][]models.NVMeTemp)
for _, node := range nodes {
+ log.Debug().
+ Str("nodeName", node.Name).
+ Bool("hasTemp", node.Temperature != nil).
+ Bool("tempAvailable", node.Temperature != nil && node.Temperature.Available).
+ Int("smartCount", func() int {
+ if node.Temperature != nil {
+ return len(node.Temperature.SMART)
+ }
+ return 0
+ }()).
+ Msg("mergeNVMeTempsIntoDisks: checking node temperature")
+
if node.Temperature == nil || !node.Temperature.Available {
continue
}
@@ -171,6 +183,10 @@ func mergeNVMeTempsIntoDisks(disks []models.PhysicalDisk, nodes []models.Node) [
temps := make([]models.DiskTemp, len(node.Temperature.SMART))
copy(temps, node.Temperature.SMART)
smartTempsByNode[node.Name] = temps
+ log.Debug().
+ Str("nodeName", node.Name).
+ Int("smartTempCount", len(temps)).
+ Msg("mergeNVMeTempsIntoDisks: collected SMART temps for node")
}
// Collect legacy NVMe temps as fallback
@@ -185,15 +201,30 @@ func mergeNVMeTempsIntoDisks(disks []models.PhysicalDisk, nodes []models.Node) [
}
if len(smartTempsByNode) == 0 && len(nvmeTempsByNode) == 0 {
+ log.Debug().
+ Int("diskCount", len(disks)).
+ Msg("mergeNVMeTempsIntoDisks: no SMART or NVMe temperature data available")
return disks
}
+ log.Debug().
+ Int("smartNodeCount", len(smartTempsByNode)).
+ Int("nvmeNodeCount", len(nvmeTempsByNode)).
+ Int("diskCount", len(disks)).
+ Msg("mergeNVMeTempsIntoDisks: starting disk temperature merge")
+
updated := make([]models.PhysicalDisk, len(disks))
copy(updated, disks)
// Process SMART temperatures first (preferred method)
for i := range updated {
smartTemps, ok := smartTempsByNode[updated[i].Node]
+ log.Debug().
+ Str("diskDevPath", updated[i].DevPath).
+ Str("diskNode", updated[i].Node).
+ Bool("hasSMARTData", ok).
+ Int("smartTempCount", len(smartTemps)).
+ Msg("mergeNVMeTempsIntoDisks: checking disk for SMART temp match")
if !ok || len(smartTemps) == 0 {
continue
}
@@ -891,8 +922,8 @@ const (
dockerOfflineGraceMultiplier = 4
dockerMinimumHealthWindow = 30 * time.Second
dockerMaximumHealthWindow = 10 * time.Minute
- hostOfflineGraceMultiplier = 4
- hostMinimumHealthWindow = 30 * time.Second
+ hostOfflineGraceMultiplier = 6
+ hostMinimumHealthWindow = 60 * time.Second
hostMaximumHealthWindow = 10 * time.Minute
nodeOfflineGracePeriod = 60 * time.Second // Grace period before marking Proxmox nodes offline
nodeRRDCacheTTL = 30 * time.Second
@@ -1732,6 +1763,63 @@ func (m *Monitor) ApplyDockerReport(report agentsdocker.Report, tokenRecord *con
m.alertManager.CheckDockerHost(host)
}
+ // Record Docker HOST metrics for sparkline charts
+ now := time.Now()
+ hostMetricKey := fmt.Sprintf("dockerHost:%s", host.ID)
+
+ // Record host CPU usage
+ m.metricsHistory.AddGuestMetric(hostMetricKey, "cpu", host.CPUUsage, now)
+
+ // Record host Memory usage
+ m.metricsHistory.AddGuestMetric(hostMetricKey, "memory", host.Memory.Usage, now)
+
+ // Record host Disk usage (use first disk or calculate total)
+ var hostDiskPercent float64
+ if len(host.Disks) > 0 {
+ hostDiskPercent = host.Disks[0].Usage
+ }
+ m.metricsHistory.AddGuestMetric(hostMetricKey, "disk", hostDiskPercent, now)
+
+ // Also write to persistent SQLite store
+ if m.metricsStore != nil {
+ m.metricsStore.Write("dockerHost", host.ID, "cpu", host.CPUUsage, now)
+ m.metricsStore.Write("dockerHost", host.ID, "memory", host.Memory.Usage, now)
+ m.metricsStore.Write("dockerHost", host.ID, "disk", hostDiskPercent, now)
+ }
+
+ // Record Docker CONTAINER metrics for sparkline charts
+ // Use a prefixed key (docker:containerID) to distinguish from Proxmox containers
+ for _, container := range containers {
+ if container.ID == "" {
+ continue
+ }
+ // Build a unique metric key for Docker containers
+ metricKey := fmt.Sprintf("docker:%s", container.ID)
+
+ // Record CPU (already a percentage 0-100)
+ m.metricsHistory.AddGuestMetric(metricKey, "cpu", container.CPUPercent, now)
+
+ // Record Memory (already a percentage 0-100)
+ m.metricsHistory.AddGuestMetric(metricKey, "memory", container.MemoryPercent, now)
+
+ // Record Disk usage as percentage of writable layer vs root filesystem
+ var diskPercent float64
+ if container.RootFilesystemBytes > 0 && container.WritableLayerBytes > 0 {
+ diskPercent = float64(container.WritableLayerBytes) / float64(container.RootFilesystemBytes) * 100
+ if diskPercent > 100 {
+ diskPercent = 100
+ }
+ }
+ m.metricsHistory.AddGuestMetric(metricKey, "disk", diskPercent, now)
+
+ // Also write to persistent SQLite store for long-term storage
+ if m.metricsStore != nil {
+ m.metricsStore.Write("dockerContainer", container.ID, "cpu", container.CPUPercent, now)
+ m.metricsStore.Write("dockerContainer", container.ID, "memory", container.MemoryPercent, now)
+ m.metricsStore.Write("dockerContainer", container.ID, "disk", diskPercent, now)
+ }
+ }
+
log.Debug().
Str("dockerHost", host.Hostname).
Int("containers", len(containers)).
@@ -1948,6 +2036,12 @@ func (m *Monitor) ApplyHostReport(report agentshost.Report, tokenRecord *config.
})
}
+ // Convert Ceph data from agent report
+ var cephData *models.HostCephCluster
+ if report.Ceph != nil {
+ cephData = convertAgentCephToModels(report.Ceph)
+ }
+
host := models.Host{
ID: identifier,
Hostname: hostname,
@@ -1970,6 +2064,7 @@ func (m *Monitor) ApplyHostReport(report agentshost.Report, tokenRecord *config.
Additional: cloneStringFloatMap(report.Sensors.Additional),
},
RAID: raid,
+ Ceph: cephData,
Status: "online",
UptimeSeconds: report.Host.UptimeSeconds,
IntervalSeconds: report.Agent.IntervalSeconds,
@@ -2016,6 +2111,19 @@ func (m *Monitor) ApplyHostReport(report agentshost.Report, tokenRecord *config.
m.state.UpsertHost(host)
m.state.SetConnectionHealth(hostConnectionPrefix+host.ID, true)
+ // If host reports Ceph data, also update the global CephClusters state
+ if report.Ceph != nil {
+ cephCluster := convertAgentCephToGlobalCluster(report.Ceph, hostname, identifier, timestamp)
+ m.state.UpsertCephCluster(cephCluster)
+ log.Debug().
+ Str("hostId", identifier).
+ Str("hostname", hostname).
+ Str("fsid", cephCluster.FSID).
+ Str("health", cephCluster.Health).
+ Int("osds", cephCluster.NumOSDs).
+ Msg("Updated Ceph cluster from host agent")
+ }
+
if m.alertManager != nil {
m.alertManager.CheckHost(host)
}
@@ -2190,19 +2298,41 @@ func (m *Monitor) evaluateHostAgents(now time.Time) {
window = hostMaximumHealthWindow
}
- healthy := !host.LastSeen.IsZero() && now.Sub(host.LastSeen) <= window
+ age := now.Sub(host.LastSeen)
+ healthy := !host.LastSeen.IsZero() && age <= window
key := hostConnectionPrefix + host.ID
m.state.SetConnectionHealth(key, healthy)
hostCopy := host
if healthy {
hostCopy.Status = "online"
+ // Log status transition from offline to online
+ if host.Status == "offline" {
+ log.Debug().
+ Str("hostID", host.ID).
+ Str("hostname", host.Hostname).
+ Dur("age", age).
+ Dur("window", window).
+ Msg("Host agent back online")
+ }
m.state.SetHostStatus(host.ID, "online")
if m.alertManager != nil {
m.alertManager.HandleHostOnline(hostCopy)
}
} else {
hostCopy.Status = "offline"
+ // Log status transition from online to offline with diagnostic info
+ if host.Status == "online" || host.Status == "" {
+ log.Debug().
+ Str("hostID", host.ID).
+ Str("hostname", host.Hostname).
+ Time("lastSeen", host.LastSeen).
+ Dur("age", age).
+ Dur("window", window).
+ Int("intervalSeconds", host.IntervalSeconds).
+ Bool("lastSeenZero", host.LastSeen.IsZero()).
+ Msg("Host agent appears offline")
+ }
m.state.SetHostStatus(host.ID, "offline")
if m.alertManager != nil {
m.alertManager.HandleHostOffline(hostCopy)
@@ -3286,6 +3416,7 @@ func (m *Monitor) Start(ctx context.Context, wsHub *websocket.Hub) {
Int("nodes", len(state.Nodes)).
Int("vms", len(state.VMs)).
Int("containers", len(state.Containers)).
+ Int("hosts", len(state.Hosts)).
Int("pbs", len(state.PBSInstances)).
Int("pbsBackups", len(state.Backups.PBS)).
Int("physicalDisks", len(state.PhysicalDisks)).
@@ -8666,3 +8797,167 @@ func isLegacyDockerAgent(agentType string) bool {
// Legacy standalone agents have empty type
return agentType != "unified"
}
+
+// convertAgentCephToModels converts agent report Ceph data to the models.HostCephCluster format.
+func convertAgentCephToModels(ceph *agentshost.CephCluster) *models.HostCephCluster {
+ if ceph == nil {
+ return nil
+ }
+
+ collectedAt, _ := time.Parse(time.RFC3339, ceph.CollectedAt)
+
+ result := &models.HostCephCluster{
+ FSID: ceph.FSID,
+ Health: models.HostCephHealth{
+ Status: ceph.Health.Status,
+ Checks: make(map[string]models.HostCephCheck),
+ },
+ MonMap: models.HostCephMonitorMap{
+ Epoch: ceph.MonMap.Epoch,
+ NumMons: ceph.MonMap.NumMons,
+ },
+ MgrMap: models.HostCephManagerMap{
+ Available: ceph.MgrMap.Available,
+ NumMgrs: ceph.MgrMap.NumMgrs,
+ ActiveMgr: ceph.MgrMap.ActiveMgr,
+ Standbys: ceph.MgrMap.Standbys,
+ },
+ OSDMap: models.HostCephOSDMap{
+ Epoch: ceph.OSDMap.Epoch,
+ NumOSDs: ceph.OSDMap.NumOSDs,
+ NumUp: ceph.OSDMap.NumUp,
+ NumIn: ceph.OSDMap.NumIn,
+ NumDown: ceph.OSDMap.NumDown,
+ NumOut: ceph.OSDMap.NumOut,
+ },
+ PGMap: models.HostCephPGMap{
+ NumPGs: ceph.PGMap.NumPGs,
+ BytesTotal: ceph.PGMap.BytesTotal,
+ BytesUsed: ceph.PGMap.BytesUsed,
+ BytesAvailable: ceph.PGMap.BytesAvailable,
+ DataBytes: ceph.PGMap.DataBytes,
+ UsagePercent: ceph.PGMap.UsagePercent,
+ DegradedRatio: ceph.PGMap.DegradedRatio,
+ MisplacedRatio: ceph.PGMap.MisplacedRatio,
+ ReadBytesPerSec: ceph.PGMap.ReadBytesPerSec,
+ WriteBytesPerSec: ceph.PGMap.WriteBytesPerSec,
+ ReadOpsPerSec: ceph.PGMap.ReadOpsPerSec,
+ WriteOpsPerSec: ceph.PGMap.WriteOpsPerSec,
+ },
+ CollectedAt: collectedAt,
+ }
+
+ // Convert monitors
+ for _, mon := range ceph.MonMap.Monitors {
+ result.MonMap.Monitors = append(result.MonMap.Monitors, models.HostCephMonitor{
+ Name: mon.Name,
+ Rank: mon.Rank,
+ Addr: mon.Addr,
+ Status: mon.Status,
+ })
+ }
+
+ // Convert health checks
+ for name, check := range ceph.Health.Checks {
+ result.Health.Checks[name] = models.HostCephCheck{
+ Severity: check.Severity,
+ Message: check.Message,
+ Detail: check.Detail,
+ }
+ }
+
+ // Convert health summary
+ for _, s := range ceph.Health.Summary {
+ result.Health.Summary = append(result.Health.Summary, models.HostCephHealthSummary{
+ Severity: s.Severity,
+ Message: s.Message,
+ })
+ }
+
+ // Convert pools
+ for _, pool := range ceph.Pools {
+ result.Pools = append(result.Pools, models.HostCephPool{
+ ID: pool.ID,
+ Name: pool.Name,
+ BytesUsed: pool.BytesUsed,
+ BytesAvailable: pool.BytesAvailable,
+ Objects: pool.Objects,
+ PercentUsed: pool.PercentUsed,
+ })
+ }
+
+ // Convert services
+ for _, svc := range ceph.Services {
+ result.Services = append(result.Services, models.HostCephService{
+ Type: svc.Type,
+ Running: svc.Running,
+ Total: svc.Total,
+ Daemons: svc.Daemons,
+ })
+ }
+
+ return result
+}
+
+// convertAgentCephToGlobalCluster converts agent Ceph data to the global CephCluster format
+// used by the State.CephClusters list.
+func convertAgentCephToGlobalCluster(ceph *agentshost.CephCluster, hostname, hostID string, timestamp time.Time) models.CephCluster {
+ // Use FSID as the primary ID since it's unique per Ceph cluster
+ id := ceph.FSID
+ if id == "" {
+ id = "agent-ceph-" + hostID
+ }
+
+ cluster := models.CephCluster{
+ ID: id,
+ Instance: "agent:" + hostname,
+ Name: hostname + " Ceph",
+ FSID: ceph.FSID,
+ Health: strings.TrimPrefix(ceph.Health.Status, "HEALTH_"),
+ TotalBytes: int64(ceph.PGMap.BytesTotal),
+ UsedBytes: int64(ceph.PGMap.BytesUsed),
+ AvailableBytes: int64(ceph.PGMap.BytesAvailable),
+ UsagePercent: ceph.PGMap.UsagePercent,
+ NumMons: ceph.MonMap.NumMons,
+ NumMgrs: ceph.MgrMap.NumMgrs,
+ NumOSDs: ceph.OSDMap.NumOSDs,
+ NumOSDsUp: ceph.OSDMap.NumUp,
+ NumOSDsIn: ceph.OSDMap.NumIn,
+ NumPGs: ceph.PGMap.NumPGs,
+ LastUpdated: timestamp,
+ }
+
+ // Build health message from checks
+ var healthMessages []string
+ for _, check := range ceph.Health.Checks {
+ if check.Message != "" {
+ healthMessages = append(healthMessages, check.Message)
+ }
+ }
+ if len(healthMessages) > 0 {
+ cluster.HealthMessage = strings.Join(healthMessages, "; ")
+ }
+
+ // Convert pools
+ for _, pool := range ceph.Pools {
+ cluster.Pools = append(cluster.Pools, models.CephPool{
+ ID: pool.ID,
+ Name: pool.Name,
+ StoredBytes: int64(pool.BytesUsed),
+ AvailableBytes: int64(pool.BytesAvailable),
+ Objects: int64(pool.Objects),
+ PercentUsed: pool.PercentUsed,
+ })
+ }
+
+ // Convert services
+ for _, svc := range ceph.Services {
+ cluster.Services = append(cluster.Services, models.CephServiceStatus{
+ Type: svc.Type,
+ Running: svc.Running,
+ Total: svc.Total,
+ })
+ }
+
+ return cluster
+}
diff --git a/internal/monitoring/monitor_host_agents_test.go b/internal/monitoring/monitor_host_agents_test.go
index a9041550fa..19613bb268 100644
--- a/internal/monitoring/monitor_host_agents_test.go
+++ b/internal/monitoring/monitor_host_agents_test.go
@@ -227,8 +227,8 @@ func TestEvaluateHostAgentsZeroIntervalUsesDefault(t *testing.T) {
t.Cleanup(func() { monitor.alertManager.Stop() })
hostID := "host-zero-interval"
- // IntervalSeconds = 0, LastSeen = now, should use default interval (30s)
- // Default window = 30s * 4 = 120s, but minimum is 30s, so window = 30s
+ // IntervalSeconds = 0, LastSeen = now, should use default interval (60s)
+ // Default window = 60s * 6 = 360s, but minimum is 60s, so window = 60s
// With LastSeen = now, the host should be healthy
monitor.state.UpsertHost(models.Host{
ID: hostID,
@@ -288,15 +288,15 @@ func TestEvaluateHostAgentsWindowClampedToMinimum(t *testing.T) {
t.Cleanup(func() { monitor.alertManager.Stop() })
hostID := "host-min-window"
- // IntervalSeconds = 1, so window = 1s * 4 = 4s, but minimum is 30s
- // Host last seen 25s ago should still be healthy (within 30s minimum window)
+ // IntervalSeconds = 1, so window = 1s * 6 = 6s, but minimum is 60s
+ // Host last seen 55s ago should still be healthy (within 60s minimum window)
now := time.Now()
monitor.state.UpsertHost(models.Host{
ID: hostID,
Hostname: "min-window.local",
Status: "unknown",
IntervalSeconds: 1, // Very small interval
- LastSeen: now.Add(-25 * time.Second),
+ LastSeen: now.Add(-55 * time.Second),
})
monitor.evaluateHostAgents(now)
@@ -304,7 +304,7 @@ func TestEvaluateHostAgentsWindowClampedToMinimum(t *testing.T) {
snapshot := monitor.state.GetSnapshot()
connKey := hostConnectionPrefix + hostID
if healthy, ok := snapshot.ConnectionHealth[connKey]; !ok || !healthy {
- t.Fatalf("expected connection health true (window clamped to minimum 30s), got %v (exists=%v)", healthy, ok)
+ t.Fatalf("expected connection health true (window clamped to minimum 60s), got %v (exists=%v)", healthy, ok)
}
for _, host := range snapshot.Hosts {
@@ -323,7 +323,7 @@ func TestEvaluateHostAgentsWindowClampedToMaximum(t *testing.T) {
t.Cleanup(func() { monitor.alertManager.Stop() })
hostID := "host-max-window"
- // IntervalSeconds = 300 (5 min), so window = 300s * 4 = 1200s (20 min)
+ // IntervalSeconds = 300 (5 min), so window = 300s * 6 = 1800s (30 min)
// But maximum is 10 min = 600s
// Host last seen 11 minutes ago should be unhealthy (outside 10 min max window)
now := time.Now()
@@ -360,7 +360,7 @@ func TestEvaluateHostAgentsRecentLastSeenIsHealthy(t *testing.T) {
hostID := "host-recent"
now := time.Now()
- // IntervalSeconds = 30, window = 30s * 4 = 120s (clamped to min 30s is not needed)
+ // IntervalSeconds = 30, window = 30s * 6 = 180s (clamped to min 60s is not needed)
// LastSeen = 10s ago, should be healthy
monitor.state.UpsertHost(models.Host{
ID: hostID,
@@ -427,7 +427,7 @@ func TestEvaluateHostAgentsOldLastSeenIsUnhealthy(t *testing.T) {
hostID := "host-old-lastseen"
now := time.Now()
- // IntervalSeconds = 30, window = 30s * 4 = 120s
+ // IntervalSeconds = 30, window = 30s * 6 = 180s
// LastSeen = 5 minutes ago, should be unhealthy
monitor.state.UpsertHost(models.Host{
ID: hostID,
diff --git a/internal/monitoring/monitor_polling.go b/internal/monitoring/monitor_polling.go
index 9655829741..412684d796 100644
--- a/internal/monitoring/monitor_polling.go
+++ b/internal/monitoring/monitor_polling.go
@@ -1734,7 +1734,7 @@ func (m *Monitor) pollPVENode(
GuestURL: guestURL,
Status: effectiveStatus,
Type: "node",
- CPU: safeFloat(node.CPU), // Already in percentage
+ CPU: safeFloat(node.CPU), // Proxmox returns 0-1 ratio (e.g., 0.15 = 15%)
Memory: models.Memory{
Total: int64(node.MaxMem),
Used: int64(node.Mem),
@@ -2198,6 +2198,22 @@ func (m *Monitor) pollPVENode(
Msg("Temperature collection failed - check SSH access")
}
}
+
+ // Debug: log proxy temp details before merge
+ if proxyTemp != nil {
+ log.Debug().
+ Str("node", node.Node).
+ Bool("proxyTempAvailable", proxyTemp.Available).
+ Bool("proxyHasSMART", proxyTemp.HasSMART).
+ Int("proxySMARTCount", len(proxyTemp.SMART)).
+ Bool("proxyHasNVMe", proxyTemp.HasNVMe).
+ Int("proxyNVMeCount", len(proxyTemp.NVMe)).
+ Msg("Proxy temperature data before merge")
+ } else {
+ log.Debug().
+ Str("node", node.Node).
+ Msg("Proxy temperature data is nil")
+ }
}
// Merge host agent and proxy temperatures
@@ -2265,11 +2281,35 @@ func (m *Monitor) pollPVENode(
Float64("cpuMaxRecord", temp.CPUMaxRecord).
Int("nvmeCount", len(temp.NVMe)).
Msg("Collected temperature data")
- } else if hostAgentTemp == nil && proxyTemp == nil {
- log.Debug().
- Str("node", node.Node).
- Bool("isCluster", modelNode.IsClusterMember).
- Msg("No temperature data available (no host agent or proxy/SSH)")
+ } else {
+ // Temperature data returned but not available (temp != nil && !temp.Available)
+ // OR no temperature data from any source - preserve previous temperature if available
+ // This prevents the temperature column from flickering when collection temporarily fails
+ var prevTemp *models.Temperature
+ for _, prevNode := range prevInstanceNodes {
+ if prevNode.ID == modelNode.ID && prevNode.Temperature != nil && prevNode.Temperature.Available {
+ prevTemp = prevNode.Temperature
+ break
+ }
+ }
+
+ if prevTemp != nil {
+ // Clone the previous temperature to avoid modifying historical data
+ preserved := *prevTemp
+ preserved.LastUpdate = prevTemp.LastUpdate // Keep original update time to indicate staleness
+ modelNode.Temperature = &preserved
+ log.Debug().
+ Str("node", node.Node).
+ Bool("isCluster", modelNode.IsClusterMember).
+ Float64("cpuPackage", preserved.CPUPackage).
+ Time("lastUpdate", preserved.LastUpdate).
+ Msg("Preserved previous temperature data (current collection failed or unavailable)")
+ } else {
+ log.Debug().
+ Str("node", node.Node).
+ Bool("isCluster", modelNode.IsClusterMember).
+ Msg("No temperature data available (collection failed, no previous data to preserve)")
+ }
}
}
diff --git a/internal/resources/ip_filtering_test.go b/internal/resources/ip_filtering_test.go
new file mode 100644
index 0000000000..a3ef888c04
--- /dev/null
+++ b/internal/resources/ip_filtering_test.go
@@ -0,0 +1,213 @@
+package resources
+
+import (
+ "testing"
+ "time"
+)
+
+// TestIsNonUniqueIP tests the IP filtering logic for deduplication.
+// This was added to fix the issue where Docker bridge IPs (172.17.0.1) were
+// causing false positive deduplication between hosts.
+func TestIsNonUniqueIP(t *testing.T) {
+ t.Parallel()
+
+ tests := []struct {
+ name string
+ ip string
+ expected bool
+ }{
+ // Localhost addresses - should be filtered
+ {"localhost IPv4", "127.0.0.1", true},
+ {"localhost IPv4 alternate", "127.0.0.2", true},
+ {"localhost IPv4 high", "127.255.255.255", true},
+ {"localhost IPv6", "::1", true},
+
+ // Docker bridge network addresses - should be filtered (the key fix)
+ {"docker bridge default", "172.17.0.1", true},
+ {"docker bridge container", "172.17.0.2", true},
+ {"docker bridge high", "172.17.255.255", true},
+ {"docker bridge 172.18.x", "172.18.0.1", true},
+ {"docker bridge 172.19.x", "172.19.0.1", true},
+ {"docker bridge 172.20.x", "172.20.0.1", true},
+ {"docker bridge 172.21.x", "172.21.0.1", true},
+ {"docker bridge 172.22.x", "172.22.0.1", true},
+
+ // Link-local IPv6 - should be filtered
+ {"link-local IPv6", "fe80::1", true},
+ {"link-local IPv6 full", "fe80:0000:0000:0000:0000:0000:0000:0001", true},
+ {"link-local uppercase", "FE80::1:2:3", true},
+
+ // Valid private IPs - should NOT be filtered
+ {"private 192.168.x", "192.168.1.100", false},
+ {"private 192.168.0.x", "192.168.0.1", false},
+ {"private 10.x", "10.0.0.1", false},
+ {"private 10.x alternate", "10.10.10.10", false},
+ {"private 172.16.x", "172.16.0.1", false},
+ {"private 172.23.x", "172.23.0.1", false}, // Outside Docker bridge range
+ {"private 172.24.x", "172.24.0.1", false},
+ {"private 172.31.x", "172.31.255.255", false},
+
+ // Public IPs - should NOT be filtered
+ {"public IP 1", "8.8.8.8", false},
+ {"public IP 2", "1.1.1.1", false},
+ {"public IP 3", "203.0.113.42", false},
+
+ // Public IPv6 - should NOT be filtered
+ {"public IPv6", "2001:db8::1", false},
+ {"public IPv6 documentation", "2001:0db8:85a3::8a2e:0370:7334", false},
+
+ // Edge cases
+ {"empty string", "", false},
+ {"random string", "not-an-ip", false},
+ }
+
+ for _, tt := range tests {
+ tt := tt
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ result := isNonUniqueIP(tt.ip)
+ if result != tt.expected {
+ t.Errorf("isNonUniqueIP(%q) = %v, want %v", tt.ip, result, tt.expected)
+ }
+ })
+ }
+}
+
+// TestNoDeduplicationForDockerBridgeIP verifies that hosts with Docker bridge IPs
+// are not incorrectly deduplicated. This was the root cause of the "delly disappearing" bug.
+func TestNoDeduplicationForDockerBridgeIP(t *testing.T) {
+ store := NewStore()
+
+ now := time.Now()
+
+ // Host 1 with Docker bridge IP
+ host1 := Resource{
+ ID: "host-1",
+ Type: ResourceTypeHost,
+ Name: "server1",
+ PlatformType: PlatformHostAgent,
+ SourceType: SourceAgent,
+ LastSeen: now,
+ Identity: &ResourceIdentity{
+ Hostname: "server1",
+ IPs: []string{"192.168.1.10", "172.17.0.1"}, // 172.17.0.1 is Docker bridge
+ },
+ }
+ store.Upsert(host1)
+
+ // Host 2 with same Docker bridge IP (every Docker host has this!)
+ host2 := Resource{
+ ID: "host-2",
+ Type: ResourceTypeHost,
+ Name: "server2",
+ PlatformType: PlatformHostAgent,
+ SourceType: SourceAgent,
+ LastSeen: now,
+ Identity: &ResourceIdentity{
+ Hostname: "server2",
+ IPs: []string{"192.168.1.20", "172.17.0.1"}, // Same Docker bridge IP
+ },
+ }
+ store.Upsert(host2)
+
+ // Both should exist (172.17.0.1 shouldn't trigger dedup)
+ all := store.GetAll()
+ if len(all) != 2 {
+ t.Errorf("Expected 2 hosts (Docker bridge IP shouldn't cause dedup), got %d", len(all))
+ }
+
+ // Verify both are retrievable
+ _, ok1 := store.Get("host-1")
+ _, ok2 := store.Get("host-2")
+ if !ok1 || !ok2 {
+ t.Error("Both hosts should be retrievable independently")
+ }
+}
+
+// TestDeduplicationStillWorksForUniqueIPs verifies that deduplication still works
+// for actual unique IPs while filtering out non-unique ones.
+func TestDeduplicationStillWorksForUniqueIPs(t *testing.T) {
+ store := NewStore()
+
+ now := time.Now()
+
+ // Add first host
+ host1 := Resource{
+ ID: "host-old",
+ Type: ResourceTypeHost,
+ Name: "server",
+ PlatformType: PlatformHostAgent,
+ SourceType: SourceAgent,
+ LastSeen: now,
+ Identity: &ResourceIdentity{
+ Hostname: "server",
+ IPs: []string{"192.168.1.100"}, // Unique IP
+ },
+ }
+ store.Upsert(host1)
+
+ // Add second host with same unique IP but different ID (same physical machine)
+ host2 := Resource{
+ ID: "host-new",
+ Type: ResourceTypeHost,
+ Name: "server-renamed",
+ PlatformType: PlatformHostAgent,
+ SourceType: SourceAgent,
+ LastSeen: now.Add(time.Second), // Newer
+ Identity: &ResourceIdentity{
+ Hostname: "server-renamed",
+ IPs: []string{"192.168.1.100"}, // Same unique IP
+ },
+ }
+ store.Upsert(host2)
+
+ // Should have only 1 host (deduplicated by unique IP)
+ all := store.GetAll()
+ if len(all) != 1 {
+ t.Errorf("Expected 1 host (deduplicated by unique IP), got %d", len(all))
+ }
+}
+
+// TestMixedIPsDeduplication verifies that a host with both unique and non-unique IPs
+// is properly handled (dedup only on unique IPs).
+func TestMixedIPsDeduplication(t *testing.T) {
+ store := NewStore()
+
+ now := time.Now()
+
+ // Host with unique IP and Docker bridge
+ host1 := Resource{
+ ID: "host-1",
+ Type: ResourceTypeHost,
+ Name: "docker-server-1",
+ PlatformType: PlatformHostAgent,
+ SourceType: SourceAgent,
+ LastSeen: now,
+ Identity: &ResourceIdentity{
+ Hostname: "docker-server-1",
+ IPs: []string{"192.168.1.50", "172.17.0.1", "127.0.0.1"},
+ },
+ }
+ store.Upsert(host1)
+
+ // Different host sharing only the non-unique IPs
+ host2 := Resource{
+ ID: "host-2",
+ Type: ResourceTypeHost,
+ Name: "docker-server-2",
+ PlatformType: PlatformHostAgent,
+ SourceType: SourceAgent,
+ LastSeen: now,
+ Identity: &ResourceIdentity{
+ Hostname: "docker-server-2",
+ IPs: []string{"192.168.1.60", "172.17.0.1", "127.0.0.1"}, // Different unique IP
+ },
+ }
+ store.Upsert(host2)
+
+ // Both should exist (only 127.0.0.1 and 172.17.0.1 are shared, which are non-unique)
+ all := store.GetAll()
+ if len(all) != 2 {
+ t.Errorf("Expected 2 hosts (shared non-unique IPs only), got %d", len(all))
+ }
+}
diff --git a/internal/resources/populate_test.go b/internal/resources/populate_test.go
index 7ec1e1893f..101271e1f0 100644
--- a/internal/resources/populate_test.go
+++ b/internal/resources/populate_test.go
@@ -18,7 +18,7 @@ func TestStorePopulateFromSnapshot(t *testing.T) {
Name: "pve-node-1",
Instance: "https://pve1.local:8006",
Status: "online",
- CPU: 45.5,
+ CPU: 0.455, // Proxmox API returns 0-1 ratio
Memory: models.Memory{
Total: 16000000000,
Used: 8000000000,
@@ -37,7 +37,7 @@ func TestStorePopulateFromSnapshot(t *testing.T) {
Node: "pve-node-1",
Instance: "https://pve1.local:8006",
Status: "running",
- CPU: 25.0,
+ CPU: 0.25, // Proxmox API returns 0-1 ratio
Memory: models.Memory{
Total: 4000000000,
Used: 2000000000,
diff --git a/internal/resources/store.go b/internal/resources/store.go
index 26d322bbf4..af391a9e60 100644
--- a/internal/resources/store.go
+++ b/internal/resources/store.go
@@ -394,22 +394,29 @@ func (s *Store) findDuplicate(r *Resource) string {
return ""
}
- // 1. Machine ID match (most reliable)
- if r.Identity.MachineID != "" {
+ // 1. Machine ID match (most reliable) - but only for same type
+ // A node and host agent on the same machine should coexist as different data sources
+ if r.Identity.MachineID != "" && r.IsInfrastructure() {
if existingID, ok := s.byMachineID[r.Identity.MachineID]; ok && existingID != r.ID {
- return existingID
+ existing := s.resources[existingID]
+ // Only match if same type
+ if existing != nil && existing.Type == r.Type {
+ return existingID
+ }
}
}
- // 2. Hostname match (case-insensitive)
- if r.Identity.Hostname != "" {
+ // 2. Hostname match (case-insensitive) - only for same infrastructure type
+ // Workloads (VMs, containers) can have duplicate names across clusters
+ if r.Identity.Hostname != "" && r.IsInfrastructure() {
hostnameLower := strings.ToLower(r.Identity.Hostname)
if existingIDs, ok := s.byHostname[hostnameLower]; ok {
for _, existingID := range existingIDs {
if existingID != r.ID {
existing := s.resources[existingID]
- // Only match infrastructure resources with each other
- if existing.IsInfrastructure() && r.IsInfrastructure() {
+ // Only match same infrastructure type (e.g., host with host, node with node)
+ // Different types represent different data sources and should coexist
+ if existing.Type == r.Type {
return existingID
}
}
@@ -417,18 +424,20 @@ func (s *Store) findDuplicate(r *Resource) string {
}
}
- // 3. IP overlap (if same non-localhost IP, likely same machine)
- for _, ip := range r.Identity.IPs {
- if isLocalhost(ip) {
- continue
- }
- if existingIDs, ok := s.byIP[ip]; ok {
- for _, existingID := range existingIDs {
- if existingID != r.ID {
- existing := s.resources[existingID]
- // Only match infrastructure resources with each other
- if existing.IsInfrastructure() && r.IsInfrastructure() {
- return existingID
+ // 3. IP overlap (if same non-localhost IP, likely same machine) - only for same infrastructure type
+ if r.IsInfrastructure() {
+ for _, ip := range r.Identity.IPs {
+ if isNonUniqueIP(ip) {
+ continue
+ }
+ if existingIDs, ok := s.byIP[ip]; ok {
+ for _, existingID := range existingIDs {
+ if existingID != r.ID {
+ existing := s.resources[existingID]
+ // Only match same infrastructure type
+ if existing.Type == r.Type {
+ return existingID
+ }
}
}
}
@@ -487,7 +496,7 @@ func (s *Store) addToIndexes(r *Resource) {
}
for _, ip := range r.Identity.IPs {
- if !isLocalhost(ip) {
+ if !isNonUniqueIP(ip) {
s.byIP[ip] = append(s.byIP[ip], r.ID)
}
}
@@ -530,8 +539,28 @@ func removeFromSlice(slice []string, item string) []string {
return result
}
-func isLocalhost(ip string) bool {
- return ip == "127.0.0.1" || ip == "::1" || strings.HasPrefix(ip, "127.")
+// isNonUniqueIP returns true if the IP address is not useful for machine identification.
+// This includes localhost addresses and Docker bridge IPs that exist on every Docker host.
+func isNonUniqueIP(ip string) bool {
+ // Localhost addresses
+ if ip == "127.0.0.1" || ip == "::1" || strings.HasPrefix(ip, "127.") {
+ return true
+ }
+
+ // Docker bridge network - 172.17.0.1/16 exists on every Docker host
+ // Also filter other Docker-assigned bridge networks (172.17-31.x.x)
+ if strings.HasPrefix(ip, "172.17.") || strings.HasPrefix(ip, "172.18.") ||
+ strings.HasPrefix(ip, "172.19.") || strings.HasPrefix(ip, "172.20.") ||
+ strings.HasPrefix(ip, "172.21.") || strings.HasPrefix(ip, "172.22.") {
+ return true
+ }
+
+ // Link-local addresses (fe80::)
+ if strings.HasPrefix(strings.ToLower(ip), "fe80:") {
+ return true
+ }
+
+ return false
}
// MarkStale marks resources that haven't been updated recently.
diff --git a/internal/resources/store_test.go b/internal/resources/store_test.go
index 6b3be13955..c32efe3595 100644
--- a/internal/resources/store_test.go
+++ b/internal/resources/store_test.go
@@ -158,7 +158,8 @@ func TestDeduplicationByHostname(t *testing.T) {
}
store.Upsert(nodeResource)
- // Add a host agent for the same server (agent source - should be preferred)
+ // Add a host agent for the same server (agent source)
+ // Different types should coexist, not merge
hostResource := Resource{
ID: "host-agent/server1",
Type: ResourceTypeHost,
@@ -166,7 +167,7 @@ func TestDeduplicationByHostname(t *testing.T) {
PlatformType: PlatformHostAgent,
SourceType: SourceAgent,
Status: StatusOnline,
- CPU: &MetricValue{Current: 55.0}, // Slightly different value
+ CPU: &MetricValue{Current: 55.0},
LastSeen: now,
Identity: &ResourceIdentity{
Hostname: "server1",
@@ -174,40 +175,51 @@ func TestDeduplicationByHostname(t *testing.T) {
}
store.Upsert(hostResource)
- // We should only have 1 resource (the agent one, as it's preferred)
+ // Different types should coexist (node + host = 2 resources)
all := store.GetAll()
- if len(all) != 1 {
- t.Errorf("Expected 1 resource after dedup, got %d", len(all))
+ if len(all) != 2 {
+ t.Errorf("Expected 2 resources (node + host coexist), got %d", len(all))
}
- // The agent resource should be the one stored
- r, ok := store.Get("host-agent/server1")
+ // Both should be retrievable
+ _, ok := store.Get("pve1/node/server1")
if !ok {
- t.Fatal("Failed to get agent resource")
+ t.Error("Node resource should be retrievable")
}
- if r.SourceType != SourceAgent {
- t.Errorf("Expected agent source, got %s", r.SourceType)
- }
- if r.CPU.Current != 55.0 {
- t.Errorf("Expected CPU 55.0 from agent, got %f", r.CPU.Current)
+ _, ok = store.Get("host-agent/server1")
+ if !ok {
+ t.Error("Host resource should be retrievable")
}
- // The node resource should redirect to the agent resource
- if !store.IsSuppressed("pve1/node/server1") {
- t.Error("Node resource should be suppressed")
+ // Now test same-type deduplication: add another node with same hostname
+ nodeResource2 := Resource{
+ ID: "pve2/node/server1",
+ Type: ResourceTypeNode,
+ Name: "server1",
+ PlatformType: PlatformProxmoxPVE,
+ SourceType: SourceAPI,
+ Status: StatusOnline,
+ CPU: &MetricValue{Current: 60.0},
+ LastSeen: now.Add(time.Second), // Newer
+ Identity: &ResourceIdentity{
+ Hostname: "server1",
+ },
}
- preferred := store.GetPreferredID("pve1/node/server1")
- if preferred != "host-agent/server1" {
- t.Errorf("Expected preferred ID to be host-agent/server1, got %s", preferred)
+ store.Upsert(nodeResource2)
+
+ // Should still have 2 (newer node replaces old node, host remains)
+ all = store.GetAll()
+ if len(all) != 2 {
+ t.Errorf("Expected 2 resources after same-type dedup, got %d", len(all))
}
- // Accessing by suppressed ID should return the preferred resource
- r, ok = store.Get("pve1/node/server1")
+ // The newer node should have replaced the old one
+ r, ok := store.Get("pve2/node/server1")
if !ok {
- t.Fatal("Should be able to get by suppressed ID")
+ t.Error("Newer node should be present")
}
- if r.ID != "host-agent/server1" {
- t.Errorf("Expected to get agent resource when accessing by node ID, got %s", r.ID)
+ if r.CPU.Current != 60.0 {
+ t.Errorf("Expected CPU 60.0 from newer node, got %f", r.CPU.Current)
}
}
@@ -233,7 +245,8 @@ func TestDeduplicationByMachineID(t *testing.T) {
}
store.Upsert(dockerHost)
- // Add a host agent with the same machine ID but different hostname
+ // Add a host agent with the same machine ID but different type
+ // Different types should coexist
hostAgent := Resource{
ID: "host-agent-1",
Type: ResourceTypeHost,
@@ -241,7 +254,7 @@ func TestDeduplicationByMachineID(t *testing.T) {
PlatformType: PlatformHostAgent,
SourceType: SourceAgent,
Status: StatusOnline,
- LastSeen: now.Add(time.Second), // Slightly newer
+ LastSeen: now.Add(time.Second),
Identity: &ResourceIdentity{
Hostname: "server-production",
MachineID: machineID,
@@ -249,10 +262,32 @@ func TestDeduplicationByMachineID(t *testing.T) {
}
store.Upsert(hostAgent)
- // Should only have 1 resource (the newer one, since both are agent sources)
+ // Different types should coexist (docker-host + host = 2 resources)
all := store.GetAll()
- if len(all) != 1 {
- t.Errorf("Expected 1 resource after dedup by machineID, got %d", len(all))
+ if len(all) != 2 {
+ t.Errorf("Expected 2 resources (different types coexist with same machineID), got %d", len(all))
+ }
+
+ // Now test same-type deduplication: add another host with same machine ID
+ hostAgent2 := Resource{
+ ID: "host-agent-2",
+ Type: ResourceTypeHost, // Same type as first host
+ Name: "server-newer",
+ PlatformType: PlatformHostAgent,
+ SourceType: SourceAgent,
+ Status: StatusOnline,
+ LastSeen: now.Add(2 * time.Second), // Newer
+ Identity: &ResourceIdentity{
+ Hostname: "server-newer",
+ MachineID: machineID,
+ },
+ }
+ store.Upsert(hostAgent2)
+
+ // Should still have 2 (newer host replaces old host, docker-host remains)
+ all = store.GetAll()
+ if len(all) != 2 {
+ t.Errorf("Expected 2 resources after same-type dedup, got %d", len(all))
}
}
@@ -278,13 +313,14 @@ func TestDeduplicationByIP(t *testing.T) {
}
store.Upsert(node)
- // Add a host agent with the same IP
+ // Add a host agent with the same IP but different type
+ // Different types should coexist
host := Resource{
ID: "host-1",
Type: ResourceTypeHost,
Name: "different-hostname",
PlatformType: PlatformHostAgent,
- SourceType: SourceAgent, // Agent preferred
+ SourceType: SourceAgent,
Status: StatusOnline,
LastSeen: now,
Identity: &ResourceIdentity{
@@ -294,16 +330,20 @@ func TestDeduplicationByIP(t *testing.T) {
}
store.Upsert(host)
- // Should only have 1 resource
+ // Different types should coexist (node + host = 2 resources)
all := store.GetAll()
- if len(all) != 1 {
- t.Errorf("Expected 1 resource after dedup by IP, got %d", len(all))
+ if len(all) != 2 {
+ t.Errorf("Expected 2 resources (different types coexist), got %d", len(all))
}
- // Agent should be preferred
- r, _ := store.Get("host-1")
- if r == nil || r.SourceType != SourceAgent {
- t.Error("Agent resource should be preferred")
+ // Both should be retrievable
+ _, ok := store.Get("node-1")
+ if !ok {
+ t.Error("Node should be retrievable")
+ }
+ _, ok = store.Get("host-1")
+ if !ok {
+ t.Error("Host should be retrievable")
}
}
@@ -549,10 +589,10 @@ func TestAPIToAgentPreference(t *testing.T) {
now := time.Now()
- // First, add an API resource
+ // First, add an API-sourced host
apiResource := Resource{
- ID: "api-node",
- Type: ResourceTypeNode,
+ ID: "api-host",
+ Type: ResourceTypeHost, // Same type as agent
Name: "server",
PlatformType: PlatformProxmoxPVE,
SourceType: SourceAPI,
@@ -564,10 +604,10 @@ func TestAPIToAgentPreference(t *testing.T) {
}
store.Upsert(apiResource)
- // Then, add an agent resource for the same machine
+ // Then, add an agent resource for the same machine (same type, different source)
agentResource := Resource{
ID: "agent-host",
- Type: ResourceTypeHost,
+ Type: ResourceTypeHost, // Same type
Name: "server",
PlatformType: PlatformHostAgent,
SourceType: SourceAgent,
@@ -579,14 +619,14 @@ func TestAPIToAgentPreference(t *testing.T) {
}
store.Upsert(agentResource)
- // Only agent resource should exist
+ // Only agent resource should exist (same type = dedup, agent preferred)
all := store.GetAll()
if len(all) != 1 {
- t.Fatalf("Expected 1 resource, got %d", len(all))
+ t.Fatalf("Expected 1 resource (same type dedup), got %d", len(all))
}
if all[0].SourceType != SourceAgent {
- t.Errorf("Expected agent source type, got %s", all[0].SourceType)
+ t.Errorf("Expected agent source type (preferred), got %s", all[0].SourceType)
}
}
diff --git a/internal/sensors/parser.go b/internal/sensors/parser.go
index 1f31ab2dc3..19444dae07 100644
--- a/internal/sensors/parser.go
+++ b/internal/sensors/parser.go
@@ -204,9 +204,14 @@ func parseNVMeTemps(chipName string, chipMap map[string]interface{}, data *Tempe
// Look for Composite temperature (main NVMe temp)
if strings.Contains(sensorName, "Composite") {
if tempVal := extractTempInput(sensorMap); !math.IsNaN(tempVal) {
- data.NVMe[chipName] = tempVal
+ // Normalize chip name to nvme0, nvme1 format
+ // Input format is like "nvme-pci-0200" or "nvme-pci-0300"
+ // We extract the device index based on how many NVMe devices we've seen
+ normalizedName := fmt.Sprintf("nvme%d", len(data.NVMe))
+ data.NVMe[normalizedName] = tempVal
log.Debug().
Str("chip", chipName).
+ Str("normalizedName", normalizedName).
Float64("temp", tempVal).
Msg("Found NVMe temperature")
}
diff --git a/pkg/agents/host/report.go b/pkg/agents/host/report.go
index 1452cc81d4..26e6fee611 100644
--- a/pkg/agents/host/report.go
+++ b/pkg/agents/host/report.go
@@ -12,6 +12,7 @@ type Report struct {
Network []NetworkInterface `json:"network,omitempty"`
Sensors Sensors `json:"sensors,omitempty"`
RAID []RAIDArray `json:"raid,omitempty"`
+ Ceph *CephCluster `json:"ceph,omitempty"`
Tags []string `json:"tags,omitempty"`
Timestamp time.Time `json:"timestamp"`
SequenceID string `json:"sequenceId,omitempty"`
@@ -125,3 +126,103 @@ type RAIDDevice struct {
State string `json:"state"` // active, spare, faulty, removed
Slot int `json:"slot"` // Position in array (-1 if not applicable)
}
+
+// CephCluster represents Ceph cluster status collected by the host agent.
+type CephCluster struct {
+ FSID string `json:"fsid"`
+ Health CephHealth `json:"health"`
+ MonMap CephMonitorMap `json:"monMap,omitempty"`
+ MgrMap CephManagerMap `json:"mgrMap,omitempty"`
+ OSDMap CephOSDMap `json:"osdMap"`
+ PGMap CephPGMap `json:"pgMap"`
+ Pools []CephPool `json:"pools,omitempty"`
+ Services []CephService `json:"services,omitempty"`
+ CollectedAt string `json:"collectedAt"`
+}
+
+// CephHealth represents Ceph cluster health status.
+type CephHealth struct {
+ Status string `json:"status"` // HEALTH_OK, HEALTH_WARN, HEALTH_ERR
+ Checks map[string]CephCheck `json:"checks,omitempty"`
+ Summary []CephHealthSummary `json:"summary,omitempty"`
+}
+
+// CephCheck represents a health check detail.
+type CephCheck struct {
+ Severity string `json:"severity"`
+ Message string `json:"message,omitempty"`
+ Detail []string `json:"detail,omitempty"`
+}
+
+// CephHealthSummary represents a health summary message.
+type CephHealthSummary struct {
+ Severity string `json:"severity"`
+ Message string `json:"message"`
+}
+
+// CephMonitorMap represents Ceph monitor information.
+type CephMonitorMap struct {
+ Epoch int `json:"epoch"`
+ NumMons int `json:"numMons"`
+ Monitors []CephMonitor `json:"monitors,omitempty"`
+}
+
+// CephMonitor represents a single Ceph monitor.
+type CephMonitor struct {
+ Name string `json:"name"`
+ Rank int `json:"rank"`
+ Addr string `json:"addr,omitempty"`
+ Status string `json:"status,omitempty"`
+}
+
+// CephManagerMap represents Ceph manager information.
+type CephManagerMap struct {
+ Available bool `json:"available"`
+ NumMgrs int `json:"numMgrs"`
+ ActiveMgr string `json:"activeMgr,omitempty"`
+ Standbys int `json:"standbys"`
+}
+
+// CephOSDMap represents OSD status summary.
+type CephOSDMap struct {
+ Epoch int `json:"epoch"`
+ NumOSDs int `json:"numOsds"`
+ NumUp int `json:"numUp"`
+ NumIn int `json:"numIn"`
+ NumDown int `json:"numDown,omitempty"`
+ NumOut int `json:"numOut,omitempty"`
+}
+
+// CephPGMap represents placement group statistics.
+type CephPGMap struct {
+ NumPGs int `json:"numPgs"`
+ BytesTotal uint64 `json:"bytesTotal"`
+ BytesUsed uint64 `json:"bytesUsed"`
+ BytesAvailable uint64 `json:"bytesAvailable"`
+ DataBytes uint64 `json:"dataBytes,omitempty"`
+ UsagePercent float64 `json:"usagePercent"`
+ DegradedRatio float64 `json:"degradedRatio,omitempty"`
+ MisplacedRatio float64 `json:"misplacedRatio,omitempty"`
+ ReadBytesPerSec uint64 `json:"readBytesPerSec,omitempty"`
+ WriteBytesPerSec uint64 `json:"writeBytesPerSec,omitempty"`
+ ReadOpsPerSec uint64 `json:"readOpsPerSec,omitempty"`
+ WriteOpsPerSec uint64 `json:"writeOpsPerSec,omitempty"`
+}
+
+// CephPool represents a Ceph pool.
+type CephPool struct {
+ ID int `json:"id"`
+ Name string `json:"name"`
+ BytesUsed uint64 `json:"bytesUsed"`
+ BytesAvailable uint64 `json:"bytesAvailable"`
+ Objects uint64 `json:"objects"`
+ PercentUsed float64 `json:"percentUsed"`
+}
+
+// CephService represents a Ceph service summary.
+type CephService struct {
+ Type string `json:"type"` // mon, mgr, osd, mds, rgw
+ Running int `json:"running"`
+ Total int `json:"total"`
+ Daemons []string `json:"daemons,omitempty"`
+}