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.

Preserve relationship endpoints in change timelines

rcourtman7e6a85548f6e
diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
index 73345f2e07..95c481439f 100644
--- a/docs/release-control/v6/internal/subsystems/api-contracts.md
+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
@@ -127,6 +127,10 @@ Those history reads now also accept governed `kind`, `sourceType`, and
`sourceAdapter` query filters, and the backend store owns the corresponding
filtered counts, so the timeline contract can narrow by change class and
adapter provenance without inventing a frontend-only slice of the graph.
+Relationship-change timeline entries also preserve the affected graph endpoints
+in `relatedResources`, so the canonical history keeps enough context for the
+detail drawer and audit views to explain which neighboring resources moved with
+the change.
Invalid `sourceAdapter` values are rejected at the API boundary, so the filter
contract stays aligned with the canonical adapter set rather than silently
falling back to an empty slice.
diff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.md
index ba091cce99..c5952d4d66 100644
--- a/docs/release-control/v6/internal/subsystems/unified-resources.md
+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md
@@ -84,6 +84,10 @@ registry rebuilds and supplemental ingest into `ResourceChange` records, and
`internal/unifiedresources/store.go` persists those changes so `RecentChanges`
can round-trip through the SQLite-backed resource store instead of living only
in memory or adapter-local state.
+Relationship-change timeline records now keep the affected graph endpoints in
+`relatedResources`, so the durable history preserves the same graph context the
+detail drawer can surface later instead of collapsing relationship changes down
+to parent-only hints.
That store also now migrates legacy `resource_changes` tables that still carry
the pre-v6 `timestamp` column by backfilling canonical `observed_at` values,
adding the newer `occurred_at` field, and preserving the legacy timestamp on
diff --git a/internal/api/contract_test.go b/internal/api/contract_test.go
index fda069a046..4d8a42a0a1 100644
--- a/internal/api/contract_test.go
+++ b/internal/api/contract_test.go
@@ -3747,6 +3747,67 @@ func TestContract_ResourceTimelineJSONSnapshot(t *testing.T) {
assertJSONSnapshot(t, got, want)
}
+func TestContract_ResourceTimelineRelationshipJSONSnapshot(t *testing.T) {
+ now := time.Date(2026, 3, 18, 17, 5, 0, 0, time.UTC)
+ payload := struct {
+ ResourceID string `json:"resourceId"`
+ RecentChanges []unifiedresources.ResourceChange `json:"recentChanges"`
+ Count int `json:"count"`
+ }{
+ ResourceID: "vm:42",
+ RecentChanges: []unifiedresources.ResourceChange{
+ {
+ ID: "chg-relationship-42",
+ ObservedAt: now,
+ OccurredAt: &now,
+ ResourceID: "vm:42",
+ Kind: unifiedresources.ChangeRelationship,
+ From: "node-1",
+ To: "node-2",
+ SourceType: unifiedresources.SourcePulseDiff,
+ SourceAdapter: unifiedresources.AdapterProxmox,
+ Confidence: unifiedresources.ConfidenceHigh,
+ RelatedResources: []string{"db:alpha", "service:beta"},
+ Reason: "dependency graph updated",
+ Metadata: map[string]any{
+ "edgeType": "depends_on",
+ "active": true,
+ },
+ },
+ },
+ Count: 1,
+ }
+
+ got, err := json.Marshal(payload)
+ if err != nil {
+ t.Fatalf("marshal resource timeline relationship response: %v", err)
+ }
+
+ const want = `{
+ "resourceId":"vm:42",
+ "recentChanges":[
+ {
+ "id":"chg-relationship-42",
+ "observedAt":"2026-03-18T17:05:00Z",
+ "occurredAt":"2026-03-18T17:05:00Z",
+ "resourceId":"vm:42",
+ "kind":"relationship_change",
+ "from":"node-1",
+ "to":"node-2",
+ "sourceType":"pulse_diff",
+ "sourceAdapter":"proxmox_adapter",
+ "confidence":"high",
+ "relatedResources":["db:alpha","service:beta"],
+ "reason":"dependency graph updated",
+ "metadata":{"active":true,"edgeType":"depends_on"}
+ }
+ ],
+ "count":1
+ }`
+
+ assertJSONSnapshot(t, got, want)
+}
+
func TestContract_ResourceFacetsJSONSnapshot(t *testing.T) {
now := time.Date(2026, 3, 18, 17, 0, 0, 0, time.UTC)
payload := struct {
diff --git a/internal/unifiedresources/change_emission.go b/internal/unifiedresources/change_emission.go
index 8473b09a96..ecb5686b49 100644
--- a/internal/unifiedresources/change_emission.go
+++ b/internal/unifiedresources/change_emission.go
@@ -117,7 +117,7 @@ func buildResourceChange(before Resource, beforeOK bool, after Resource, afterOK
change.From = resourceRelationSummary(before)
change.To = resourceRelationSummary(after)
change.Reason = "resource relationship changed"
- change.RelatedResources = relatedResourceIDs(before, after)
+ change.RelatedResources = relatedResourceIDs(change.ResourceID, before, after)
case !reflect.DeepEqual(before.Capabilities, after.Capabilities):
change.Kind = ChangeCapability
change.From = fmt.Sprintf("%d", len(before.Capabilities))
@@ -155,6 +155,12 @@ func resourceChangedFields(before, after Resource) []string {
if !equalStringPtr(before.ParentID, after.ParentID) {
changed = append(changed, "parentId")
}
+ if !reflect.DeepEqual(before.Relationships, after.Relationships) {
+ changed = append(changed, "relationships")
+ }
+ if !reflect.DeepEqual(before.Capabilities, after.Capabilities) {
+ changed = append(changed, "capabilities")
+ }
if !sameStringSet(before.Tags, after.Tags) {
changed = append(changed, "tags")
}
@@ -262,12 +268,13 @@ func resourceStatusString(status ResourceStatus) string {
return strings.TrimSpace(string(status))
}
-func relatedResourceIDs(before, after Resource) []string {
+func relatedResourceIDs(primaryID string, before, after Resource) []string {
+ primaryID = CanonicalResourceID(primaryID)
seen := make(map[string]struct{}, 2)
var out []string
appendID := func(id string) {
id = CanonicalResourceID(id)
- if id == "" {
+ if id == "" || id == primaryID {
return
}
if _, ok := seen[id]; ok {
@@ -282,6 +289,14 @@ func relatedResourceIDs(before, after Resource) []string {
if after.ParentID != nil {
appendID(*after.ParentID)
}
+ for _, relationship := range before.Relationships {
+ appendID(relationship.SourceID)
+ appendID(relationship.TargetID)
+ }
+ for _, relationship := range after.Relationships {
+ appendID(relationship.SourceID)
+ appendID(relationship.TargetID)
+ }
return out
}
diff --git a/internal/unifiedresources/change_emission_test.go b/internal/unifiedresources/change_emission_test.go
new file mode 100644
index 0000000000..2672e781e9
--- /dev/null
+++ b/internal/unifiedresources/change_emission_test.go
@@ -0,0 +1,143 @@
+package unifiedresources
+
+import (
+ "testing"
+ "time"
+)
+
+func TestBuildResourceChange_ReturnsNilWhenUnchanged(t *testing.T) {
+ before := Resource{
+ ID: "vm:1",
+ Type: ResourceTypeVM,
+ Name: "vm-1",
+ Status: StatusOnline,
+ }
+
+ if change := buildResourceChange(before, true, before, true, time.Now().UTC(), nil, SourcePulseDiff, ""); change != nil {
+ t.Fatalf("expected nil change for identical resources, got %+v", change)
+ }
+}
+
+func TestBuildResourceChange_ClassifiesStateTransition(t *testing.T) {
+ before := Resource{
+ ID: "vm:1",
+ Type: ResourceTypeVM,
+ Name: "vm-1",
+ Status: StatusOnline,
+ }
+ after := before
+ after.Status = StatusWarning
+
+ change := buildResourceChange(before, true, after, true, time.Now().UTC(), nil, SourcePlatformEvent, "")
+ if change == nil {
+ t.Fatal("expected state transition change, got nil")
+ }
+ if change.Kind != ChangeStateTransition {
+ t.Fatalf("Kind = %q, want %q", change.Kind, ChangeStateTransition)
+ }
+ if change.From != "online" || change.To != "warning" {
+ t.Fatalf("From/To = %q/%q, want online/warning", change.From, change.To)
+ }
+ if !sameStringSet(mustChangedFields(t, change), []string{"status"}) {
+ t.Fatalf("changedFields = %+v, want status", mustChangedFields(t, change))
+ }
+}
+
+func TestBuildResourceChange_ClassifiesRelationshipChangeAndIncludesEndpoints(t *testing.T) {
+ before := Resource{
+ ID: "vm:1",
+ Type: ResourceTypeVM,
+ Name: "vm-1",
+ Status: StatusOnline,
+ Relationships: []ResourceRelationship{
+ {SourceID: "vm:1", TargetID: "db:1", Type: RelDependsOn, Confidence: 1},
+ },
+ }
+ after := before
+ after.Relationships = []ResourceRelationship{
+ {SourceID: "vm:1", TargetID: "db:2", Type: RelDependsOn, Confidence: 1},
+ }
+
+ change := buildResourceChange(before, true, after, true, time.Now().UTC(), nil, SourcePulseDiff, "")
+ if change == nil {
+ t.Fatal("expected relationship change, got nil")
+ }
+ if change.Kind != ChangeRelationship {
+ t.Fatalf("Kind = %q, want %q", change.Kind, ChangeRelationship)
+ }
+ if !sameStringSet(change.RelatedResources, []string{"db:1", "db:2"}) {
+ t.Fatalf("RelatedResources = %+v, want db:1 and db:2", change.RelatedResources)
+ }
+ for _, id := range change.RelatedResources {
+ if id == change.ResourceID {
+ t.Fatalf("RelatedResources unexpectedly included primary resource ID %q", id)
+ }
+ }
+ if !sameStringSet(mustChangedFields(t, change), []string{"relationships"}) {
+ t.Fatalf("changedFields = %+v, want relationships", mustChangedFields(t, change))
+ }
+}
+
+func TestBuildResourceChange_ClassifiesCapabilityChange(t *testing.T) {
+ before := Resource{
+ ID: "vm:1",
+ Type: ResourceTypeVM,
+ Name: "vm-1",
+ Status: StatusOnline,
+ }
+ after := before
+ after.Capabilities = []ResourceCapability{{Name: "restart", Type: CapabilityTypeCommon}}
+
+ change := buildResourceChange(before, true, after, true, time.Now().UTC(), nil, SourcePulseDiff, "")
+ if change == nil {
+ t.Fatal("expected capability change, got nil")
+ }
+ if change.Kind != ChangeCapability {
+ t.Fatalf("Kind = %q, want %q", change.Kind, ChangeCapability)
+ }
+ if change.From != "0" || change.To != "1" {
+ t.Fatalf("From/To = %q/%q, want 0/1", change.From, change.To)
+ }
+ if !sameStringSet(mustChangedFields(t, change), []string{"capabilities"}) {
+ t.Fatalf("changedFields = %+v, want capabilities", mustChangedFields(t, change))
+ }
+}
+
+func TestBuildResourceChange_ClassifiesConfigUpdate(t *testing.T) {
+ before := Resource{
+ ID: "vm:1",
+ Type: ResourceTypeVM,
+ Name: "vm-1",
+ Status: StatusOnline,
+ }
+ after := before
+ after.Name = "vm-1-renamed"
+ after.CustomURL = "https://example.invalid/vm-1"
+
+ change := buildResourceChange(before, true, after, true, time.Now().UTC(), nil, SourcePulseDiff, "")
+ if change == nil {
+ t.Fatal("expected config update, got nil")
+ }
+ if change.Kind != ChangeConfigUpdate {
+ t.Fatalf("Kind = %q, want %q", change.Kind, ChangeConfigUpdate)
+ }
+ if change.From == change.To {
+ t.Fatalf("expected distinct config summaries, got %q", change.From)
+ }
+ if !sameStringSet(mustChangedFields(t, change), []string{"name", "customUrl"}) {
+ t.Fatalf("changedFields = %+v, want name and customUrl", mustChangedFields(t, change))
+ }
+}
+
+func mustChangedFields(t *testing.T, change *ResourceChange) []string {
+ t.Helper()
+ raw, ok := change.Metadata["changedFields"]
+ if !ok {
+ t.Fatal("expected changedFields metadata")
+ }
+ fields, ok := raw.([]string)
+ if !ok {
+ t.Fatalf("changedFields type = %T, want []string", raw)
+ }
+ return fields
+}
diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.go
index 297433ad0e..eebfa957ca 100644
--- a/internal/unifiedresources/code_standards_test.go
+++ b/internal/unifiedresources/code_standards_test.go
@@ -281,6 +281,26 @@ func TestResourceTimelineStoreIndexesSupportFilteredReads(t *testing.T) {
}
}
+func TestResourceChangeEmissionCoversRelationshipAndCapabilityChanges(t *testing.T) {
+ data, err := os.ReadFile(filepath.Join("change_emission.go"))
+ if err != nil {
+ t.Fatalf("failed to read change_emission.go: %v", err)
+ }
+ source := string(data)
+ requiredSnippets := []string{
+ "relatedResourceIDs(change.ResourceID, before, after)",
+ "if !reflect.DeepEqual(before.Relationships, after.Relationships) {",
+ "changed = append(changed, \"relationships\")",
+ "if !reflect.DeepEqual(before.Capabilities, after.Capabilities) {",
+ "changed = append(changed, \"capabilities\")",
+ }
+ for _, snippet := range requiredSnippets {
+ if !strings.Contains(source, snippet) {
+ t.Fatalf("internal/unifiedresources/change_emission.go must pin canonical relationship/capability change detection snippet %q", snippet)
+ }
+ }
+}
+
func TestResourceFacetCountsAreCanonicalResourceFields(t *testing.T) {
typesData, err := os.ReadFile(filepath.Join("types.go"))
if err != nil {