2024-03-29 13:23:39 -04:00
|
|
|
import datetime
|
|
|
|
import tracemalloc
|
|
|
|
|
|
|
|
from oslo_config import cfg
|
|
|
|
|
|
|
|
import aprsd
|
|
|
|
from aprsd import utils
|
2024-05-07 19:58:11 -04:00
|
|
|
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
|
|
|
|
|
2024-04-02 14:07:37 -04:00
|
|
|
def stats(self, serializable=False) -> dict:
|
2024-03-29 13:23:39 -04:00
|
|
|
current, peak = tracemalloc.get_traced_memory()
|
2024-04-02 14:07:37 -04:00
|
|
|
uptime = self.uptime()
|
2024-05-07 19:58:11 -04:00
|
|
|
qsize = aprsd_log.logging_queue.qsize()
|
2024-04-02 14:07:37 -04:00
|
|
|
if serializable:
|
|
|
|
uptime = str(uptime)
|
2024-03-29 13:23:39 -04:00
|
|
|
stats = {
|
|
|
|
"version": aprsd.__version__,
|
2024-04-02 14:07:37 -04:00
|
|
|
"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),
|
2024-05-07 19:58:11 -04:00
|
|
|
"loging_queue": qsize,
|
2024-03-29 13:23:39 -04:00
|
|
|
}
|
|
|
|
return stats
|