← Back to Pull requests

[AAP-103748] perf(analytics): use optimistic update-first in APIUsageCounter.record_request #556

Closed
unknown closed this pull request without merging on Nov 27, 2026 11:04 UTC
View on GitHub
unknown unknown commented on Nov 26, 2026 15:07 UTC
Author

Description


  • What is being changed?

  • - Updates APIUsageCounter.record_request() in apps/analytics/models.py to execute the atomic UPDATE ... SET request_count = request_count + 1 first, only falling back to get_or_create(..., defaults={"request_count": 1}) (with a race-safe increment if not created) when 0 rows were updated.
    - Adds unit tests in tests/unit/analytics/test_models.py covering first-request creation, single-query steady-state increments, and the concurrent-insert race fallback.
  • Why is this change needed?

  • - APIUsageMiddleware calls APIUsageCounter.record_request() on every tracked analytics API request. Previously, every call ran two database round trips (get_or_create SELECT followed by UPDATE) even though the daily counter row already exists for 99.9%+ of requests.
  • How does this change address the issue?

  • - 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


  • Bug fix (non-breaking change which fixes an issue)

  • New feature (non-breaking change which adds functionality)

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

  • Documentation update

  • Test update

  • Refactoring (no functional changes)

  • Development environment change

  • Configuration change
  • Self-Review Checklist


  • I have performed a self-review of my code

  • I have added relevant comments to complex code sections

  • I have updated documentation where needed

  • I have considered the security impact of these changes

  • I have considered performance implications

  • I have thought about error handling and edge cases

  • I have tested the changes in my local environment
  • Testing Instructions


    Prerequisites


  • Local PostgreSQL instance running (e.g. make compose or local test DB)
  • Steps to Test


    1. Run lint and format checks:
       uv run ruff format --check apps/analytics/models.py tests/unit/analytics/test_models.py
       uv run ruff check apps/analytics/models.py tests/unit/analytics/test_models.py
       

    2. Run the analytics model and middleware tests:
       METRICS_SERVICE_MODE=test uv run pytest \
         tests/unit/analytics/test_models.py \
         tests/unit/analytics/test_usage_middleware.py \
         tests/unit/tasks/test_rollup_and_anonymize_telemetry.py
       

    Expected Results


  • Ruff checks pass and all 38 tests pass, confirming steady-state APIUsageCounter.record_request() runs a single UPDATE query while first-request inserts and concurrent-insert races remain accurate.
  • Diff: 364 lines (+273 / -91) across 4 files Reviewer: redhat-chai-bot Review duration: 1197m
    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
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.7% (MEDIUM) · Higher = larger blast radius
    42.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 16.9% · Safe to merge
    16.9%