Files
Pulse/internal/monitoring/broadcast_projection_reference_test.go
rcourtman 876ce14ab7 Keep action refusals current on websocket rows
A Docker page open while a host's agent disconnected kept Stop and Restart
enabled, and the click then failed at plan time. After the agent came back,
the old refusal stayed until the page remounted. Only a REST read carried
refusals, and a mounted Docker page reads REST once. The websocket merge
kept that REST answer, so nothing overwrote it; it just never changed.

Every broadcast now runs the same availability check, and a refused row
carries a ref into a new actionReadinessCatalog. A row with no ref refuses
nothing, so the store and the merge take the row's own answer and a refusal
ends with the frame that ends it. A first paint from the websocket is right
too, which closes the moment after in-app navigation when refused actions
looked enabled.

Broadcast rows keep capabilities whole. The drawer's operator policy form
needs what a disconnected host offers, and a stable set means a disconnect
moves one ref per row.

A check runs per capability per resource, host and token lookups scan every
session, and each session check takes config.Mu, so one pass now answers each
lookup once. Planning and dispatch still read live. Refusals leave Detail off
the wire: it names one host's identity and would give every host its own
catalog entry.
2026-11-04 21:22:02 +00:00

105 lines
3.4 KiB
Go

package monitoring
import (
"encoding/json"
"github.com/rcourtman/pulse-go-rewrite/internal/models"
"github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
"sort"
"strings"
)
// Retain the pre-repair projection as a content oracle, including its second
// coalesce and input-first sorting. This is never production or persistence code.
func convertResourcesForBroadcastReference(
allResources []unifiedresources.Resource,
metricsTargetResolvers ...MetricsTargetResourceStore,
) ([]models.ResourceFrontend, broadcastResourceCatalogs) {
if len(allResources) == 0 {
return []models.ResourceFrontend{}, broadcastResourceCatalogs{}
}
allResources = attachBroadcastMetricsTargets(
allResources,
firstBroadcastMetricsTargetResolver(metricsTargetResolvers),
)
allResources = unifiedresources.CoalescePresentationHostResources(allResources)
type broadcastResource struct {
input models.ResourceConvertInput
sortKey string
resourceID string
}
converted := make([]broadcastResource, 0, len(allResources))
catalogs := broadcastResourceCatalogs{
capabilities: make(map[string]json.RawMessage),
policies: make(map[string]json.RawMessage),
aiSafeSummaries: make(map[string]string),
}
for _, r := range allResources {
input := monitorResourceToConvertInput(r)
// The parent map encoder independently checks the fixed wire records.
if input.Type == "vm" || input.Type == "system-container" || input.Type == "oci-container" {
input.PlatformData = parentProxmoxPlatformJSON(&r)
}
if len(input.Capabilities) > 0 {
id := capabilityCatalogID(input.Capabilities)
catalogs.capabilities[id] = input.Capabilities
input.CapabilitiesRef = id
input.Capabilities = nil
}
if encoded := broadcastActionRefusalsJSON(r.ActionReadiness); len(encoded) > 0 {
id := capabilityCatalogID(encoded)
if catalogs.actionReadiness == nil {
catalogs.actionReadiness = make(map[string]json.RawMessage)
}
catalogs.actionReadiness[id] = encoded
input.ActionReadinessRef = id
}
// Non-default policies and AI-safe summaries dedupe the same way:
// estates carry a handful of distinct postures and templated summary
// strings, so refs replace per-resource inline duplication.
if len(input.Policy) > 0 {
id := capabilityCatalogID(input.Policy)
catalogs.policies[id] = input.Policy
input.PolicyRef = id
input.Policy = nil
}
if input.AISafeSummary != "" {
id := capabilityCatalogID(json.RawMessage(input.AISafeSummary))
catalogs.aiSafeSummaries[id] = input.AISafeSummary
input.AISafeSummaryRef = id
input.AISafeSummary = ""
}
sortKey := strings.ToLower(input.DisplayName)
if sortKey == "" {
sortKey = strings.ToLower(input.Name)
}
converted = append(converted, broadcastResource{
input: input,
sortKey: sortKey,
resourceID: input.ID,
})
}
sort.Slice(converted, func(i, j int) bool {
if converted[i].sortKey == converted[j].sortKey {
return converted[i].resourceID < converted[j].resourceID
}
return converted[i].sortKey < converted[j].sortKey
})
result := make([]models.ResourceFrontend, len(converted))
for i, resource := range converted {
result[i] = models.ConvertResourceToFrontend(resource.input)
}
if len(catalogs.capabilities) == 0 {
catalogs.capabilities = nil
}
if len(catalogs.policies) == 0 {
catalogs.policies = nil
}
if len(catalogs.aiSafeSummaries) == 0 {
catalogs.aiSafeSummaries = nil
}
return result, catalogs
}