Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(repo): extract unionDuration from businessExecutionDuration (#437)

fx-kurumi49c6d67b78e7
diff --git a/internal/repo/analytics_business_delivery.go b/internal/repo/analytics_business_delivery.go
index ac1def30..5ad16457 100644
--- a/internal/repo/analytics_business_delivery.go
+++ b/internal/repo/analytics_business_delivery.go
@@ -238,6 +238,13 @@ func businessExecutionDuration(item businessIssueTiming, completedAt time.Time,
if !observed {
return nil
}
+ value := unionDuration(intervals).Milliseconds()
+ return &value
+}
+
+// unionDuration returns the total length of the union of intervals: spans
+// that overlap or touch are counted once. intervals is sorted in place.
+func unionDuration(intervals []domain.RunExecutionInterval) time.Duration {
sort.Slice(intervals, func(i, j int) bool { return intervals[i].StartedAt.Before(intervals[j].StartedAt) })
var total time.Duration
for i := 0; i < len(intervals); {
@@ -251,8 +258,7 @@ func businessExecutionDuration(item businessIssueTiming, completedAt time.Time,
}
total += span.FinishedAt.Sub(span.StartedAt)
}
- value := total.Milliseconds()
- return &value
+ return total
}
// populateBusinessDeliveryMetrics 计算业务域交付指标。prefetchedRuns/prefetchedEvents
diff --git a/internal/repo/analytics_union_duration_test.go b/internal/repo/analytics_union_duration_test.go
new file mode 100644
index 00000000..26f2856c
--- /dev/null
+++ b/internal/repo/analytics_union_duration_test.go
@@ -0,0 +1,38 @@
+package repo
+
+import (
+ "testing"
+ "time"
+
+ "agentflow/internal/domain"
+)
+
+func TestUnionDuration(t *testing.T) {
+ base := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
+ span := func(from, to int) domain.RunExecutionInterval {
+ return domain.RunExecutionInterval{
+ StartedAt: base.Add(time.Duration(from) * time.Minute),
+ FinishedAt: base.Add(time.Duration(to) * time.Minute),
+ }
+ }
+ cases := []struct {
+ name string
+ intervals []domain.RunExecutionInterval
+ want time.Duration
+ }{
+ {"empty", nil, 0},
+ {"single", []domain.RunExecutionInterval{span(0, 10)}, 10 * time.Minute},
+ {"disjoint", []domain.RunExecutionInterval{span(0, 10), span(20, 25)}, 15 * time.Minute},
+ {"overlapping", []domain.RunExecutionInterval{span(0, 10), span(5, 15)}, 15 * time.Minute},
+ {"contained", []domain.RunExecutionInterval{span(0, 30), span(5, 10)}, 30 * time.Minute},
+ {"touching", []domain.RunExecutionInterval{span(0, 10), span(10, 20)}, 20 * time.Minute},
+ {"unsorted", []domain.RunExecutionInterval{span(40, 50), span(0, 10), span(8, 12)}, 22 * time.Minute},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ if got := unionDuration(tc.intervals); got != tc.want {
+ t.Fatalf("unionDuration() = %v, want %v", got, tc.want)
+ }
+ })
+ }
+}