Skip to content

Commit

Permalink
remove transaction.atomic in account stats task
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlanglen committed May 6, 2024
1 parent 02f6157 commit b1d07c4
Showing 1 changed file with 41 additions and 41 deletions.
82 changes: 41 additions & 41 deletions indexer_app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,47 +73,47 @@ def update_account_statistics():
accounts = Account.objects.all()
for account in accounts:
try:
with transaction.atomic():
logger.info(f"Updating statistics for account {account.id}...")
# donors count
account.donors_count = Donation.objects.filter(
recipient=account
).aggregate(Count("donor", distinct=True))["donor__count"]

# donations received usd
account.total_donations_in_usd = (
Donation.objects.filter(recipient=account).aggregate(
Sum("total_amount_usd")
)["total_amount_usd__sum"]
or 0
)

# donations sent usd
account.total_donations_out_usd = (
Donation.objects.filter(donor=account).aggregate(
Sum("total_amount_usd")
)["total_amount_usd__sum"]
or 0
)

# matching pool allocations usd
account.total_matching_pool_allocations_usd = (
PotPayout.objects.filter(
recipient=account, paid_at__isnull=False
).aggregate(Sum("amount_paid_usd"))["amount_paid_usd__sum"]
or 0
)

# Save changes
account.save(
update_fields=[
"donors_count",
"total_donations_in_usd",
"total_donations_out_usd",
"total_matching_pool_allocations_usd",
]
)
logger.info(f"Account {account.id} statistics updated.")
# with transaction.atomic():
logger.info(f"Updating statistics for account {account.id}...")
# donors count
account.donors_count = Donation.objects.filter(recipient=account).aggregate(
Count("donor", distinct=True)
)["donor__count"]

# donations received usd
account.total_donations_in_usd = (
Donation.objects.filter(recipient=account).aggregate(
Sum("total_amount_usd")
)["total_amount_usd__sum"]
or 0
)

# donations sent usd
account.total_donations_out_usd = (
Donation.objects.filter(donor=account).aggregate(
Sum("total_amount_usd")
)["total_amount_usd__sum"]
or 0
)

# matching pool allocations usd
account.total_matching_pool_allocations_usd = (
PotPayout.objects.filter(
recipient=account, paid_at__isnull=False
).aggregate(Sum("amount_paid_usd"))["amount_paid_usd__sum"]
or 0
)

# Save changes
account.save(
update_fields=[
"donors_count",
"total_donations_in_usd",
"total_donations_out_usd",
"total_matching_pool_allocations_usd",
]
)
logger.info(f"Account {account.id} statistics updated.")
except Exception as e:
logger.error(f"Failed to update statistics for account {account.id}: {e}")

Expand Down

0 comments on commit b1d07c4

Please sign in to comment.