From fbb30d4f889d02d3d51b38a7f984bba8e90ea56b Mon Sep 17 00:00:00 2001 From: Someone Date: Mon, 13 Nov 2023 13:09:02 +0100 Subject: [PATCH] [stats] base for dumping various stats into channels --- stats/.gitignore | 1 + stats/config.py.example | 42 +++++++++++++++++++++++++++++++++++++++++ stats/main.py | 37 ++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 stats/.gitignore create mode 100644 stats/config.py.example create mode 100755 stats/main.py diff --git a/stats/.gitignore b/stats/.gitignore new file mode 100644 index 0000000..4acd06b --- /dev/null +++ b/stats/.gitignore @@ -0,0 +1 @@ +config.py diff --git a/stats/config.py.example b/stats/config.py.example new file mode 100644 index 0000000..96334c9 --- /dev/null +++ b/stats/config.py.example @@ -0,0 +1,42 @@ +# +# Someone's Mattermost scripts. +# Copyright (c) 2016-2022 by Someone (aka. Jan Vales ) +# 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 index 0000000..269a8f0 --- /dev/null +++ b/stats/main.py @@ -0,0 +1,37 @@ +#!/usr/bin/env -S python3 -Bu +# Someone's Mattermost scripts. +# Copyright (c) 2016-2022 by Someone (aka. Jan Vales ) +# 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() -- 2.43.0