【架构优化】合并 repo/analytics.go 中重复的用户/项目/需求来源累加器取用或新建逻辑

未关闭
#473 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@fx-kurumi 已经在做这个了。

开始于 2026年11月13日。

  • #474 来自 @fx-kurumi —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
重构
描述清晰度
描述清楚
活跃度
活跃
技术栈
go
领域
backend

调研方向

The duplicated get-or-create logic lives in internal/repo/analytics.go, in accumulateUserUsage, accumulateProjectUsage, accumulateIssueSource and accumulateConversationRun. Read those four functions first to see the shared default values and key formats. The issue names the three helper functions to add, but pull request #474 is already open against this work, so check it before starting. Done means the helpers are used by both callers, the new analytics_accumulator_entry_test.go passes, and go test ./internal/repo/ passes.

由索引模型根据 Issue 内容生成。

描述

现状问题

internal/repo/analytics.go 中「按键取累加器、不存在就新建并写回 map」的代码在两处各写了一遍:

  • 责任人 × 自然日:accumulateUserUsage 与 accumulateConversationRun(含空责任人归为 unassigned、key 拼接)
  • 项目:accumulateProjectUsage 与 accumulateConversationRun(含空项目归为 unassigned)
  • 需求来源:accumulateIssueSource 与 accumulateConversationRun

accumulateConversationRun 因此长达约 90 行,且默认值、key 格式、初始字段(如 CostKnown: true、issues map)需要在两处同步维护,容易漂移。

重构方案

新增三个包内函数,两处调用方都改为使用它们:

  • analyticsUserEntry(values, userID, day)
  • analyticsProjectEntry(values, projectID)
  • analyticsSourceEntry(values, source)(空来源归为 local,与 accumulateIssueSource 原逻辑一致;analyticsIssueSource 本身不会返回空串,会话侧行为不变)

只抽取「取或建」部分,累加逻辑保持原样。

影响范围

仅 internal/repo/analytics.go 内部私有函数,不改对外接口、统计口径和返回结果。

验证方式

  • 新增 internal/repo/analytics_accumulator_entry_test.go,覆盖默认值、复用同一累加器、不同键新建
  • gofmt、go vet ./internal/repo/、go test ./internal/repo/(含现有 analytics 全部用例)
主要语言
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 摘要。