修复用户分布地图显示问题
- 修改后端返回正确的国家英文名称(China)以匹配世界地图 - 正确计算在线/离线设备数量 - 确保数据一致性:总数 = 在线 + 离线
This commit is contained in:
@@ -115,27 +115,34 @@ func handleGetDashboard(c *gin.Context) {
|
|||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
if len(appIDs) > 0 {
|
if len(appIDs) > 0 {
|
||||||
type DeviceCount struct {
|
var totalOnline int64
|
||||||
DeviceType string
|
var totalOffline int64
|
||||||
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)
|
|
||||||
|
|
||||||
provinces := make([]gin.H, 0, len(deviceCounts))
|
var devices []model.UserDevice
|
||||||
for _, dc := range deviceCounts {
|
database.DB.Where("application_id IN ?", appIDs).Find(&devices)
|
||||||
provinces = append(provinces, gin.H{
|
|
||||||
"name": dc.DeviceType,
|
for _, device := range devices {
|
||||||
"count": dc.Count,
|
var sessionCount int64
|
||||||
"online": 0,
|
database.DB.Model(&model.DeviceSession{}).
|
||||||
"offline": dc.Count,
|
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()
|
mu.Lock()
|
||||||
userDistribution["provinces"] = provinces
|
userDistribution["provinces"] = provinces
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
|
|||||||
Reference in New Issue
Block a user