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
165 lines
5.9 KiB
Go
165 lines
5.9 KiB
Go
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
|
||
}
|