From 0501e120f64e48b1b564d53e2c485ccf703ec6d5 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Thu, 4 Jul 2024 14:32:19 +0800 Subject: [PATCH] fix: timestamp conversion (#383) --- model/rule.go | 6 +++--- service/singleton/singleton.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/model/rule.go b/model/rule.go index e90b9cab17..6e549906e5 100644 --- a/model/rule.go +++ b/model/rule.go @@ -106,21 +106,21 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, src = float64(server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)) if u.CycleInterval != 0 { var res NResult - db.Model(&Transfer{}).Select("SUM(`in`) AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res) + db.Model(&Transfer{}).Select("SUM(`in`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res) src += float64(res.N) } case "transfer_out_cycle": src = float64(server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut)) if u.CycleInterval != 0 { var res NResult - db.Model(&Transfer{}).Select("SUM(`out`) AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res) + db.Model(&Transfer{}).Select("SUM(`out`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res) src += float64(res.N) } case "transfer_all_cycle": src = float64(server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut) + server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)) if u.CycleInterval != 0 { var res NResult - db.Model(&Transfer{}).Select("SUM(`in`+`out`) AS n").Where("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res) + db.Model(&Transfer{}).Select("SUM(`in`+`out`) AS n").Where("datetime(`created_at`) >= datetime(?) AND server_id = ?", u.GetTransferDurationStart().UTC(), server.ID).Scan(&res) src += float64(res.N) } case "load1": diff --git a/service/singleton/singleton.go b/service/singleton/singleton.go index e726e6b287..4cca923c02 100644 --- a/service/singleton/singleton.go +++ b/service/singleton/singleton.go @@ -93,7 +93,7 @@ func RecordTransferHourlyUsage() { ServerLock.Lock() defer ServerLock.Unlock() now := time.Now() - nowTrimSeconds := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, Loc) + nowTrimSeconds := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()) var txs []model.Transfer for id, server := range ServerList { tx := model.Transfer{ @@ -136,7 +136,7 @@ func CleanMonitorHistory() { if !rule.IsTransferDurationRule() { continue } - dataCouldRemoveBefore := rule.GetTransferDurationStart() + dataCouldRemoveBefore := rule.GetTransferDurationStart().UTC() // 判断规则影响的机器范围 if rule.Cover == model.RuleCoverAll { // 更新全局可以清理的数据点 @@ -155,12 +155,12 @@ func CleanMonitorHistory() { } } for id, couldRemove := range specialServerKeep { - DB.Unscoped().Delete(&model.Transfer{}, "server_id = ? AND created_at < ?", id, couldRemove) + DB.Unscoped().Delete(&model.Transfer{}, "server_id = ? AND datetime(`created_at`) < datetime(?)", id, couldRemove) } if allServerKeep.IsZero() { DB.Unscoped().Delete(&model.Transfer{}, "server_id NOT IN (?)", specialServerIDs) } else { - DB.Unscoped().Delete(&model.Transfer{}, "server_id NOT IN (?) AND created_at < ?", specialServerIDs, allServerKeep) + DB.Unscoped().Delete(&model.Transfer{}, "server_id NOT IN (?) AND datetime(`created_at`) < datetime(?)", specialServerIDs, allServerKeep) } }