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

Đang mở
#473 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@fx-kurumi đang làm issue này rồi.

Từ ngày 13/11/2026.

  • #474 của @fx-kurumi — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
35/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
backend

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

现状问题

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 全部用例)
Ngôn ngữ chính
Go
Star
141
Fork
28
Merge trung bình
1 ngày 15 giờ
Pull request đã merge (30 ngày)
113

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Tencent/LoopForge

Tất cả issue của Tencent/LoopForge

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.