Handle NULL/string peer totals in Amnezia config

pull/1107/head
Super User 2026-01-28 22:48:59 -05:00
parent 0f98bb2537
commit 306f00134f
1 changed files with 27 additions and 4 deletions

View File

@ -11,6 +11,23 @@ from .WireguardConfiguration import WireguardConfiguration
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 AmneziaWireguardConfiguration(WireguardConfiguration):
def __init__(self, DashboardConfig,
AllPeerJobs: PeerJobs,
@ -31,6 +48,12 @@ class AmneziaWireguardConfiguration(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,
@ -45,9 +68,9 @@ class AmneziaWireguardConfiguration(WireguardConfiguration):
"SaveConfig": self.SaveConfig,
"Info": self.configurationInfo.model_dump(),
"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),
@ -319,4 +342,4 @@ class AmneziaWireguardConfiguration(WireguardConfiguration):
with self.engine.connect() as conn:
restricted = conn.execute(self.peersRestrictedTable.select()).mappings().fetchall()
for i in restricted:
self.RestrictedPeers.append(AmneziaWGPeer(i, self))
self.RestrictedPeers.append(AmneziaWGPeer(i, self))