Files
Barış Keserandgoogle-labs-jules[bot] 5dde0cafbf perf: optimize N+1 query in encoder and fansub wallet updates (#350)
Refactored the `calculate_revenue` task in `billing/tasks.py` to eliminate N+1
database queries when distributing revenue to encoders and fansub group owners.

Changes:
- Replaced individual `Wallet.objects.get_or_create` and `wallet.save()` calls
  inside loops with bulk operations.
- Implemented a 'fetch-create-update' pattern:
  1. Identify all required user IDs.
  2. Bulk-create missing `Wallet` objects.
  3. Fetch all relevant wallets in a single query.
  4. Perform in-memory balance updates.
  5. Use `bulk_update` to persist changes in a single query per pool.
- Cleaned up unused imports (`VideoFile`) and variables.
- Ensured `python-dotenv` is an optional dependency in `manage.py` and
  `aniscrap_core/settings.py` for improved environment resilience.

This optimization reduces the number of database queries from O(N) to O(1)
per distribution pool, significantly improving performance for tasks with
many unique revenue recipients.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
2026-04-21 15:25:32 +00:00

27 lines
755 B
Python
Executable File

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
pass
def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aniscrap_core.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == "__main__":
main()