add caching and logging for kpis#936
Conversation
WalkthroughThe changes introduce caching and logging infrastructure to the Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
commcare_connect/reports/helpers.py (1)
75-93: Cache key does not reflect dynamically computed defaults, causing stale data.The
vary_onkeys use the raw parameter values (from_date=None,to_date=None), but the function body appliesnow().date()as the actual value (lines 92-93). This means:
- A call with
from_date=Noneon January 1st caches with key(..., None, ...).- A call with
from_date=Noneon January 2nd hits the same cache entry and returns stale January 1st data.Normalize defaults before caching, or pass the cache key computation through a skip condition.
Suggested fix: normalize defaults before caching
Extract the default normalization into a wrapper or compute the actual values first:
+def get_table_data_for_year_month( + from_date=None, + to_date=None, + delivery_type=None, + program=None, + network_manager=None, + opportunity=None, + country_currency=None, +): + today = now().date() + from_date = from_date or today + to_date = to_date if to_date and to_date <= today else today + return _get_table_data_for_year_month_cached( + from_date, to_date, delivery_type, program, network_manager, opportunity, country_currency + ) + + `@quickcache`( vary_on=["from_date", "to_date", "delivery_type", "program", "network_manager", "opportunity", "country_currency"], timeout=60 * 60 * 24, ) -def get_table_data_for_year_month( +def _get_table_data_for_year_month_cached( from_date=None, to_date=None, ... ): - from_date = from_date or now().date() - to_date = to_date if to_date and to_date <= now().date() else now().date() + # from_date and to_date are already normalized by the wrapper
🧹 Nitpick comments (1)
commcare_connect/reports/helpers.py (1)
88-91: Logging inside cached function only captures cache misses.The PR states the intent is to "understand how often other filters are used." Since
logger.infois inside the cached function, it only logs when the cache is bypassed. Cache hits (potentially the majority of requests) are not logged.If you need full usage analytics, move logging to the calling view or add a non-cached wrapper. If logging cache misses only is intentional (e.g., to track cache effectiveness), this is fine—consider adding a brief comment to clarify.
Technical Summary
Adds simple caching and logging to the kpi report page. The logging is designed to inform future optimizations, and specifically to understand how often other filters are used.
Safety Assurance
Safety story
No change to the calculations just caching, and internal use only.
Labels & Review