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
Centralize shared duration formatting
rcourtman409c2605c29f
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.mdindex cfcdb70f3a..434dd831a4 100644--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md@@ -149,6 +149,9 @@ detectors only serving as fallback coverage when the canonical store is notavailable. When that patrol-local fallback is used, it must render through theshared memory change presentation helper so the same heading, scope prefix, andchange-type labels are reused instead of being rebuilt ad hoc in AI-local code.+That shared presentation layer also owns the elapsed-time wording utility, so+the same "time ago" phrasing stays consistent across resource, incident, and+fallback memory summaries instead of being reformatted independently.Resource-only incident context should follow the same rule: if an alerttimeline is absent, the incident prompt path should fall back to the canonicalunified-resource timeline rather than depending only on patrol-local changediff --git a/docs/release-control/v6/internal/subsystems/unified-resources.md b/docs/release-control/v6/internal/subsystems/unified-resources.mdindex d2ced07db6..40f661ad9b 100644--- a/docs/release-control/v6/internal/subsystems/unified-resources.md+++ b/docs/release-control/v6/internal/subsystems/unified-resources.md@@ -127,6 +127,10 @@ The same surfaces now also render recent changes through the shared`frontend-modern/src/components/Infrastructure/ResourceChangeSummary.tsx`card, so canonical timeline wording and ordering stay governed by onefrontend feed instead of separate page-local loops.+The canonical unified-resource change and relationship presenters now also+share the same elapsed-time wording utility, so `observed`, `last seen`, and+`ago` fragments stay consistent without each formatter maintaining its own+"time ago" implementation.The change emitter now also classifies canonical restart changes for Dockerand Kubernetes resources when restart counters increase or uptime resets, sothe timeline can distinguish restarts from generic state transitions insteaddiff --git a/internal/ai/memory/changes.go b/internal/ai/memory/changes.goindex 54aa1e26ae..69a7c60c80 100644--- a/internal/ai/memory/changes.go+++ b/internal/ai/memory/changes.go@@ -414,26 +414,6 @@ func generateChangeID() string {return time.Now().Format("20060102150405") + "-" + intToString(int(count%1000))}-func formatDuration(d time.Duration) string {- if d < time.Minute {- return "just now"- }- if d < time.Hour {- return formatUnit(int(d.Minutes()), "minute")- }- if d < 24*time.Hour {- return formatUnit(int(d.Hours()), "hour")- }- return formatUnit(int(d.Hours()/24), "day")-}--func formatUnit(n int, unit string) string {- if n == 1 {- return "1 " + unit- }- return intToString(n) + " " + unit + "s"-}-func formatBytes(bytes int64) string {const (KB = 1024diff --git a/internal/ai/memory/memory_extended_test.go b/internal/ai/memory/memory_extended_test.goindex 6d663b7c91..64c153371e 100644--- a/internal/ai/memory/memory_extended_test.go+++ b/internal/ai/memory/memory_extended_test.go@@ -5,6 +5,8 @@ import ("path/filepath""testing""time"++ "github.com/rcourtman/pulse-go-rewrite/internal/utils")// Additional tests to improve coverage@@ -346,9 +348,9 @@ func TestFormatDuration(t *testing.T) {}for _, tt := range tests {- result := formatDuration(tt.input)+ result := utils.FormatDuration(tt.input)if result != tt.expected {- t.Errorf("formatDuration(%v) = %q, want %q", tt.input, result, tt.expected)+ t.Errorf("FormatDuration(%v) = %q, want %q", tt.input, result, tt.expected)}}}diff --git a/internal/ai/memory/presentation.go b/internal/ai/memory/presentation.goindex 97081012a4..23a662767b 100644--- a/internal/ai/memory/presentation.go+++ b/internal/ai/memory/presentation.go@@ -4,6 +4,8 @@ import ("fmt""strings""time"++ "github.com/rcourtman/pulse-go-rewrite/internal/utils")// ChangeTypeLabel returns the canonical human-readable label for a detected@@ -67,7 +69,7 @@ func FormatRecentChangesContext(changes []Change, includeResourcePrefix bool, heentry = fmt.Sprintf("%s: %s", scope, entry)}ago := time.Since(change.DetectedAt).Truncate(time.Minute)- lines = append(lines, fmt.Sprintf("- %s (%s ago)", entry, formatDuration(ago)))+ lines = append(lines, fmt.Sprintf("- %s (%s ago)", entry, utils.FormatDuration(ago)))}return strings.Join(lines, "\n")}diff --git a/internal/ai/memory/remediation.go b/internal/ai/memory/remediation.goindex 26524ccd24..a8f118f585 100644--- a/internal/ai/memory/remediation.go+++ b/internal/ai/memory/remediation.go@@ -9,6 +9,7 @@ import ("sync""time"+ "github.com/rcourtman/pulse-go-rewrite/internal/utils""github.com/rs/zerolog/log")@@ -289,7 +290,7 @@ func (r *RemediationLog) FormatForContext(resourceID string, limit int) string {for _, rec := range records {ago := time.Since(rec.Timestamp)outcomeStr := string(rec.Outcome)- result += "- " + formatDuration(ago) + " ago: " + rec.Problem + "\n"+ result += "- " + utils.FormatDuration(ago) + " ago: " + rec.Problem + "\n"result += " Action: " + rec.Action + " (" + outcomeStr + ")\n"if rec.Note != "" {result += " Note: " + rec.Note + "\n"diff --git a/internal/unifiedresources/change_presentation.go b/internal/unifiedresources/change_presentation.goindex e83ca46ba9..41fa25e1b0 100644--- a/internal/unifiedresources/change_presentation.go+++ b/internal/unifiedresources/change_presentation.go@@ -4,6 +4,8 @@ import ("fmt""strings""time"++ "github.com/rcourtman/pulse-go-rewrite/internal/utils")// ChangePresentation captures the canonical human-readable components for a@@ -110,7 +112,7 @@ func FormatResourceChangeSummary(change ResourceChange) string {ago := "recently"if !change.ObservedAt.IsZero() {- ago = formatDuration(time.Since(change.ObservedAt).Truncate(time.Minute))+ ago = utils.FormatDuration(time.Since(change.ObservedAt).Truncate(time.Minute))}return summary + fmt.Sprintf(" (%s ago)", ago)}diff --git a/internal/unifiedresources/code_standards_test.go b/internal/unifiedresources/code_standards_test.goindex f78b3eb011..8e89800fc5 100644--- a/internal/unifiedresources/code_standards_test.go+++ b/internal/unifiedresources/code_standards_test.go@@ -419,6 +419,22 @@ func TestIntelligenceRecentChangesUseCanonicalSummaryFormatter(t *testing.T) {}}+func TestResourcePresentationsUseSharedDurationHelper(t *testing.T) {+ requiredFiles := []string{+ "change_presentation.go",+ "relationship_presentation.go",+ }+ for _, name := range requiredFiles {+ data, err := os.ReadFile(filepath.Join(name))+ if err != nil {+ t.Fatalf("failed to read %s: %v", name, err)+ }+ if !strings.Contains(string(data), "utils.FormatDuration(") {+ t.Fatalf("%s must use the shared utils.FormatDuration helper", name)+ }+ }+}+func TestResourceFacetCountsAreCanonicalResourceFields(t *testing.T) {typesData, err := os.ReadFile(filepath.Join("types.go"))if err != nil {diff --git a/internal/unifiedresources/relationship_presentation.go b/internal/unifiedresources/relationship_presentation.goindex 480ee6c1eb..a9865d401d 100644--- a/internal/unifiedresources/relationship_presentation.go+++ b/internal/unifiedresources/relationship_presentation.go@@ -4,6 +4,8 @@ import ("fmt""strings""time"++ "github.com/rcourtman/pulse-go-rewrite/internal/utils")// RelationshipPresentation captures the canonical human-readable fragments for@@ -96,10 +98,10 @@ func FormatResourceGraphContext(resource *Resource, limit int) string {parts = append(parts, fmt.Sprintf("confidence %s", presentation.Confidence))}if !rel.ObservedAt.IsZero() {- parts = append(parts, fmt.Sprintf("observed %s ago", formatDuration(time.Since(rel.ObservedAt).Truncate(time.Minute))))+ parts = append(parts, fmt.Sprintf("observed %s ago", utils.FormatDuration(time.Since(rel.ObservedAt).Truncate(time.Minute))))}if !rel.LastSeenAt.IsZero() {- parts = append(parts, fmt.Sprintf("last seen %s ago", formatDuration(time.Since(rel.LastSeenAt).Truncate(time.Minute))))+ parts = append(parts, fmt.Sprintf("last seen %s ago", utils.FormatDuration(time.Since(rel.LastSeenAt).Truncate(time.Minute))))}if presentation.HasMetadata {parts = append(parts, "metadata present")@@ -112,21 +114,3 @@ func FormatResourceGraphContext(resource *Resource, limit int) string {}return "\n\n### Resource Graph\n" + strings.Join(lines, "\n")}--func formatDuration(d time.Duration) string {- if d < time.Minute {- return "seconds"- }- if d < time.Hour {- mins := int(d.Minutes())- if mins == 1 {- return "1 minute"- }- return fmt.Sprintf("%d minutes", mins)- }- hours := int(d.Hours())- if hours == 1 {- return "1 hour"- }- return fmt.Sprintf("%d hours", hours)-}diff --git a/internal/utils/duration.go b/internal/utils/duration.gonew file mode 100644index 0000000000..ae71540fb6--- /dev/null+++ b/internal/utils/duration.go@@ -0,0 +1,27 @@+package utils++import (+ "strconv"+ "time"+)++// FormatDuration returns a short human-readable elapsed-time string.+func FormatDuration(d time.Duration) string {+ if d < time.Minute {+ return "just now"+ }+ if d < time.Hour {+ return formatUnit(int(d.Minutes()), "minute")+ }+ if d < 24*time.Hour {+ return formatUnit(int(d.Hours()), "hour")+ }+ return formatUnit(int(d.Hours()/24), "day")+}++func formatUnit(n int, unit string) string {+ if n == 1 {+ return "1 " + unit+ }+ return strconv.Itoa(n) + " " + unit + "s"+}diff --git a/internal/utils/duration_test.go b/internal/utils/duration_test.gonew file mode 100644index 0000000000..9f5551bd9a--- /dev/null+++ b/internal/utils/duration_test.go@@ -0,0 +1,31 @@+package utils++import (+ "testing"+ "time"+)++func TestFormatDuration(t *testing.T) {+ t.Parallel()++ cases := []struct {+ name string+ d time.Duration+ want string+ }{+ {name: "just now", d: 30 * time.Second, want: "just now"},+ {name: "minutes", d: 2 * time.Minute, want: "2 minutes"},+ {name: "hours", d: 3 * time.Hour, want: "3 hours"},+ {name: "days", d: 48 * time.Hour, want: "2 days"},+ }++ for _, tc := range cases {+ tc := tc+ t.Run(tc.name, func(t *testing.T) {+ t.Parallel()+ if got := FormatDuration(tc.d); got != tc.want {+ t.Fatalf("FormatDuration(%v) = %q, want %q", tc.d, got, tc.want)+ }+ })+ }+}