feat: 卡流量改为日快照计量并统一停复机规则
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
卡流量计量(按名单灰度,默认 off): - 新增卡日快照与切换水位表(000241),按通道上游流量重置日换算自然月、重置日防旧值 - 已切换的卡按月对账产生增量,无生效中套餐只结算不发事件;第一次切换写水位 - 已切换卡读数有效即判定通道阈值,卡表本月用量用快照覆盖,日流量读快照 - 配置 JUNHONG_TRAFFIC_CALC_MODE/CARDS、JUNHONG_TRAFFIC_FLOW_SNAPSHOT_ENABLED 停复机规则(总开关 JUNHONG_STOP_RESUME_UNIFIED_RULE_ENABLED,默认关闭): - 复机不看停机原因,持通道阈值锁仍拒绝;暂停轮询的卡不自动停复机 - 通道新增实名管控 realname_control_enabled(000242,默认 0),开启后不区分行业卡与实名策略 - 实名管控从 0 改 1 需确认,修改并入通道更新审计 OpenSpec Change: rebuild-card-traffic-metering-and-stop-resume
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/outboxid"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
@@ -73,6 +74,24 @@ type Service struct {
|
||||
auditWriter StateAuditWriter
|
||||
// channelThreshold 为可选的通道阈值判定能力;未注入时流量观测不做阈值判定。
|
||||
channelThreshold ChannelThresholdEvaluator
|
||||
// trafficCalc 判定卡是否走日快照计量算法;未注入时全部卡走卡表基线算法且不写快照。
|
||||
trafficCalc *TrafficCalcPolicy
|
||||
// logger 记录快照写入失败等不影响观测结果的告警;未注入时不输出。
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// SetTrafficCalcPolicy 注入卡流量计量切换策略与告警日志。
|
||||
func (s *Service) SetTrafficCalcPolicy(policy *TrafficCalcPolicy, logger *zap.Logger) {
|
||||
s.trafficCalc = policy
|
||||
s.logger = logger
|
||||
}
|
||||
|
||||
// warnLogger 返回可用的告警日志器。
|
||||
func (s *Service) warnLogger() *zap.Logger {
|
||||
if s.logger == nil {
|
||||
return zap.NewNop()
|
||||
}
|
||||
return s.logger
|
||||
}
|
||||
|
||||
// NewService 创建卡实名观测应用服务。
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
cardflow "github.com/break/junhong_cmp_fiber/internal/domain/cardflow"
|
||||
domain "github.com/break/junhong_cmp_fiber/internal/domain/cardobservation"
|
||||
carrierthresholddomain "github.com/break/junhong_cmp_fiber/internal/domain/carrierthreshold"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
@@ -53,6 +54,23 @@ func (s *Service) ApplyTrafficObservation(ctx context.Context, observation domai
|
||||
return decisionErr
|
||||
}
|
||||
decision = nextDecision
|
||||
// 已切换日快照算法的卡:增量、通道阈值与本月用量改由快照决定;读数为 0 或无效时不写快照、不产生增量。
|
||||
switched := s.trafficCalc.Switched(card.ID)
|
||||
var flow *switchedFlowOutcome
|
||||
if switched && cardflow.ValidReading(observation.GatewayReadingMB) {
|
||||
outcome, flowErr := s.applySwitchedFlow(ctx, tx, &card, observation, decision)
|
||||
if flowErr != nil {
|
||||
return flowErr
|
||||
}
|
||||
flow = outcome
|
||||
}
|
||||
if switched {
|
||||
decision.IncrementMB = 0
|
||||
if flow != nil {
|
||||
decision.IncrementMB = flow.EmitMB
|
||||
decision.CurrentMonthUsageMB = flow.MonthMaxMB
|
||||
}
|
||||
}
|
||||
updates := map[string]any{
|
||||
"last_data_check_at": observation.Metadata.ObservedAt, "last_sync_time": observation.Metadata.ObservedAt,
|
||||
"current_month_start_date": decision.CurrentMonthStartDate, "last_month_total_mb": decision.LastMonthTotalMB,
|
||||
@@ -71,6 +89,7 @@ func (s *Service) ApplyTrafficObservation(ctx context.Context, observation domai
|
||||
return errors.New(errors.CodeConflict, "卡流量基线已被其他请求更新")
|
||||
}
|
||||
if decision.IncrementMB > 0 {
|
||||
// 未切换卡沿用卡表基线算法的增量;已切换卡的增量已替换为按月对账结果,每次观测最多一条 V1 事件。
|
||||
eventID := outboxid.Stable("card-traffic:", strconv.FormatUint(uint64(card.ID), 10)+":"+observation.Metadata.ObservationID+":incremented")
|
||||
if err := s.eventWriter.AppendTraffic(ctx, tx, TrafficIncrementedEvent{
|
||||
EventID: eventID, CardID: card.ID, IncrementMB: decision.IncrementMB,
|
||||
@@ -81,12 +100,17 @@ func (s *Service) ApplyTrafficObservation(ctx context.Context, observation domai
|
||||
return err
|
||||
}
|
||||
}
|
||||
if decision.ReadingAccepted && s.channelThreshold != nil {
|
||||
// 达量判定只使用本次已接受的网关累计读数,异常下降保护命中的观测不参与判定;
|
||||
evaluateThreshold := decision.ReadingAccepted
|
||||
if switched {
|
||||
evaluateThreshold = switchedThresholdEvaluable(flow, decision.ReadingAccepted)
|
||||
}
|
||||
if evaluateThreshold && s.channelThreshold != nil {
|
||||
// 未切换卡只使用本次已接受的网关累计读数,异常下降保护命中的观测不参与判定;
|
||||
// 已切换卡读数有效且未被重置日防旧值处理即判定,不再受卡表基线影响。
|
||||
// 判定失败必须回滚整个流量事务,与既有流量事实保持同事务语义。
|
||||
if err := s.channelThreshold.EvaluateInTx(ctx, tx, carrierthresholddomain.Evaluation{
|
||||
CardID: card.ID, CarrierID: card.CarrierID,
|
||||
ReadingMB: decision.LastGatewayReadingMB, ObservedAt: observation.Metadata.ObservedAt,
|
||||
ReadingMB: thresholdReading(switched, flow, observation, decision), ObservedAt: observation.Metadata.ObservedAt,
|
||||
// 观测来源是判定端获取触发来源的唯一位置:命中留痕据此区分自动判定、人工同步与回调。
|
||||
Source: observation.Metadata.Source,
|
||||
}); err != nil {
|
||||
@@ -152,5 +176,28 @@ func (s *Service) ApplyTrafficObservation(ctx context.Context, observation domai
|
||||
if s.cache != nil {
|
||||
s.cache.Invalidate(ctx, observation.CardID)
|
||||
}
|
||||
if auditedCard != nil && !s.trafficCalc.Switched(auditedCard.ID) {
|
||||
s.writeUnswitchedSnapshot(ctx, auditedCard, observation)
|
||||
}
|
||||
return decision, nil
|
||||
}
|
||||
|
||||
// switchedThresholdEvaluable 判定已切换卡本次是否做通道阈值判定:读数有效且本次未被防旧值按 0 处理;
|
||||
// 第一次切换观测仍按卡表基线算法是否接受读数判定。
|
||||
func switchedThresholdEvaluable(flow *switchedFlowOutcome, oldAccepted bool) bool {
|
||||
if flow == nil {
|
||||
return false
|
||||
}
|
||||
if flow.FirstSwitch {
|
||||
return oldAccepted
|
||||
}
|
||||
return !flow.StaleGuarded
|
||||
}
|
||||
|
||||
// thresholdReading 返回通道阈值判定使用的网关累计读数:已切换卡(非第一次切换观测)用本次读数。
|
||||
func thresholdReading(switched bool, flow *switchedFlowOutcome, observation domain.TrafficObservation, decision domain.TrafficDecision) float64 {
|
||||
if switched && flow != nil && !flow.FirstSwitch {
|
||||
return observation.GatewayReadingMB
|
||||
}
|
||||
return decision.LastGatewayReadingMB
|
||||
}
|
||||
|
||||
164
internal/application/cardobservation/traffic_calc_policy.go
Normal file
164
internal/application/cardobservation/traffic_calc_policy.go
Normal file
@@ -0,0 +1,164 @@
|
||||
package cardobservation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/config"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
// 灰度名单中扣减载体的前缀。
|
||||
const (
|
||||
trafficCalcCarrierDevice = "device"
|
||||
trafficCalcCarrierIotCard = "iot_card"
|
||||
)
|
||||
|
||||
// TrafficCalcPolicy 判定卡是否走日快照计量算法,以及未切换卡是否写快照。
|
||||
//
|
||||
// 名单在进程启动时展开为内存卡集合,观测事务内只查内存、不查库;API 与全部 Worker 必须使用一致的配置。
|
||||
// 零值策略等同于 off 模式且不写快照。
|
||||
type TrafficCalcPolicy struct {
|
||||
mode string
|
||||
cards map[uint]struct{}
|
||||
snapshotEnabled bool
|
||||
}
|
||||
|
||||
// Switched 判定卡是否走日快照算法。
|
||||
func (p *TrafficCalcPolicy) Switched(cardID uint) bool {
|
||||
if p == nil {
|
||||
return false
|
||||
}
|
||||
switch p.mode {
|
||||
case config.TrafficCalcModeFull:
|
||||
return true
|
||||
case config.TrafficCalcModeGray:
|
||||
_, ok := p.cards[cardID]
|
||||
return ok
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// SnapshotEnabled 判定未切换卡是否写日快照。
|
||||
func (p *TrafficCalcPolicy) SnapshotEnabled() bool {
|
||||
return p != nil && p.snapshotEnabled
|
||||
}
|
||||
|
||||
// BuildTrafficCalcPolicy 按配置展开灰度名单:device 展开为当前绑定的全部卡;iot_card 实际使用设备套餐时
|
||||
// 记录错误并跳过该项(不阻止启动)。展开查询失败时整体退回 off 模式,保证判定出错一律走卡表基线算法。
|
||||
func BuildTrafficCalcPolicy(ctx context.Context, db *gorm.DB, cfg config.TrafficCalcConfig, logger *zap.Logger) *TrafficCalcPolicy {
|
||||
if logger == nil {
|
||||
logger = zap.NewNop()
|
||||
}
|
||||
policy := &TrafficCalcPolicy{mode: cfg.Mode, cards: map[uint]struct{}{}, snapshotEnabled: cfg.FlowSnapshotEnabled}
|
||||
if cfg.Mode != config.TrafficCalcModeGray {
|
||||
logger.Info("卡流量计量模式", zap.String("mode", cfg.Mode), zap.Bool("flow_snapshot_enabled", cfg.FlowSnapshotEnabled))
|
||||
return policy
|
||||
}
|
||||
for _, item := range strings.Split(cfg.Cards, ",") {
|
||||
item = strings.TrimSpace(item)
|
||||
if item == "" {
|
||||
continue
|
||||
}
|
||||
if err := policy.expandItem(ctx, db, item, logger); err != nil {
|
||||
logger.Error("展开卡流量计量灰度名单失败,本进程全部卡走卡表基线算法", zap.String("item", item), zap.Error(err))
|
||||
return &TrafficCalcPolicy{mode: config.TrafficCalcModeOff, snapshotEnabled: cfg.FlowSnapshotEnabled}
|
||||
}
|
||||
}
|
||||
logger.Info("卡流量计量灰度名单已展开", zap.String("cards", cfg.Cards), zap.Int("card_count", len(policy.cards)))
|
||||
return policy
|
||||
}
|
||||
|
||||
// expandItem 展开一项扣减载体;格式非法或单卡实际用设备套餐时只记录错误并跳过,查询失败时返回错误。
|
||||
func (p *TrafficCalcPolicy) expandItem(ctx context.Context, db *gorm.DB, item string, logger *zap.Logger) error {
|
||||
kind, rawID, found := strings.Cut(item, ":")
|
||||
id, parseErr := strconv.ParseUint(strings.TrimSpace(rawID), 10, 64)
|
||||
if !found || parseErr != nil || id == 0 {
|
||||
logger.Error("卡流量计量灰度名单项格式非法,已跳过", zap.String("item", item))
|
||||
return nil
|
||||
}
|
||||
switch strings.TrimSpace(kind) {
|
||||
case trafficCalcCarrierDevice:
|
||||
var cardIDs []uint
|
||||
if err := db.WithContext(ctx).Model(&model.DeviceSimBinding{}).
|
||||
Where("device_id = ? AND bind_status = ?", id, constants.BindStatusBound).
|
||||
Pluck("iot_card_id", &cardIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, cardID := range cardIDs {
|
||||
p.cards[cardID] = struct{}{}
|
||||
}
|
||||
return nil
|
||||
case trafficCalcCarrierIotCard:
|
||||
usesDevice, err := cardUsesDevicePackage(ctx, db, uint(id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if usesDevice {
|
||||
logger.Error("灰度名单中的单卡实际从设备套餐扣减,已跳过;请改为按设备整台切换", zap.String("item", item))
|
||||
return nil
|
||||
}
|
||||
p.cards[uint(id)] = struct{}{}
|
||||
return nil
|
||||
default:
|
||||
logger.Error("卡流量计量灰度名单项载体类型非法,已跳过", zap.String("item", item))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// cardUsesDevicePackage 与套餐扣减的载体解析同口径:卡自身没有生效中套餐、且当前绑定设备有生效中套餐时,
|
||||
// 卡的流量扣到设备套餐上。
|
||||
func cardUsesDevicePackage(ctx context.Context, db *gorm.DB, cardID uint) (bool, error) {
|
||||
ownActive, err := countActivePackages(ctx, db, "iot_card_id = ?", cardID)
|
||||
if err != nil || ownActive > 0 {
|
||||
return false, err
|
||||
}
|
||||
deviceID, bound, err := activeBoundDeviceID(ctx, db, cardID)
|
||||
if err != nil || !bound {
|
||||
return false, err
|
||||
}
|
||||
deviceActive, err := countActivePackages(ctx, db, "device_id = ?", deviceID)
|
||||
return deviceActive > 0, err
|
||||
}
|
||||
|
||||
// hasActivePackageForCard 判定卡本次增量是否有可扣的生效中套餐:卡自身或当前绑定设备存在生效中套餐。
|
||||
func hasActivePackageForCard(ctx context.Context, db *gorm.DB, cardID uint) (bool, error) {
|
||||
ownActive, err := countActivePackages(ctx, db, "iot_card_id = ?", cardID)
|
||||
if err != nil || ownActive > 0 {
|
||||
return ownActive > 0, err
|
||||
}
|
||||
deviceID, bound, err := activeBoundDeviceID(ctx, db, cardID)
|
||||
if err != nil || !bound {
|
||||
return false, err
|
||||
}
|
||||
deviceActive, err := countActivePackages(ctx, db, "device_id = ?", deviceID)
|
||||
return deviceActive > 0, err
|
||||
}
|
||||
|
||||
func countActivePackages(ctx context.Context, db *gorm.DB, carrierCondition string, carrierID uint) (int64, error) {
|
||||
var count int64
|
||||
err := db.WithContext(ctx).Model(&model.PackageUsage{}).
|
||||
Where(carrierCondition, carrierID).
|
||||
Where("status = ?", constants.PackageUsageStatusActive).
|
||||
Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func activeBoundDeviceID(ctx context.Context, db *gorm.DB, cardID uint) (uint, bool, error) {
|
||||
var deviceIDs []uint
|
||||
if err := db.WithContext(ctx).Model(&model.DeviceSimBinding{}).
|
||||
Where("iot_card_id = ? AND bind_status = ?", cardID, constants.BindStatusBound).
|
||||
Limit(1).Pluck("device_id", &deviceIDs).Error; err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
if len(deviceIDs) == 0 {
|
||||
return 0, false, nil
|
||||
}
|
||||
return deviceIDs[0], true, nil
|
||||
}
|
||||
150
internal/application/cardobservation/traffic_flow.go
Normal file
150
internal/application/cardobservation/traffic_flow.go
Normal file
@@ -0,0 +1,150 @@
|
||||
package cardobservation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
|
||||
cardflow "github.com/break/junhong_cmp_fiber/internal/domain/cardflow"
|
||||
domain "github.com/break/junhong_cmp_fiber/internal/domain/cardobservation"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
// unswitchedSnapshotTimeout 是未切换卡在观测事务提交后写快照的超时,超时只告警。
|
||||
const unswitchedSnapshotTimeout = 3 * time.Second
|
||||
|
||||
// switchedFlowOutcome 是已切换卡一次观测的日快照计量结果。
|
||||
type switchedFlowOutcome struct {
|
||||
// EmitMB 是本次应写入 V1 流量事件的增量,0 表示不写事件。
|
||||
EmitMB float64
|
||||
// MonthMaxMB 是写入本次快照后的本月最大快照,用于覆盖卡表本月用量。
|
||||
MonthMaxMB float64
|
||||
// StaleGuarded 为 true 表示本次读数在重置日触发防旧值、按 0 计。
|
||||
StaleGuarded bool
|
||||
// FirstSwitch 为 true 表示这是卡的第一次切换观测(本次写入了切换水位)。
|
||||
FirstSwitch bool
|
||||
}
|
||||
|
||||
// snapshotResult 是一次快照换算与写入的结果。
|
||||
type snapshotResult struct {
|
||||
flowDate time.Time
|
||||
conversion cardflow.Conversion
|
||||
}
|
||||
|
||||
// writeSnapshot 按卡所属通道的重置日换算并原子写入当天快照;读数必须已通过 cardflow.ValidReading。
|
||||
func (s *Service) writeSnapshot(ctx context.Context, db *gorm.DB, card *model.IotCard, readingMB float64, observedAt time.Time) (snapshotResult, error) {
|
||||
resetDay := s.strictResetDay(ctx, db, card)
|
||||
flowDate := cardflow.ShanghaiDate(observedAt)
|
||||
days := postgres.NewCardFlowDayStore(db)
|
||||
monthCtx, err := days.MonthContext(ctx, card.ID, flowDate, resetDay)
|
||||
if err != nil {
|
||||
return snapshotResult{}, errors.Wrap(errors.CodeDatabaseError, err, "查询卡日流量快照失败")
|
||||
}
|
||||
conversion := cardflow.Convert(readingMB, flowDate.Day(), resetDay, monthCtx)
|
||||
if err := days.Upsert(ctx, postgres.CardFlowSnapshotWrite{
|
||||
CardID: card.ID, FlowDate: flowDate, MonthUsedMB: conversion.MonthUsedMB,
|
||||
VendorUsedMB: readingMB, StaleGuarded: conversion.StaleGuarded,
|
||||
}); err != nil {
|
||||
return snapshotResult{}, errors.Wrap(errors.CodeDatabaseError, err, "写入卡日流量快照失败")
|
||||
}
|
||||
return snapshotResult{flowDate: flowDate, conversion: conversion}, nil
|
||||
}
|
||||
|
||||
// strictResetDay 在当前连接内按卡的 carrier_id 读取上游流量重置日,不使用调用方预先计算的值;
|
||||
// 查询失败、为空或非法时按卡的运营商类型兜底(联通 27,其余 1)并记录日志。
|
||||
func (s *Service) strictResetDay(ctx context.Context, db *gorm.DB, card *model.IotCard) int {
|
||||
var days []int
|
||||
queryErr := db.WithContext(ctx).Model(&model.Carrier{}).
|
||||
Where("id = ?", card.CarrierID).Limit(1).Pluck("data_reset_day", &days).Error
|
||||
queried := 0
|
||||
if len(days) > 0 {
|
||||
queried = days[0]
|
||||
}
|
||||
resetDay, fallback := cardflow.ResolveResetDay(queried, queryErr == nil && len(days) > 0, card.CarrierType)
|
||||
if fallback {
|
||||
s.warnLogger().Warn("通道上游流量重置日不可用,按运营商类型兜底",
|
||||
zap.Uint("card_id", card.ID), zap.Uint("carrier_id", card.CarrierID),
|
||||
zap.String("carrier_type", card.CarrierType), zap.Int("queried", queried),
|
||||
zap.Int("reset_day", resetDay), zap.Error(queryErr))
|
||||
}
|
||||
return resetDay
|
||||
}
|
||||
|
||||
// applySwitchedFlow 在观测事务内为已切换卡写快照、按月对账结算增量,并在第一次切换观测时写切换水位。
|
||||
//
|
||||
// oldDecision 是卡表基线算法对本次观测的结果:只在第一次切换观测时沿用其增量,此后不再使用。
|
||||
func (s *Service) applySwitchedFlow(ctx context.Context, tx *gorm.DB, card *model.IotCard, observation domain.TrafficObservation, oldDecision domain.TrafficDecision) (*switchedFlowOutcome, error) {
|
||||
readingMB := observation.GatewayReadingMB
|
||||
snapshot, err := s.writeSnapshot(ctx, tx, card, readingMB, observation.Metadata.ObservedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
days := postgres.NewCardFlowDayStore(tx)
|
||||
monthMax, settled, err := days.MonthTotals(ctx, card.ID, snapshot.flowDate)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询卡本月流量结算失败")
|
||||
}
|
||||
hasPackage, pkgErr := hasActivePackageForCard(ctx, tx, card.ID)
|
||||
if pkgErr != nil {
|
||||
// 判断出错按有套餐处理,保持不丢量。
|
||||
s.warnLogger().Warn("判断卡是否有生效中套餐失败,按有套餐处理", zap.Uint("card_id", card.ID), zap.Error(pkgErr))
|
||||
hasPackage = true
|
||||
}
|
||||
outcome := &switchedFlowOutcome{MonthMaxMB: monthMax, StaleGuarded: snapshot.conversion.StaleGuarded}
|
||||
switches := postgres.NewCardFlowSwitchStore(tx)
|
||||
watermark, err := switches.Get(ctx, card.ID)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询卡流量切换水位失败")
|
||||
}
|
||||
if watermark == nil {
|
||||
return outcome, s.firstSwitch(ctx, tx, card, observation, oldDecision, snapshot.flowDate, monthMax, settled, hasPackage, outcome)
|
||||
}
|
||||
delta := cardflow.MonthlyDelta(monthMax, settled)
|
||||
if delta <= 0 {
|
||||
return outcome, nil
|
||||
}
|
||||
if err := days.AddSettled(ctx, card.ID, snapshot.flowDate, delta); err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "结算卡流量增量失败")
|
||||
}
|
||||
if hasPackage {
|
||||
outcome.EmitMB = delta
|
||||
}
|
||||
return outcome, nil
|
||||
}
|
||||
|
||||
// firstSwitch 处理第一次切换观测:本次沿用卡表基线算法的增量,此前部分记为已结算,并写入切换水位。
|
||||
func (s *Service) firstSwitch(ctx context.Context, tx *gorm.DB, card *model.IotCard, observation domain.TrafficObservation,
|
||||
oldDecision domain.TrafficDecision, flowDate time.Time, monthMax, settled float64, hasPackage bool, outcome *switchedFlowOutcome) error {
|
||||
outcome.FirstSwitch = true
|
||||
if oldDecision.IncrementMB > 0 && hasPackage {
|
||||
outcome.EmitMB = oldDecision.IncrementMB
|
||||
}
|
||||
if err := postgres.NewCardFlowDayStore(tx).AddSettled(ctx, card.ID, flowDate, cardflow.MonthlyDelta(monthMax, settled)); err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "结算卡流量切换前用量失败")
|
||||
}
|
||||
if err := postgres.NewCardFlowSwitchStore(tx).Create(ctx, &model.CardFlowSwitch{
|
||||
IotCardID: card.ID, SwitchedAt: observation.Metadata.ObservedAt, MonthUsedMB: monthMax,
|
||||
VendorUsedMB: observation.GatewayReadingMB, OldBaselineMB: card.LastGatewayReadingMB,
|
||||
OldMonthUsageMB: card.CurrentMonthUsageMB, OldReadingAccepted: oldDecision.ReadingAccepted,
|
||||
OldIncrementMB: oldDecision.IncrementMB,
|
||||
}); err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "写入卡流量切换水位失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeUnswitchedSnapshot 在观测事务提交后为未切换卡写快照;失败只告警,不影响本次观测结果。
|
||||
func (s *Service) writeUnswitchedSnapshot(ctx context.Context, card *model.IotCard, observation domain.TrafficObservation) {
|
||||
if card == nil || !s.trafficCalc.SnapshotEnabled() || !cardflow.ValidReading(observation.GatewayReadingMB) {
|
||||
return
|
||||
}
|
||||
writeCtx, cancel := context.WithTimeout(ctx, unswitchedSnapshotTimeout)
|
||||
defer cancel()
|
||||
if _, err := s.writeSnapshot(writeCtx, s.db, card, observation.GatewayReadingMB, observation.Metadata.ObservedAt); err != nil {
|
||||
s.warnLogger().Warn("未切换卡写日流量快照失败,不影响本次观测", zap.Uint("card_id", card.ID), zap.Error(err))
|
||||
}
|
||||
}
|
||||
@@ -14,8 +14,8 @@ import (
|
||||
|
||||
// EvaluateInTx 在流量观测事务内判定通道阈值,达量时写周期锁并同事务写停机事件。
|
||||
//
|
||||
// 判定前提由调用方保证:只有「读数被接受」(ReadingAccepted)的观测才进入本方法,
|
||||
// 异常下降保护命中的观测不参与判定。重复达量观测由部分唯一索引的 23505 识别为
|
||||
// 判定前提由调用方保证:未切换日快照算法的卡只有「读数被接受」(ReadingAccepted)的观测才进入本方法,
|
||||
// 异常下降保护命中的观测不参与判定;已切换的卡读数有效且未被重置日防旧值处理即进入本方法。重复达量观测由部分唯一索引的 23505 识别为
|
||||
// 「该周期已处理」并幂等跳过:不重复建锁、不重复写停机事件,该冲突与流量基线 CAS 的
|
||||
// CodeConflict 语义不同,绝不能混流。返回错误表示必须整体回滚(与既有流量事实同事务)。
|
||||
func (s *Service) EvaluateInTx(ctx context.Context, tx *gorm.DB, evaluation domain.Evaluation) error {
|
||||
|
||||
Reference in New Issue
Block a user