From be46f79ef761e090a6c1a3a46deebcdd75f12473 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 29 Dec 2021 15:03:47 +0000 Subject: [PATCH] Archive old reference IDs --- handler/agora.py | 1 + handler/transactions.py | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/handler/agora.py b/handler/agora.py index 592f44a..46b21de 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -99,6 +99,7 @@ class Agora(object): for ref in list(self.last_dash): # We're removing from the list on the fly if ref not in current_trades: self.last_dash.remove(ref) + self.tx.cleanup(current_trades) return dash_tmp def dashboard_release_urls(self): diff --git a/handler/transactions.py b/handler/transactions.py index bddca55..5cbcf0d 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -163,6 +163,19 @@ class Transactions(object): references.append(r.get(key)) return convert(references) + def get_ref_map(self): + """ + Get all reference IDs for trades. + :return: dict of references keyed by TXID + :rtype: dict + """ + references = {} + ref_keys = r.keys("trade.*.reference") + for key in ref_keys: + tx = convert(key).split(".")[1] + references[tx] = r.get(key) + return convert(references) + def get_ref(self, reference): """ Get a reference ID for a single trade. @@ -178,8 +191,16 @@ class Transactions(object): """ Delete a given reference from the Redis database. """ + tx = self.ref_to_tx(reference) r.delete(f"trade.{reference}") - r.delete(f"trade.{reference}.reference") + r.delete(f"trade.{tx}.reference") + + def cleanup(self, references): + for tx, reference in self.get_ref_map().items(): + if reference not in references: + self.log.info("Archiving trade reference: {reference} / TX: {tx}", reference=reference, tx=tx) + r.rename(f"trade.{tx}.reference", f"archive.trade.{tx}.reference") + r.rename(f"trade.{reference}", f"archive.trade.{reference}") def del_tx(self, txid): pass