From 7e7b145b04a362a766e50d7d2878ffeb06e913a1 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 20 Apr 2023 08:25:50 +0100 Subject: [PATCH] Only check transactions when we have a trade and make the fetch less efficient to throttle requests more --- core/management/commands/scheduling.py | 25 ++++++++++++++++++++----- core/models.py | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/core/management/commands/scheduling.py b/core/management/commands/scheduling.py index 770b413..432c3c9 100644 --- a/core/management/commands/scheduling.py +++ b/core/management/commands/scheduling.py @@ -18,23 +18,38 @@ INTERVALS_PLATFORM = [x[0] for x in INTERVAL_CHOICES] async def aggregator_job(): aggregators = Aggregator.objects.filter(enabled=True) for aggregator in aggregators: + open_trade_currencies = aggregator.trades_currencies + log.debug(f"Currencies of open trades: {open_trade_currencies}") if aggregator.service == "nordigen": - instance = await NordigenClient(aggregator) + instance = None if aggregator.fetch_accounts is True: aggregator.account_info = {} aggregator.save() + instance = await NordigenClient(aggregator) await instance.get_all_account_info(store=True) - fetch_tasks = [] + # fetch_tasks = [] for bank, accounts in aggregator.account_info.items(): for account in accounts: account_id = account["account_id"] requisition_id = account["requisition_id"] - task = instance.get_transactions( + log.debug(f"Polling currency {account['currency']}") + if account["currency"] not in open_trade_currencies: + log.debug( + f"Skipping {account_id}, not in {open_trade_currencies}" + ) + continue # Next account + # Avoid hammering the API with new access token requests + if instance is None: + instance = await NordigenClient(aggregator) + # task = instance.get_transactions( + # account_id, req=requisition_id, process=True + # ) + await instance.get_transactions( account_id, req=requisition_id, process=True ) - fetch_tasks.append(task) - await asyncio.gather(*fetch_tasks) + # fetch_tasks.append(task) + # await asyncio.gather(*fetch_tasks) else: raise NotImplementedError(f"No such client library: {aggregator.service}") aggregator.fetch_accounts = False diff --git a/core/models.py b/core/models.py index f8b63f5..766b73d 100644 --- a/core/models.py +++ b/core/models.py @@ -224,6 +224,31 @@ class Aggregator(models.Model): return None return transaction + @property + def trades(self): + """ + Get all trades for the platforms of this aggregator's link group. + """ + trades = [] + for platform in self.platforms: + platform_trades = platform.trades + for trade in platform_trades: + trades.append(trade) + + return trades + + @property + def trades_currencies(self): + """ + Get all the trade fiat currencies. + """ + currencies = [] + for trade in self.trades: + if trade.currency not in currencies: + currencies.append(trade.currency) + + return currencies + class Wallet(models.Model): """