feat(stats): #10 第②层 —— 统计页按设备过滤(per-device 本地日曲线)
ci-pangolin / Lint — shellcheck (push) Successful in 8s
ci-pangolin / OpenAPI Sync Check (push) Successful in 19s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 6s
ci-pangolin / Flutter — analyze + test (push) Failing after 14s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 6s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 4s
ci-pangolin / Go — build + test (push) Successful in 11s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m8s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 15s

镜像账户级时区曲线到「按设备」:
- migration 000018 `usage_device_hourly`(每设备 UTC 小时桶,稀疏)。
- ReportUsage deviceID>0 时加 AccumulateDeviceHourly;NodeStore 接口+impl+mock。
- usage.Store.DeviceHourlyRange(JOIN devices 校验归属+解析 uuid→id);UsageCurve
  增 deviceUUID 形参:空=账户级,非空=该设备本地日曲线(分桶逻辑复用)。
- /v1/usage?device=<uuid>;客户端 account_api.usage(device)、usageProvider key 改
  记录 (days,device)、stats_page 接 statsDeviceProvider → 选设备即重取该设备曲线。
测试:per-device 曲线隔离+归属校验(A的设备不进B)、UsageCurve(deviceUUID)、
migration v18、客户端 widget(选设备→/v1/usage 带 device=)。全量 go/flutter 绿。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
wangjia
2026-06-29 08:04:33 +08:00
parent 5dcebb77fd
commit e333f59cb3
21 changed files with 343 additions and 82 deletions
+6
View File
@@ -132,6 +132,12 @@ func (m *mockNodeStore) AccumulateHourly(_ context.Context, userID int64, hour t
return nil
}
func (m *mockNodeStore) AccumulateDeviceHourly(_ context.Context, _, _ int64, _ time.Time,
_, _, _ int64,
) error {
return nil
}
func (m *mockNodeStore) EnsureDeviceDpUUID(_ context.Context, _ int64, _ string) (string, int64, error) {
return "", 0, nil
}
+7
View File
@@ -325,6 +325,13 @@ func (h *Handler) ReportUsage(ctx context.Context, req *agentv1.UsageReport) (*a
slog.Warn("nodes.Handler.ReportUsage: device accumulate failed",
"user_id", userID, "device_id", deviceID, "err", err)
}
// Per-device hourly (tz-aware per-device display curve, #10②).
if err := h.store.AccumulateDeviceHourly(ctx, userID, deviceID, windowEnd,
entry.BytesUp, entry.BytesDown, entry.SessionMinutes,
); err != nil {
slog.Warn("nodes.Handler.ReportUsage: device hourly accumulate failed",
"user_id", userID, "device_id", deviceID, "err", err)
}
// Heartbeat: keep the device's "online" status fresh while it reports.
if err := h.store.TouchDeviceLastSeen(ctx, deviceID); err != nil {
slog.Warn("nodes.Handler.ReportUsage: touch last_seen failed",
+26
View File
@@ -108,6 +108,12 @@ type NodeStore interface {
AccumulateDeviceUsage(ctx context.Context, userID, deviceID int64, date time.Time,
bytesUp, bytesDown int64, minutes int64) error
// AccumulateDeviceHourly adds bytes/minutes to usage_device_hourly keyed by
// (deviceID, UTC epoch hour) — the per-device counterpart of AccumulateHourly,
// for timezone-aware per-device day bucketing at query time.
AccumulateDeviceHourly(ctx context.Context, userID, deviceID int64, hour time.Time,
bytesUp, bytesDown int64, minutes int64) error
// TouchDeviceLastSeen bumps devices.last_seen to now for an active device,
// driving the "online" status (connect + periodic usage reports refresh it).
TouchDeviceLastSeen(ctx context.Context, deviceID int64) error
@@ -501,3 +507,23 @@ func (s *SQLNodeStore) AccumulateHourly(
}
return nil
}
// AccumulateDeviceHourly adds bytes/minutes to usage_device_hourly for
// (deviceID, UTC epoch hour). Per-device counterpart of AccumulateHourly.
func (s *SQLNodeStore) AccumulateDeviceHourly(
ctx context.Context, userID, deviceID int64, hour time.Time,
bytesUp, bytesDown int64, minutes int64,
) error {
h := hour.UTC().Unix() / 3600
q := `
INSERT INTO usage_device_hourly (user_id, device_id, hour, bytes_up, bytes_down, minutes_used)
VALUES (?, ?, ?, ?, ?, ?)
` + s.dialect.Upsert([]string{"device_id", "hour"},
"bytes_up = bytes_up + EXCLUDED.bytes_up",
"bytes_down = bytes_down + EXCLUDED.bytes_down",
"minutes_used = minutes_used + EXCLUDED.minutes_used")
if _, err := s.db.ExecContext(ctx, q, userID, deviceID, h, bytesUp, bytesDown, minutes); err != nil {
return fmt.Errorf("nodes.SQLNodeStore.AccumulateDeviceHourly: %w", err)
}
return nil
}