From dc4879a367dd43720d21a2b8cbe65a0568ae0a6e Mon Sep 17 00:00:00 2001 From: Hemna Date: Tue, 16 Apr 2024 11:06:38 -0400 Subject: [PATCH] Changed Stats Collector registration This patch changes the stats Collector object registration to take a class name instead of an object. This allows the app to start up and fetch the configuration correctly so that when objects are created the CONF has the proper values. This is so singleton objects can assign settings values at creation time. --- aprsd/packets/packet_list.py | 4 ---- aprsd/stats/__init__.py | 18 +++++++++--------- aprsd/stats/collector.py | 21 +++++++++++---------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/aprsd/packets/packet_list.py b/aprsd/packets/packet_list.py index f492797..1ed2ec6 100644 --- a/aprsd/packets/packet_list.py +++ b/aprsd/packets/packet_list.py @@ -67,10 +67,6 @@ class PacketList(objectstore.ObjectStoreMixin): def copy(self): return self.data.copy() - @wrapt.synchronized(lock) - def set_maxlen(self, maxlen): - self.maxlen = maxlen - @wrapt.synchronized(lock) def find(self, packet): return self.data["packets"][packet.key] diff --git a/aprsd/stats/__init__.py b/aprsd/stats/__init__.py index 44602fd..759698a 100644 --- a/aprsd/stats/__init__.py +++ b/aprsd/stats/__init__.py @@ -9,12 +9,12 @@ from aprsd.threads import aprsd # Create the collector and register all the objects # that APRSD has that implement the stats protocol stats_collector = collector.Collector() -stats_collector.register_producer(app.APRSDStats()) -stats_collector.register_producer(packet_list.PacketList()) -stats_collector.register_producer(watch_list.WatchList()) -stats_collector.register_producer(tracker.PacketTrack()) -stats_collector.register_producer(plugin.PluginManager()) -stats_collector.register_producer(aprsd.APRSDThreadList()) -stats_collector.register_producer(email.EmailStats()) -stats_collector.register_producer(aprs_client.APRSClientStats()) -stats_collector.register_producer(seen_list.SeenList()) +stats_collector.register_producer(app.APRSDStats) +stats_collector.register_producer(packet_list.PacketList) +stats_collector.register_producer(watch_list.WatchList) +stats_collector.register_producer(tracker.PacketTrack) +stats_collector.register_producer(plugin.PluginManager) +stats_collector.register_producer(aprsd.APRSDThreadList) +stats_collector.register_producer(email.EmailStats) +stats_collector.register_producer(aprs_client.APRSClientStats) +stats_collector.register_producer(seen_list.SeenList) diff --git a/aprsd/stats/collector.py b/aprsd/stats/collector.py index c58b242..3325669 100644 --- a/aprsd/stats/collector.py +++ b/aprsd/stats/collector.py @@ -1,8 +1,9 @@ -from typing import Protocol +from typing import Callable, Protocol, runtime_checkable from aprsd.utils import singleton +@runtime_checkable class StatsProducer(Protocol): """The StatsProducer protocol is used to define the interface for collecting stats.""" def stats(self, serializeable=False) -> dict: @@ -14,17 +15,17 @@ class StatsProducer(Protocol): class Collector: """The Collector class is used to collect stats from multiple StatsProducer instances.""" def __init__(self): - self.producers: dict[str, StatsProducer] = {} + self.producers: list[Callable] = [] def collect(self, serializable=False) -> dict: stats = {} - for name, producer in self.producers.items(): - # No need to put in empty stats - tmp_stats = producer.stats(serializable=serializable) - if tmp_stats: - stats[name] = tmp_stats + for name in self.producers: + cls = name() + if isinstance(cls, StatsProducer): + stats[cls.__class__.__name__] = cls.stats(serializable=serializable) + else: + raise TypeError(f"{cls} is not an instance of StatsProducer") return stats - def register_producer(self, producer: StatsProducer): - name = producer.__class__.__name__ - self.producers[name] = producer + def register_producer(self, producer_name: Callable): + self.producers.append(producer_name)