Files
mengyaping/mengyaping-backend/config/monitor_interval.go
2026-05-16 19:03:32 +08:00

39 lines
921 B
Go

package config
import "slices"
// AllowedMonitorIntervalMinutes 后台预设的全局检测周期(分钟)
var AllowedMonitorIntervalMinutes = []int{10, 20, 30, 60, 180, 360}
// IsAllowedMonitorInterval 是否为允许的检测间隔(分钟)
func IsAllowedMonitorInterval(minutes int) bool {
return minutes > 0 && slices.Contains(AllowedMonitorIntervalMinutes, minutes)
}
// SnapMonitorIntervalMinutes 将分钟数裁剪到最近的预设值(用于读取旧配置)
func SnapMonitorIntervalMinutes(minutes int) int {
if minutes <= 0 {
return 30
}
if IsAllowedMonitorInterval(minutes) {
return minutes
}
best := AllowedMonitorIntervalMinutes[0]
bestDist := absInt(minutes - best)
for _, v := range AllowedMonitorIntervalMinutes[1:] {
d := absInt(minutes - v)
if d < bestDist {
bestDist = d
best = v
}
}
return best
}
func absInt(x int) int {
if x < 0 {
return -x
}
return x
}