1
0
mirror of https://github.com/craigerl/aprsd.git synced 2024-10-31 15:07:13 -04:00
aprsd/aprsd/stats/app.py

50 lines
1.3 KiB
Python
Raw Permalink Normal View History

2024-03-29 13:23:39 -04:00
import datetime
import tracemalloc
from oslo_config import cfg
import aprsd
from aprsd import utils
from aprsd.log import log as aprsd_log
2024-03-29 13:23:39 -04:00
CONF = cfg.CONF
class APRSDStats:
"""The AppStats class is used to collect stats from the application."""
2024-04-01 21:46:23 -04:00
_instance = None
2024-04-20 17:07:48 -04:00
start_time = None
2024-04-01 21:46:23 -04:00
def __new__(cls, *args, **kwargs):
"""Have to override the new method to make this a singleton
instead of using @singletone decorator so the unit tests work.
"""
if not cls._instance:
cls._instance = super().__new__(cls)
2024-04-20 17:07:48 -04:00
cls._instance.start_time = datetime.datetime.now()
2024-04-01 21:46:23 -04:00
return cls._instance
2024-03-29 13:23:39 -04:00
def uptime(self):
return datetime.datetime.now() - self.start_time
def stats(self, serializable=False) -> dict:
2024-03-29 13:23:39 -04:00
current, peak = tracemalloc.get_traced_memory()
uptime = self.uptime()
qsize = aprsd_log.logging_queue.qsize()
if serializable:
uptime = str(uptime)
2024-03-29 13:23:39 -04:00
stats = {
"version": aprsd.__version__,
"uptime": uptime,
2024-03-29 13:23:39 -04:00
"callsign": CONF.callsign,
"memory_current": int(current),
"memory_current_str": utils.human_size(current),
"memory_peak": int(peak),
"memory_peak_str": utils.human_size(peak),
"loging_queue": qsize,
2024-03-29 13:23:39 -04:00
}
return stats