feat(通道流量阈值): AUG26-011 运营商通道流量阈值达量停机与周期复机
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 12m59s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 12m59s
This commit is contained in:
69
internal/domain/carrierthreshold/threshold.go
Normal file
69
internal/domain/carrierthreshold/threshold.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Package carrierthreshold 定义运营商通道流量阈值的领域规则。
|
||||
//
|
||||
// 本包只表达与传输、持久化无关的纯规则:阈值单位与 GB→MB 换算、按上游流量重置日计算的
|
||||
// 计费周期起点、以及周期锁与停复机子任务的状态取值。运营商通道即既有 Carrier,
|
||||
// 计费周期与网关计数器清零周期是同一事实,因此周期起点只由 carrier.data_reset_day 决定。
|
||||
package carrierthreshold
|
||||
|
||||
import (
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
// 阈值单位枚举:只允许 MB 与 GB,换算后统一以 MB 与网关累计读数比较。
|
||||
const (
|
||||
UnitMB = "MB"
|
||||
UnitGB = "GB"
|
||||
)
|
||||
|
||||
// MBPerGB 是 GB→MB 的固定换算系数(1 GB = 1024 MB)。
|
||||
const MBPerGB = 1024
|
||||
|
||||
// 计费周期起点的合法重置日范围:1-28 保证每个月都有定义,不存在 2 月 30 日问题。
|
||||
const (
|
||||
MinResetDay = 1
|
||||
MaxResetDay = 28
|
||||
)
|
||||
|
||||
// Threshold 是通道阈值配置的领域值。
|
||||
type Threshold struct {
|
||||
// Enabled 为 true 表示该通道参与达量停机判定。
|
||||
Enabled bool
|
||||
// Value 是阈值数值,启用时必须为正数。
|
||||
Value float64
|
||||
// Unit 是阈值单位,取值 UnitMB 或 UnitGB。
|
||||
Unit string
|
||||
}
|
||||
|
||||
// Valid 判断阈值配置是否为可用于判定的完整配置。
|
||||
// 未启用、数值非正、单位未知都视为不可判定;不可判定必须跳过判定而不是按 0 停机。
|
||||
func (t Threshold) Valid() bool {
|
||||
if !t.Enabled || t.Value <= 0 {
|
||||
return false
|
||||
}
|
||||
return t.Unit == UnitMB || t.Unit == UnitGB
|
||||
}
|
||||
|
||||
// LimitMB 返回换算为 MB 的阈值上限;单位未知返回稳定参数错误。
|
||||
func (t Threshold) LimitMB() (float64, error) {
|
||||
switch t.Unit {
|
||||
case UnitMB:
|
||||
return t.Value, nil
|
||||
case UnitGB:
|
||||
return t.Value * MBPerGB, nil
|
||||
default:
|
||||
return 0, errors.New(errors.CodeInvalidParam, "通道流量阈值单位仅支持 MB 与 GB")
|
||||
}
|
||||
}
|
||||
|
||||
// Reached 判断运营商回传的当前周期累计读数是否达到或超过阈值。
|
||||
// readingMB 只来自网关累计读数(last_gateway_reading_mb),不使用本地用量或套餐真流量。
|
||||
func (t Threshold) Reached(readingMB float64) (bool, error) {
|
||||
if !t.Valid() {
|
||||
return false, nil
|
||||
}
|
||||
limitMB, err := t.LimitMB()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return readingMB >= limitMB, nil
|
||||
}
|
||||
Reference in New Issue
Block a user