Only check transactions when we have a trade and make the fetch less efficient to throttle requests more

This commit is contained in:
Mark Veidemanis 2023-04-20 08:25:50 +01:00
parent da5d1badd8
commit 7e7b145b04
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 45 additions and 5 deletions

View File

@ -18,23 +18,38 @@ INTERVALS_PLATFORM = [x[0] for x in INTERVAL_CHOICES]
async def aggregator_job(): async def aggregator_job():
aggregators = Aggregator.objects.filter(enabled=True) aggregators = Aggregator.objects.filter(enabled=True)
for aggregator in aggregators: for aggregator in aggregators:
open_trade_currencies = aggregator.trades_currencies
log.debug(f"Currencies of open trades: {open_trade_currencies}")
if aggregator.service == "nordigen": if aggregator.service == "nordigen":
instance = await NordigenClient(aggregator) instance = None
if aggregator.fetch_accounts is True: if aggregator.fetch_accounts is True:
aggregator.account_info = {} aggregator.account_info = {}
aggregator.save() aggregator.save()
instance = await NordigenClient(aggregator)
await instance.get_all_account_info(store=True) await instance.get_all_account_info(store=True)
fetch_tasks = [] # fetch_tasks = []
for bank, accounts in aggregator.account_info.items(): for bank, accounts in aggregator.account_info.items():
for account in accounts: for account in accounts:
account_id = account["account_id"] account_id = account["account_id"]
requisition_id = account["requisition_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 account_id, req=requisition_id, process=True
) )
fetch_tasks.append(task) # fetch_tasks.append(task)
await asyncio.gather(*fetch_tasks) # await asyncio.gather(*fetch_tasks)
else: else:
raise NotImplementedError(f"No such client library: {aggregator.service}") raise NotImplementedError(f"No such client library: {aggregator.service}")
aggregator.fetch_accounts = False aggregator.fetch_accounts = False

View File

@ -224,6 +224,31 @@ class Aggregator(models.Model):
return None return None
return transaction 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): class Wallet(models.Model):
""" """