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>
67 lines
2.5 KiB
Go
67 lines
2.5 KiB
Go
package asset
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/domain/cardflow"
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
)
|
|
|
|
// maxCollectionQuerySpan 是采集记录单次查询的最长时间跨度。
|
|
const maxCollectionQuerySpan = 31 * 24 * time.Hour
|
|
|
|
// collectionTimeLayouts 是采集记录查询时间参数接受的格式(无时区时按上海时间解析)。
|
|
var collectionTimeLayouts = []string{time.RFC3339, "2006-01-02 15:04:05", "2006-01-02"}
|
|
|
|
// ListCardTrafficCollections 按卡与采集时间范围分页查询卡流量采集记录,按采集时间倒序。
|
|
func (s *Service) ListCardTrafficCollections(ctx context.Context, cardID uint, req *dto.CardTrafficCollectionListRequest) (*dto.CardTrafficCollectionListResponse, error) {
|
|
from, err := parseCollectionTime(req.StartTime)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
to, err := parseCollectionTime(req.EndTime)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !to.After(from) || to.Sub(from) > maxCollectionQuerySpan {
|
|
return nil, errors.New(errors.CodeInvalidParam, "结束时间必须晚于开始时间,且跨度不超过 31 天")
|
|
}
|
|
page, size := req.Page, req.PageSize
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if size <= 0 {
|
|
size = 50
|
|
}
|
|
rows, total, err := postgres.NewCardTrafficCollectionStore(s.db).List(ctx, postgres.CardTrafficCollectionQuery{
|
|
CardID: cardID, From: from, To: to, Page: page, PageSize: size,
|
|
})
|
|
if err != nil {
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询卡流量采集记录失败")
|
|
}
|
|
items := make([]dto.CardTrafficCollectionItem, 0, len(rows))
|
|
for _, row := range rows {
|
|
items = append(items, dto.CardTrafficCollectionItem{
|
|
ID: row.ID, ObservedAt: row.ObservedAt, ObservationID: row.ObservationID, Source: row.Source,
|
|
Scene: row.Scene, Result: row.Result, VendorUsedMB: row.VendorUsedMB, QueuePoints: json.RawMessage(row.QueuePoints),
|
|
Decision: row.Decision, MonthUsedMB: row.MonthUsedMB, StaleGuarded: row.StaleGuarded,
|
|
DeltaMB: row.DeltaMB, EventID: row.EventID,
|
|
})
|
|
}
|
|
return &dto.CardTrafficCollectionListResponse{Items: items, Total: total}, nil
|
|
}
|
|
|
|
func parseCollectionTime(value string) (time.Time, error) {
|
|
location := cardflow.ShanghaiDate(time.Now()).Location()
|
|
for _, layout := range collectionTimeLayouts {
|
|
if t, err := time.ParseInLocation(layout, value, location); err == nil {
|
|
return t, nil
|
|
}
|
|
}
|
|
return time.Time{}, errors.New(errors.CodeInvalidParam, "时间格式无效")
|
|
}
|