Skip to content

Commit b938867

Browse files
committed
Update.
1 parent e39104f commit b938867

File tree

4 files changed

+136
-19
lines changed

4 files changed

+136
-19
lines changed

model/host.go

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

9898
type Host struct {
99-
OS string `json:"OS"`
100-
Platform string `json:"Platform"`
101-
PlatformVersion string `json:"PlatformVersion"`
99+
OS string `json:"OS,omitempty"`
100+
Platform string `json:"Platform,omitempty"`
101+
PlatformVersion string `json:"PlatformVersion,omitempty"`
102102
CPU []string `json:"CPU"`
103103
MemTotal uint64 `json:"MemTotal"`
104104
DiskTotal uint64 `json:"DiskTotal"`
105105
SwapTotal uint64 `json:"SwapTotal"`
106-
Arch string `json:"Arch"`
107-
Virtualization string `json:"Virtualization"`
106+
Arch string `json:"Arch,omitempty"`
107+
Virtualization string `json:"Virtualization,omitempty"`
108108
BootTime uint64 `json:"BootTime"`
109109
IP string `json:"-"`
110-
CountryCode string `json:"CountryCode"`
111-
Version string `json:"Version"`
110+
CountryCode string `json:"CountryCode,omitempty"`
111+
Version string `json:"Version,omitempty"`
112112
GPU []string `json:"GPU"`
113113
}
114114

