Skip to content

Commit 676bf68

Browse files
committed
Update.
1 parent 916f3d5 commit 676bf68

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

model/server.go

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,49 @@ import (
1212
"gorm.io/gorm"
1313
)
1414

15+
// State 服务器状态
16+
type State struct {
17+
CPU float64 `json:"cpu"`
18+
MemUsed uint64 `json:"mem_used"`
19+
SwapUsed uint64 `json:"swap_used"`
20+
DiskUsed uint64 `json:"disk_used"`
21+
NetInTransfer uint64 `json:"net_in_transfer"`
22+
NetOutTransfer uint64 `json:"net_out_transfer"`
23+
NetInSpeed uint64 `json:"net_in_speed"`
24+
NetOutSpeed uint64 `json:"net_out_speed"`
25+
Uptime uint64 `json:"uptime"`
26+
Load1 float64 `json:"load1"`
27+
Load5 float64 `json:"load5"`
28+
Load15 float64 `json:"load15"`
29+
TcpConnCount int64 `json:"tcp_conn_count"`
30+
UdpConnCount int64 `json:"udp_conn_count"`
31+
ProcessCount int64 `json:"process_count"`
32+
Temperatures []float64 `json:"temperatures"`
33+
}
34+
1535
type Server struct {
16-
ID uint64 `json:"id" gorm:"primaryKey"`
17-
Name string `json:"name"`
18-
Secret string `json:"secret"`
19-
Tag string `json:"tag"`
20-
DisplayIndex int `json:"display_index"`
21-
Host *Host `json:"host" gorm:"-"`
22-
State *State `json:"state" gorm:"-"`
23-
LastActive time.Time `json:"last_active"`
24-
Note string `json:"note"`
25-
PublicNote string `json:"public_note"`
26-
EnableDDNS bool `json:"enable_ddns"`
27-
DDNSProfilesRaw string `json:"ddns_profiles_raw"`
28-
HideForGuest bool `json:"hide_for_guest"`
36+
ID uint64 `json:"id" gorm:"primaryKey"`
37+
Name string `json:"name"`
38+
Secret string `json:"secret"`
39+
Tag string `json:"tag"` // 分组名
40+
DisplayIndex int `json:"display_index"` // 展示排序,越大越靠前
41+
Host *Host `json:"host" gorm:"-"`
42+
State *State `json:"state" gorm:"-"`
43+
LastActive time.Time `json:"last_active"`
44+
Note string `json:"note"` // 管理员可见备注
45+
PublicNote string `json:"public_note,omitempty"` // 公开备注
46+
EnableDDNS bool `json:"enable_ddns"` // 启用DDNS
47+
DDNSProfilesRaw string `json:"ddns_profiles_raw" gorm:"default:'[]'"`
48+
HideForGuest bool `json:"hide_for_guest"`
49+
DDNSProfiles []uint64 `gorm:"-" json:"-"` // DDNS配置
50+
LastStateBeforeOffline *HostState `gorm:"-" json:"-"` // 离线前最后一次状态
51+
IsOnline bool `gorm:"-" json:"is_online"` // 是否在线
52+
2953
// 累计流量统计
3054
CumulativeNetInTransfer uint64 `json:"cumulative_net_in_transfer" gorm:"default:0"`
3155
CumulativeNetOutTransfer uint64 `json:"cumulative_net_out_transfer" gorm:"default:0"`
3256
LastTrafficReset time.Time `json:"last_traffic_reset"`
3357

34-
Tag string // 分组名
35-
Note string `json:"-"` // 管理员可见备注
36-
PublicNote string `json:"PublicNote,omitempty"` // 公开备注
37-
DisplayIndex int // 展示排序,越大越靠前
38-
EnableDDNS bool // 启用DDNS
39-
DDNSProfiles []uint64 `gorm:"-" json:"-"` // DDNS配置
40-
41-
DDNSProfilesRaw string `gorm:"default:'[]';column:ddns_profiles_raw" json:"-"`
42-
43-
LastStateBeforeOffline *HostState `gorm:"-" json:"-"` // 离线前最后一次状态
44-
IsOnline bool `gorm:"-" json:"is_online"` // 是否在线
45-
4658
// 持久化保存的最后状态
4759
LastStateJSON string `gorm:"type:text" json:"-"` // 最后一次状态的JSON格式
4860
LastOnline time.Time // 最后一次在线时间

service/singleton/traffic_manager.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type TrafficManager struct {
2323
batchBuffer []*model.Transfer
2424
// 上次批量写入时间
2525
lastBatchWrite time.Time
26+
// 数据库连接
27+
db *gorm.DB
2628
}
2729

2830
// TrafficStats 流量统计数据
@@ -72,6 +74,11 @@ func GetTrafficManager() *TrafficManager {
7274
return trafficManager
7375
}
7476

77+
// SetDB 设置数据库连接
78+
func (tm *TrafficManager) SetDB(db *gorm.DB) {
79+
tm.db = db
80+
}
81+
7582
// UpdateTraffic 更新服务器流量统计
7683
func (tm *TrafficManager) UpdateTraffic(serverID uint64, inBytes, outBytes uint64) {
7784
tm.Lock()
@@ -182,7 +189,7 @@ func (tm *TrafficManager) writeBatchToDatabase() {
182189
}
183190

184191
// 创建数据库事务
185-
tx := model.DB.Begin()
192+
tx := tm.db.Begin()
186193
if tx.Error != nil {
187194
log.Printf("创建事务失败: %v", tx.Error)
188195
return
@@ -220,7 +227,7 @@ func (tm *TrafficManager) CleanupOldData(days int) error {
220227
// 分批删除数据以减少数据库压力
221228
batchSize := 1000
222229
for {
223-
result := DB.Where("created_at < ?", deadline).Limit(batchSize).Delete(&model.Transfer{})
230+
result := tm.db.Where("created_at < ?", deadline).Limit(batchSize).Delete(&model.Transfer{})
224231
if result.Error != nil {
225232
return fmt.Errorf("failed to cleanup old traffic data: %v", result.Error)
226233
}

0 commit comments

Comments
 (0)