修复用户分布地图显示问题
- 修改后端返回正确的国家英文名称(China)以匹配世界地图 - 正确计算在线/离线设备数量 - 确保数据一致性:总数 = 在线 + 离线
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user