refactor: 流量系统重构 — 增量累加算法 + 日粒度缓冲 + 旧详单清理
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m16s

核心改造:
- 增量算法:流量计算从覆盖式改为增量累加(gateway - lastReading),支持上游运营商重置检测
- 日流量缓冲:insertDataUsageRecord 改为 Redis INCRBYFLOAT,每日凌晨落盘到 tb_card_daily_usage
- 运营商:新增 data_reset_day 字段(联通=27,其余=1)
- IoT卡:新增 last_gateway_reading_mb 字段存储上次网关读数
- 查询层:新建 TrafficQueryService 合并 Redis(今日)+ DB(历史)数据源
- 清理:删除 DataUsageRecord model/store,移除 polling_handler 旧引用

迁移:000094-000097(carrier字段、iot_card字段、数据初始化、日流量表)
This commit is contained in:
2026-03-30 09:59:30 +08:00
parent f5dd2ce4ab
commit cebcada950
44 changed files with 1149 additions and 1613 deletions

View File

@@ -67,8 +67,9 @@ const (
TaskTypeAutoPurchaseAfterRecharge = "task:auto_purchase_after_recharge" // 充值后自动购包
// 定时任务类型(由 Asynq Scheduler 调度)
TaskTypeAlertCheck = "alert:check" // 告警检查
TaskTypeDataCleanup = "data:cleanup" // 数据清理
TaskTypeAlertCheck = "alert:check" // 告警检查
TaskTypeDataCleanup = "data:cleanup" // 数据清理
TaskTypeDailyTrafficFlush = "traffic:daily:flush" // 每日流量落盘
)
// 用户状态常量

View File

@@ -385,3 +385,14 @@ func RedisPollingQueueProtectKey() string {
func RedisWechatConfigActiveKey() string {
return "wechat:config:active"
}
// ========================================
// 流量日缓冲相关 Redis Key
// ========================================
// RedisCardDailyTrafficKey 生成卡日流量缓冲的 Redis 键
// 用途INCRBYFLOAT 累计当日流量增量,每日落盘任务消费后删除
// 过期时间48 小时
func RedisCardDailyTrafficKey(cardID uint, date string) string {
return fmt.Sprintf("traffic:daily:%d:%s", cardID, date)
}