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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
feat: enhance API handlers and router with new endpoints
rcourtman8bf31214f54f
diff --git a/internal/api/ai_handler.go b/internal/api/ai_handler.goindex 47210da84a..0cc771b213 100644--- a/internal/api/ai_handler.go+++ b/internal/api/ai_handler.go@@ -316,11 +316,34 @@ func (h *AIHandler) Stop(ctx context.Context) error {// Restart restarts the AI chat service with updated configuration// Call this when model or other settings changefunc (h *AIHandler) Restart(ctx context.Context) error {- if h.legacyService == nil || !h.legacyService.IsRunning() {- return nil // Not running, nothing to restart- }// Load fresh config from persistence to get latest settingsnewCfg := h.loadAIConfig(ctx)++ if h.legacyService == nil {+ return nil+ }++ if !h.legacyService.IsRunning() {+ // If not running but enabled, try to start+ if newCfg != nil && newCfg.Enabled {+ log.Info().Msg("Starting AI service via restart trigger")++ // We need a state provider to start+ // Try to get default state provider from existing map if available+ var sp AIStateProvider+ h.stateProvidersMu.RLock()+ for _, p := range h.stateProviders {+ sp = p+ break+ }+ h.stateProvidersMu.RUnlock()++ // Reuse start logic+ return h.Start(ctx, sp)+ }+ return nil // Not running and not enabled, nothing to do+ }+return h.legacyService.Restart(ctx, newCfg)}diff --git a/internal/api/ai_handlers.go b/internal/api/ai_handlers.goindex 7bd09f4953..16ef3d84c4 100644--- a/internal/api/ai_handlers.go+++ b/internal/api/ai_handlers.go@@ -493,7 +493,7 @@ type AISettingsResponse struct {// Request timeout (seconds) - for slow hardware running local modelsRequestTimeoutSeconds int `json:"request_timeout_seconds,omitempty"`// Infrastructure control settings- ControlLevel string `json:"control_level"` // "read_only", "suggest", "controlled", "autonomous"+ ControlLevel string `json:"control_level"` // "read_only", "controlled", "autonomous"ProtectedGuests []string `json:"protected_guests,omitempty"` // VMIDs/names that AI cannot control}@@ -534,7 +534,7 @@ type AISettingsUpdateRequest struct {// Request timeout (seconds) - for slow hardware running local modelsRequestTimeoutSeconds *int `json:"request_timeout_seconds,omitempty"`// Infrastructure control settings- ControlLevel *string `json:"control_level,omitempty"` // "read_only", "suggest", "controlled", "autonomous"+ ControlLevel *string `json:"control_level,omitempty"` // "read_only", "controlled", "autonomous"ProtectedGuests []string `json:"protected_guests,omitempty"` // VMIDs/names that AI cannot control (nil = don't update, empty = clear)}@@ -742,7 +742,7 @@ func (h *AISettingsHandler) HandleUpdateAISettings(w http.ResponseWriter, r *httsettings.AutonomousMode = true} else if settings.GetControlLevel() == config.ControlLevelAutonomous {// Only downgrade from autonomous to controlled; preserve other levels- // (e.g., don't change read_only or suggest to controlled)+ // (e.g., don't change read_only to controlled)settings.ControlLevel = config.ControlLevelControlledsettings.AutonomousMode = false}@@ -897,12 +897,16 @@ func (h *AISettingsHandler) HandleUpdateAISettings(w http.ResponseWriter, r *htt// Handle infrastructure control settingsif req.ControlLevel != nil {- if !config.IsValidControlLevel(*req.ControlLevel) {- http.Error(w, "invalid control_level: must be read_only, suggest, controlled, or autonomous", http.StatusBadRequest)+ level := strings.TrimSpace(*req.ControlLevel)+ if level == "suggest" {+ level = config.ControlLevelControlled+ }+ if !config.IsValidControlLevel(level) {+ http.Error(w, "invalid control_level: must be read_only, controlled, or autonomous", http.StatusBadRequest)return}// "autonomous" requires Pro license (same as autonomous_mode)- if *req.ControlLevel == config.ControlLevelAutonomous {+ if level == config.ControlLevelAutonomous {if !h.GetAIService(r.Context()).HasLicenseFeature(ai.FeatureAIAutoFix) {w.Header().Set("Content-Type", "application/json")w.WriteHeader(http.StatusPaymentRequired)@@ -915,9 +919,9 @@ func (h *AISettingsHandler) HandleUpdateAISettings(w http.ResponseWriter, r *httreturn}}- settings.ControlLevel = *req.ControlLevel+ settings.ControlLevel = level// Keep legacy AutonomousMode in sync to prevent fallback issues- settings.AutonomousMode = (*req.ControlLevel == config.ControlLevelAutonomous)+ settings.AutonomousMode = (level == config.ControlLevelAutonomous)}// Handle protected guests (nil = don't update)@@ -947,7 +951,9 @@ func (h *AISettingsHandler) HandleUpdateAISettings(w http.ResponseWriter, r *htt// Trigger AI chat service restart if model changed// This ensures the new model is picked up by the service- if h.onModelChange != nil && (req.Model != nil || req.ChatModel != nil) {+ // Trigger AI chat service restart if model changed or AI enabled+ // This ensures the new model is picked up by the service+ if h.onModelChange != nil && (req.Model != nil || req.ChatModel != nil || req.Enabled != nil) {h.onModelChange()}@@ -2274,6 +2280,11 @@ func (h *AISettingsHandler) SetAlertProvider(ap ai.AlertProvider) {h.GetAIService(context.Background()).SetAlertProvider(ap)}+// SetAlertResolver sets the alert resolver for AI Patrol autonomous alert management+func (h *AISettingsHandler) SetAlertResolver(resolver ai.AlertResolver) {+ h.GetAIService(context.Background()).SetAlertResolver(resolver)+}+// oauthSessions stores active OAuth sessions (state -> session)// In production, consider using a more robust session store with expiryvar oauthSessions = make(map[string]*providers.OAuthSession)diff --git a/internal/api/diagnostics.go b/internal/api/diagnostics.goindex 3bab92a018..0d907e2f67 100644--- a/internal/api/diagnostics.go+++ b/internal/api/diagnostics.go@@ -29,18 +29,19 @@ import (// DiagnosticsInfo contains comprehensive diagnostic informationtype DiagnosticsInfo struct {- Version string `json:"version"`- Runtime string `json:"runtime"`- Uptime float64 `json:"uptime"`- Nodes []NodeDiagnostic `json:"nodes"`- PBS []PBSDiagnostic `json:"pbs"`- System SystemDiagnostic `json:"system"`- Discovery *DiscoveryDiagnostic `json:"discovery,omitempty"`- APITokens *APITokenDiagnostic `json:"apiTokens,omitempty"`- DockerAgents *DockerAgentDiagnostic `json:"dockerAgents,omitempty"`- Alerts *AlertsDiagnostic `json:"alerts,omitempty"`- AIChat *AIChatDiagnostic `json:"aiChat,omitempty"`- Errors []string `json:"errors"`+ Version string `json:"version"`+ Runtime string `json:"runtime"`+ Uptime float64 `json:"uptime"`+ Nodes []NodeDiagnostic `json:"nodes"`+ PBS []PBSDiagnostic `json:"pbs"`+ System SystemDiagnostic `json:"system"`+ MetricsStore *MetricsStoreDiagnostic `json:"metricsStore,omitempty"`+ Discovery *DiscoveryDiagnostic `json:"discovery,omitempty"`+ APITokens *APITokenDiagnostic `json:"apiTokens,omitempty"`+ DockerAgents *DockerAgentDiagnostic `json:"dockerAgents,omitempty"`+ Alerts *AlertsDiagnostic `json:"alerts,omitempty"`+ AIChat *AIChatDiagnostic `json:"aiChat,omitempty"`+ Errors []string `json:"errors"`// NodeSnapshots captures the raw memory payload and derived usage Pulse last observed per node.NodeSnapshots []monitoring.NodeMemorySnapshot `json:"nodeSnapshots,omitempty"`// GuestSnapshots captures recent per-guest memory breakdowns (VM/LXC) with the raw Proxmox fields.@@ -88,6 +89,21 @@ type MemorySourceStat struct {Fallback bool `json:"fallback"`}+// MetricsStoreDiagnostic summarizes metrics store health and data availability.+type MetricsStoreDiagnostic struct {+ Enabled bool `json:"enabled"`+ Status string `json:"status"`+ DBSize int64 `json:"dbSize,omitempty"`+ RawCount int64 `json:"rawCount,omitempty"`+ MinuteCount int64 `json:"minuteCount,omitempty"`+ HourlyCount int64 `json:"hourlyCount,omitempty"`+ DailyCount int64 `json:"dailyCount,omitempty"`+ TotalPoints int64 `json:"totalPoints,omitempty"`+ BufferSize int `json:"bufferSize,omitempty"`+ Notes []string `json:"notes,omitempty"`+ Error string `json:"error,omitempty"`+}+func isFallbackMemorySource(source string) bool {switch strings.ToLower(source) {case "", "unknown", "nodes-endpoint", "node-status-used", "previous-snapshot":@@ -97,6 +113,52 @@ func isFallbackMemorySource(source string) bool {}}+func buildMetricsStoreDiagnostic(monitor *monitoring.Monitor) *MetricsStoreDiagnostic {+ if monitor == nil {+ return &MetricsStoreDiagnostic{+ Enabled: false,+ Status: "unavailable",+ Error: "monitor not initialized",+ }+ }++ store := monitor.GetMetricsStore()+ if store == nil {+ return &MetricsStoreDiagnostic{+ Enabled: false,+ Status: "unavailable",+ Error: "metrics store not initialized",+ }+ }++ stats := store.GetStats()+ total := stats.RawCount + stats.MinuteCount + stats.HourlyCount + stats.DailyCount+ status := "healthy"+ notes := []string{}++ switch {+ case total == 0 && stats.BufferSize > 0:+ status = "buffering"+ notes = append(notes, "Metrics are buffered but not yet flushed")+ case total == 0:+ status = "empty"+ notes = append(notes, "No historical metrics written yet")+ }++ return &MetricsStoreDiagnostic{+ Enabled: true,+ Status: status,+ DBSize: stats.DBSize,+ RawCount: stats.RawCount,+ MinuteCount: stats.MinuteCount,+ HourlyCount: stats.HourlyCount,+ DailyCount: stats.DailyCount,+ TotalPoints: total,+ BufferSize: stats.BufferSize,+ Notes: notes,+ }+}+const diagnosticsCacheTTL = 45 * time.Secondvar (@@ -390,6 +452,7 @@ func (r *Router) computeDiagnostics(ctx context.Context) DiagnosticsInfo {}diag.APITokens = buildAPITokenDiagnostic(r.config, r.monitor)+ diag.MetricsStore = buildMetricsStoreDiagnostic(r.monitor)// Test each configured nodefor _, node := range r.config.PVEInstances {diff --git a/internal/api/router.go b/internal/api/router.goindex d51b6d0d17..6360bc5307 100644--- a/internal/api/router.go+++ b/internal/api/router.go@@ -1219,8 +1219,11 @@ func (r *Router) setupRoutes() {if r.monitor != nil {r.aiSettingsHandler.SetStateProvider(r.monitor)// Inject alert provider so AI has awareness of current alerts+ // Also inject alert resolver so AI Patrol can autonomously resolve alerts when issues are fixedif alertManager := r.monitor.GetAlertManager(); alertManager != nil {- r.aiSettingsHandler.SetAlertProvider(ai.NewAlertManagerAdapter(alertManager))+ alertAdapter := ai.NewAlertManagerAdapter(alertManager)+ r.aiSettingsHandler.SetAlertProvider(alertAdapter)+ r.aiSettingsHandler.SetAlertResolver(alertAdapter)}if incidentStore := r.monitor.GetIncidentStore(); incidentStore != nil {r.aiSettingsHandler.SetIncidentStore(incidentStore)@@ -4280,16 +4283,6 @@ func (r *Router) handleMetricsHistory(w http.ResponseWriter, req *http.Request)return}- store := r.monitor.GetMetricsStore()- if store == nil {- w.Header().Set("Content-Type", "application/json")- w.WriteHeader(http.StatusServiceUnavailable)- json.NewEncoder(w).Encode(map[string]interface{}{- "error": "Persistent metrics store not available",- })- return- }-query := req.URL.Query()resourceType := query.Get("resourceType")resourceID := query.Get("resourceId")@@ -4303,42 +4296,58 @@ func (r *Router) handleMetricsHistory(w http.ResponseWriter, req *http.Request)// Parse time rangevar duration time.Duration- var stepSecs int64 = 0 // Default to no downsampling (raw)+ var stepSecs int64 = 0 // Default to no downsampling (use tier resolution)switch timeRange {case "1h":duration = time.Hour- stepSecs = 0 // Raw for 1hcase "6h":duration = 6 * time.Hour- stepSecs = 60 // 1m for 6h (~360 points)case "12h":duration = 12 * time.Hour- stepSecs = 120 // 2m for 12h (~360 points)case "24h", "1d", "":duration = 24 * time.Hour- stepSecs = 600 // 10m for 24h (~144 points)case "7d":duration = 7 * 24 * time.Hour- stepSecs = 3600 // 1h for 7d (~168 points)case "30d":duration = 30 * 24 * time.Hour- stepSecs = 14400 // 4h for 30d (~180 points)case "90d":duration = 90 * 24 * time.Hour- stepSecs = 43200 // 12h for 90d (~180 points)default:// Try parsing as durationvar err errorduration, err = time.ParseDuration(timeRange)if err != nil {duration = 24 * time.Hour // Default to 24 hours- stepSecs = 600- } else {- // Dynamic step for custom durations: target ~200 points- stepSecs = int64(duration.Seconds()) / 200- if stepSecs < 60 {- stepSecs = 0 // No downsampling if very short+ }+ }++ // Optional downsampling based on requested max points.+ // When omitted, we return the native tier resolution.+ if maxPointsStr := query.Get("maxPoints"); maxPointsStr != "" {+ if maxPoints, err := strconv.Atoi(maxPointsStr); err == nil && maxPoints > 0 {+ durationSecs := int64(duration.Seconds())+ if durationSecs > 0 {+ stepSecs = (durationSecs + int64(maxPoints) - 1) / int64(maxPoints)+ if stepSecs <= 1 {+ stepSecs = 0+ } else {+ minStep := func(d time.Duration) int64 {+ switch {+ case d <= 2*time.Hour:+ return 5+ case d <= 24*time.Hour:+ return 60+ case d <= 7*24*time.Hour:+ return 3600+ default:+ return 86400+ }+ }+ if stepSecs < minStep(duration) {+ stepSecs = 0+ }+ }}}}@@ -4363,9 +4372,435 @@ func (r *Router) handleMetricsHistory(w http.ResponseWriter, req *http.Request)end := time.Now()start := end.Add(-duration)+ const (+ historySourceStore = "store"+ historySourceMemory = "memory"+ historySourceLive = "live"+ )++ fallbackAllowed := duration <= 24*time.Hour+ buildHistoryPoints := func(points []monitoring.MetricPoint, bucketSecs int64) []map[string]interface{} {+ if len(points) == 0 {+ return []map[string]interface{}{}+ }+ if bucketSecs <= 1 {+ apiPoints := make([]map[string]interface{}, 0, len(points))+ for _, p := range points {+ apiPoints = append(apiPoints, map[string]interface{}{+ "timestamp": p.Timestamp.UnixMilli(),+ "value": p.Value,+ "min": p.Value,+ "max": p.Value,+ })+ }+ return apiPoints+ }++ type bucket struct {+ sum float64+ count int+ min float64+ max float64+ }++ buckets := make(map[int64]*bucket)+ for _, p := range points {+ ts := p.Timestamp.Unix()+ if ts <= 0 {+ continue+ }+ start := (ts / bucketSecs) * bucketSecs+ b, ok := buckets[start]+ if !ok {+ b = &bucket{+ sum: p.Value,+ count: 1,+ min: p.Value,+ max: p.Value,+ }+ buckets[start] = b+ continue+ }+ b.sum += p.Value+ b.count+++ if p.Value < b.min {+ b.min = p.Value+ }+ if p.Value > b.max {+ b.max = p.Value+ }+ }++ keys := make([]int64, 0, len(buckets))+ for k := range buckets {+ keys = append(keys, k)+ }+ sort.Slice(keys, func(i, j int) bool { return keys[i] < keys[j] })++ apiPoints := make([]map[string]interface{}, 0, len(keys))+ for _, k := range keys {+ b := buckets[k]+ if b.count == 0 {+ continue+ }+ ts := time.Unix(k+(bucketSecs/2), 0)+ apiPoints = append(apiPoints, map[string]interface{}{+ "timestamp": ts.UnixMilli(),+ "value": b.sum / float64(b.count),+ "min": b.min,+ "max": b.max,+ })+ }+ return apiPoints+ }+ state := r.monitor.GetState()++ parseGuestID := func(id string) (string, string, int, bool) {+ parts := strings.Split(id, ":")+ if len(parts) != 3 {+ return "", "", 0, false+ }+ vmid, err := strconv.Atoi(parts[2])+ if err != nil {+ return "", "", 0, false+ }+ return parts[0], parts[1], vmid, true+ }++ findVM := func(id string) *models.VM {+ for i := range state.VMs {+ if state.VMs[i].ID == id {+ return &state.VMs[i]+ }+ }+ if instance, node, vmid, ok := parseGuestID(id); ok {+ for i := range state.VMs {+ vm := &state.VMs[i]+ if vm.VMID == vmid && vm.Node == node && vm.Instance == instance {+ return vm+ }+ }+ }+ return nil+ }++ findContainer := func(id string) *models.Container {+ for i := range state.Containers {+ if state.Containers[i].ID == id {+ return &state.Containers[i]+ }+ }+ if instance, node, vmid, ok := parseGuestID(id); ok {+ for i := range state.Containers {+ ct := &state.Containers[i]+ if ct.VMID == vmid && ct.Node == node && ct.Instance == instance {+ return ct+ }+ }+ }+ return nil+ }++ findNode := func(id string) *models.Node {+ for i := range state.Nodes {+ if state.Nodes[i].ID == id {+ return &state.Nodes[i]+ }+ }+ return nil+ }++ findStorage := func(id string) *models.Storage {+ for i := range state.Storage {+ if state.Storage[i].ID == id {+ return &state.Storage[i]+ }+ }+ return nil+ }++ findDockerHost := func(id string) *models.DockerHost {+ for i := range state.DockerHosts {+ if state.DockerHosts[i].ID == id {+ return &state.DockerHosts[i]+ }+ }+ return nil+ }++ findDockerContainer := func(id string) *models.DockerContainer {+ for i := range state.DockerHosts {+ host := &state.DockerHosts[i]+ for j := range host.Containers {+ if host.Containers[j].ID == id {+ return &host.Containers[j]+ }+ }+ }+ return nil+ }++ liveMetricPoints := func(resourceType, resourceID string) map[string]monitoring.MetricPoint {+ now := time.Now()+ points := make(map[string]monitoring.MetricPoint)++ switch resourceType {+ case "vm", "guest":+ vm := findVM(resourceID)+ if vm == nil {+ return points+ }+ points["cpu"] = monitoring.MetricPoint{Timestamp: now, Value: vm.CPU * 100}+ points["memory"] = monitoring.MetricPoint{Timestamp: now, Value: vm.Memory.Usage}+ if vm.Disk.Usage >= 0 {+ points["disk"] = monitoring.MetricPoint{Timestamp: now, Value: vm.Disk.Usage}+ }+ points["diskread"] = monitoring.MetricPoint{Timestamp: now, Value: float64(vm.DiskRead)}+ points["diskwrite"] = monitoring.MetricPoint{Timestamp: now, Value: float64(vm.DiskWrite)}+ points["netin"] = monitoring.MetricPoint{Timestamp: now, Value: float64(vm.NetworkIn)}+ points["netout"] = monitoring.MetricPoint{Timestamp: now, Value: float64(vm.NetworkOut)}+ case "container":+ ct := findContainer(resourceID)+ if ct == nil {+ return points+ }+ points["cpu"] = monitoring.MetricPoint{Timestamp: now, Value: ct.CPU * 100}+ points["memory"] = monitoring.MetricPoint{Timestamp: now, Value: ct.Memory.Usage}+ if ct.Disk.Usage >= 0 {+ points["disk"] = monitoring.MetricPoint{Timestamp: now, Value: ct.Disk.Usage}+ }+ points["diskread"] = monitoring.MetricPoint{Timestamp: now, Value: float64(ct.DiskRead)}+ points["diskwrite"] = monitoring.MetricPoint{Timestamp: now, Value: float64(ct.DiskWrite)}+ points["netin"] = monitoring.MetricPoint{Timestamp: now, Value: float64(ct.NetworkIn)}+ points["netout"] = monitoring.MetricPoint{Timestamp: now, Value: float64(ct.NetworkOut)}+ case "node":+ node := findNode(resourceID)+ if node == nil {+ return points+ }+ points["cpu"] = monitoring.MetricPoint{Timestamp: now, Value: node.CPU * 100}+ points["memory"] = monitoring.MetricPoint{Timestamp: now, Value: node.Memory.Usage}+ points["disk"] = monitoring.MetricPoint{Timestamp: now, Value: node.Disk.Usage}+ case "storage":+ storage := findStorage(resourceID)+ if storage == nil {+ return points+ }+ usagePercent := float64(0)+ if storage.Total > 0 {+ usagePercent = (float64(storage.Used) / float64(storage.Total)) * 100+ }+ points["disk"] = monitoring.MetricPoint{Timestamp: now, Value: usagePercent}+ points["usage"] = monitoring.MetricPoint{Timestamp: now, Value: usagePercent}+ points["used"] = monitoring.MetricPoint{Timestamp: now, Value: float64(storage.Used)}+ points["total"] = monitoring.MetricPoint{Timestamp: now, Value: float64(storage.Total)}+ points["avail"] = monitoring.MetricPoint{Timestamp: now, Value: float64(storage.Free)}+ case "dockerHost":+ host := findDockerHost(resourceID)+ if host == nil {+ return points+ }+ points["cpu"] = monitoring.MetricPoint{Timestamp: now, Value: host.CPUUsage}+ points["memory"] = monitoring.MetricPoint{Timestamp: now, Value: host.Memory.Usage}+ diskPercent := float64(0)+ if len(host.Disks) > 0 {+ diskPercent = host.Disks[0].Usage+ }+ points["disk"] = monitoring.MetricPoint{Timestamp: now, Value: diskPercent}+ case "docker", "dockerContainer":+ container := findDockerContainer(resourceID)+ if container == nil {+ return points+ }+ points["cpu"] = monitoring.MetricPoint{Timestamp: now, Value: container.CPUPercent}+ points["memory"] = monitoring.MetricPoint{Timestamp: now, Value: container.MemoryPercent}+ if container.RootFilesystemBytes > 0 && container.WritableLayerBytes > 0 {+ diskPercent := float64(container.WritableLayerBytes) / float64(container.RootFilesystemBytes) * 100+ if diskPercent > 100 {+ diskPercent = 100+ }+ points["disk"] = monitoring.MetricPoint{Timestamp: now, Value: diskPercent}+ }+ }++ return points+ }++ fallbackSingle := func() ([]map[string]interface{}, string, bool) {+ if !fallbackAllowed || metricType == "" {+ return nil, "", false+ }++ switch resourceType {+ case "vm", "container", "guest":+ metrics := r.monitor.GetGuestMetrics(resourceID, duration)+ points := metrics[metricType]+ if len(points) == 0 {+ livePoints := liveMetricPoints(resourceType, resourceID)+ if live, ok := livePoints[metricType]; ok {+ return buildHistoryPoints([]monitoring.MetricPoint{live}, 0), historySourceLive, true+ }+ return nil, "", false+ }+ return buildHistoryPoints(points, stepSecs), historySourceMemory, true+ case "dockerHost":+ metrics := r.monitor.GetGuestMetrics(fmt.Sprintf("dockerHost:%s", resourceID), duration)+ points := metrics[metricType]+ if len(points) == 0 {+ livePoints := liveMetricPoints(resourceType, resourceID)+ if live, ok := livePoints[metricType]; ok {+ return buildHistoryPoints([]monitoring.MetricPoint{live}, 0), historySourceLive, true+ }+ return nil, "", false+ }+ return buildHistoryPoints(points, stepSecs), historySourceMemory, true+ case "docker", "dockerContainer":+ metrics := r.monitor.GetGuestMetrics(fmt.Sprintf("docker:%s", resourceID), duration)+ points := metrics[metricType]+ if len(points) == 0 {+ livePoints := liveMetricPoints(resourceType, resourceID)+ if live, ok := livePoints[metricType]; ok {+ return buildHistoryPoints([]monitoring.MetricPoint{live}, 0), historySourceLive, true+ }+ return nil, "", false+ }+ return buildHistoryPoints(points, stepSecs), historySourceMemory, true+ case "node":+ points := r.monitor.GetNodeMetrics(resourceID, metricType, duration)+ if len(points) == 0 {+ livePoints := liveMetricPoints(resourceType, resourceID)+ if live, ok := livePoints[metricType]; ok {+ return buildHistoryPoints([]monitoring.MetricPoint{live}, 0), historySourceLive, true+ }+ return nil, "", false+ }+ return buildHistoryPoints(points, stepSecs), historySourceMemory, true+ case "storage":+ metrics := r.monitor.GetStorageMetrics(resourceID, duration)+ points := metrics[metricType]+ if len(points) == 0 {+ livePoints := liveMetricPoints(resourceType, resourceID)+ if live, ok := livePoints[metricType]; ok {+ return buildHistoryPoints([]monitoring.MetricPoint{live}, 0), historySourceLive, true+ }+ return nil, "", false+ }+ return buildHistoryPoints(points, stepSecs), historySourceMemory, true+ default:+ livePoints := liveMetricPoints(resourceType, resourceID)+ if live, ok := livePoints[metricType]; ok {+ return buildHistoryPoints([]monitoring.MetricPoint{live}, 0), historySourceLive, true+ }+ return nil, "", false+ }+ }++ fallbackAll := func() (map[string][]map[string]interface{}, string, bool) {+ if !fallbackAllowed || metricType != "" {+ return nil, "", false+ }++ var metrics map[string][]monitoring.MetricPoint+ switch resourceType {+ case "vm", "container", "guest":+ metrics = r.monitor.GetGuestMetrics(resourceID, duration)+ case "dockerHost":+ metrics = r.monitor.GetGuestMetrics(fmt.Sprintf("dockerHost:%s", resourceID), duration)+ case "docker", "dockerContainer":+ metrics = r.monitor.GetGuestMetrics(fmt.Sprintf("docker:%s", resourceID), duration)+ case "storage":+ metrics = r.monitor.GetStorageMetrics(resourceID, duration)+ default:+ if resourceType == "node" {+ metrics = map[string][]monitoring.MetricPoint{+ "cpu": r.monitor.GetNodeMetrics(resourceID, "cpu", duration),+ "memory": r.monitor.GetNodeMetrics(resourceID, "memory", duration),+ "disk": r.monitor.GetNodeMetrics(resourceID, "disk", duration),+ }+ } else {+ return nil, "", false+ }+ }++ apiData := make(map[string][]map[string]interface{})+ source := historySourceMemory+ for metric, points := range metrics {+ if len(points) == 0 {+ continue+ }+ apiData[metric] = buildHistoryPoints(points, stepSecs)+ }+ if len(apiData) == 0 {+ livePoints := liveMetricPoints(resourceType, resourceID)+ for metric, point := range livePoints {+ apiData[metric] = buildHistoryPoints([]monitoring.MetricPoint{point}, 0)+ }+ source = historySourceLive+ }+ if len(apiData) == 0 {+ return nil, "", false+ }+ return apiData, source, true+ }++ store := r.monitor.GetMetricsStore()+ if store == nil {+ if metricType != "" {+ if apiPoints, source, ok := fallbackSingle(); ok {+ log.Warn().+ Str("resourceType", resourceType).+ Str("resourceId", resourceID).+ Str("metric", metricType).+ Str("source", source).+ Msg("Metrics store unavailable; serving history from fallback source")+ response := map[string]interface{}{+ "resourceType": resourceType,+ "resourceId": resourceID,+ "metric": metricType,+ "range": timeRange,+ "start": start.UnixMilli(),+ "end": end.UnixMilli(),+ "points": apiPoints,+ "source": source,+ }+ w.Header().Set("Content-Type", "application/json")+ json.NewEncoder(w).Encode(response)+ return+ }+ } else {+ if apiData, source, ok := fallbackAll(); ok {+ log.Warn().+ Str("resourceType", resourceType).+ Str("resourceId", resourceID).+ Str("source", source).+ Msg("Metrics store unavailable; serving history from fallback source")+ response := map[string]interface{}{+ "resourceType": resourceType,+ "resourceId": resourceID,+ "range": timeRange,+ "start": start.UnixMilli(),+ "end": end.UnixMilli(),+ "metrics": apiData,+ "source": source,+ }+ w.Header().Set("Content-Type", "application/json")+ json.NewEncoder(w).Encode(response)+ return+ }+ }++ w.Header().Set("Content-Type", "application/json")+ w.WriteHeader(http.StatusServiceUnavailable)+ json.NewEncoder(w).Encode(map[string]interface{}{+ "error": "Persistent metrics store not available",+ })+ return+ }+var response interface{}if metricType != "" {+ source := historySourceStore// Query single metric typepoints, err := store.Query(resourceType, resourceID, metricType, start, end, stepSecs)if err != nil {@@ -4378,27 +4813,53 @@ func (r *Router) handleMetricsHistory(w http.ResponseWriter, req *http.Request)return}- // Convert to frontend format (timestamps in milliseconds)- apiPoints := make([]map[string]interface{}, len(points))- for i, p := range points {- apiPoints[i] = map[string]interface{}{- "timestamp": p.Timestamp.UnixMilli(),- "value": p.Value,- "min": p.Min,- "max": p.Max,+ if len(points) == 0 {+ if apiPoints, fallbackSource, ok := fallbackSingle(); ok {+ source = fallbackSource+ log.Info().+ Str("resourceType", resourceType).+ Str("resourceId", resourceID).+ Str("metric", metricType).+ Str("source", source).+ Msg("Metrics store empty; serving history from fallback source")+ response = map[string]interface{}{+ "resourceType": resourceType,+ "resourceId": resourceID,+ "metric": metricType,+ "range": timeRange,+ "start": start.UnixMilli(),+ "end": end.UnixMilli(),+ "points": apiPoints,+ "source": source,+ }}}- response = map[string]interface{}{- "resourceType": resourceType,- "resourceId": resourceID,- "metric": metricType,- "range": timeRange,- "start": start.UnixMilli(),- "end": end.UnixMilli(),- "points": apiPoints,+ // Convert to frontend format (timestamps in milliseconds)+ if response == nil {+ apiPoints := make([]map[string]interface{}, len(points))+ for i, p := range points {+ apiPoints[i] = map[string]interface{}{+ "timestamp": p.Timestamp.UnixMilli(),+ "value": p.Value,+ "min": p.Min,+ "max": p.Max,+ }+ }++ response = map[string]interface{}{+ "resourceType": resourceType,+ "resourceId": resourceID,+ "metric": metricType,+ "range": timeRange,+ "start": start.UnixMilli(),+ "end": end.UnixMilli(),+ "points": apiPoints,+ "source": source,+ }}} else {+ source := historySourceStore// Query all metrics for this resourcemetricsMap, err := store.QueryAll(resourceType, resourceID, start, end, stepSecs)if err != nil {@@ -4410,28 +4871,51 @@ func (r *Router) handleMetricsHistory(w http.ResponseWriter, req *http.Request)return}- // Convert to frontend format- apiData := make(map[string][]map[string]interface{})- for metric, points := range metricsMap {- apiPoints := make([]map[string]interface{}, len(points))- for i, p := range points {- apiPoints[i] = map[string]interface{}{- "timestamp": p.Timestamp.UnixMilli(),- "value": p.Value,- "min": p.Min,- "max": p.Max,+ if len(metricsMap) == 0 {+ if apiData, fallbackSource, ok := fallbackAll(); ok {+ source = fallbackSource+ log.Info().+ Str("resourceType", resourceType).+ Str("resourceId", resourceID).+ Str("source", source).+ Msg("Metrics store empty; serving history from fallback source")+ response = map[string]interface{}{+ "resourceType": resourceType,+ "resourceId": resourceID,+ "range": timeRange,+ "start": start.UnixMilli(),+ "end": end.UnixMilli(),+ "metrics": apiData,+ "source": source,}}- apiData[metric] = apiPoints}- response = map[string]interface{}{- "resourceType": resourceType,- "resourceId": resourceID,- "range": timeRange,- "start": start.UnixMilli(),- "end": end.UnixMilli(),- "metrics": apiData,+ // Convert to frontend format+ if response == nil {+ apiData := make(map[string][]map[string]interface{})+ for metric, points := range metricsMap {+ apiPoints := make([]map[string]interface{}, len(points))+ for i, p := range points {+ apiPoints[i] = map[string]interface{}{+ "timestamp": p.Timestamp.UnixMilli(),+ "value": p.Value,+ "min": p.Min,+ "max": p.Max,+ }+ }+ apiData[metric] = apiPoints+ }++ response = map[string]interface{}{+ "resourceType": resourceType,+ "resourceId": resourceID,+ "range": timeRange,+ "start": start.UnixMilli(),+ "end": end.UnixMilli(),+ "metrics": apiData,+ "source": source,+ }}}