perf(spp_programs): fetch fund balance once per approval batch#127
perf(spp_programs): fetch fund balance once per approval batch#127kneckinator wants to merge 1 commit into19.0from
Conversation
Both DefaultCashEntitlementManager and SppCashEntitlementManager called check_fund_balance() (2 SQL queries) per entitlement inside the approval loop. Now the balance is fetched once before the loop and tracked via a running total in Python.
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 introduces a key performance enhancement to the entitlement approval process 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 a significant performance optimization by fetching the fund balance only once per approval batch, which is a great improvement. The changes are implemented correctly in both DefaultCashEntitlementManager and SppCashEntitlementManager, and are well-covered by new tests. However, I've found a critical issue where the new logic can cause a crash if an empty list of entitlements is processed. I've provided suggestions to fix this. I've also suggested adding a test case to cover this scenario.
| entitlements.mapped("journal_id.currency_id") | ||
|
|
||
| # Fetch fund balance once for the whole batch instead of per entitlement | ||
| fund_balance = self.check_fund_balance(entitlements[0].cycle_id.program_id.id) |
There was a problem hiding this comment.
This line will raise an IndexError if entitlements is an empty list. You should handle this case to prevent a crash. A simple way is to use a conditional expression to avoid accessing the list if it's empty.
| fund_balance = self.check_fund_balance(entitlements[0].cycle_id.program_id.id) | |
| fund_balance = self.check_fund_balance(entitlements[0].cycle_id.program_id.id) if entitlements else 0.0 |
| entitlements.mapped("journal_id.currency_id") | ||
|
|
||
| # Fetch fund balance once for the whole batch instead of per entitlement | ||
| fund_balance = self.check_fund_balance(entitlements[0].cycle_id.program_id.id) |
There was a problem hiding this comment.
This line will raise an IndexError if entitlements is an empty list. You should handle this case to prevent a crash. A simple way is to use a conditional expression to avoid accessing the list if it's empty.
| fund_balance = self.check_fund_balance(entitlements[0].cycle_id.program_id.id) | |
| fund_balance = self.check_fund_balance(entitlements[0].cycle_id.program_id.id) if entitlements else 0.0 |
| class TestFundBalanceOptimization(TransactionCase): | ||
| """Test that fund balance is fetched once per approval batch, not per entitlement. | ||
|
|
||
| The approve_entitlements methods previously called check_fund_balance() | ||
| (2 SQL queries) inside the per-entitlement loop. Now the balance is | ||
| fetched once and tracked via a running total in Python. | ||
| """ |
There was a problem hiding this comment.
It's great that you've added tests for this optimization. However, there's a critical edge case that is not covered: when approve_entitlements is called with an empty list of entitlements. The current implementation would crash with an IndexError. I've left comments on the implementation files with a fix. It would be beneficial to add a test case to cover this scenario and prevent future regressions.
You could add a test like this:
def test_approve_entitlements_with_empty_list(self):
"""approve_entitlements should not fail with an empty list of entitlements."""
manager = self._create_default_manager()
# For DefaultCashEntitlementManager
state_err, message = manager.approve_entitlements(self.env["spp.entitlement"])
self.assertEqual(state_err, 0)
self.assertEqual(message, "")
# For SppCashEntitlementManager
cash_manager = self._create_cash_manager()
state_err, message = cash_manager.approve_entitlements(self.env["spp.entitlement"])
self.assertEqual(state_err, 0)
self.assertEqual(message, "")
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 19.0 #127 +/- ##
==========================================
- Coverage 70.14% 70.02% -0.13%
==========================================
Files 739 783 +44
Lines 43997 46599 +2602
==========================================
+ Hits 30863 32630 +1767
- Misses 13134 13969 +835
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Summary
DefaultCashEntitlementManagerandSppCashEntitlementManagerChanges
managers/entitlement_manager_base.pycheck_fund_balance()call before the loop, replacefund_balancewithremaining_balancecomputed from running totalmanagers/entitlement_manager_cash.pytests/test_fund_balance.pyBefore
After
Context
Phase 4 of 9 in the
spp_programsperformance optimization effort. Independent of other phases.Test Plan
./scripts/test_single_module.sh spp_programspassestest_fund_balance_insufficient_stops_early)test_fund_balance_running_total_correct)