Fix: Add boundary checks for negative increment values

This commit is contained in:
DullJZ
2025-11-03 22:50:39 +08:00
parent c35ff96397
commit 86d2bc1f3e

View File

@@ -680,6 +680,14 @@ func (s *Service) ArchiveMonthlyStats(year, month int) error {
// 如果是首次运行没有历史数据incrementA/B 可能等于累计值
// 这是预期行为首月记录的就是从0到当前的增量
// 边界情况如果计算出负值说明数据不一致设置为0
if incrementA < 0 {
incrementA = 0
}
if incrementB < 0 {
incrementB = 0
}
monthlyStats := BucketMonthlyStats{
BucketName: stat.BucketName,
Year: year,
@@ -762,6 +770,14 @@ func (s *Service) GetCurrentMonthStats() ([]BucketMonthlyStats, error) {
incrementA := current.OperationCountA - lastMonthCumulativeA[current.BucketName]
incrementB := current.OperationCountB - lastMonthCumulativeB[current.BucketName]
// 边界情况如果计算出负值说明数据不一致设置为0
if incrementA < 0 {
incrementA = 0
}
if incrementB < 0 {
incrementB = 0
}
result = append(result, BucketMonthlyStats{
BucketName: current.BucketName,
Year: year,