aprsd/aprsd/threads/stats.py

45 lines
959 B
Python
Raw Permalink Normal View History

2024-03-29 13:23:39 -04:00
import logging
import threading
import time
from oslo_config import cfg
2024-04-11 22:55:01 -04:00
import wrapt
2024-03-29 13:23:39 -04:00
from aprsd.stats import collector
from aprsd.threads import APRSDThread
from aprsd.utils import objectstore
CONF = cfg.CONF
LOG = logging.getLogger("APRSD")
class StatsStore(objectstore.ObjectStoreMixin):
"""Container to save the stats from the collector."""
lock = threading.Lock()
data = {}
2024-03-29 13:23:39 -04:00
2024-04-11 22:55:01 -04:00
@wrapt.synchronized(lock)
def add(self, stats: dict):
self.data = stats
2024-03-29 13:23:39 -04:00
class APRSDStatsStoreThread(APRSDThread):
"""Save APRSD Stats to disk periodically."""
# how often in seconds to write the file
save_interval = 10
def __init__(self):
super().__init__("StatsStore")
def loop(self):
if self.loop_count % self.save_interval == 0:
stats = collector.Collector().collect()
ss = StatsStore()
2024-04-11 22:55:01 -04:00
ss.add(stats)
2024-03-29 13:23:39 -04:00
ss.save()
time.sleep(1)
return True