修复前端节点在线情况
Some checks failed
build / build (api, amd64, linux) (push) Failing after -50s
build / build (api, arm64, linux) (push) Failing after -52s
build / build (api.exe, amd64, windows) (push) Failing after -52s

This commit is contained in:
CN-JS-HuiBai
2026-04-18 02:32:03 +08:00
parent 8cf8bd6724
commit 6e75b7d7d5

View File

@@ -213,7 +213,51 @@ func UserServerFetch(c *gin.Context) {
Fail(c, 500, "failed to fetch servers")
return
}
Success(c, servers)
// Fetch all servers for parent lookup
var allServers []model.Server
if err := database.DB.Find(&allServers).Error; err != nil {
Fail(c, 500, "failed to fetch all servers")
return
}
serverMap := make(map[int]model.Server, len(allServers))
for _, s := range allServers {
serverMap[s.ID] = s
}
now := time.Now().Unix()
result := make([]gin.H, 0, len(servers))
for _, server := range servers {
lastCheckAt, _ := database.CacheGetJSON[int64](nodeLastCheckKey(&server))
isOnline := 0
if lastCheckAt > 0 && now-lastCheckAt <= 300 {
isOnline = 1
}
// Inherit online status from parent if current is offline
if isOnline == 0 && server.ParentID != nil {
if parent, ok := serverMap[*server.ParentID]; ok {
pLastCheckAt, _ := database.CacheGetJSON[int64](nodeLastCheckKey(&parent))
if pLastCheckAt > 0 && now-pLastCheckAt <= 300 {
isOnline = 1
lastCheckAt = pLastCheckAt
}
}
}
result = append(result, gin.H{
"id": server.ID,
"type": server.Type,
"name": server.Name,
"rate": server.Rate,
"tags": server.Tags,
"is_online": isOnline,
"last_check_at": lastCheckAt,
"cache_key": fmt.Sprintf("%s_%d_%d", server.Type, server.ID, server.UpdatedAt.Unix()),
})
}
Success(c, result)
}
func currentUser(c *gin.Context) (*model.User, bool) {