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