【架构优化】从 businessExecutionDuration 中拆出区间并集求和 unionDuration

未關閉
#436 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

@fx-kurumi 已經在處理了。

開始於 2026年11月13日。

  • #437 來自 @fx-kurumi —— 未關閉

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
25/100
Issue 類型
重構
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
go
領域
backend

研究方向

The logic lives in businessExecutionDuration in internal/repo/analytics_business_delivery.go, at the end of the function where intervals are sorted, merged and summed. Read that function, then extract the merge-and-sum loop into a package-private unionDuration helper. Done means the helper has table-driven tests for empty, disjoint, overlapping, nested, touching and unsorted input, and go test ./internal/repo/ passes. Linked PR #437 already covers this work, so check with maintainers before starting.

由索引模型根據 Issue 內容生成。

描述

现状问题

internal/repo/analytics_business_delivery.go 中 businessExecutionDuration 约 85 行,同时承担三件事:筛选可计入的 run、逐 run 收集并裁剪执行区间、最后对区间排序 → 合并重叠 → 求总时长。末尾的「区间并集求和」是一段与业务无关的纯算法(约 15 行双层循环),内联在长函数里,既不便阅读,也无法单独测试。

重构方案

在同文件内抽出包级纯函数:

// unionDuration 返回区间并集的总时长(重叠/相接的区间只计一次)。
func unionDuration(intervals []domain.RunExecutionInterval) time.Duration

businessExecutionDuration 末尾改为 value := unionDuration(intervals).Milliseconds()。排序、合并条件(!StartedAt.After(FinishedAt),相接也合并)逐字保留。

影响范围

  • 仅 internal/repo/analytics_business_delivery.go,包内私有函数,无对外接口变化,无行为变化。
  • 不改 domain/run_timing.go 中同类逻辑(跨包,不在本次范围)。

验证方式

  • 新增 unionDuration 表驱动单测:空输入、单区间、不相交、重叠、包含、首尾相接、乱序输入。
  • 既有 TestBusinessExecutionDuration* 等用例保持通过;go vet ./internal/repo/、go test ./internal/repo/。
主要語言
Go
星號
141
分支
28
平均合併
1 天 15 小時
30 天內合併 PR
113

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

Tencent/LoopForge 的其他 Issue

查看 Tencent/LoopForge 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。