feat: 分层轮询、重置日前加密与激活前流量结算
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m37s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m37s
- 轮询配置新增套餐状态、资产状态、重置日前窗口与窗口内流量间隔(迁移 248),间隔三态:正数按间隔、0 不检查、空继承 - 配置按 priority, id 排序合并;在库与已销售无套餐未匹配时告警不入队 - 新增优先级整体重排、匹配预览、立即重算到期时间(异步只提前)、分层统计与重置日前加密统计接口 - 已切换日快照计量的卡流量按观测时间归属,激活前用量只记已结算 - 订单购买与实名激活补齐资产级激活锁,激活前对已切换卡结算一次流量,失败只告警不阻塞,结算不触发通道阈值判定
This commit is contained in:
@@ -25,12 +25,24 @@ type PollingConfigManager struct {
|
||||
mu sync.RWMutex
|
||||
configs []*model.PollingConfig
|
||||
refreshOnce sync.Once
|
||||
tier *TierResolver
|
||||
}
|
||||
|
||||
// SetTierResolver 注入分层事实解析器;未注入时要求套餐状态的配置不匹配。
|
||||
func (m *PollingConfigManager) SetTierResolver(tier *TierResolver) {
|
||||
m.tier = tier
|
||||
}
|
||||
|
||||
// TierResolver 返回已注入的分层事实解析器。
|
||||
func (m *PollingConfigManager) TierResolver() *TierResolver {
|
||||
return m.tier
|
||||
}
|
||||
|
||||
// TaskTypeInterval 某任务类型的轮询间隔信息
|
||||
type TaskTypeInterval struct {
|
||||
Interval int // 秒
|
||||
Priority int // 来源配置的 priority
|
||||
Interval int // 秒
|
||||
Priority int // 来源配置的 priority
|
||||
ConfigID uint // 来源配置 ID
|
||||
}
|
||||
|
||||
// NewPollingConfigManager 创建配置管理器
|
||||
@@ -149,74 +161,158 @@ func (m *PollingConfigManager) MatchConfig(card *model.IotCard) *model.PollingCo
|
||||
return configs[0]
|
||||
}
|
||||
|
||||
// MatchConfigs 按 priority ASC 遍历所有启用的配置,返回所有满足匹配条件且至少有一个非 NULL interval 的配置
|
||||
// 无匹配时返回空切片
|
||||
// MatchConfigs 按 priority、id 升序遍历所有启用的配置,返回满足匹配条件且至少配置了一种检查间隔(含 0 不检查)的配置。
|
||||
// 无匹配时返回空切片。
|
||||
func (m *PollingConfigManager) MatchConfigs(card *model.IotCard) []*model.PollingConfig {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
configs := m.configs
|
||||
m.mu.RUnlock()
|
||||
return m.matchAgainst(configs, card)
|
||||
}
|
||||
|
||||
func (m *PollingConfigManager) matchAgainst(configs []*model.PollingConfig, card *model.IotCard) []*model.PollingConfig {
|
||||
facts := m.lazyTierFacts(card)
|
||||
var result []*model.PollingConfig
|
||||
for _, cfg := range m.configs {
|
||||
if matchConfigConditions(cfg, card) && hasAnyEnabledInterval(cfg) {
|
||||
for _, cfg := range configs {
|
||||
if matchConfigConditions(cfg, card, facts) && hasAnyConfiguredInterval(cfg) {
|
||||
result = append(result, cfg)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// hasAnyEnabledInterval 检查配置是否有至少一个非 NULL 且大于 0 的轮询间隔
|
||||
func hasAnyEnabledInterval(cfg *model.PollingConfig) bool {
|
||||
return (cfg.RealnameCheckInterval != nil && *cfg.RealnameCheckInterval > 0) ||
|
||||
(cfg.CarddataCheckInterval != nil && *cfg.CarddataCheckInterval > 0) ||
|
||||
(cfg.PackageCheckInterval != nil && *cfg.PackageCheckInterval > 0) ||
|
||||
(cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0) ||
|
||||
(cfg.CardStatusCheckInterval != nil && *cfg.CardStatusCheckInterval > 0)
|
||||
// lazyTierFacts 只在某个配置要求套餐状态时才解析卡的分层事实,避免无分层配置时额外查库。
|
||||
func (m *PollingConfigManager) lazyTierFacts(card *model.IotCard) func() TierFacts {
|
||||
var once sync.Once
|
||||
var facts TierFacts
|
||||
return func() TierFacts {
|
||||
once.Do(func() {
|
||||
if m.tier != nil {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
facts = m.tier.Resolve(ctx, card)
|
||||
}
|
||||
})
|
||||
return facts
|
||||
}
|
||||
}
|
||||
|
||||
// MergedTaskIntervals 对所有匹配配置按 priority 遍历,对每种 task type 选取最高优先级的非 nil interval
|
||||
// hasAnyConfiguredInterval 检查配置是否至少配置了一种检查间隔(正数或 0)。
|
||||
func hasAnyConfiguredInterval(cfg *model.PollingConfig) bool {
|
||||
return cfg.RealnameCheckInterval != nil || cfg.CarddataCheckInterval != nil || cfg.PackageCheckInterval != nil ||
|
||||
(cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0) || cfg.CardStatusCheckInterval != nil
|
||||
}
|
||||
|
||||
// IntervalSource 是某检查类型最终采用的间隔及其来源,供合并结果与匹配预览共用。
|
||||
type IntervalSource struct {
|
||||
TaskType string
|
||||
Interval int // 秒;Disabled 为 true 时为 0
|
||||
Disabled bool // 命中取值 0(不检查)
|
||||
ConfigID uint // 来源配置;未匹配时为 0
|
||||
Priority int
|
||||
Config *model.PollingConfig
|
||||
}
|
||||
|
||||
// taskIntervalFields 返回配置中各检查类型的间隔字段(保护期按既有语义只认正数)。
|
||||
func taskIntervalFields(cfg *model.PollingConfig) []struct {
|
||||
taskType string
|
||||
value *int
|
||||
} {
|
||||
protect := cfg.ProtectCheckInterval
|
||||
if protect != nil && *protect <= 0 {
|
||||
protect = nil
|
||||
}
|
||||
return []struct {
|
||||
taskType string
|
||||
value *int
|
||||
}{
|
||||
{constants.TaskTypePollingRealname, cfg.RealnameCheckInterval},
|
||||
{constants.TaskTypePollingCarddata, cfg.CarddataCheckInterval},
|
||||
{constants.TaskTypePollingPackage, cfg.PackageCheckInterval},
|
||||
{constants.TaskTypePollingProtect, protect},
|
||||
{constants.TaskTypePollingCardStatus, cfg.CardStatusCheckInterval},
|
||||
}
|
||||
}
|
||||
|
||||
// ResolveIntervalSources 按三态规则合并匹配配置:每种检查类型取第一个非空取值,正数按间隔、0 不检查且停止向下查找、空继承。
|
||||
func ResolveIntervalSources(configs []*model.PollingConfig) map[string]IntervalSource {
|
||||
result := make(map[string]IntervalSource)
|
||||
for _, cfg := range configs {
|
||||
for _, field := range taskIntervalFields(cfg) {
|
||||
if field.value == nil {
|
||||
continue
|
||||
}
|
||||
if _, exists := result[field.taskType]; exists {
|
||||
continue
|
||||
}
|
||||
source := IntervalSource{TaskType: field.taskType, ConfigID: cfg.ID, Priority: cfg.Priority, Config: cfg}
|
||||
if *field.value <= 0 {
|
||||
source.Disabled = true
|
||||
} else {
|
||||
source.Interval = *field.value
|
||||
}
|
||||
result[field.taskType] = source
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// MergedTaskIntervals 返回卡各检查类型最终采用的正数间隔;取值 0(不检查)的类型不出现在结果中。
|
||||
// 返回 map: taskType → TaskTypeInterval{Interval, Priority}
|
||||
func (m *PollingConfigManager) MergedTaskIntervals(card *model.IotCard) map[string]TaskTypeInterval {
|
||||
configs := m.MatchConfigs(card)
|
||||
merged := make(map[string]TaskTypeInterval)
|
||||
|
||||
for _, cfg := range configs {
|
||||
priority := int(cfg.Priority)
|
||||
if cfg.RealnameCheckInterval != nil && *cfg.RealnameCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingRealname
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.RealnameCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.CarddataCheckInterval != nil && *cfg.CarddataCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingCarddata
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.CarddataCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.PackageCheckInterval != nil && *cfg.PackageCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingPackage
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.PackageCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.ProtectCheckInterval != nil && *cfg.ProtectCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingProtect
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.ProtectCheckInterval, Priority: priority}
|
||||
}
|
||||
}
|
||||
if cfg.CardStatusCheckInterval != nil && *cfg.CardStatusCheckInterval > 0 {
|
||||
taskType := constants.TaskTypePollingCardStatus
|
||||
if _, exists := merged[taskType]; !exists {
|
||||
merged[taskType] = TaskTypeInterval{Interval: *cfg.CardStatusCheckInterval, Priority: priority}
|
||||
}
|
||||
sources := ResolveIntervalSources(m.MatchConfigs(card))
|
||||
merged := make(map[string]TaskTypeInterval, len(sources))
|
||||
for taskType, source := range sources {
|
||||
if source.Disabled {
|
||||
continue
|
||||
}
|
||||
merged[taskType] = TaskTypeInterval{Interval: source.Interval, Priority: source.Priority, ConfigID: source.ConfigID}
|
||||
}
|
||||
if len(sources) == 0 {
|
||||
m.warnUnmatchedFallback(card)
|
||||
}
|
||||
return merged
|
||||
}
|
||||
|
||||
// matchConfigConditions 检查卡是否满足配置的匹配条件
|
||||
func matchConfigConditions(cfg *model.PollingConfig, card *model.IotCard) bool {
|
||||
// PreviewIntervalSources 用给定配置集合(可含未保存修改)计算卡的匹配配置与最终间隔,只读。
|
||||
func (m *PollingConfigManager) PreviewIntervalSources(configs []*model.PollingConfig, card *model.IotCard) ([]*model.PollingConfig, map[string]IntervalSource) {
|
||||
matched := m.matchAgainst(configs, card)
|
||||
return matched, ResolveIntervalSources(matched)
|
||||
}
|
||||
|
||||
// EnabledConfigs 返回当前内存中的启用配置快照(已按 priority、id 排序)。
|
||||
func (m *PollingConfigManager) EnabledConfigs() []*model.PollingConfig {
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
out := make([]*model.PollingConfig, len(m.configs))
|
||||
copy(out, m.configs)
|
||||
return out
|
||||
}
|
||||
|
||||
// TierFactsOf 返回卡的分层事实(未配置解析器时 Known 为 false)。
|
||||
func (m *PollingConfigManager) TierFactsOf(card *model.IotCard) TierFacts {
|
||||
return m.lazyTierFacts(card)()
|
||||
}
|
||||
|
||||
// warnUnmatchedFallback 已销售无套餐、在库的卡未匹配任何配置时告警:这两类卡依赖卡状态检查兜底开机即停。
|
||||
func (m *PollingConfigManager) warnUnmatchedFallback(card *model.IotCard) {
|
||||
if card == nil || m.logger == nil {
|
||||
return
|
||||
}
|
||||
if card.AssetStatus == constants.AssetStatusInStock {
|
||||
m.logger.Warn("在库卡未匹配任何轮询配置,将不入队", zap.Uint("card_id", card.ID), zap.String("metric", "polling.tier.unmatched_in_stock"))
|
||||
return
|
||||
}
|
||||
if card.AssetStatus == constants.AssetStatusSold {
|
||||
facts := m.TierFactsOf(card)
|
||||
if facts.Known && facts.PackageState == constants.PollingPackageStateNone {
|
||||
m.logger.Warn("已销售无套餐卡未匹配任何轮询配置,将不入队", zap.Uint("card_id", card.ID), zap.String("metric", "polling.tier.unmatched_sold_no_package"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// matchConfigConditions 检查卡是否满足配置的匹配条件;套餐状态仅在配置要求时解析,解析失败视为不匹配。
|
||||
func matchConfigConditions(cfg *model.PollingConfig, card *model.IotCard, facts func() TierFacts) bool {
|
||||
if cfg.CardCondition != "" {
|
||||
if cfg.CardCondition != getCardCondition(card) {
|
||||
return false
|
||||
@@ -232,9 +328,23 @@ func matchConfigConditions(cfg *model.PollingConfig, card *model.IotCard) bool {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if cfg.AssetStatus != nil && *cfg.AssetStatus != card.AssetStatus {
|
||||
return false
|
||||
}
|
||||
if cfg.PackageState != nil && *cfg.PackageState != "" {
|
||||
f := facts()
|
||||
if !f.Known || f.PackageState != *cfg.PackageState {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// CardConditionOf 返回卡用于匹配轮询配置的状态条件(not_real_name / activated / suspended)。
|
||||
func CardConditionOf(card *model.IotCard) string {
|
||||
return getCardCondition(card)
|
||||
}
|
||||
|
||||
// getCardCondition 获取卡的状态条件(用于匹配轮询配置)
|
||||
// ⚠️ 注意判断顺序:停机优先,避免停机卡错误匹配 activated 或 not_real_name 配置
|
||||
// 停机卡需要继续轮询 carddata/package 以检测复机条件(套餐购买、流量重置、实名完成)
|
||||
|
||||
Reference in New Issue
Block a user