From 5e7995542862d7d3905068c2390a3f91ed58edf6 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 8 May 2026 15:58:18 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7=E5=88=86?= =?UTF-8?q?=E5=B8=83=E5=9C=B0=E5=9B=BE=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改后端返回正确的国家英文名称(China)以匹配世界地图 - 正确计算在线/离线设备数量 - 确保数据一致性:总数 = 在线 + 离线 --- backend/internal/router/admin/dashboard.go | 45 +++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/backend/internal/router/admin/dashboard.go b/backend/internal/router/admin/dashboard.go index 8b06b34..7b5b126 100644 --- a/backend/internal/router/admin/dashboard.go +++ b/backend/internal/router/admin/dashboard.go @@ -115,27 +115,34 @@ func handleGetDashboard(c *gin.Context) { go func() { defer wg.Done() if len(appIDs) > 0 { - type DeviceCount struct { - DeviceType string - Count int - } - var deviceCounts []DeviceCount - database.DB.Model(&model.UserDevice{}). - Select("device_type, COUNT(*) as count"). - Where("application_id IN ?", appIDs). - Group("device_type"). - Order("count DESC"). - Find(&deviceCounts) + var totalOnline int64 + var totalOffline int64 - provinces := make([]gin.H, 0, len(deviceCounts)) - for _, dc := range deviceCounts { - provinces = append(provinces, gin.H{ - "name": dc.DeviceType, - "count": dc.Count, - "online": 0, - "offline": dc.Count, - }) + var devices []model.UserDevice + database.DB.Where("application_id IN ?", appIDs).Find(&devices) + + for _, device := range devices { + var sessionCount int64 + database.DB.Model(&model.DeviceSession{}). + Where("device_id = ? AND last_heartbeat > ?", device.ID, time.Now().Add(-5*time.Minute)). + Count(&sessionCount) + + if sessionCount > 0 { + totalOnline++ + } else { + totalOffline++ + } } + + provinces := []gin.H{ + { + "name": "China", + "count": totalOnline + totalOffline, + "online": totalOnline, + "offline": totalOffline, + }, + } + mu.Lock() userDistribution["provinces"] = provinces mu.Unlock()