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): 从 WorkflowMetricsFor 中拆出节点级指标累计 accumulateNodeMetrics (#338)

fx-kurumifa8d59dfe008
diff --git a/internal/repo/runs.go b/internal/repo/runs.go
index 643ad939..5d6a2a9a 100644
--- a/internal/repo/runs.go
+++ b/internal/repo/runs.go
@@ -1296,39 +1296,7 @@ func (r *RunsRepo) WorkflowMetricsFor(projectID string) []domain.WorkflowMetrics
m.LastRunAt = &createdAt
m.LastRunStatus = run.Status
}
- index := map[string]int{}
- for i := range m.NodeMetrics {
- index[m.NodeMetrics[i].NodeID] = i + 1
- }
- for nodeID, node := range run.Nodes {
- pos := index[nodeID] - 1
- if pos < 0 {
- name := nodeID
- for _, definition := range workflowDefs[run.WorkflowID].Nodes {
- if definition.ID == nodeID {
- name = definition.Name
- }
- }
- m.NodeMetrics = append(m.NodeMetrics, domain.WorkflowNodeMetric{NodeID: nodeID, Name: name})
- pos = len(m.NodeMetrics) - 1
- }
- nm := &m.NodeMetrics[pos]
- if node.Attempt > 0 {
- nm.Executions += node.Attempt
- }
- switch node.Status {
- case "succeeded":
- nm.Succeeded++
- case "failed":
- nm.Failed++
- case "waiting_approval":
- nm.Waiting++
- }
- nm.Reworks += node.ReworkCount
- if !node.StartedAt.IsZero() && !node.FinishedAt.IsZero() {
- nodeDurations[run.WorkflowID][nodeID] += node.FinishedAt.Sub(node.StartedAt).Milliseconds()
- }
- }
+ accumulateNodeMetrics(m, run, workflowDefs[run.WorkflowID], nodeDurations[run.WorkflowID])
}
out := make([]domain.WorkflowMetrics, 0, len(metrics))
for id, m := range metrics {
@@ -1368,6 +1336,45 @@ func (r *RunsRepo) WorkflowMetricsFor(projectID string) []domain.WorkflowMetrics
return out
}
+// accumulateNodeMetrics folds one run's node results into m.NodeMetrics,
+// appending a metric (named from def, falling back to the node ID) for nodes
+// not seen before, and adds finished node durations to durations by node ID.
+func accumulateNodeMetrics(m *domain.WorkflowMetrics, run domain.Run, def domain.WorkflowDefinition, durations map[string]int64) {
+ index := map[string]int{}
+ for i := range m.NodeMetrics {
+ index[m.NodeMetrics[i].NodeID] = i + 1
+ }
+ for nodeID, node := range run.Nodes {
+ pos := index[nodeID] - 1
+ if pos < 0 {
+ name := nodeID
+ for _, definition := range def.Nodes {
+ if definition.ID == nodeID {
+ name = definition.Name
+ }
+ }
+ m.NodeMetrics = append(m.NodeMetrics, domain.WorkflowNodeMetric{NodeID: nodeID, Name: name})
+ pos = len(m.NodeMetrics) - 1
+ }
+ nm := &m.NodeMetrics[pos]
+ if node.Attempt > 0 {
+ nm.Executions += node.Attempt
+ }
+ switch node.Status {
+ case "succeeded":
+ nm.Succeeded++
+ case "failed":
+ nm.Failed++
+ case "waiting_approval":
+ nm.Waiting++
+ }
+ nm.Reworks += node.ReworkCount
+ if !node.StartedAt.IsZero() && !node.FinishedAt.IsZero() {
+ durations[nodeID] += node.FinishedAt.Sub(node.StartedAt).Milliseconds()
+ }
+ }
+}
+
// parallel waits for a set of concurrent loads and returns the first error.
func parallel(fns ...func() error) error {
var load sync.WaitGroup
diff --git a/internal/repo/workflow_node_metrics_test.go b/internal/repo/workflow_node_metrics_test.go
new file mode 100644
index 00000000..6f3f7a6c
--- /dev/null
+++ b/internal/repo/workflow_node_metrics_test.go
@@ -0,0 +1,66 @@
+package repo
+
+import (
+ "testing"
+ "time"
+
+ "agentflow/internal/domain"
+)
+
+func TestAccumulateNodeMetricsAppendsNamedNodes(t *testing.T) {
+ start := time.Date(2026, 11, 13, 8, 0, 0, 0, time.UTC)
+ def := domain.WorkflowDefinition{Nodes: []domain.WorkflowNode{{ID: "plan", Name: "规划"}}}
+ run := domain.Run{Nodes: map[string]*domain.NodeRun{
+ "plan": {Status: "succeeded", Attempt: 2, ReworkCount: 1, StartedAt: start, FinishedAt: start.Add(3 * time.Second)},
+ "ghost": {Status: "waiting_approval"},
+ }}
+ m := &domain.WorkflowMetrics{}
+ durations := map[string]int64{}
+
+ accumulateNodeMetrics(m, run, def, durations)
+
+ if len(m.NodeMetrics) != 2 {
+ t.Fatalf("node metrics = %#v", m.NodeMetrics)
+ }
+ byID := map[string]domain.WorkflowNodeMetric{}
+ for _, nm := range m.NodeMetrics {
+ byID[nm.NodeID] = nm
+ }
+ plan := byID["plan"]
+ if plan.Name != "规划" || plan.Executions != 2 || plan.Succeeded != 1 || plan.Reworks != 1 {
+ t.Fatalf("plan metric = %#v", plan)
+ }
+ if ghost := byID["ghost"]; ghost.Name != "ghost" || ghost.Waiting != 1 || ghost.Executions != 0 {
+ t.Fatalf("ghost metric = %#v", ghost)
+ }
+ if durations["plan"] != 3000 || durations["ghost"] != 0 {
+ t.Fatalf("durations = %#v", durations)
+ }
+}
+
+func TestAccumulateNodeMetricsUpdatesExistingInPlace(t *testing.T) {
+ start := time.Date(2026, 11, 13, 8, 0, 0, 0, time.UTC)
+ m := &domain.WorkflowMetrics{NodeMetrics: []domain.WorkflowNodeMetric{
+ {NodeID: "a", Name: "A", Executions: 1, Succeeded: 1},
+ {NodeID: "b", Name: "B", Failed: 1},
+ }}
+ durations := map[string]int64{"b": 500}
+ run := domain.Run{Nodes: map[string]*domain.NodeRun{
+ "b": {Status: "failed", Attempt: 1, StartedAt: start, FinishedAt: start.Add(time.Second)},
+ }}
+
+ accumulateNodeMetrics(m, run, domain.WorkflowDefinition{}, durations)
+
+ if len(m.NodeMetrics) != 2 || m.NodeMetrics[0].NodeID != "a" || m.NodeMetrics[1].NodeID != "b" {
+ t.Fatalf("node order changed: %#v", m.NodeMetrics)
+ }
+ if b := m.NodeMetrics[1]; b.Name != "B" || b.Failed != 2 || b.Executions != 1 {
+ t.Fatalf("b metric = %#v", b)
+ }
+ if m.NodeMetrics[0].Executions != 1 || m.NodeMetrics[0].Succeeded != 1 {
+ t.Fatalf("a metric touched: %#v", m.NodeMetrics[0])
+ }
+ if durations["b"] != 1500 {
+ t.Fatalf("durations = %#v", durations)
+ }
+}