[stats] base for dumping various stats into channels
authorSomeone <someone@somenet.org>
Mon, 10 Mar 2025 18:14:53 +0000 (19:14 +0100)
committerSomeone <someone@somenet.org>
Mon, 10 Mar 2025 18:14:53 +0000 (19:14 +0100)
stats/.gitignore [new file with mode: 0644]
stats/config.py.example [new file with mode: 0644]
stats/main.py [new file with mode: 0755]

diff --git a/stats/.gitignore b/stats/.gitignore
new file mode 100644 (file)
index 0000000..4acd06b
--- /dev/null
@@ -0,0 +1 @@
+config.py
diff --git a/stats/config.py.example b/stats/config.py.example
new file mode 100644 (file)
index 0000000..bb6074a
--- /dev/null
@@ -0,0 +1,42 @@
+#
+# Someone's Mattermost scripts.
+#   Copyright (c) 2016-2025 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#   published under MIT-License
+#
+# Config file.
+#   mv to config.py.examle config.py + edit.
+#
+# consider running as cronjob
+#   0 0 * * *  (cd /home/someone/mattermost/priviledged/stats; python3 -u main.py)
+#
+
+# Mattermost Postgres-DB-Connect string.
+# Needed priviledges: read only.
+dbconnstring = "host=... dbname=... user=... password=..."
+
+# sysadmin-bot
+# Needed priviledges: post-to-a-single-channel.
+mm_api_url = "http://localhost:8065/api"
+mm_user = "..."
+mm_user_pw = "..."
+
+# Ignore events before this date. Useful for some stats.
+cutoff_date = "2020-02-15"
+
+# Ids of stats-channel.
+stats_daily_channel_id = "..."
+stats_weekly_channel_id = "..."
+
+# stats to run
+import channel_activity
+import posts_with_reactions
+import user_activity
+
+stats_daily = [
+        channel_activity,
+        user_activity,
+    ]
+
+stats_weekly = [
+        posts_with_reactions,
+    ]
diff --git a/stats/main.py b/stats/main.py
new file mode 100755 (executable)
index 0000000..5861b4e
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/env -S python3 -Bu
+# Someone's Mattermost scripts.
+#   Copyright (c) 2016-2025 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
+#   published under MIT-License
+#
+# Run stats and post results to stats-channel(s).
+#
+
+import sys
+import traceback
+
+import mattermost
+import psycopg2
+
+import config
+
+def run_stats(prefix, module):
+    try:
+        return prefix+module.main(dbconn)
+    except:
+        return "``AUTODELETE-DAY`` Error in module: ``"+repr(module)+"``\n# :boom::boom::boom::boom::boom:\n```\n"+traceback.format_exc()+"\n```"
+
+
+dbconn = psycopg2.connect(config.dbconnstring)
+dbconn.set_session(autocommit=False, isolation_level=psycopg2.extensions.ISOLATION_LEVEL_SERIALIZABLE, readonly=True)
+
+mm = mattermost.MMApi(config.mm_api_url)
+mm.login(config.mm_user, config.mm_user_pw)
+
+# run daily stats
+[mm.create_post(config.stats_daily_channel_id, run_stats("``AUTODELETE-WEEK`` ", m)) for m in config.stats_daily]
+
+# weekly
+if len(sys.argv) > 1 and sys.argv[1] == "week":
+    [mm.create_post(config.stats_weekly_channel_id, run_stats("``AUTODELETE-MONTH`` ", m)) for m in config.stats_weekly]
+
+mm.logout()