refactor(analytic): improved network stat #913

This commit is contained in:
Jacky 2025-03-15 09:09:53 +08:00
parent ec29a77f99
commit 789698a0d4
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
6 changed files with 247 additions and 108 deletions

View file

@ -11,7 +11,6 @@ import (
"github.com/shirou/gopsutil/v4/cpu"
"github.com/shirou/gopsutil/v4/host"
"github.com/shirou/gopsutil/v4/load"
"github.com/shirou/gopsutil/v4/net"
"github.com/spf13/cast"
"github.com/uozi-tech/cosy/logger"
@ -75,15 +74,13 @@ func Analytic(c *gin.Context) {
continue
}
network, err := net.IOCounters(false)
network, err := analytic.GetNetworkStat()
if err != nil {
logger.Error(err)
continue
}
if len(network) > 0 {
stat.Network = network[0]
}
stat.Network = *network
// write
err = ws.WriteJSON(stat)
@ -104,7 +101,7 @@ func GetAnalyticInit(c *gin.Context) {
logger.Error(err)
}
network, err := net.IOCounters(false)
network, err := analytic.GetNetworkStat()
if err != nil {
logger.Error(err)
}
@ -119,11 +116,6 @@ func GetAnalyticInit(c *gin.Context) {
logger.Error(err)
}
var _net net.IOCountersStat
if len(network) > 0 {
_net = network[0]
}
hostInfo, err := host.Info()
if err != nil {
logger.Error(err)
@ -151,7 +143,7 @@ func GetAnalyticInit(c *gin.Context) {
Total: analytic.CpuTotalRecord,
},
Network: NetworkRecords{
Init: _net,
Init: *network,
BytesRecv: analytic.NetRecvRecord,
BytesSent: analytic.NetSentRecord,
},