diff --git a/Makefile b/Makefile index 70b06a0..62d24ca 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ python: $(PYTHON) dump: - $(PYTHON) -m snek.dump + @$(PYTHON) -m snek.dump run: $(GUNICORN) -w $(GUNICORN_WORKERS) -k aiohttp.worker.GunicornWebWorker snek.gunicorn:app --bind 0.0.0.0:$(PORT) --reload diff --git a/src/snek/dump.py b/src/snek/dump.py index 6401637..cf3d417 100644 --- a/src/snek/dump.py +++ b/src/snek/dump.py @@ -1,17 +1,33 @@ +import asyncio import json from snek.app import app +async def fix_message(message): + message = dict( + uid=message['uid'], + user_uid=message['user_uid'], + text=message['message'], + sent=message['created_at'] + ) + user = await app.services.user.get(uid=message['user_uid']) + message['user'] = user and user['username'] or None + return message -def dump_public_channels(): +async def dump_public_channels(): result = {'channels':{}} - - for channel in app.db['channel'].find(is_private=False,is_listed=True): + for channel in app.db['channel'].find(is_private=False,is_listed=True,tag='public'): + print(f"Dumping channel: {channel['label']}.") result['channels'][channel['label']] = dict(channel) - result['channels'][channel['label']]['messages'] = list(dict(record) for record in app.db['channel_message'].find(channel_uid=channel['uid'])) - - print(json.dumps(result, sort_keys=True, indent=4,default=str),end='',flush=True) + result['channels'][channel['label']]['messages'] = [await fix_message(record) for record in app.db['channel_message'].find(channel_uid=channel['uid'],order_by='created_at')] + print("Dump succesfull!") + print("Converting to json.") + data = json.dumps(result, indent=4,default=str) + print("Converting succesful, now writing to dump.json") + with open("dump.json","w") as f: + f.write(data) + print("Dump written to dump.json") if __name__ == '__main__': - dump_public_channels() + asyncio.run(dump_public_channels()) diff --git a/src/snek/system/cache.py b/src/snek/system/cache.py index f9c4761..cd9484d 100644 --- a/src/snek/system/cache.py +++ b/src/snek/system/cache.py @@ -20,13 +20,13 @@ class Cache: try: self.lru.pop(self.lru.index(args)) except: - print("Cache miss!", args, flush=True) + #print("Cache miss!", args, flush=True) return None self.lru.insert(0, args) while len(self.lru) > self.max_items: self.cache.pop(self.lru[-1]) self.lru.pop() - print("Cache hit!", args, flush=True) + #print("Cache hit!", args, flush=True) return self.cache[args] def json_default(self, value): @@ -61,7 +61,7 @@ class Cache: if is_new: self.version += 1 - print(f"Cache store! {len(self.lru)} items. New version:", self.version, flush=True) + #print(f"Cache store! {len(self.lru)} items. New version:", self.version, flush=True) async def delete(self, args): if args in self.cache: