展示月流量使用情况

This commit is contained in:
naiba
2021-11-06 16:00:08 +08:00
parent 611cabf369
commit 561a3c85fb
8 changed files with 136 additions and 39 deletions

View File

@@ -2,10 +2,20 @@ package model
import (
"encoding/json"
"time"
"gorm.io/gorm"
)
type CycleTransferStats struct {
Name string
From time.Time
To time.Time
ServerName map[uint64]string
Transfer map[uint64]uint64
NextUpdate map[uint64]time.Time
}
type AlertRule struct {
Common
Name string
@@ -27,10 +37,14 @@ func (r *AlertRule) AfterFind(tx *gorm.DB) error {
return json.Unmarshal([]byte(r.RulesRaw), &r.Rules)
}
func (r *AlertRule) Snapshot(server *Server, db *gorm.DB) []interface{} {
func (r *AlertRule) Enabled() bool {
return r.Enable != nil && *r.Enable
}
func (r *AlertRule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, db *gorm.DB) []interface{} {
var point []interface{}
for i := 0; i < len(r.Rules); i++ {
point = append(point, r.Rules[i].Snapshot(server, db))
point = append(point, r.Rules[i].Snapshot(cycleTransferStats, server, db))
}
return point
}

View File

@@ -42,7 +42,7 @@ func percentage(used, total uint64) float64 {
}
// Snapshot 未通过规则返回 struct{}{}, 通过返回 nil
func (u *Rule) Snapshot(server *Server, db *gorm.DB) interface{} {
func (u *Rule) Snapshot(cycleTransferStats *CycleTransferStats, server *Server, db *gorm.DB) interface{} {
// 监控全部但是排除了此服务器
if u.Cover == RuleCoverAll && u.Ignore[server.ID] {
return nil
@@ -139,6 +139,11 @@ func (u *Rule) Snapshot(server *Server, db *gorm.DB) interface{} {
} else {
u.LastCycleStatus[server.ID] = nil
}
if cycleTransferStats.ServerName[server.ID] != server.Name {
cycleTransferStats.ServerName[server.ID] = server.Name
}
cycleTransferStats.Transfer[server.ID] = uint64(src)
cycleTransferStats.NextUpdate[server.ID] = u.NextTransferAt[server.ID]
}
if u.Type == "offline" && float64(time.Now().Unix())-src > 6 {