aprsd/aprsd/stats/collector.py

39 lines
1.2 KiB
Python
Raw Normal View History

import logging
from typing import Callable, Protocol, runtime_checkable
2024-03-29 13:23:39 -04:00
from aprsd.utils import singleton
LOG = logging.getLogger("APRSD")
@runtime_checkable
2024-03-29 13:23:39 -04:00
class StatsProducer(Protocol):
"""The StatsProducer protocol is used to define the interface for collecting stats."""
def stats(self, serializeable=False) -> dict:
"""provide stats in a dictionary format."""
2024-03-29 13:23:39 -04:00
...
@singleton
class Collector:
"""The Collector class is used to collect stats from multiple StatsProducer instances."""
def __init__(self):
self.producers: list[Callable] = []
2024-03-29 13:23:39 -04:00
def collect(self, serializable=False) -> dict:
2024-03-29 13:23:39 -04:00
stats = {}
for name in self.producers:
cls = name()
if isinstance(cls, StatsProducer):
try:
stats[cls.__class__.__name__] = cls.stats(serializable=serializable).copy()
except Exception as e:
LOG.error(f"Error in producer {name} (stats): {e}")
else:
raise TypeError(f"{cls} is not an instance of StatsProducer")
2024-03-29 13:23:39 -04:00
return stats
def register_producer(self, producer_name: Callable):
self.producers.append(producer_name)