From 48ec4c7f6fbc685a8dd224637b0663a3cf8d1c4a Mon Sep 17 00:00:00 2001 From: Donald Zou Date: Wed, 27 Aug 2025 11:42:06 +0800 Subject: [PATCH] A potential fix for #854 Currently it is showing a sum of all interfaces on sent and receive... not sure if that's right --- src/modules/SystemStatus.py | 41 ++++++++++++++++++++--- src/static/app/src/views/systemStatus.vue | 28 ++++++++-------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/modules/SystemStatus.py b/src/modules/SystemStatus.py index 26d2390b..fd927327 100644 --- a/src/modules/SystemStatus.py +++ b/src/modules/SystemStatus.py @@ -1,3 +1,5 @@ +import time +import threading import psutil class SystemStatus: def __init__(self): @@ -8,6 +10,17 @@ class SystemStatus: self.NetworkInterfaces = NetworkInterfaces() self.Processes = Processes() def toJson(self): + process = [ + threading.Thread(target=self.CPU.getCPUPercent), + threading.Thread(target=self.CPU.getPerCPUPercent), + threading.Thread(target=self.NetworkInterfaces.getData) + ] + for p in process: + p.start() + for p in process: + p.join() + + return { "CPU": self.CPU, "Memory": { @@ -25,11 +38,25 @@ class CPU: self.cpu_percent: float = 0 self.cpu_percent_per_cpu: list[float] = [] def getData(self): + pass + # try: + # self.cpu_percent_per_cpu = psutil.cpu_percent(interval=1, percpu=True) + # + # except Exception as e: + # pass + def getCPUPercent(self): try: - self.cpu_percent_per_cpu = psutil.cpu_percent(interval=0.5, percpu=True) - self.cpu_percent = psutil.cpu_percent(interval=0.5) + self.cpu_percent = psutil.cpu_percent(interval=1) except Exception as e: pass + + def getPerCPUPercent(self): + try: + self.cpu_percent_per_cpu = psutil.cpu_percent(interval=1, percpu=True) + + except Exception as e: + pass + def toJson(self): self.getData() return self.__dict__ @@ -95,10 +122,16 @@ class NetworkInterfaces: network = psutil.net_io_counters(pernic=True, nowrap=True) for i in network.keys(): self.interfaces[i] = network[i]._asdict() + time.sleep(1) + network = psutil.net_io_counters(pernic=True, nowrap=True) + for i in network.keys(): + self.interfaces[i]['realtime'] = { + 'sent': round((network[i].bytes_sent - self.interfaces[i]['bytes_sent']) / 1024 / 1024, 4), + 'recv': round((network[i].bytes_recv - self.interfaces[i]['bytes_recv']) / 1024 / 1024, 4) + } except Exception as e: - pass + print(str(e)) def toJson(self): - self.getData() return self.interfaces class Process: diff --git a/src/static/app/src/views/systemStatus.vue b/src/static/app/src/views/systemStatus.vue index 288de0eb..7ab46d3d 100644 --- a/src/static/app/src/views/systemStatus.vue +++ b/src/static/app/src/views/systemStatus.vue @@ -74,23 +74,23 @@ const getData = () => { historicalCpuUsage.value.push(res.data.CPU.cpu_percent) historicalVirtualMemoryUsage.value.push(res.data.Memory.VirtualMemory.percent) historicalSwapMemoryUsage.value.push(res.data.Memory.SwapMemory.percent) - - historicalNetworkData.value.bytes_recv.push( - Object.values(res.data.NetworkInterfaces).map(x => x.bytes_recv).reduce((x, y) => x + y) + + historicalNetworkSpeed.value.bytes_recv.push( + Object.values(res.data.NetworkInterfaces).map(x => x.realtime.recv).reduce((x, y) => x + y) ) - historicalNetworkData.value.bytes_sent.push( - Object.values(res.data.NetworkInterfaces).map(x => x.bytes_sent).reduce((x, y) => x + y) + historicalNetworkSpeed.value.bytes_sent.push( + Object.values(res.data.NetworkInterfaces).map(x => x.realtime.sent).reduce((x, y) => x + y) ) - if (historicalNetworkData.value.bytes_recv.length === 1 && historicalNetworkData.value.bytes_sent.length === 1){ - historicalNetworkSpeed.value.bytes_recv.push(0) - historicalNetworkSpeed.value.bytes_sent.push(0) - }else{ - let bytes_recv_diff = historicalNetworkData.value.bytes_recv[historicalNetworkData.value.bytes_recv.length - 1] - historicalNetworkData.value.bytes_recv[historicalNetworkData.value.bytes_recv.length - 2] - let bytes_sent_diff = historicalNetworkData.value.bytes_sent[historicalNetworkData.value.bytes_sent.length - 1] - historicalNetworkData.value.bytes_sent[historicalNetworkData.value.bytes_sent.length - 2] - historicalNetworkSpeed.value.bytes_recv.push(Math.round((bytes_recv_diff / 1024000 + Number.EPSILON) * 10000) / 10000) - historicalNetworkSpeed.value.bytes_sent.push(Math.round((bytes_sent_diff / 1024000 + Number.EPSILON) * 10000) / 10000) - } + // if (historicalNetworkData.value.bytes_recv.length === 1 && historicalNetworkData.value.bytes_sent.length === 1){ + // historicalNetworkSpeed.value.bytes_recv.push(0) + // historicalNetworkSpeed.value.bytes_sent.push(0) + // }else{ + // let bytes_recv_diff = historicalNetworkData.value.bytes_recv[historicalNetworkData.value.bytes_recv.length - 1] - historicalNetworkData.value.bytes_recv[historicalNetworkData.value.bytes_recv.length - 2] + // let bytes_sent_diff = historicalNetworkData.value.bytes_sent[historicalNetworkData.value.bytes_sent.length - 1] - historicalNetworkData.value.bytes_sent[historicalNetworkData.value.bytes_sent.length - 2] + // historicalNetworkSpeed.value.bytes_recv.push(Math.round(((bytes_recv_diff / 1024 / 1024) + Number.EPSILON) * 10000) / 10000) + // historicalNetworkSpeed.value.bytes_sent.push(Math.round(((bytes_sent_diff / 1024 / 1024) + Number.EPSILON) * 10000) / 10000) + // } }) }