From 1b0d2b6acca5b23079a158ade7d67b6b249d3972 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Tue, 16 Jan 2024 13:30:55 +0200 Subject: [PATCH] Bumped version to 8.2; added a simple /stats page --- 50_bubble.py | 5 ++++- admin.py | 17 +++++++++++++++++ model.py | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/50_bubble.py b/50_bubble.py index 61d83bc..bfa699b 100644 --- a/50_bubble.py +++ b/50_bubble.py @@ -18,7 +18,7 @@ from utils import * from worker import * -__version__ = '8.1' +__version__ = '8.2' class Bubble: @@ -961,6 +961,9 @@ Deleting a subspace will delete all posts and comments in the subspace, i.e., th if session.user and session.user.flags & User.HOME_FOLLOWED_FEED_FLAG \ else 'all' + elif req.path == self.path + 'stats': + return make_stats_page(session) + else: return 51, "Not found" diff --git a/admin.py b/admin.py index da01c48..bb7b351 100644 --- a/admin.py +++ b/admin.py @@ -222,3 +222,20 @@ def admin_actions(session): page += f'\n=> {session.path}admin/ Back to Administration\n' return page + + +def make_stats_page(session): + db = session.db + page = '# Statistics\n\n' + stats = db.get_statistics() + page += f"""```Table: Accounts and activity +Total accounts │ {stats['total']:4d} +Total posters │ {stats['posters']:4d} +Total commenters │ {stats['commenters']:4d} +Visited <= 30 days │ {stats['m_visited']:4d} +Post/comment <= 30 days │ {stats['m_post_cmt']:4d} +``` + +=> {session.path} Back to front page\n +""" + return page \ No newline at end of file diff --git a/model.py b/model.py index 18a102d..9f3a7af 100644 --- a/model.py +++ b/model.py @@ -2886,6 +2886,25 @@ class Database: cur.execute("INSERT INTO log (remote, type) VALUES (?, ?)", (address_hash(from_addr), type)) self.commit() + def get_statistics(self): + cur = self.conn.cursor() + cur.execute("""SELECT + (SELECT COUNT(id) FROM users) AS total, + (SELECT COUNT(id) FROM users WHERE TIMESTAMPDIFF(DAY, ts_active, CURRENT_TIMESTAMP()) <= 30) AS m_visited, + (SELECT COUNT(DISTINCT(user)) FROM posts WHERE TIMESTAMPDIFF(DAY, ts_created, CURRENT_TIMESTAMP()) <= 30) AS m_post_cmt, + (SELECT COUNT(DISTINCT(user)) FROM posts WHERE parent=0) AS posters, + (SELECT COUNT(DISTINCT(user)) FROM posts WHERE parent!=0) AS commenters + """) + for (total, m_visited, m_post_cmt, posters, commenters) in cur: + return { + 'total': total, + 'posters': posters, + 'commenters': commenters, + 'm_visited': m_visited, + 'm_post_cmt': m_post_cmt + } + return {} + class Search: def __init__(self, db):