115+
func (h *Host) Initialize() {
116+
if h.CPU == nil {
117+
h.CPU = []string{}
118+
}
119+
if h.GPU == nil {
120+
h.GPU = []string{}
121+
}
122+
}
123+
115124
func (h *Host) PB() *pb.Host {
125+
h.Initialize()
126+
116127
return &pb.Host{
117128
Os: h.OS,
118129
Platform: h.Platform,
@@ -132,7 +143,7 @@ func (h *Host) PB() *pb.Host {
132143
}
133144

134145
func PB2Host(h *pb.Host) Host {
135-
return Host{
146+
host := Host{
136147
OS: h.GetOs(),
137148
Platform: h.GetPlatform(),
138149
PlatformVersion: h.GetPlatformVersion(),
@@ -148,4 +159,8 @@ func PB2Host(h *pb.Host) Host {
148159
Version: h.GetVersion(),
149160
GPU: h.GetGpu(),
150161
}
162+
163+
host.Initialize()
164+
165+
return host
151166
}

resource/static/main.js

Lines changed: 94 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,100 @@ function fetchTrafficData(serverId) {
744744
});
745745
}
746746

747+
/**
748+
* 调试并修复离线服务器的配置显示问题
749+
* 确保即使某些字段为空,前端也能正常显示离线服务器信息
750+
*/
751+
function debugOfflineServersConfig() {
752+
// 检查全局状态数据是否存在
753+
if (!window.statusCards || !window.statusCards.servers) {
754+
console.log("状态数据尚未加载,稍后再试");
755+
return;
756+
}
757+
758+
// 遍历所有服务器,检查并修复离线服务器的配置数据
759+
window.statusCards.servers.forEach(server => {
760+
// 重点关注ID为39的服务器
761+
const isTargetServer = server.ID === 39;
762+
763+
if (!server.IsOnline) {
764+
if (isTargetServer) {
765+
console.log(`服务器: ${server.Name} (ID: ${server.ID})`, server);
766+
console.log("Host 配置信息:", server.Host);
767+
}
768+
769+
// 确保Host对象存在
770+
if (!server.Host) {
771+
server.Host = {};
772+
if (isTargetServer) console.log("创建空Host对象");
773+
}
774+
775+
// 确保CPU数组存在且不为null
776+
if (!server.Host.CPU || !Array.isArray(server.Host.CPU)) {
777+
server.Host.CPU = [];
778+
if (isTargetServer) console.log("CPU信息为空");
779+
}
780+
781+
// 确保GPU数组存在且不为null
782+
if (!server.Host.GPU || !Array.isArray(server.Host.GPU)) {
783+
server.Host.GPU = [];
784+
if (isTargetServer) console.log("GPU信息为空");
785+
}
786+
787+
// 确保其他必要的属性存在
788+
if (!server.Host.MemTotal) {
789+
server.Host.MemTotal = 0;
790+
if (isTargetServer) console.log("内存信息为空");
791+
}
792+
793+
if (!server.Host.Platform) {
794+
server.Host.Platform = "";
795+
if (isTargetServer) console.log("平台信息为空");
796+
}
797+
798+
if (!server.Host.PlatformVersion) {
799+
server.Host.PlatformVersion = "";
800+
if (isTargetServer) console.log("平台版本信息为空");
801+
}
802+
803+
if (!server.Host.Virtualization) {
804+
server.Host.Virtualization = "";
805+
if (isTargetServer) console.log("虚拟化信息为空");
806+
}
807+
808+
if (!server.Host.Arch) {
809+
server.Host.Arch = "";
810+
if (isTargetServer) console.log("架构信息为空");
811+
}
812+
813+
// 更新原始对象,确保Vue能检测到变化
814+
if (isTargetServer) {
815+
console.log("修复后的Host对象:", server.Host);
816+
}
817+
}
818+
});
819+
820+
// 触发Vue更新
821+
if (typeof window.statusCards.updateServerData === 'function') {
822+
window.statusCards.updateServerData();
823+
}
824+
}
825+
826+
// 在页面加载完成后运行调试函数
827+
$(document).ready(function() {
828+
// 等待状态数据加载完成后再运行调试
829+
setTimeout(function() {
830+
debugOfflineServersConfig();
831+
}, 1000);
832+
833+
// 每30秒检查一次,确保数据始终正确
834+
setInterval(debugOfflineServersConfig, 30000);
835+
836+
// 原有的流量数据提取
837+
setTimeout(window.extractTrafficData, 500);
838+
setInterval(window.extractTrafficData, 30000);
839+
});
840+
747841
// 初始化全局流量数据变量
748842
window.serverTrafficData = {};
749843
window.lastTrafficUpdateTime = 0;
@@ -901,14 +995,3 @@ window.formatTrafficUnit = function(trafficStr) {
901995

902996
return trafficStr; // 无法识别时返回原始字符串
903997
};
904-
905-
/**
906-
* 初始化流量数据提取和更新
907-
*/
908-
$(document).ready(function() {
909-
// 立即提取数据
910-
setTimeout(window.extractTrafficData, 500);
911-
912-
// 设置定期更新数据的间隔(每30秒)
913-
setInterval(window.extractTrafficData, 30000);
914-
});

service/singleton/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ func (s *ServerAPIService) GetStatusByIDList(idList []uint64) *ServerStatusRespo
152152
if host == nil {
153153
// 确保至少有一个空的Host对象,避免前端报错
154154
host = &model.Host{}
155+
// 确保初始化数组
156+
host.Initialize()
157+
} else {
158+
// 确保已有的Host对象数组已初始化
159+
host.Initialize()
155160
}
156161

157162
ipv4, ipv6, validIP := utils.SplitIPAddr(host.IP)
@@ -234,6 +239,17 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
234239
state = v.LastStateBeforeOffline
235240
}
236241

242+
// 如果没有有效的Host或状态数据,跳过该服务器
243+
if host == nil {
244+
// 确保至少有一个空的Host对象,避免前端报错
245+
host = &model.Host{}
246+
// 确保初始化数组
247+
host.Initialize()
248+
} else {
249+
// 确保已有的Host对象数组已初始化
250+
host.Initialize()
251+
}
252+
237253
ipv4, ipv6, validIP := utils.SplitIPAddr(host.IP)
238254
info := CommonServerInfo{
239255
ID: v.ID,

service/singleton/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func loadServers() {
4040
CPU: []string{},
4141
GPU: []string{},
4242
}
43+
// 显式调用初始化方法,确保数组正确
44+
innerS.Host.Initialize()
45+
4346
innerS.State = &model.HostState{}
4447
innerS.LastStateBeforeOffline = nil
4548
innerS.IsOnline = false // 初始状态为离线,等待agent报告

0 commit comments

Comments
 (0)