From c7d10f53a36f84304190dea2c7154f827b2cf479 Mon Sep 17 00:00:00 2001 From: Hemna Date: Wed, 24 Mar 2021 16:07:09 -0400 Subject: [PATCH] Updated web stats index to show messages and ram usage This patch updates the main index page to show both the graph of tx/rx messages as well as peak/current ram usage. --- README.rst | 3 + aprsd/flask.py | 7 ++ aprsd/stats.py | 28 ++++- aprsd/threads.py | 2 + aprsd/web/templates/index.html | 222 +++++++++++++++++++++++++++++++-- 5 files changed, 254 insertions(+), 8 deletions(-) diff --git a/README.rst b/README.rst index 0401272..f7ea08e 100644 --- a/README.rst +++ b/README.rst @@ -5,6 +5,9 @@ APRSD .. image:: https://badge.fury.io/py/aprsd.svg :target: https://badge.fury.io/py/aprsd +.. image:: http://hits.dwyl.com/craigerl/aprsd.svg + :target: http://hits.dwyl.com/craigerl/aprsd + .. image:: https://github.com/craigerl/aprsd/workflows/python/badge.svg :target: https://github.com/craigerl/aprsd/actions diff --git a/aprsd/flask.py b/aprsd/flask.py index f0dbf39..e9b51ca 100644 --- a/aprsd/flask.py +++ b/aprsd/flask.py @@ -1,5 +1,7 @@ +import datetime import json import logging +import tracemalloc import aprsd from aprsd import messaging, plugin, stats @@ -69,12 +71,17 @@ class APRSDFlask(flask_classful.FlaskView): def stats(self): stats_obj = stats.APRSDStats() track = messaging.MsgTrack() + now = datetime.datetime.now() + current, peak = tracemalloc.get_traced_memory() result = { "version": aprsd.__version__, "uptime": stats_obj.uptime, "size_tracker": len(track), "stats": stats_obj.stats(), + "time": now.strftime("%m-%d-%Y %H:%M:%S"), + "memory_current": current, + "memory_peak": peak, } return json.dumps(result) diff --git a/aprsd/stats.py b/aprsd/stats.py index 30c9f5a..986a795 100644 --- a/aprsd/stats.py +++ b/aprsd/stats.py @@ -26,6 +26,9 @@ class APRSDStats: _email_tx = 0 _email_rx = 0 + _mem_current = 0 + _mem_peak = 0 + def __new__(cls, *args, **kwargs): if cls._instance is None: cls._instance = super().__new__(cls) @@ -43,6 +46,24 @@ class APRSDStats: with self.lock: return str(datetime.datetime.now() - self.start_time) + @property + def memory(self): + with self.lock: + return self._mem_current + + def set_memory(self, memory): + with self.lock: + self._mem_curent = memory + + @property + def memory_peak(self): + with self.lock: + return self._mem_peak + + def set_memory_peak(self, memory): + with self.lock: + self._mem_peak = memory + @property def msgs_tx(self): with self.lock: @@ -126,6 +147,11 @@ class APRSDStats: def stats(self): now = datetime.datetime.now() + if self._email_thread_last_time: + last_update = str(now - self._email_thread_last_time) + else: + last_update = "never" + stats = { "messages": { "tracked": self.msgs_tracked, @@ -138,7 +164,7 @@ class APRSDStats: "email": { "sent": self._email_tx, "recieved": self._email_rx, - "thread_last_update": str(now - self._email_thread_last_time), + "thread_last_update": last_update, }, } return stats diff --git a/aprsd/threads.py b/aprsd/threads.py index 736a052..03b4fc7 100644 --- a/aprsd/threads.py +++ b/aprsd/threads.py @@ -84,6 +84,8 @@ class KeepAliveThread(APRSDThread): email_thread_time = "N/A" current, peak = tracemalloc.get_traced_memory() + stats_obj.set_memory(current) + stats_obj.set_memory_peak(peak) LOG.debug( "Uptime ({}) Tracker({}) " "Msgs: TX:{} RX:{} EmailThread: {} RAM: Current:{} Peak:{}".format( diff --git a/aprsd/web/templates/index.html b/aprsd/web/templates/index.html index d016f1c..68cc58b 100644 --- a/aprsd/web/templates/index.html +++ b/aprsd/web/templates/index.html @@ -1,14 +1,133 @@ + + + + + -
{{ stats }}
- +
+
APRSD version
+
+
+ +
+
+
+ +
+ +
+ +
{{ stats }}
+
+
+ +