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.
This commit is contained in:
Hemna 2024-04-16 11:06:38 -04:00
parent 4542c0a643
commit dc4879a367
3 changed files with 20 additions and 23 deletions

View File

@ -67,10 +67,6 @@ class PacketList(objectstore.ObjectStoreMixin):
def copy(self): def copy(self):
return self.data.copy() return self.data.copy()
@wrapt.synchronized(lock)
def set_maxlen(self, maxlen):
self.maxlen = maxlen
@wrapt.synchronized(lock) @wrapt.synchronized(lock)
def find(self, packet): def find(self, packet):
return self.data["packets"][packet.key] return self.data["packets"][packet.key]

View File

@ -9,12 +9,12 @@ from aprsd.threads import aprsd
# Create the collector and register all the objects # Create the collector and register all the objects
# that APRSD has that implement the stats protocol # that APRSD has that implement the stats protocol
stats_collector = collector.Collector() stats_collector = collector.Collector()
stats_collector.register_producer(app.APRSDStats()) stats_collector.register_producer(app.APRSDStats)
stats_collector.register_producer(packet_list.PacketList()) stats_collector.register_producer(packet_list.PacketList)
stats_collector.register_producer(watch_list.WatchList()) stats_collector.register_producer(watch_list.WatchList)
stats_collector.register_producer(tracker.PacketTrack()) stats_collector.register_producer(tracker.PacketTrack)
stats_collector.register_producer(plugin.PluginManager()) stats_collector.register_producer(plugin.PluginManager)
stats_collector.register_producer(aprsd.APRSDThreadList()) stats_collector.register_producer(aprsd.APRSDThreadList)
stats_collector.register_producer(email.EmailStats()) stats_collector.register_producer(email.EmailStats)
stats_collector.register_producer(aprs_client.APRSClientStats()) stats_collector.register_producer(aprs_client.APRSClientStats)
stats_collector.register_producer(seen_list.SeenList()) stats_collector.register_producer(seen_list.SeenList)

View File

@ -1,8 +1,9 @@
from typing import Protocol from typing import Callable, Protocol, runtime_checkable
from aprsd.utils import singleton from aprsd.utils import singleton
@runtime_checkable
class StatsProducer(Protocol): class StatsProducer(Protocol):
"""The StatsProducer protocol is used to define the interface for collecting stats.""" """The StatsProducer protocol is used to define the interface for collecting stats."""
def stats(self, serializeable=False) -> dict: def stats(self, serializeable=False) -> dict:
@ -14,17 +15,17 @@ class StatsProducer(Protocol):
class Collector: class Collector:
"""The Collector class is used to collect stats from multiple StatsProducer instances.""" """The Collector class is used to collect stats from multiple StatsProducer instances."""
def __init__(self): def __init__(self):
self.producers: dict[str, StatsProducer] = {} self.producers: list[Callable] = []
def collect(self, serializable=False) -> dict: def collect(self, serializable=False) -> dict:
stats = {} stats = {}
for name, producer in self.producers.items(): for name in self.producers:
# No need to put in empty stats cls = name()
tmp_stats = producer.stats(serializable=serializable) if isinstance(cls, StatsProducer):
if tmp_stats: stats[cls.__class__.__name__] = cls.stats(serializable=serializable)
stats[name] = tmp_stats else:
raise TypeError(f"{cls} is not an instance of StatsProducer")
return stats return stats
def register_producer(self, producer: StatsProducer): def register_producer(self, producer_name: Callable):
name = producer.__class__.__name__ self.producers.append(producer_name)
self.producers[name] = producer