fix: 恢复Agent指标并修复时间弹层

- 使用按主键更新避免SQLite字段错误
- 自定义时间选择器改用Portal避免卡片遮挡
This commit is contained in:
Logan.Li committed 2026-07-25 15:51:12 +08:00
1 parent af98940211
commit d9106dde6e
2 files changed
+32 -58

No files matched your search

+13 -30
View File
@@ -127,51 +127,34 @@ func (h *AgentHandler) ReceiveReport(c *gin.Context) {
memMB := int64(memGB * 1024) // GB → MB
diskGB := int64(totalDiskGB + 0.5) // 39.99 → 40
// 直接 SQL 更新 asset_servers 监控字段
// 若 AgentVersion 非空则同步写入,否则保留数据库中已有版本号(防止旧版Agent空字段覆盖已设置的版本)
updateSQL := `UPDATE asset_servers SET
cpu_usage = ?, cpu_iowait = ?, memory_usage = ?, disk_usage = ?,
cpu = ?, memory = ?, disk = ?,
status = ?, agent_status = ?` + func() string {
if req.AgentVersion != "" {
return `, agent_version = ?`
}
return ``
}()
args := []interface{}{
req.CPU.UsagePercent, req.CPU.IOWait, req.Memory.UsagePercent, diskAvg,
req.CPU.CoreCount, memMB, diskGB,
"running", "online",
// 使用 map 按主键更新,避免可选系统字段导致原始 SQL 占位参数错位。 // Logan.Li
updates := map[string]interface{}{
"cpu_usage": req.CPU.UsagePercent, "memory_usage": req.Memory.UsagePercent,
"disk_usage": diskAvg,
"cpu": req.CPU.CoreCount, "memory": memMB, "disk": diskGB,
"status": "running", "agent_status": "online", "updated_at": time.Now(),
}
if req.AgentVersion != "" {
args = append(args, req.AgentVersion)
updates["agent_version"] = req.AgentVersion
}
if req.System != nil {
if req.System.OSVersion != "" {
updateSQL += ", os = ?"
args = append(args, req.System.OSVersion)
updates["os"] = req.System.OSVersion
}
if req.System.Kernel != "" {
updateSQL += ", kernel = ?"
args = append(args, req.System.Kernel)
updates["kernel"] = req.System.Kernel
}
if req.System.Uptime > 0 {
updateSQL += ", uptime = ?"
args = append(args, req.System.Uptime)
updates["uptime"] = req.System.Uptime
}
}
updateSQL += ", updated_at = ?"
args = append(args, time.Now())
var result *gorm.DB
if assetMatched {
updateSQL += " WHERE id = ?"
args = append(args, matchedServer.ID)
result = h.db.Model(&model.Server{}).Where("id = ?", matchedServer.ID).Updates(updates)
} else {
updateSQL += " WHERE hostname = ? OR ip = ?"
args = append(args, req.Hostname, matchIP)
result = h.db.Model(&model.Server{}).Where("hostname = ? OR ip = ?", req.Hostname, matchIP).Updates(updates)
}
result := h.db.Exec(updateSQL, args...)
// 如果还是没匹配到 —— 按 hostname 显式查重后再注册 // Logan.Li
// asset_servers.hostname 无 UNIQUE 索引,OnConflict 不可靠,故先 First 查重,
// 避免 Agent 每次上报(10s)都新建记录导致资产越来越多。