【架构优化】从 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 摘要。