perf(spp_programs): batch create entitlements and payments#128
perf(spp_programs): batch create entitlements and payments#128kneckinator wants to merge 1 commit into19.0from
Conversation
Cash entitlement manager now collects all entitlement dicts and calls create() once instead of per-beneficiary. Payment manager collects all payment dicts per batch tag and calls create() once, then batch-assigns via write(). Also prefetches bank_ids to avoid N+1 queries.
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly improves performance within the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces significant performance improvements by batching the creation of entitlements and payments, replacing loops of single create calls with batch create(vals_list) operations. The changes are well-implemented and include new tests to verify the batching behavior. My review includes a few suggestions to further improve code readability by using more idiomatic Python constructs and to enhance the new tests for better coverage.
| for ent in new_entitlements_to_create: | ||
| initial_amount = new_entitlements_to_create[ent]["initial_amount"] | ||
| new_entitlements_to_create[ent]["initial_amount"] = self._check_subsidy(initial_amount) | ||
| # Create non-zero entitlements only | ||
| if new_entitlements_to_create[ent]["initial_amount"] > 0.0: | ||
| self.env["spp.entitlement"].create(new_entitlements_to_create[ent]) | ||
| vals_list.append(new_entitlements_to_create[ent]) |
There was a problem hiding this comment.
While the logic is correct, the loop can be made more direct and Pythonic by iterating over the dictionary's values instead of its keys. This improves readability by removing the extra dictionary lookup new_entitlements_to_create[ent] in each iteration.
| for ent in new_entitlements_to_create: | |
| initial_amount = new_entitlements_to_create[ent]["initial_amount"] | |
| new_entitlements_to_create[ent]["initial_amount"] = self._check_subsidy(initial_amount) | |
| # Create non-zero entitlements only | |
| if new_entitlements_to_create[ent]["initial_amount"] > 0.0: | |
| self.env["spp.entitlement"].create(new_entitlements_to_create[ent]) | |
| vals_list.append(new_entitlements_to_create[ent]) | |
| for vals in new_entitlements_to_create.values(): | |
| initial_amount = vals["initial_amount"] | |
| vals["initial_amount"] = self._check_subsidy(initial_amount) | |
| # Create non-zero entitlements only | |
| if vals["initial_amount"] > 0.0: | |
| vals_list.append(vals) |
| payment_vals_list = [] | ||
| for entitlement_id in tag_entitlements: | ||
| account_number = None | ||
| if entitlement_id.partner_id.bank_ids: | ||
| account_number = entitlement_id.partner_id.bank_ids[0].acc_number | ||
| payment_vals_list.append( | ||
| { | ||
| "name": str(uuid4()), | ||
| "entitlement_id": entitlement_id.id, | ||
| "cycle_id": entitlement_id.cycle_id.id, | ||
| "amount_issued": entitlement_id.initial_amount, | ||
| "payment_fee": entitlement_id.transfer_fee, | ||
| "state": "issued", | ||
| "account_number": account_number, | ||
| } | ||
| ) |
There was a problem hiding this comment.
The logic for building payment_vals_list can be made more concise and Pythonic by using a list comprehension. This improves readability and reduces boilerplate code.
payment_vals_list = [
{
"name": str(uuid4()),
"entitlement_id": ent.id,
"cycle_id": ent.cycle_id.id,
"amount_issued": ent.initial_amount,
"payment_fee": ent.transfer_fee,
"state": "issued",
"account_number": ent.partner_id.bank_ids[0].acc_number if ent.partner_id.bank_ids else None,
}
for ent in tag_entitlements
]| call_count = 0 | ||
|
|
||
| def counting_create(self_model, vals_list): | ||
| nonlocal call_count | ||
| call_count += 1 | ||
| return original_create(self_model, vals_list) | ||
|
|
||
| # Add a batch tag so we enter the loop | ||
| batch_tag = self.env["spp.payment.batch.tag"].create( | ||
| { | ||
| "name": "Test Tag", | ||
| "order": 1, | ||
| "domain": "[]", | ||
| "max_batch_size": 500, | ||
| } | ||
| ) | ||
| self.payment_manager.batch_tag_ids = [(4, batch_tag.id)] | ||
|
|
||
| with patch.object( | ||
| type(self.env["spp.payment"]), | ||
| "create", | ||
| counting_create, | ||
| ): | ||
| self.payment_manager._prepare_payments(self.cycle, self.entitlements) | ||
|
|
||
| self.assertEqual( | ||
| call_count, | ||
| 1, | ||
| f"create() should be called once (batch), was called {call_count} times", | ||
| ) |
There was a problem hiding this comment.
For better test coverage and consistency with test_cash_manager_batch_creates_entitlements, this test should also verify the total number of records created, not just the number of create() calls. This ensures that the batch operation not only runs once but also processes the correct number of items.
call_count = 0
total_created = 0
def counting_create(self_model, vals_list):
nonlocal call_count, total_created
call_count += 1
if isinstance(vals_list, list):
total_created += len(vals_list)
else:
total_created += 1
return original_create(self_model, vals_list)
# Add a batch tag so we enter the loop
batch_tag = self.env["spp.payment.batch.tag"].create(
{
"name": "Test Tag",
"order": 1,
"domain": "[]",
"max_batch_size": 500,
}
)
self.payment_manager.batch_tag_ids = [(4, batch_tag.id)]
with patch.object(
type(self.env["spp.payment"]),
"create",
counting_create,
):
self.payment_manager._prepare_payments(self.cycle, self.entitlements)
self.assertEqual(
call_count,
1,
f"create() should be called once (batch), was called {call_count} times",
)
self.assertEqual(total_created, 5)
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## 19.0 #128 +/- ##
==========================================
- Coverage 70.14% 69.98% -0.17%
==========================================
Files 739 783 +44
Lines 43997 46605 +2608
==========================================
+ Hits 30863 32617 +1754
- Misses 13134 13988 +854
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Summary
SppCashEntitlementManager.prepare_entitlements()(singlecreate(vals_list)instead of per-beneficiary loop)DefaultFilePaymentManager._prepare_payments()(singlecreate(vals_list)per batch tag instead of per-entitlement loop)bank_idsprefetch to avoid N+1 queries during payment preparationChanges
entitlement_manager_cash.py:159-165for ent: self.env["spp.entitlement"].create(dict)— one INSERT per beneficiarycreate(vals_list)payment_manager.py:214-249for entitlement: self.env["spp.payment"].create(dict)— one INSERT per entitlement, plus per-recordpayment.account_number =writeaccount_numberincluded, singlecreate(vals_list), thenbatch_payments.write({"batch_id": ...})for batch assignmentContext
Phase 5 of 9 in the
spp_programsperformance optimization effort. Independent of other phases.Test Plan
./scripts/test_single_module.sh spp_programspassestest_cash_manager_batch_creates_entitlements— verifies singlecreate()call for 5 beneficiariestest_payment_manager_batch_creates_payments— verifies singlecreate()call for 5 entitlements