diff --git a/api_livegbs.go b/api_livegbs.go index e82a1e6..623c768 100644 --- a/api_livegbs.go +++ b/api_livegbs.go @@ -3,7 +3,6 @@ package main import ( "fmt" "gb-cms/common" - "gb-cms/dao" "gb-cms/log" "gb-cms/stack" "net/http" @@ -132,7 +131,7 @@ func registerLiveGBSApi() { ChannelOnline: ChannelOnlineCount, ChannelTotal: ChannelTotalCount, DeviceOnline: stack.OnlineDeviceManager.Count(), - DeviceTotal: dao.DeviceCount, + DeviceTotal: DeviceCount, } _ = common.HttpResponseSuccess(writer, response) diff --git a/dao/device.go b/dao/device.go index 884d260..f40e5fd 100644 --- a/dao/device.go +++ b/dao/device.go @@ -8,10 +8,6 @@ import ( "time" ) -var ( - DeviceCount int -) - type DeviceModel struct { GBModel DeviceID string `json:"device_id" gorm:"index"` @@ -64,7 +60,6 @@ func (d *daoDevice) LoadDevices() (map[string]*DeviceModel, error) { deviceMap[device.DeviceID] = device } - DeviceCount = len(devices) return deviceMap, nil } @@ -77,11 +72,7 @@ func (d *daoDevice) SaveDevice(device *DeviceModel) error { if device.ID == 0 { //return tx.Create(&old).Error - err := tx.Save(device).Error - if err == nil { - DeviceCount++ - } - return err + return tx.Save(device).Error } else { 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 { 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 }) } @@ -237,3 +223,9 @@ func (d *daoDevice) DeleteDevicesByUA(ua string) 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 +} diff --git a/stats.go b/stats.go index 9763448..be7fbcb 100644 --- a/stats.go +++ b/stats.go @@ -27,6 +27,7 @@ var ( ChannelTotalCount int // 包含目录 ChannelOnlineCount int // 不包含目录 + DeviceCount int // 设备基数 ) const ( @@ -445,6 +446,9 @@ func StartStats() { } else { ChannelOnlineCount = onlineCount } + + // 统计设备总数 + DeviceCount, _ = dao.Device.Count() } count++