From ec105ede837be77f5396794306acdfe3cfa89734 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Thu, 6 Jul 2023 14:28:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BF=BD=E7=95=A5=E7=9B=91=E6=8E=A7?= =?UTF-8?q?=E9=87=87=E9=9B=86=E7=9A=84=E5=BC=82=E5=B8=B8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=20(#1559)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/monitor.go | 47 ++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/backend/app/service/monitor.go b/backend/app/service/monitor.go index c06847584..3fa543923 100644 --- a/backend/app/service/monitor.go +++ b/backend/app/service/monitor.go @@ -76,17 +76,31 @@ func loadDiskIO() { if io2.Name == io1.Name { var itemIO model.MonitorIO itemIO.Name = io1.Name - itemIO.Read = uint64(float64(io2.ReadBytes-io1.ReadBytes) / 60) - itemIO.Write = uint64(float64(io2.WriteBytes-io1.WriteBytes) / 60) + if io2.ReadBytes != 0 && io1.ReadBytes != 0 { + itemIO.Read = uint64(float64(io2.ReadBytes-io1.ReadBytes) / 60) + } + if io2.WriteBytes != 0 && io1.WriteBytes != 0 { + itemIO.Write = uint64(float64(io2.WriteBytes-io1.WriteBytes) / 60) + } - itemIO.Count = uint64(float64(io2.ReadCount-io1.ReadCount) / 60) - writeCount := uint64(float64(io2.WriteCount-io1.WriteCount) / 60) + if io2.ReadCount != 0 && io1.ReadCount != 0 { + itemIO.Count = uint64(float64(io2.ReadCount-io1.ReadCount) / 60) + } + writeCount := uint64(0) + if io2.WriteCount != 0 && io1.WriteCount != 0 { + writeCount = uint64(float64(io2.WriteCount-io1.WriteCount) / 60) + } if writeCount > itemIO.Count { itemIO.Count = writeCount } - itemIO.Time = uint64(float64(io2.ReadTime-io1.ReadTime) / 60) - writeTime := uint64(float64(io2.WriteTime-io1.WriteTime) / 60) + if io2.ReadTime != 0 && io1.ReadTime != 0 { + itemIO.Time = uint64(float64(io2.ReadTime-io1.ReadTime) / 60) + } + writeTime := uint64(0) + if io2.WriteTime != 0 && io1.WriteTime != 0 { + writeTime = uint64(float64(io2.WriteTime-io1.WriteTime) / 60) + } if writeTime > itemIO.Time { itemIO.Time = writeTime } @@ -113,8 +127,13 @@ func loadNetIO() { if net2.Name == net1.Name { var itemNet model.MonitorNetwork itemNet.Name = net1.Name - itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / 60 - itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / 60 + + if net2.BytesSent != 0 && net1.BytesSent != 0 { + itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / 60 + } + if net2.BytesRecv != 0 && net1.BytesRecv != 0 { + itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / 60 + } netList = append(netList, itemNet) break } @@ -123,14 +142,16 @@ func loadNetIO() { netStatAll2, _ := net.IOCounters(false) for _, net2 := range netStatAll2 { for _, net1 := range netStatAll { - if net1.BytesSent == 0 || net1.BytesRecv == 0 { - continue - } if net2.Name == net1.Name { var itemNet model.MonitorNetwork itemNet.Name = net1.Name - itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / 60 - itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / 60 + if net2.BytesSent != 0 && net1.BytesSent != 0 { + itemNet.Up = float64(net2.BytesSent-net1.BytesSent) / 1024 / 60 + } + + if net2.BytesRecv != 0 && net1.BytesRecv != 0 { + itemNet.Down = float64(net2.BytesRecv-net1.BytesRecv) / 1024 / 60 + } netList = append(netList, itemNet) break }