Skip to content

Commit 1095e70

Browse files
committed
Update.
1 parent 6a745c6 commit 1095e70

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

model/host.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,20 @@ func PB2State(s *pb.State) HostState {
9696
}
9797

9898
type Host struct {
99-
OS string
100-
Platform string
101-
PlatformVersion string
102-
CPU []string
103-
MemTotal uint64
104-
DiskTotal uint64
105-
SwapTotal uint64
106-
Arch string
107-
Virtualization string
108-
BootTime uint64
109-
IP string `json:"-"`
110-
CountryCode string
111-
Version string
112-
GPU []string
99+
OS string `json:"OS"`
100+
Platform string `json:"Platform"`
101+
PlatformVersion string `json:"PlatformVersion"`
102+
CPU []string `json:"CPU"`
103+
MemTotal uint64 `json:"MemTotal"`
104+
DiskTotal uint64 `json:"DiskTotal"`
105+
SwapTotal uint64 `json:"SwapTotal"`
106+
Arch string `json:"Arch"`
107+
Virtualization string `json:"Virtualization"`
108+
BootTime uint64 `json:"BootTime"`
109+
IP string `json:"-"`
110+
CountryCode string `json:"CountryCode"`
111+
Version string `json:"Version"`
112+
GPU []string `json:"GPU"`
113113
}
114114

115115
func (h *Host) PB() *pb.Host {

service/singleton/api.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package singleton
22

33
import (
4+
"log"
45
"sync"
56
"time"
67

@@ -119,11 +120,19 @@ func (s *ServerAPIService) GetStatusByIDList(idList []uint64) *ServerStatusRespo
119120
if host == nil || host.MemTotal == 0 || len(host.CPU) == 0 {
120121
var hostJSON []byte
121122
if err := DB.Raw("SELECT host_json FROM last_reported_host WHERE server_id = ?", server.ID).Scan(&hostJSON).Error; err == nil && len(hostJSON) > 0 {
122-
tempHost := &model.Host{}
123+
tempHost := &model.Host{
124+
CPU: []string{},
125+
GPU: []string{},
126+
}
123127
if err := utils.Json.Unmarshal(hostJSON, tempHost); err == nil {
124128
// 不再填充默认数据,只使用实际数据
129+
if Conf.Debug {
130+
log.Printf("NG>> API - 服务器 %s (ID: %d) 成功加载Host数据", server.Name, server.ID)
131+
}
125132
host = tempHost
126133
server.Host = tempHost // 更新内存中的数据
134+
} else if Conf.Debug {
135+
log.Printf("NG>> API - 服务器 %s (ID: %d) 解析Host数据失败: %v", server.Name, server.ID, err)
127136
}
128137
}
129138
}
@@ -187,11 +196,19 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
187196
if host == nil || host.MemTotal == 0 || len(host.CPU) == 0 {
188197
var hostJSON []byte
189198
if err := DB.Raw("SELECT host_json FROM last_reported_host WHERE server_id = ?", v.ID).Scan(&hostJSON).Error; err == nil && len(hostJSON) > 0 {
190-
tempHost := &model.Host{}
199+
tempHost := &model.Host{
200+
CPU: []string{},
201+
GPU: []string{},
202+
}
191203
if err := utils.Json.Unmarshal(hostJSON, tempHost); err == nil {
192204
// 不再填充默认数据,只使用实际数据
205+
if Conf.Debug {
206+
log.Printf("NG>> API - 服务器 %s (ID: %d) 成功加载Host数据", v.Name, v.ID)
207+
}
193208
host = tempHost
194209
v.Host = tempHost // 更新内存中的数据
210+
} else if Conf.Debug {
211+
log.Printf("NG>> API - 服务器 %s (ID: %d) 解析Host数据失败: %v", v.Name, v.ID, err)
195212
}
196213
}
197214
}

service/singleton/server.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,33 @@ func loadServers() {
3535
DB.Find(&servers)
3636
for _, s := range servers {
3737
innerS := s
38-
innerS.Host = &model.Host{}
38+
// 确保创建完整的Host对象
39+
innerS.Host = &model.Host{
40+
CPU: []string{},
41+
GPU: []string{},
42+
}
3943
innerS.State = &model.HostState{}
4044
innerS.LastStateBeforeOffline = nil
4145
innerS.IsOnline = false // 初始状态为离线,等待agent报告
4246

4347
// 从数据库加载最后一次上报的Host信息
4448
var hostJSON []byte
4549
if err := DB.Raw("SELECT host_json FROM last_reported_host WHERE server_id = ?", innerS.ID).Scan(&hostJSON).Error; err == nil && len(hostJSON) > 0 {
50+
if Conf.Debug {
51+
log.Printf("NG>> 服务器 %s (ID: %d) 加载Host数据: %d字节", innerS.Name, innerS.ID, len(hostJSON))
52+
}
4653
if err := utils.Json.Unmarshal(hostJSON, innerS.Host); err != nil {
47-
log.Printf("警告: 解析服务器 %s 的Host数据失败: %v", innerS.Name, err)
48-
} else {
54+
log.Printf("NG>> 解析服务器 %s 的Host数据失败: %v", innerS.Name, err)
55+
} else if Conf.Debug {
4956
// 记录Host数据不完整的情况,但不添加假数据
5057
if len(innerS.Host.CPU) == 0 || innerS.Host.MemTotal == 0 {
51-
log.Printf("警告: 服务器 %s 的Host数据不完整: CPU=%v, MemTotal=%v, Platform=%v",
58+
log.Printf("NG>> 服务器 %s 的Host数据不完整: CPU=%v, MemTotal=%v, Platform=%v",
5259
innerS.Name, len(innerS.Host.CPU) > 0, innerS.Host.MemTotal > 0, innerS.Host.Platform != "")
5360
}
5461
}
5562
} else {
56-
if err != nil {
57-
log.Printf("警告: 服务器 %s 从数据库加载Host数据失败: %v", innerS.Name, err)
63+
if err != nil && Conf.Debug {
64+
log.Printf("NG>> 服务器 %s 从数据库加载Host数据失败: %v", innerS.Name, err)
5865
}
5966
}
6067

@@ -154,7 +161,10 @@ func printServerLoadSummary() {
154161
withHost++
155162
} else {
156163
incompleteHost++
157-
// 仅记录不完整的Host对象,但不输出详细信息
164+
// 仅在Debug模式下记录不完整的Host对象
165+
if Conf.Debug {
166+
log.Printf("NG>> 服务器 %s 的Host数据不完整", server.Name)
167+
}
158168
}
159169
} else {
160170
noHost++
@@ -172,7 +182,7 @@ func printServerLoadSummary() {
172182
}
173183
}
174184

175-
// 只输出一次总体摘要
176-
log.Printf("警告: 服务器状态统计 - 总计=%d, 有完整Host=%d, Host不完整=%d, 无Host=%d, 有State=%d, 有离线前状态=%d",
185+
// 输出摘要日志
186+
log.Printf("NG>> 服务器状态统计 - 总计=%d, 有完整Host=%d, Host不完整=%d, 无Host=%d, 有State=%d, 有离线前状态=%d",
177187
len(ServerList), withHost, incompleteHost, noHost, withState, loaded)
178188
}

0 commit comments

Comments
 (0)