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.
This commit is contained in:
rcourtman 2025-12-07 14:47:29 +00:00
parent b99ba78c6e
commit cac109b497
2 changed files with 20 additions and 1 deletions

View file

@ -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

View file

@ -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)
}
}
}