From 3f945ea1e38b8fdd45f18145395a3bc21da20dba Mon Sep 17 00:00:00 2001 From: Akizon77 Date: Mon, 3 Nov 2025 21:25:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B5=81=E9=87=8F=E4=B8=8A=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E9=87=8D=E7=BD=AE=E6=97=A5=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monitoring/unit/net.go | 5 ++-- utils/utils.go | 58 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 utils/utils.go diff --git a/monitoring/unit/net.go b/monitoring/unit/net.go index 1ff46a9..bde50de 100644 --- a/monitoring/unit/net.go +++ b/monitoring/unit/net.go @@ -7,6 +7,7 @@ import ( "github.com/komari-monitor/komari-agent/cmd/flags" "github.com/komari-monitor/komari-agent/monitoring/netstatic" + "github.com/komari-monitor/komari-agent/utils" "github.com/shirou/gopsutil/v4/net" ) @@ -133,8 +134,8 @@ func NetworkSpeed() (totalUp, totalDown, upSpeed, downSpeed uint64, err error) { if flags.MonthRotate != 0 { netstatic.StartOrContinue() // 确保netstatic在运行 now := uint64(time.Now().Unix()) - aMonthAgo := now - uint64(30*24*3600) - nicStatics, err := netstatic.GetTotalTrafficBetween(aMonthAgo, now) + resetDay := uint64(utils.GetLastResetDate(flags.MonthRotate, time.Now()).Unix()) + nicStatics, err := netstatic.GetTotalTrafficBetween(resetDay, now) if err != nil { // 如果netstatic失败,回退到原来的方法,并返回额外的错误信息 fallbackUp, fallbackDown, fallbackUpSpeed, fallbackDownSpeed, fallbackErr := getNetworkSpeedFallback(includeNics, excludeNics) diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..f824093 --- /dev/null +++ b/utils/utils.go @@ -0,0 +1,58 @@ +package utils + +import "time" + +// GetLastResetDate 计算上一个重置日期 +// resetDay: 每月重置的日期 (1-31) +// currentDate: 当前日期 +// 返回: 上一个重置日期 +func GetLastResetDate(resetDay int, currentDate time.Time) time.Time { + if resetDay < 1 || resetDay > 31 { + // 如果重置日不合法,返回当前日期 + return currentDate + } + + currentYear := currentDate.Year() + currentMonth := currentDate.Month() + + // 获取本月实际的重置日(考虑顺延情况) + actualResetDateThisMonth := getActualResetDate(currentYear, currentMonth, resetDay, currentDate.Location()) + + // 如果当前日期 >= 本月实际重置日,返回本月的重置日 + if !currentDate.Before(actualResetDateThisMonth) { + return actualResetDateThisMonth + } + + // 如果当前日期 < 本月实际重置日,需要找上个月的重置日 + // 获取上个月 + prevMonth := currentMonth - 1 + prevYear := currentYear + if prevMonth < 1 { + prevMonth = 12 + prevYear-- + } + + return getActualResetDate(prevYear, prevMonth, resetDay, currentDate.Location()) +} + +// getActualResetDate 获取某月的实际重置日期(考虑顺延) +// 如果该月没有指定的日期,则顺延到下个月1日 +func getActualResetDate(year int, month time.Month, resetDay int, location *time.Location) time.Time { + // 获取该月的最后一天 + firstDayOfNextMonth := time.Date(year, month+1, 1, 0, 0, 0, 0, location) + lastDayOfMonth := firstDayOfNextMonth.AddDate(0, 0, -1).Day() + + // 如果该月有这个日期,返回该日期 + if resetDay <= lastDayOfMonth { + return time.Date(year, month, resetDay, 0, 0, 0, 0, location) + } + + // 如果该月没有这个日期,顺延到下个月1日 + nextMonth := month + 1 + nextYear := year + if nextMonth > 12 { + nextMonth = 1 + nextYear++ + } + return time.Date(nextYear, nextMonth, 1, 0, 0, 0, 0, location) +}