Script now purges the correct data it seems
This commit is contained in:
parent
9b64afc27f
commit
2efc163f8e
@ -1,48 +1,71 @@
|
||||
# Channer front-end
|
||||
|
||||
from time import time
|
||||
from os import getenv
|
||||
import argparse
|
||||
import mysql.connector as sql
|
||||
|
||||
CONFIG = {
|
||||
'database': getenv('DATABASE_NAME'),
|
||||
'password': getenv('DATABASE_PASS'),
|
||||
'user': getenv('DATABASE_USER'),
|
||||
'host': getenv('DATABASE_HOST'),
|
||||
'port': int(getenv('DATABASE_PORT')),
|
||||
}
|
||||
|
||||
def purge_clamped_messages(cursor, channels: list, limit: int):
|
||||
pass
|
||||
def _format_cfg(config: dict):
|
||||
# NOTE: we' assuming that the config is _not_ malformed here
|
||||
return {
|
||||
'user': config['DATABASE_USER'],
|
||||
'password': config['DATABASE_PASS'],
|
||||
'host': config['DATABASE_HOST'],
|
||||
'database': config['DATABASE_NAME'],
|
||||
'port': config['DATABASE_PORT']
|
||||
}
|
||||
|
||||
def message_limit(limit: int):
|
||||
count_query = '''
|
||||
SELECT channel_name, COUNT(time) FROM messages
|
||||
GROUP BY channel_name;
|
||||
'''
|
||||
conn = sql.connect(**CONFIG)
|
||||
|
||||
def remove_old(config: dict, limit: int):
|
||||
config = _format_cfg(config)
|
||||
count_query = 'DELETE FROM messages WHERE time < ?'
|
||||
# TODO: don't remove pinned items
|
||||
|
||||
max_age = int(time()) - (limit * 24 * 3600)
|
||||
query = ('DELETE FROM messages WHERE time < %s')
|
||||
params = (max_age,)
|
||||
|
||||
conn = sql.connect(**config)
|
||||
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.execute(count_query, params)
|
||||
conn.commit()
|
||||
|
||||
cursor.close()
|
||||
conn.close()
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
def message_timeout():
|
||||
pass
|
||||
|
||||
def read_config(path: str) -> dict:
|
||||
pairs = {}
|
||||
with open(path, 'r') as cfg:
|
||||
# Line format key=value
|
||||
for line in cfg:
|
||||
line = line.strip()
|
||||
key = line[:line.find('=')]
|
||||
value = line[line.find('=')+1:]
|
||||
pairs[key] = value
|
||||
|
||||
if 'DATABASE_PORT' not in pairs:
|
||||
raise KeyError('DATABASE_PORT missing from `.env` file')
|
||||
else:
|
||||
pairs['DATABASE_PORT'] = int(pairs['DATABASE_PORT'])
|
||||
|
||||
assert('DATABASE_URL' in pairs)
|
||||
assert('DATABASE_NAME' in pairs)
|
||||
assert('DATABASE_PASS' in pairs)
|
||||
assert('DATABASE_USER' in pairs)
|
||||
assert('DATABASE_HOST' in pairs)
|
||||
assert('DATABASE_PORT' in pairs)
|
||||
|
||||
return pairs
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(
|
||||
@ -50,16 +73,18 @@ if __name__ == '__main__':
|
||||
' 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('-m', '--max-age-days',
|
||||
help='Sets the value of how old messages are allowed to be (in days)',
|
||||
type=int,
|
||||
default=7,
|
||||
required=False)
|
||||
|
||||
parser.add_argument('-f', '---file-env', type=str, required=True)
|
||||
|
||||
args = vars(parser.parse_args())
|
||||
|
||||
limit = args['message_limit']
|
||||
timeout = args['time_out']
|
||||
timeout = args['max_age_days']
|
||||
config = read_config(args['file_env'])
|
||||
|
||||
# timeout is given in days 30 => 30 day time limit
|
||||
if limit: message_limit(limit)
|
||||
if timeout: message_timeout(timeout)
|
||||
exit(remove_old(config, timeout))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user