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:
@@ -138,6 +138,64 @@ type FlowUsageResp struct {
|
||||
ICCID string `json:"iccid" description:"ICCID"`
|
||||
Used FlexFloat `json:"used" description:"当月已用流量(MB,兼容数字或字符串)"`
|
||||
Unit string `json:"unit" description:"流量单位(MB)"`
|
||||
// UsedState 区分运营商"没有给值"与真实读数;零值表示没有给值(响应缺少 used 字段时不会调用解析)。
|
||||
UsedState FlowReadingState `json:"-"`
|
||||
}
|
||||
|
||||
// FlowReadingState 是流量读数的解析结果。
|
||||
type FlowReadingState int
|
||||
|
||||
const (
|
||||
// FlowReadingMissing 表示 used 缺失、为 null、为空或无法解析。
|
||||
FlowReadingMissing FlowReadingState = iota
|
||||
// FlowReadingPresent 表示 used 解析为数字(可能为负数、NaN、Inf,由调用方判断是否有效)。
|
||||
FlowReadingPresent
|
||||
)
|
||||
|
||||
// UnmarshalJSON 解析流量响应,并记录 used 是否给出了可解析的数字。
|
||||
func (r *FlowUsageResp) UnmarshalJSON(data []byte) error {
|
||||
type plain FlowUsageResp
|
||||
var raw struct {
|
||||
plain
|
||||
Used json.RawMessage `json:"used"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
*r = FlowUsageResp(raw.plain)
|
||||
r.Used, r.UsedState = 0, FlowReadingMissing
|
||||
if value, ok := parseFlowReading(raw.Used); ok {
|
||||
r.Used, r.UsedState = FlexFloat(value), FlowReadingPresent
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HasReading 返回运营商是否给出了可解析的读数。
|
||||
func (r *FlowUsageResp) HasReading() bool {
|
||||
return r != nil && r.UsedState == FlowReadingPresent
|
||||
}
|
||||
|
||||
// parseFlowReading 按 FlexFloat 的兼容规则解析读数,null、空、无法解析时返回 false。
|
||||
func parseFlowReading(data json.RawMessage) (float64, bool) {
|
||||
raw := strings.TrimSpace(string(data))
|
||||
if raw == "" || raw == "null" {
|
||||
return 0, false
|
||||
}
|
||||
if strings.HasPrefix(raw, "\"") && strings.HasSuffix(raw, "\"") {
|
||||
unquoted, err := strconv.Unquote(raw)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
raw = strings.TrimSpace(unquoted)
|
||||
}
|
||||
if raw == "" {
|
||||
return 0, false
|
||||
}
|
||||
value, err := strconv.ParseFloat(raw, 64)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return value, true
|
||||
}
|
||||
|
||||
// CardOperationReq 是停机/复机请求
|
||||
|
||||
Reference in New Issue
Block a user