[AAP-103748] perf(analytics): use optimistic update-first in APIUsageCounter.record_request #556
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 364 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.353
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #556"
general
weight: 0.35
redhat-chai-bot
Description
- Updates
APIUsageCounter.record_request()inapps/analytics/models.pyto execute the atomicUPDATE ... SET request_count = request_count + 1first, only falling back toget_or_create(..., defaults={"request_count": 1})(with a race-safe increment ifnot created) when 0 rows were updated.- Adds unit tests in
tests/unit/analytics/test_models.pycovering first-request creation, single-query steady-state increments, and the concurrent-insert race fallback.-
APIUsageMiddlewarecallsAPIUsageCounter.record_request()on every tracked analytics API request. Previously, every call ran two database round trips (get_or_createSELECTfollowed byUPDATE) even though the daily counter row already exists for 99.9%+ of requests.- Cuts steady-state database queries per tracked analytics request from 2 to 1 with no schema migration required. In paired benchmarks on a containerized reference deployment (RHEL 9.6, Python 3.12, Django 5.2, PostgreSQL 15, 1,000 steady-state increments + 1 cold insert, identical
daily_summary()SHA-256 digests), wall time improved 1.35× (2.129s → 1.572s,-26.2%).Type of Change
Self-Review Checklist
Testing Instructions
Prerequisites
make composeor local test DB)Steps to Test
1. Run lint and format checks:
2. Run the analytics model and middleware tests:
Expected Results
APIUsageCounter.record_request()runs a singleUPDATEquery while first-request inserts and concurrent-insert races remain accurate.