fix: 错误计算设备总数问题

This commit is contained in:
ydajiang
2025-09-24 18:31:28 +08:00
parent 9b663cb7b5
commit ae154dfb0a
3 changed files with 12 additions and 17 deletions

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"gb-cms/common" "gb-cms/common"
"gb-cms/dao"
"gb-cms/log" "gb-cms/log"
"gb-cms/stack" "gb-cms/stack"
"net/http" "net/http"
@@ -132,7 +131,7 @@ func registerLiveGBSApi() {
ChannelOnline: ChannelOnlineCount, ChannelOnline: ChannelOnlineCount,
ChannelTotal: ChannelTotalCount, ChannelTotal: ChannelTotalCount,
DeviceOnline: stack.OnlineDeviceManager.Count(), DeviceOnline: stack.OnlineDeviceManager.Count(),
DeviceTotal: dao.DeviceCount, DeviceTotal: DeviceCount,
} }
_ = common.HttpResponseSuccess(writer, response) _ = common.HttpResponseSuccess(writer, response)

View File

@@ -8,10 +8,6 @@ import (
"time" "time"
) )
var (
DeviceCount int
)
type DeviceModel struct { type DeviceModel struct {
GBModel GBModel
DeviceID string `json:"device_id" gorm:"index"` DeviceID string `json:"device_id" gorm:"index"`
@@ -64,7 +60,6 @@ func (d *daoDevice) LoadDevices() (map[string]*DeviceModel, error) {
deviceMap[device.DeviceID] = device deviceMap[device.DeviceID] = device
} }
DeviceCount = len(devices)
return deviceMap, nil return deviceMap, nil
} }
@@ -77,11 +72,7 @@ func (d *daoDevice) SaveDevice(device *DeviceModel) error {
if device.ID == 0 { if device.ID == 0 {
//return tx.Create(&old).Error //return tx.Create(&old).Error
err := tx.Save(device).Error return tx.Save(device).Error
if err == nil {
DeviceCount++
}
return err
} else { } else {
return tx.Model(device).Select("Transport", "RemoteIP", "RemotePort", "Status", "RegisterTime", "LastHeartbeat").Updates(*device).Error return tx.Model(device).Select("Transport", "RemoteIP", "RemotePort", "Status", "RegisterTime", "LastHeartbeat").Updates(*device).Error
} }
@@ -109,11 +100,6 @@ func (d *daoDevice) UpdateDeviceInfo(deviceId string, device *DeviceModel) error
func (d *daoDevice) UpdateDeviceStatus(deviceId string, status common.OnlineStatus) error { func (d *daoDevice) UpdateDeviceStatus(deviceId string, status common.OnlineStatus) error {
return DBTransaction(func(tx *gorm.DB) error { return DBTransaction(func(tx *gorm.DB) error {
if status == common.ON {
DeviceCount++
} else if status == common.OFF {
DeviceCount--
}
return tx.Model(&DeviceModel{}).Where("device_id =?", deviceId).Update("status", status).Error return tx.Model(&DeviceModel{}).Where("device_id =?", deviceId).Update("status", status).Error
}) })
} }
@@ -237,3 +223,9 @@ func (d *daoDevice) DeleteDevicesByUA(ua string) error {
return tx.Where("user_agent =?", ua).Unscoped().Delete(&DeviceModel{}).Error return tx.Where("user_agent =?", ua).Unscoped().Delete(&DeviceModel{}).Error
}) })
} }
func (d *daoDevice) Count() (int, error) {
var count int64
db.Model(&DeviceModel{}).Count(&count)
return int(count), nil
}

View File

@@ -27,6 +27,7 @@ var (
ChannelTotalCount int // 包含目录 ChannelTotalCount int // 包含目录
ChannelOnlineCount int // 不包含目录 ChannelOnlineCount int // 不包含目录
DeviceCount int // 设备基数
) )
const ( const (
@@ -445,6 +446,9 @@ func StartStats() {
} else { } else {
ChannelOnlineCount = onlineCount ChannelOnlineCount = onlineCount
} }
// 统计设备总数
DeviceCount, _ = dao.Device.Count()
} }
count++ count++