Skip to content

Commit 6a0259f

Browse files
committed
Update.
1 parent ad323f5 commit 6a0259f

File tree

1 file changed

+47
-30
lines changed

1 file changed

+47
-30
lines changed

service/singleton/api.go

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

33
import (
4-
"log"
54
"sync"
65
"time"
76

@@ -115,12 +114,31 @@ func (s *ServerAPIService) GetStatusByIDList(idList []uint64) *ServerStatusRespo
115114
continue
116115
}
117116

118-
// 确保host不为空
119-
if server.Host == nil {
117+
// 尝试从数据库加载Host信息
118+
host := server.Host
119+
if host == nil || host.MemTotal == 0 || len(host.CPU) == 0 {
120+
var hostJSON []byte
121+
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+
if err := utils.Json.Unmarshal(hostJSON, tempHost); err == nil {
124+
host = tempHost
125+
server.Host = tempHost // 更新内存中的数据
126+
}
127+
}
128+
}
129+
130+
// 获取状态数据,优先使用当前状态,没有则使用离线前保存的最后状态
131+
state := server.State
132+
if state == nil && server.LastStateBeforeOffline != nil {
133+
state = server.LastStateBeforeOffline
134+
}
135+
136+
// 如果没有有效的Host或状态数据,跳过该服务器
137+
if host == nil {
120138
continue
121139
}
122140

123-
ipv4, ipv6, validIP := utils.SplitIPAddr(server.Host.IP)
141+
ipv4, ipv6, validIP := utils.SplitIPAddr(host.IP)
124142
info := CommonServerInfo{
125143
ID: server.ID,
126144
Name: server.Name,
@@ -133,22 +151,11 @@ func (s *ServerAPIService) GetStatusByIDList(idList []uint64) *ServerStatusRespo
133151
HideForGuest: server.HideForGuest,
134152
}
135153

136-
// 确保状态数据不为空
137-
if server.State == nil {
138-
// 如果状态为空但有历史状态,使用历史状态
139-
if server.LastStateBeforeOffline != nil {
140-
server.State = server.LastStateBeforeOffline
141-
log.Printf("NG>> 服务器 %s API返回使用了历史状态数据", server.Name)
142-
} else {
143-
continue
144-
}
145-
}
146-
147154
// 构建状态响应
148155
statusData := &StatusResponse{
149156
CommonServerInfo: info,
150-
Host: server.Host,
151-
Status: server.State,
157+
Host: host,
158+
Status: state,
152159
}
153160

154161
res.Result = append(res.Result, statusData)
@@ -173,22 +180,28 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
173180
defer ServerLock.RUnlock()
174181

175182
for _, v := range ServerList {
176-
// 确保host不为空
183+
// 尝试从数据库加载Host信息
177184
host := v.Host
185+
if host == nil || host.MemTotal == 0 || len(host.CPU) == 0 {
186+
var hostJSON []byte
187+
if err := DB.Raw("SELECT host_json FROM last_reported_host WHERE server_id = ?", v.ID).Scan(&hostJSON).Error; err == nil && len(hostJSON) > 0 {
188+
tempHost := &model.Host{}
189+
if err := utils.Json.Unmarshal(hostJSON, tempHost); err == nil {
190+
host = tempHost
191+
v.Host = tempHost // 更新内存中的数据
192+
}
193+
}
194+
}
195+
196+
// 如果Host信息不可用,跳过此服务器
178197
if host == nil {
179198
continue
180199
}
181200

182-
// 确保状态数据不为空
201+
// 获取状态数据,优先使用当前状态,没有则使用离线前保存的最后状态
183202
state := v.State
184-
if state == nil {
185-
// 如果状态为空但有历史状态,使用历史状态
186-
if v.LastStateBeforeOffline != nil {
187-
state = v.LastStateBeforeOffline
188-
log.Printf("NG>> 服务器 %s GetAllStatus使用了历史状态数据", v.Name)
189-
} else {
190-
continue
191-
}
203+
if state == nil && v.LastStateBeforeOffline != nil {
204+
state = v.LastStateBeforeOffline
192205
}
193206

194207
ipv4, ipv6, validIP := utils.SplitIPAddr(host.IP)
@@ -204,12 +217,16 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
204217
HideForGuest: v.HideForGuest,
205218
}
206219

207-
res.Result = append(res.Result, &StatusResponse{
220+
// 构建状态响应
221+
statusData := &StatusResponse{
208222
CommonServerInfo: info,
209-
Host: v.Host,
223+
Host: host,
210224
Status: state,
211-
})
225+
}
226+
227+
res.Result = append(res.Result, statusData)
212228
}
229+
213230
res.CommonResponse = CommonResponse{
214231
Code: 0,
215232
Message: "success",

0 commit comments

Comments
 (0)