From 626aaebba0de812e4019cde7e82fe96d5ce192b0 Mon Sep 17 00:00:00 2001 From: MikoyChinese Date: Thu, 13 Jan 2022 13:46:41 +0800 Subject: [PATCH] Add cycle_unit to Network Traffic Statistics. --- model/rule.go | 97 +++++++++++++++---- .../template/theme-mdui/viewpassword.html | 1 - 2 files changed, 76 insertions(+), 22 deletions(-) diff --git a/model/rule.go b/model/rule.go index 25bcfd6..e8a3acd 100644 --- a/model/rule.go +++ b/model/rule.go @@ -5,7 +5,6 @@ import ( "time" "gorm.io/gorm" - "github.com/gorhill/cronexpr" ) const ( @@ -25,7 +24,8 @@ type Rule struct { Min float64 `json:"min,omitempty"` // 最小阈值 (百分比、字节 kb ÷ 1024) Max float64 `json:"max,omitempty"` // 最大阈值 (百分比、字节 kb ÷ 1024) CycleStart time.Time `json:"cycle_start,omitempty"` // 流量统计的开始时间 - CycleInterval string `json:"cycle_interval,omitempty"` // crontab 表达式 + CycleInterval uint64 `json:"cycle_interval,omitempty"` // 流量统计周期 + CycleUnit string `json:"cycle_unit,omitempty"` // 流量统计周期单位,默认hour,可选(hour, day, week, month, year) Duration uint64 `json:"duration,omitempty"` // 持续时间 (秒) Cover uint64 `json:"cover,omitempty"` // 覆盖范围 RuleCoverAll/IgnoreAll Ignore map[uint64]bool `json:"ignore,omitempty"` // 覆盖范围的排除 @@ -89,21 +89,21 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, } case "transfer_in_cycle": src = float64(server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn)) - if u.CycleInterval != nil { + 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) src += float64(res.N) } case "transfer_out_cycle": src = float64(server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut)) - if u.CycleInterval != nil { + 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) 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 != nil { + 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) src += float64(res.N) @@ -164,25 +164,80 @@ func (rule Rule) IsTransferDurationRule() bool { } func (rule Rule) GetTransferDurationStart() time.Time { - expr := cronexpr.MustParse(rule.CycleInterval) - durationStart := rule.CycleInterval - nextTime := expr.Next(rule.CycleStart) - for nextTime.Before(time.Now()) { - durationStart = nextTime - nextTime = expr.Next(rule.CycleStart) + // Accept uppercase and lowercase + unit := strings.ToLower(rule.CycleUnit) + startTime := rule.CycleStart + var nextTime time.Time + switch unit { + case "year": + nextTime = startTime.AddDate(int(rule.CycleInterval), 0, 0) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(int(rule.CycleInterval), 0, 0) + } + case "month": + nextTime = startTime.AddDate(0, int(rule.CycleInterval), 0) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(0, int(rule.CycleInterval), 0) + } + case "week": + nextTime = startTime.AddDate(0, 0, 7 * int(rule.CycleInterval)) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(0, 0, 7 * int(rule.CycleInterval)) + } + case "day": + nextTime = startTime.AddDate(0, 0, int(rule.CycleInterval)) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(0, 0, int(rule.CycleInterval)) + } + default: + // For hour unit or not set. + interval := 3600 * int64(rule.CycleInterval) + startTime = time.Unix(rule.CycleStart.Unix()+(time.Now().Unix()-rule.CycleStart.Unix())/interval*interval, 0) } - // TODO: catch the err from parse String and Next function. - return time.Unix(durationStart.Unix, 0) + + return startTime } func (rule Rule) GetTransferDurationEnd() time.Time { - expr := cronexpr.MustParse(rule.CycleInterval) - durationStart := rule.CycleInterval - nextTime := expr.Next(rule.CycleStart) - for nextTime.Before(time.Now()) { - durationStart = nextTime - nextTime = expr.Next(rule.CycleStart) + // Accept uppercase and lowercase + unit := strings.ToLower(rule.CycleUnit) + startTime := rule.CycleStart + var nextTime time.Time + switch unit { + case "year": + nextTime = startTime.AddDate(int(rule.CycleInterval), 0, 0) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(int(rule.CycleInterval), 0, 0) + } + case "month": + nextTime = startTime.AddDate(0, int(rule.CycleInterval), 0) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(0, int(rule.CycleInterval), 0) + } + case "week": + nextTime = startTime.AddDate(0, 0, 7 * int(rule.CycleInterval)) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(0, 0, 7 * int(rule.CycleInterval)) + } + case "day": + nextTime = startTime.AddDate(0, 0, int(rule.CycleInterval)) + for time.Now().After(nextTime) { + startTime = nextTime + nextTime = nextTime.AddDate(0, 0, int(rule.CycleInterval)) + } + default: + // For hour unit or not set. + interval := 3600 * int64(rule.CycleInterval) + startTime = time.Unix(rule.CycleStart.Unix()+(time.Now().Unix()-rule.CycleStart.Unix())/interval*interval, 0) + nextTime = time.Unix(startTime.Unix() + interval, 0) } - // TODO: catch the err from parse String and Next function. - return time.Unix(nextTime.Unix, 0) + + return nextTime } diff --git a/resource/template/theme-mdui/viewpassword.html b/resource/template/theme-mdui/viewpassword.html index 9b84443..f48c22b 100644 --- a/resource/template/theme-mdui/viewpassword.html +++ b/resource/template/theme-mdui/viewpassword.html @@ -57,7 +57,6 @@ timeout: 2000, }); mdui.mutation(); - $input[0].focus(); } } };