mirror of
https://github.com/barkeser2002/offline-db.git
synced 2026-09-25 00:19:51 +03:00
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>