mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
Change circle interval to crontab expression.
This commit is contained in:
parent
3a916886e8
commit
9af8bb8c18
@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"github.com/gorhill/cronexpr"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -24,7 +25,7 @@ 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 uint64 `json:"cycle_interval,omitempty"` // 流量统计周期
|
||||
CycleInterval string `json:"cycle_interval,omitempty"` // crontab 表达式
|
||||
Duration uint64 `json:"duration,omitempty"` // 持续时间 (秒)
|
||||
Cover uint64 `json:"cover,omitempty"` // 覆盖范围 RuleCoverAll/IgnoreAll
|
||||
Ignore map[uint64]bool `json:"ignore,omitempty"` // 覆盖范围的排除
|
||||
@ -88,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 != 1 {
|
||||
if u.CycleInterval != nil {
|
||||
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 != 1 {
|
||||
if u.CycleInterval != nil {
|
||||
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 != 1 {
|
||||
if u.CycleInterval != nil {
|
||||
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)
|
||||
@ -146,7 +147,7 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server,
|
||||
cycleTransferStats.NextUpdate[server.ID] = u.NextTransferAt[server.ID]
|
||||
// 自动更新周期流量展示起止时间
|
||||
cycleTransferStats.From = u.GetTransferDurationStart()
|
||||
cycleTransferStats.To = cycleTransferStats.From.Add(time.Hour * time.Duration(u.CycleInterval))
|
||||
cycleTransferStats.To = u.GetTransferDurationEnd()
|
||||
}
|
||||
|
||||
if u.Type == "offline" && float64(time.Now().Unix())-src > 6 {
|
||||
@ -163,6 +164,25 @@ func (rule Rule) IsTransferDurationRule() bool {
|
||||
}
|
||||
|
||||
func (rule Rule) GetTransferDurationStart() time.Time {
|
||||
interval := 3600 * int64(rule.CycleInterval)
|
||||
return time.Unix(rule.CycleStart.Unix()+(time.Now().Unix()-rule.CycleStart.Unix())/interval*interval, 0)
|
||||
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)
|
||||
}
|
||||
// TODO: catch the err from parse String and Next function.
|
||||
return time.Unix(durationStart.Unix, 0)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
// TODO: catch the err from parse String and Next function.
|
||||
return time.Unix(nextTime.Unix, 0)
|
||||
}
|
||||
|
@ -38,10 +38,11 @@ func addCycleTransferStatsInfo(alert *model.AlertRule) {
|
||||
}
|
||||
if AlertsCycleTransferStatsStore[alert.ID] == nil {
|
||||
from := alert.Rules[j].GetTransferDurationStart()
|
||||
to := alert.Rules[j].GetTransferDurationEnd()
|
||||
AlertsCycleTransferStatsStore[alert.ID] = &model.CycleTransferStats{
|
||||
Name: alert.Name,
|
||||
From: from,
|
||||
To: from.Add(time.Hour * time.Duration(alert.Rules[j].CycleInterval)),
|
||||
To: to,
|
||||
Max: uint64(alert.Rules[j].Max),
|
||||
Min: uint64(alert.Rules[j].Min),
|
||||
ServerName: make(map[uint64]string),
|
||||
|
Loading…
Reference in New Issue
Block a user