A potential fix for #854

Currently it is showing a sum of all interfaces on sent and receive... not sure if that's right
This commit is contained in:
Donald Zou
2025-08-27 11:42:06 +08:00
parent f865317600
commit 48ec4c7f6f
2 changed files with 51 additions and 18 deletions

View File

@@ -1,3 +1,5 @@
import time
import threading
import psutil import psutil
class SystemStatus: class SystemStatus:
def __init__(self): def __init__(self):
@@ -8,6 +10,17 @@ class SystemStatus:
self.NetworkInterfaces = NetworkInterfaces() self.NetworkInterfaces = NetworkInterfaces()
self.Processes = Processes() self.Processes = Processes()
def toJson(self): 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 { return {
"CPU": self.CPU, "CPU": self.CPU,
"Memory": { "Memory": {
@@ -25,11 +38,25 @@ class CPU:
self.cpu_percent: float = 0 self.cpu_percent: float = 0
self.cpu_percent_per_cpu: list[float] = [] self.cpu_percent_per_cpu: list[float] = []
def getData(self): 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: try:
self.cpu_percent_per_cpu = psutil.cpu_percent(interval=0.5, percpu=True) self.cpu_percent = psutil.cpu_percent(interval=1)
self.cpu_percent = psutil.cpu_percent(interval=0.5)
except Exception as e: except Exception as e:
pass 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): def toJson(self):
self.getData() self.getData()
return self.__dict__ return self.__dict__
@@ -95,10 +122,16 @@ class NetworkInterfaces:
network = psutil.net_io_counters(pernic=True, nowrap=True) network = psutil.net_io_counters(pernic=True, nowrap=True)
for i in network.keys(): for i in network.keys():
self.interfaces[i] = network[i]._asdict() 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: except Exception as e:
pass print(str(e))
def toJson(self): def toJson(self):
self.getData()
return self.interfaces return self.interfaces
class Process: class Process:

View File

@@ -74,23 +74,23 @@ const getData = () => {
historicalCpuUsage.value.push(res.data.CPU.cpu_percent) historicalCpuUsage.value.push(res.data.CPU.cpu_percent)
historicalVirtualMemoryUsage.value.push(res.data.Memory.VirtualMemory.percent) historicalVirtualMemoryUsage.value.push(res.data.Memory.VirtualMemory.percent)
historicalSwapMemoryUsage.value.push(res.data.Memory.SwapMemory.percent) historicalSwapMemoryUsage.value.push(res.data.Memory.SwapMemory.percent)
historicalNetworkData.value.bytes_recv.push( historicalNetworkSpeed.value.bytes_recv.push(
Object.values(res.data.NetworkInterfaces).map(x => x.bytes_recv).reduce((x, y) => x + y) Object.values(res.data.NetworkInterfaces).map(x => x.realtime.recv).reduce((x, y) => x + y)
) )
historicalNetworkData.value.bytes_sent.push( historicalNetworkSpeed.value.bytes_sent.push(
Object.values(res.data.NetworkInterfaces).map(x => x.bytes_sent).reduce((x, y) => x + y) 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){ // if (historicalNetworkData.value.bytes_recv.length === 1 && historicalNetworkData.value.bytes_sent.length === 1){
historicalNetworkSpeed.value.bytes_recv.push(0) // historicalNetworkSpeed.value.bytes_recv.push(0)
historicalNetworkSpeed.value.bytes_sent.push(0) // historicalNetworkSpeed.value.bytes_sent.push(0)
}else{ // }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_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] // 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_recv.push(Math.round(((bytes_recv_diff / 1024 / 1024) + Number.EPSILON) * 10000) / 10000)
historicalNetworkSpeed.value.bytes_sent.push(Math.round((bytes_sent_diff / 1024000 + Number.EPSILON) * 10000) / 10000) // historicalNetworkSpeed.value.bytes_sent.push(Math.round(((bytes_sent_diff / 1024 / 1024) + Number.EPSILON) * 10000) / 10000)
} // }
}) })
} }