From cac109b497904b498ee2f073733a1ad017d0b294 Mon Sep 17 00:00:00 2001 From: rcourtman Date: Sun, 7 Dec 2025 14:47:29 +0000 Subject: [PATCH] fix: Populate resources on-demand when /api/resources is called The Resources page was showing 0 resources because the store was only populated when /api/state was called (from the dashboard). Now the resources are populated on-demand when /api/resources is accessed. Changes: - Added StateProvider interface to ResourceHandlers - SetStateProvider() method for injecting the monitor - HandleGetResources now calls PopulateFromSnapshot before querying - Router injects monitor as state provider during SetMonitor() This ensures the /resources page works even when accessed directly without visiting the main dashboard first. --- internal/api/resource_handlers.go | 19 ++++++++++++++++++- internal/api/router.go | 2 ++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/internal/api/resource_handlers.go b/internal/api/resource_handlers.go index a6ce5a2..c1cad68 100644 --- a/internal/api/resource_handlers.go +++ b/internal/api/resource_handlers.go @@ -9,9 +9,15 @@ import ( "github.com/rcourtman/pulse-go-rewrite/internal/resources" ) +// StateProvider allows ResourceHandlers to fetch current state on demand. +type StateProvider interface { + GetState() models.StateSnapshot +} + // ResourceHandlers provides HTTP handlers for the unified resource API. type ResourceHandlers struct { - store *resources.Store + store *resources.Store + stateProvider StateProvider } // NewResourceHandlers creates resource handlers with a new store. @@ -21,6 +27,11 @@ func NewResourceHandlers() *ResourceHandlers { } } +// SetStateProvider sets the state provider for on-demand resource population. +func (h *ResourceHandlers) SetStateProvider(provider StateProvider) { + h.stateProvider = provider +} + // Store returns the underlying resource store for populating from the monitor. func (h *ResourceHandlers) Store() *resources.Store { return h.store @@ -41,6 +52,12 @@ func (h *ResourceHandlers) HandleGetResources(w http.ResponseWriter, r *http.Req return } + // Populate from current state if we have a state provider + // This ensures fresh data even if the store hasn't been populated yet + if h.stateProvider != nil { + h.PopulateFromSnapshot(h.stateProvider.GetState()) + } + query := h.store.Query() // Parse type filter diff --git a/internal/api/router.go b/internal/api/router.go index e7f0cc3..cf0e6fb 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -1279,6 +1279,8 @@ func (r *Router) SetMonitor(m *monitoring.Monitor) { // Inject resource store for polling optimization if r.resourceHandlers != nil { m.SetResourceStore(r.resourceHandlers.Store()) + // Also set state provider for on-demand resource population + r.resourceHandlers.SetStateProvider(m) } } }