]> git.somenet.org - pub/jan/mattermost-privileged.git/blob - stats/channel_activity.py
maintenance/fs.py
[pub/jan/mattermost-privileged.git] / stats / channel_activity.py
1 #!/usr/bin/env python3
2 # Someone's Mattermost scripts.
3 #   Copyright (c) 2016-2022 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
4 #   Copyright (c) 2020 by michi <michi@fsinf.at> (SQL-fix)
5 #   published under MIT-License
6 #
7 # Active channels, order by last 7 days.
8 #
9
10 import psycopg2
11 import psycopg2.extras
12
13 import config
14
15
16 def main(dbconn):
17     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
18     cur.execute("""
19                     SELECT teams.name||'|'||channels.name AS cname, COALESCE(A.cntp,0) AS cutoff_cntp, COALESCE(A.cntu,0) AS cutoff_cntu, COALESCE(B.cntp,0) AS month_cntp, COALESCE(B.cntu,0) AS month_cntu,
20                         COALESCE(C.cntp,0) AS week_cntp, COALESCE(C.cntu,0) AS week_cntu, COALESCE(D.cntp,0) AS day_cntp, COALESCE(D.cntu,0) AS day_cntu
21                     FROM (select channelid, count(*) as cntp, count(distinct userid) as cntu FROM posts WHERE deleteat = 0 AND createat > extract(epoch FROM TIMESTAMP '"""+config.cutoff_date+"""')*1000 GROUP BY channelid) as A
22                     LEFT JOIN (select channelid, count(*) as cntp, count(distinct userid) as cntu FROM posts WHERE deleteat = 0 AND createat > extract(epoch FROM (NOW() - INTERVAL '1 month'))*1000 GROUP BY channelid) as B ON (A.channelid=B.channelid)
23                     LEFT JOIN (select channelid, count(*) as cntp, count(distinct userid) as cntu FROM posts WHERE deleteat = 0 AND createat > extract(epoch FROM (NOW() - INTERVAL '1 week'))*1000 GROUP BY channelid) as C ON (A.channelid=C.channelid)
24                     LEFT JOIN (select channelid, count(*) as cntp, count(distinct userid) as cntu FROM posts WHERE deleteat = 0 AND createat > extract(epoch FROM (NOW() - INTERVAL '1 day'))*1000 GROUP BY channelid) as D ON (A.channelid=D.channelid)
25                     JOIN channels ON (channels.id = A.channelid AND channels.type = 'O') JOIN teams ON (teams.id = channels.teamid) ORDER BY A.cntp DESC
26                 """)
27
28     totalp_day = totalp_week = totalp_month = totalp_cutoff = 0
29     msg = "#channel_activity #mmstats posts and distinct users in course-channels.\n\n|team|channel|since "+config.cutoff_date+" posts|du|month posts|du|week posts|du|day posts|du|\n|---|---|---:|---:|---:|---:|---:|---:|---:|---:|\n"
30     for record in cur.fetchall():
31
32         if record["cutoff_cntp"] > 2 and "w-inf-tuwien|" in record["cname"]:
33             totalp_day += record["day_cntp"]
34             totalp_week += record["week_cntp"]
35             totalp_month += record["month_cntp"]
36             totalp_cutoff += record["cutoff_cntp"]
37
38             msg += "|"+record["cname"]+"|"+str(record["cutoff_cntp"])+"|"+str(record["cutoff_cntu"])+"|"+str(record["month_cntp"])+"|"+str(record["month_cntu"])+"|"+str(record["week_cntp"])+"|"+str(record["week_cntu"])+"|"+str(record["day_cntp"])+"|"+str(record["day_cntu"])+"|\n"
39
40     return msg + "||**Totals**|**"+str(totalp_cutoff)+"**||**"+str(totalp_month)+"**||**"+str(totalp_week)+"**||**"+str(totalp_day)+"**||"