Change circle interval to crontab expression.
This commit is contained in:
		
							parent
							
								
									3a916886e8
								
							
						
					
					
						commit
						9af8bb8c18
					
				@ -5,6 +5,7 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
 | 
						"github.com/gorhill/cronexpr"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const (
 | 
					const (
 | 
				
			||||||
@ -24,7 +25,7 @@ type Rule struct {
 | 
				
			|||||||
	Min           float64         `json:"min,omitempty"`            // 最小阈值 (百分比、字节 kb ÷ 1024)
 | 
						Min           float64         `json:"min,omitempty"`            // 最小阈值 (百分比、字节 kb ÷ 1024)
 | 
				
			||||||
	Max           float64         `json:"max,omitempty"`            // 最大阈值 (百分比、字节 kb ÷ 1024)
 | 
						Max           float64         `json:"max,omitempty"`            // 最大阈值 (百分比、字节 kb ÷ 1024)
 | 
				
			||||||
	CycleStart    time.Time       `json:"cycle_start,omitempty"`    // 流量统计的开始时间
 | 
						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"`       // 持续时间 (秒)
 | 
						Duration      uint64          `json:"duration,omitempty"`       // 持续时间 (秒)
 | 
				
			||||||
	Cover         uint64          `json:"cover,omitempty"`          // 覆盖范围 RuleCoverAll/IgnoreAll
 | 
						Cover         uint64          `json:"cover,omitempty"`          // 覆盖范围 RuleCoverAll/IgnoreAll
 | 
				
			||||||
	Ignore        map[uint64]bool `json:"ignore,omitempty"`         // 覆盖范围的排除
 | 
						Ignore        map[uint64]bool `json:"ignore,omitempty"`         // 覆盖范围的排除
 | 
				
			||||||
@ -88,21 +89,21 @@ func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server,
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	case "transfer_in_cycle":
 | 
						case "transfer_in_cycle":
 | 
				
			||||||
		src = float64(server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn))
 | 
							src = float64(server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn))
 | 
				
			||||||
		if u.CycleInterval != 1 {
 | 
							if u.CycleInterval != nil {
 | 
				
			||||||
			var res NResult
 | 
								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("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
 | 
				
			||||||
			src += float64(res.N)
 | 
								src += float64(res.N)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case "transfer_out_cycle":
 | 
						case "transfer_out_cycle":
 | 
				
			||||||
		src = float64(server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut))
 | 
							src = float64(server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut))
 | 
				
			||||||
		if u.CycleInterval != 1 {
 | 
							if u.CycleInterval != nil {
 | 
				
			||||||
			var res NResult
 | 
								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("created_at > ? AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
 | 
				
			||||||
			src += float64(res.N)
 | 
								src += float64(res.N)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	case "transfer_all_cycle":
 | 
						case "transfer_all_cycle":
 | 
				
			||||||
		src = float64(server.State.NetOutTransfer - uint64(server.PrevHourlyTransferOut) + server.State.NetInTransfer - uint64(server.PrevHourlyTransferIn))
 | 
							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
 | 
								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("created_at > ?  AND server_id = ?", u.GetTransferDurationStart(), server.ID).Scan(&res)
 | 
				
			||||||
			src += float64(res.N)
 | 
								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.NextUpdate[server.ID] = u.NextTransferAt[server.ID]
 | 
				
			||||||
		// 自动更新周期流量展示起止时间
 | 
							// 自动更新周期流量展示起止时间
 | 
				
			||||||
		cycleTransferStats.From = u.GetTransferDurationStart()
 | 
							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 {
 | 
						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 {
 | 
					func (rule Rule) GetTransferDurationStart() time.Time {
 | 
				
			||||||
	interval := 3600 * int64(rule.CycleInterval)
 | 
						expr := cronexpr.MustParse(rule.CycleInterval)
 | 
				
			||||||
	return time.Unix(rule.CycleStart.Unix()+(time.Now().Unix()-rule.CycleStart.Unix())/interval*interval, 0)
 | 
						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 {
 | 
							if AlertsCycleTransferStatsStore[alert.ID] == nil {
 | 
				
			||||||
			from := alert.Rules[j].GetTransferDurationStart()
 | 
								from := alert.Rules[j].GetTransferDurationStart()
 | 
				
			||||||
 | 
								to := alert.Rules[j].GetTransferDurationEnd()
 | 
				
			||||||
			AlertsCycleTransferStatsStore[alert.ID] = &model.CycleTransferStats{
 | 
								AlertsCycleTransferStatsStore[alert.ID] = &model.CycleTransferStats{
 | 
				
			||||||
				Name:       alert.Name,
 | 
									Name:       alert.Name,
 | 
				
			||||||
				From:       from,
 | 
									From:       from,
 | 
				
			||||||
				To:         from.Add(time.Hour * time.Duration(alert.Rules[j].CycleInterval)),
 | 
									To:         to,
 | 
				
			||||||
				Max:        uint64(alert.Rules[j].Max),
 | 
									Max:        uint64(alert.Rules[j].Max),
 | 
				
			||||||
				Min:        uint64(alert.Rules[j].Min),
 | 
									Min:        uint64(alert.Rules[j].Min),
 | 
				
			||||||
				ServerName: make(map[uint64]string),
 | 
									ServerName: make(map[uint64]string),
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user