mirror of
https://github.com/donaldzou/WGDashboard.git
synced 2025-10-03 07:46:18 +00:00
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:
@@ -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:
|
||||
|
@@ -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)
|
||||
// }
|
||||
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user