Script now purges the correct data it seems
This commit is contained in:
parent
9b64afc27f
commit
2efc163f8e
@ -1,48 +1,71 @@
|
|||||||
# Channer front-end
|
# Channer front-end
|
||||||
|
|
||||||
|
from time import time
|
||||||
from os import getenv
|
from os import getenv
|
||||||
import argparse
|
import argparse
|
||||||
import mysql.connector as sql
|
import mysql.connector as sql
|
||||||
|
|
||||||
CONFIG = {
|
|
||||||
'database': getenv('DATABASE_NAME'),
|
def _format_cfg(config: dict):
|
||||||
'password': getenv('DATABASE_PASS'),
|
# NOTE: we' assuming that the config is _not_ malformed here
|
||||||
'user': getenv('DATABASE_USER'),
|
return {
|
||||||
'host': getenv('DATABASE_HOST'),
|
'user': config['DATABASE_USER'],
|
||||||
'port': int(getenv('DATABASE_PORT')),
|
'password': config['DATABASE_PASS'],
|
||||||
|
'host': config['DATABASE_HOST'],
|
||||||
|
'database': config['DATABASE_NAME'],
|
||||||
|
'port': config['DATABASE_PORT']
|
||||||
}
|
}
|
||||||
|
|
||||||
def purge_clamped_messages(cursor, channels: list, limit: int):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def message_limit(limit: int):
|
def remove_old(config: dict, limit: int):
|
||||||
count_query = '''
|
config = _format_cfg(config)
|
||||||
SELECT channel_name, COUNT(time) FROM messages
|
count_query = 'DELETE FROM messages WHERE time < ?'
|
||||||
GROUP BY channel_name;
|
# TODO: don't remove pinned items
|
||||||
'''
|
|
||||||
conn = sql.connect(**CONFIG)
|
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 = conn.cursor(prepared=True)
|
||||||
|
|
||||||
cursor.execute(count_query,())
|
cursor.execute(count_query, params)
|
||||||
# figure out which channels need pruning
|
conn.commit()
|
||||||
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()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def message_timeout():
|
def message_timeout():
|
||||||
pass
|
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__':
|
if __name__ == '__main__':
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
@ -50,16 +73,18 @@ if __name__ == '__main__':
|
|||||||
' similar to how image boards clean up threads'
|
' similar to how image boards clean up threads'
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument('-t', '--time-out', type=float, required=False)
|
parser.add_argument('-m', '--max-age-days',
|
||||||
parser.add_argument('-m', '--message-limit', type=int, required=False)
|
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())
|
args = vars(parser.parse_args())
|
||||||
|
|
||||||
limit = args['message_limit']
|
timeout = args['max_age_days']
|
||||||
timeout = args['time_out']
|
config = read_config(args['file_env'])
|
||||||
|
|
||||||
# timeout is given in days 30 => 30 day time limit
|
exit(remove_old(config, timeout))
|
||||||
if limit: message_limit(limit)
|
|
||||||
if timeout: message_timeout(timeout)
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user