1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-11-21 15:51:52 -05:00

Update PacketList

This patch updates some of the code in PacketList to be
a bit more efficient.  Thanks to the Cursor IDE :P
This commit is contained in:
Hemna 2024-11-05 11:34:12 -05:00
parent add18f1a6f
commit 7e8d7cdf86

View File

@ -37,9 +37,10 @@ class PacketList(objectstore.ObjectStoreMixin):
self._total_rx += 1 self._total_rx += 1
self._add(packet) self._add(packet)
ptype = packet.__class__.__name__ ptype = packet.__class__.__name__
if ptype not in self.data["types"]: type_stats = self.data["types"].setdefault(
self.data["types"][ptype] = {"tx": 0, "rx": 0} ptype, {"tx": 0, "rx": 0},
self.data["types"][ptype]["rx"] += 1 )
type_stats["rx"] += 1
def tx(self, packet: type[core.Packet]): def tx(self, packet: type[core.Packet]):
"""Add a packet that was received.""" """Add a packet that was received."""
@ -47,9 +48,10 @@ class PacketList(objectstore.ObjectStoreMixin):
self._total_tx += 1 self._total_tx += 1
self._add(packet) self._add(packet)
ptype = packet.__class__.__name__ ptype = packet.__class__.__name__
if ptype not in self.data["types"]: type_stats = self.data["types"].setdefault(
self.data["types"][ptype] = {"tx": 0, "rx": 0} ptype, {"tx": 0, "rx": 0},
self.data["types"][ptype]["tx"] += 1 )
type_stats["tx"] += 1
def add(self, packet): def add(self, packet):
with self.lock: with self.lock:
@ -81,28 +83,16 @@ class PacketList(objectstore.ObjectStoreMixin):
return self._total_tx return self._total_tx
def stats(self, serializable=False) -> dict: def stats(self, serializable=False) -> dict:
# limit the number of packets to return to 50
with self.lock: with self.lock:
tmp = OrderedDict( # Get last N packets directly using list slicing
reversed( packets_list = list(self.data.get("packets", {}).values())
list( pkts = packets_list[-CONF.packet_list_stats_maxlen:][::-1]
self.data.get("packets", OrderedDict()).items(),
),
),
)
pkts = []
count = 1
for packet in tmp:
pkts.append(tmp[packet])
count += 1
if count > CONF.packet_list_stats_maxlen:
break
stats = { stats = {
"total_tracked": self._total_rx + self._total_rx, "total_tracked": self._total_rx + self._total_tx, # Fixed typo: was rx + rx
"rx": self._total_rx, "rx": self._total_rx,
"tx": self._total_tx, "tx": self._total_tx,
"types": self.data.get("types", []), "types": self.data.get("types", {}), # Changed default from [] to {}
"packet_count": len(self.data.get("packets", [])), "packet_count": len(self.data.get("packets", [])),
"maxlen": self.maxlen, "maxlen": self.maxlen,
"packets": pkts, "packets": pkts,