Fix null peer totals in DataUsage

pull/1106/head
Super User 2026-01-28 22:30:38 -05:00
parent 0f98bb2537
commit 935ce15626
1 changed files with 27 additions and 4 deletions

View File

@ -21,6 +21,23 @@ from .WireguardConfigurationInfo import WireguardConfigurationInfo, PeerGroupsCl
from .DashboardWebHooks import DashboardWebHooks
def _safe_int(value) -> int:
try:
if value is None:
return 0
if isinstance(value, bool):
return int(value)
if isinstance(value, (int, float)):
return int(value)
if isinstance(value, str):
if value.strip() == "":
return 0
return int(float(value))
return int(value)
except Exception:
return 0
class WireguardConfiguration:
class InvalidConfigurationFileException(Exception):
def __init__(self, m):
@ -871,6 +888,12 @@ class WireguardConfiguration:
def toJson(self):
self.Status = self.getStatus()
def peer_total(peer):
return _safe_int(peer.cumu_data) + _safe_int(peer.total_data)
def peer_sent(peer):
return _safe_int(peer.cumu_sent) + _safe_int(peer.total_sent)
def peer_receive(peer):
return _safe_int(peer.cumu_receive) + _safe_int(peer.total_receive)
return {
"Status": self.Status,
"Name": self.Name,
@ -884,9 +907,9 @@ class WireguardConfiguration:
"PostDown": self.PostDown,
"SaveConfig": self.SaveConfig,
"DataUsage": {
"Total": sum(list(map(lambda x: x.cumu_data + x.total_data, self.Peers))),
"Sent": sum(list(map(lambda x: x.cumu_sent + x.total_sent, self.Peers))),
"Receive": sum(list(map(lambda x: x.cumu_receive + x.total_receive, self.Peers)))
"Total": sum(list(map(peer_total, self.Peers))),
"Sent": sum(list(map(peer_sent, self.Peers))),
"Receive": sum(list(map(peer_receive, self.Peers)))
},
"ConnectedPeers": len(list(filter(lambda x: x.status == "running", self.Peers))),
"TotalPeers": len(self.Peers),
@ -1291,4 +1314,4 @@ class WireguardConfiguration:
conn.execute(sqlalchemy.text('VACUUM;'))
except Exception as e:
return False
return True
return True