From 981bd6fcf788e4f9d308626aae30481484c30620 Mon Sep 17 00:00:00 2001 From: shockrah Date: Sun, 23 Aug 2020 17:47:25 -0700 Subject: [PATCH] adding func for finding channels with too many messages --- chan-like/channer.py | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/chan-like/channer.py b/chan-like/channer.py index a2d48dc..c44048b 100644 --- a/chan-like/channer.py +++ b/chan-like/channer.py @@ -12,33 +12,46 @@ CONFIG = { 'port': int(getenv('DATABASE_PORT')), } -def add_message_limit(limit: int): +def purge_clamped_messages(cursor, channels: list, limit: int): + pass + +def message_limit(limit: int): count_query = ''' SELECT channel_name, COUNT(time) FROM messages GROUP BY channel_name; ''' conn = sql.connect(**CONFIG) - cursor = conn.cursor() + cursor = conn.cursor(prepared=True) + + cursor.execute(count_query,()) + # figure out which channels need pruning + for i in cursor: + print(i) + + # collect the channels that require filtering + channels = list(filter((None).__ne__, [limit if chan[1] > limit else None for chan in cursor])) + if len(channels) > 0: + purge_clamped_messages(cursor, channels, limit) + else: + print(f'[ {__file__} ] : No messages to remove from clamped set') + + cursor.close() + conn.close() -def add_message_timeout(): - pass -def rm_message_limit(): - pass - -def rm_message_timeout(): +def message_timeout(): pass if __name__ == '__main__': parser = argparse.ArgumentParser( - description='An optional microservice for freechat to make the messaging system more transient' + description='A script which can be setup as a chronjob to make messaging' + ' similar to how image boards clean up threads' ) parser.add_argument('-t', '--time-out', type=float, required=False) parser.add_argument('-m', '--message-limit', type=int, required=False) - parser.add_argument('-r', '--remove' type=string, required=False, choices=['timeout', 'limit']) args = vars(parser.parse_args()) @@ -47,12 +60,6 @@ if __name__ == '__main__': timeout = args['time_out'] # timeout is given in days 30 => 30 day time limit - if limit: add_message_limit(limit) - if timeout: add_message_timeout(timeout) + if limit: message_limit(limit) + if timeout: message_timeout(timeout) - if args['remove']: - remove_opts = { - 'timeout': rm_message_timeout, - 'limit': rm_message_limit, - } - remove_opts[f]()