Files
junhong_cmp_fiber/internal/domain/cardobservation/traffic.go
邹进 9c3c258f16
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m27s
feat: 卡流量读数先过采集队列再入账,扣减按采集时刻确定套餐
- 每卡 Redis 采集队列(默认 N=3、跨天清空、回退容差 1 MB),攒满且不下降才写最后一个点
- 测试环境全量切换日快照计量,下线灰度名单、切换水位与卡表基线扣费;全部卡置待建起点
- 读数区分没给值与 0;有套餐的卡读数 0 也写快照与日用量 0 行;重置日比较基准为 0 照常防旧值
- 新增采集记录(按月分区保留 90 天)、人工冲正流水与后台接口、采集告警
- 扣减按采集时刻有效套餐查找,只对生效中套餐做状态迁移,查不到套餐按成功跳过
- 换货新卡(新设备下的卡)打待建起点;采集间隔上限只校验启用中的轮询配置

Change: add-card-traffic-queue-and-observed-deduction

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 12:18:47 +09:00

70 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cardobservation
import (
"math"
"github.com/break/junhong_cmp_fiber/pkg/constants"
"github.com/break/junhong_cmp_fiber/pkg/errors"
)
// TrafficObservation 是领域层接收的标准运营商流量读数。
type TrafficObservation struct {
CardID uint
GatewayReadingMB float64
// ReadingResult 是读数结果(constants.TrafficCollectionResult*);为空表示运营商给出了读数,
// 是否有效(有限且 ≥ 0)由观测用例判断。非 ok 的观测只写采集记录,不入队、不写快照。
ReadingResult string
ResetDay int
Metadata ObservationMetadata
}
// TrafficDecision 描述一次流量观测的处理结果。
type TrafficDecision struct {
// IncrementMB 是本次写入流量事件的增量,0 表示没有写事件。
IncrementMB float64
// ReadingAccepted 为 true 表示本次读数通过采集队列判断并写入了日快照。
ReadingAccepted bool
// Decision 是写入判断(constants.TrafficDecision*);读数无效时为空。
Decision string
// Result 是读数结果(constants.TrafficCollectionResult*)。
Result string
// ReadingMB 是本次运营商读数,没有给值时为 nil。
ReadingMB *float64
// CrossMonth 为 true 表示本次写入使卡表本月用量跨入新的自然月。
CrossMonth bool
// CurrentMonthUsageMB 是写入后的本月最大快照(未写入时为卡表原值)。
CurrentMonthUsageMB float64
// LastGatewayReadingMB 保留卡表原值,卡表基线算法已下线,不再更新。
LastGatewayReadingMB float64
}
// ValidateTrafficObservation 校验观测元数据与读数结果;给出读数时读数必须是数字(有效性由用例判断)。
func ValidateTrafficObservation(observation TrafficObservation) error {
if err := validateObservationMetadata(observation.CardID, observation.Metadata); err != nil {
return err
}
switch observation.ReadingResult {
case "", constants.TrafficCollectionResultOK, constants.TrafficCollectionResultFailed, constants.TrafficCollectionResultRateLimited,
constants.TrafficCollectionResultInvalidPayload, constants.TrafficCollectionResultNoReading, constants.TrafficCollectionResultInvalidReading:
return nil
default:
return errors.New(errors.CodeInvalidParam, "流量观测读数结果无效")
}
}
// ResolveReadingResult 返回观测的最终读数结果:给出了读数但不是有限且 ≥ 0 的数时为读数无效。
func ResolveReadingResult(observation TrafficObservation) string {
if observation.ReadingResult != "" && observation.ReadingResult != constants.TrafficCollectionResultOK {
return observation.ReadingResult
}
reading := observation.GatewayReadingMB
if math.IsNaN(reading) || math.IsInf(reading, 0) || reading < 0 {
return constants.TrafficCollectionResultInvalidReading
}
return constants.TrafficCollectionResultOK
}
func validateObservationMetadata(cardID uint, metadata ObservationMetadata) error {
return validateObservation(RealnameObservation{CardID: cardID, Verified: true, Metadata: metadata})
}