feat: 卡流量读数先过采集队列再入账,扣减按采集时刻确定套餐
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m27s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m27s
- 每卡 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>
This commit is contained in:
50
internal/domain/cardflow/queue.go
Normal file
50
internal/domain/cardflow/queue.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package cardflow
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 队列写入判断结果,与采集记录的写入判断取值一致。
|
||||
const (
|
||||
QueueWrite = "written"
|
||||
QueueNotFull = "queue_not_full"
|
||||
QueueSlopeNegative = "slope_negative"
|
||||
QueueOutOfOrder = "out_of_order"
|
||||
)
|
||||
|
||||
// QueuePoint 是采集队列中的一个点:采集时间与运营商原始读数。
|
||||
type QueuePoint struct {
|
||||
ObservedAt time.Time `json:"observed_at"`
|
||||
ReadingMB float64 `json:"reading_mb"`
|
||||
}
|
||||
|
||||
// JudgeQueue 判断本次新点能否写入:新点在队尾、队列满 size 个点、相邻两点后一个 ≥ 前一个 − 回退容差。
|
||||
// 不满足时返回不写原因;满足时写入的是队列最后一个点(即本次新点)。
|
||||
func JudgeQueue(points []QueuePoint, newPointIsTail bool, size int, dropToleranceMB float64) string {
|
||||
if !newPointIsTail {
|
||||
return QueueOutOfOrder
|
||||
}
|
||||
if len(points) < size {
|
||||
return QueueNotFull
|
||||
}
|
||||
for i := 1; i < len(points); i++ {
|
||||
if points[i].ReadingMB < points[i-1].ReadingMB-dropToleranceMB {
|
||||
return QueueSlopeNegative
|
||||
}
|
||||
}
|
||||
return QueueWrite
|
||||
}
|
||||
|
||||
// MaxCollectionInterval 返回启用中轮询配置流量采集间隔的上限:留 2 次冗余,
|
||||
// 跨天清空开启时为 24 小时 ÷ (N+2),关闭时为最长点龄 ÷ (N+2);最长点龄为 0(不限)时不设上限,返回 0。
|
||||
func MaxCollectionInterval(size int, clearOnNewDay bool, maxPointAgeHours int, spare int) time.Duration {
|
||||
window := 24 * time.Hour
|
||||
if !clearOnNewDay {
|
||||
if maxPointAgeHours <= 0 {
|
||||
return 0
|
||||
}
|
||||
window = time.Duration(maxPointAgeHours) * time.Hour
|
||||
}
|
||||
return time.Duration(math.Floor(float64(window) / float64(size+spare)))
|
||||
}
|
||||
@@ -87,7 +87,8 @@ func Convert(readingMB float64, day int, resetDay int, ctx MonthContext) Convers
|
||||
return Conversion{MonthUsedMB: nonNegative(round3(month)), StaleGuarded: guarded}
|
||||
}
|
||||
|
||||
// isStale 判断重置日当天的读数是否仍接近上周期总量。
|
||||
// isStale 判断重置日当天的读数是否仍接近上周期总量;只看配置的重置日,当天全天判断。
|
||||
// 比较基准为 0(没有快照可比)时,不论重置日是几号,当天任何正读数都按旧值处理,次日如实写入。
|
||||
func isStale(readingMB float64, day int, resetDay int, ctx MonthContext) bool {
|
||||
if day != resetDay || readingMB <= 0 {
|
||||
return false
|
||||
@@ -95,7 +96,7 @@ func isStale(readingMB float64, day int, resetDay int, ctx MonthContext) bool {
|
||||
if resetDay <= 1 {
|
||||
return readingMB > ctx.PrevMonthMax*StaleGuardRatio
|
||||
}
|
||||
return ctx.ThisMonthBeforeResetMax > 0 && readingMB > ctx.ThisMonthBeforeResetMax*StaleGuardRatio
|
||||
return readingMB > ctx.ThisMonthBeforeResetMax*StaleGuardRatio
|
||||
}
|
||||
|
||||
// MonthlyDelta 返回按月对账的待结算增量:本月最大快照 − 本月已结算合计,不大于 0 时返回 0。
|
||||
@@ -108,9 +109,10 @@ func DailyUsage(dayMaxMB float64, priorMaxMB float64) float64 {
|
||||
return nonNegative(round3(dayMaxMB - priorMaxMB))
|
||||
}
|
||||
|
||||
// ValidReading 判断运营商读数能否写入快照:必须为有限正数。
|
||||
// ValidReading 判断运营商读数是否有效:必须为有限且不小于 0 的数。
|
||||
// 读数为 0 写不写快照由卡在采集时刻有没有套餐决定,不在这里拦。
|
||||
func ValidReading(readingMB float64) bool {
|
||||
return !math.IsNaN(readingMB) && !math.IsInf(readingMB, 0) && readingMB > 0
|
||||
return !math.IsNaN(readingMB) && !math.IsInf(readingMB, 0) && readingMB >= 0
|
||||
}
|
||||
|
||||
func nonNegative(v float64) float64 {
|
||||
|
||||
@@ -2,8 +2,8 @@ package cardobservation
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -11,82 +11,59 @@ import (
|
||||
type TrafficObservation struct {
|
||||
CardID uint
|
||||
GatewayReadingMB float64
|
||||
ResetDay int
|
||||
Metadata ObservationMetadata
|
||||
// ReadingResult 是读数结果(constants.TrafficCollectionResult*);为空表示运营商给出了读数,
|
||||
// 是否有效(有限且 ≥ 0)由观测用例判断。非 ok 的观测只写采集记录,不入队、不写快照。
|
||||
ReadingResult string
|
||||
ResetDay int
|
||||
Metadata ObservationMetadata
|
||||
}
|
||||
|
||||
// CardTrafficSnapshot 是流量规则需要的最小卡快照。
|
||||
type CardTrafficSnapshot struct {
|
||||
CardID uint
|
||||
DataUsageMB int64
|
||||
CurrentMonthUsageMB float64
|
||||
CurrentMonthStartDate *time.Time
|
||||
LastMonthTotalMB float64
|
||||
LastGatewayReadingMB float64
|
||||
}
|
||||
|
||||
// TrafficDecision 描述一次流量观测应持久化的最终值。
|
||||
// TrafficDecision 描述一次流量观测的处理结果。
|
||||
type TrafficDecision struct {
|
||||
IncrementMB float64
|
||||
ReadingAccepted bool
|
||||
CrossMonth bool
|
||||
DataUsageMB int64
|
||||
CurrentMonthUsageMB float64
|
||||
CurrentMonthStartDate time.Time
|
||||
LastMonthTotalMB float64
|
||||
LastGatewayReadingMB float64
|
||||
// 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
|
||||
}
|
||||
|
||||
// ApplyTraffic 应用运营商重置、跨月和异常下降保护规则。
|
||||
func ApplyTraffic(snapshot CardTrafficSnapshot, observation TrafficObservation) (TrafficDecision, error) {
|
||||
// ValidateTrafficObservation 校验观测元数据与读数结果;给出读数时读数必须是数字(有效性由用例判断)。
|
||||
func ValidateTrafficObservation(observation TrafficObservation) error {
|
||||
if err := validateObservationMetadata(observation.CardID, observation.Metadata); err != nil {
|
||||
return TrafficDecision{}, err
|
||||
return err
|
||||
}
|
||||
if math.IsNaN(observation.GatewayReadingMB) || math.IsInf(observation.GatewayReadingMB, 0) || observation.GatewayReadingMB < 0 {
|
||||
return TrafficDecision{}, errors.New(errors.CodeInvalidParam, "流量观测读数无效")
|
||||
switch observation.ReadingResult {
|
||||
case "", constants.TrafficCollectionResultOK, constants.TrafficCollectionResultFailed, constants.TrafficCollectionResultRateLimited,
|
||||
constants.TrafficCollectionResultInvalidPayload, constants.TrafficCollectionResultNoReading, constants.TrafficCollectionResultInvalidReading:
|
||||
return nil
|
||||
default:
|
||||
return errors.New(errors.CodeInvalidParam, "流量观测读数结果无效")
|
||||
}
|
||||
if observation.ResetDay < 1 || observation.ResetDay > 31 {
|
||||
return TrafficDecision{}, errors.New(errors.CodeInvalidParam, "运营商流量重置日无效")
|
||||
}
|
||||
|
||||
// ResolveReadingResult 返回观测的最终读数结果:给出了读数但不是有限且 ≥ 0 的数时为读数无效。
|
||||
func ResolveReadingResult(observation TrafficObservation) string {
|
||||
if observation.ReadingResult != "" && observation.ReadingResult != constants.TrafficCollectionResultOK {
|
||||
return observation.ReadingResult
|
||||
}
|
||||
now := observation.Metadata.ObservedAt
|
||||
monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
|
||||
decision := TrafficDecision{
|
||||
ReadingAccepted: true, DataUsageMB: snapshot.DataUsageMB,
|
||||
CurrentMonthUsageMB: snapshot.CurrentMonthUsageMB, CurrentMonthStartDate: monthStart,
|
||||
LastMonthTotalMB: snapshot.LastMonthTotalMB, LastGatewayReadingMB: observation.GatewayReadingMB,
|
||||
reading := observation.GatewayReadingMB
|
||||
if math.IsNaN(reading) || math.IsInf(reading, 0) || reading < 0 {
|
||||
return constants.TrafficCollectionResultInvalidReading
|
||||
}
|
||||
increment := observation.GatewayReadingMB - snapshot.LastGatewayReadingMB
|
||||
if increment < 0 {
|
||||
if isTrafficResetWindow(now, observation.ResetDay) {
|
||||
increment = observation.GatewayReadingMB
|
||||
} else {
|
||||
increment = 0
|
||||
decision.ReadingAccepted = false
|
||||
decision.LastGatewayReadingMB = snapshot.LastGatewayReadingMB
|
||||
}
|
||||
}
|
||||
decision.IncrementMB = increment
|
||||
decision.CrossMonth = snapshot.CurrentMonthStartDate == nil || snapshot.CurrentMonthStartDate.Before(monthStart)
|
||||
if decision.CrossMonth {
|
||||
decision.LastMonthTotalMB = snapshot.CurrentMonthUsageMB
|
||||
decision.CurrentMonthUsageMB = increment
|
||||
} else if increment > 0 {
|
||||
decision.CurrentMonthUsageMB += increment
|
||||
}
|
||||
if increment > 0 {
|
||||
decision.DataUsageMB += int64(increment)
|
||||
}
|
||||
return decision, nil
|
||||
return constants.TrafficCollectionResultOK
|
||||
}
|
||||
|
||||
func validateObservationMetadata(cardID uint, metadata ObservationMetadata) error {
|
||||
return validateObservation(RealnameObservation{CardID: cardID, Verified: true, Metadata: metadata})
|
||||
}
|
||||
|
||||
func isTrafficResetWindow(now time.Time, resetDay int) bool {
|
||||
if now.Day() == resetDay {
|
||||
return true
|
||||
}
|
||||
resetDate := time.Date(now.Year(), now.Month(), resetDay, 0, 0, 0, 0, now.Location())
|
||||
return now.Day() == resetDate.AddDate(0, 0, -1).Day()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user