← Back to Pull requests

[AAP-103746] perf(api): optimize health check, viewset queries, and API root pattern lookup #554

Closed
unknown closed this pull request without merging on Nov 27, 2026 11:01 UTC
View on GitHub
unknown unknown commented on Nov 26, 2026 14:36 UTC
Author

Description


  • What is being changed?

  • - apps/core/views/health.py: uses .only("status", "modified") when fetching the latest AnonymizedMetricsPayload in HealthView.get() so the anonymized_data JSONB blob is not transferred or deserialized on health probes.
    - apps/tasks/v1/views.py & apps/tasks/v1/serializers.py: annotates _executions_count=Count("executions") on TaskViewSet actions serialized by TaskSerializer (with .order_by("id") preserving Task.Meta.ordering on pending/running and a live-count fallback for standalone serializer callers), and uses .only("id", "name", "status", "completed_at") on the dry-run cleanup preview.
    - apps/core/v1/viewsets/organization.py & apps/core/v1/viewsets/team.py: adds select_related("created_by", "modified_by", "resource") (and "organization" on TeamViewSet) to eliminate per-row queries when serializing summary_fields.
    - apps/core/views/api_root.py: caches extracted (clean_path, pattern_name) tuples per URLResolver instance in a WeakKeyDictionary.
    - Adds unit tests guarding SQL column exclusion, constant query counts, ordering preservation, and resolver-scoped caching.
  • Why is this change needed?

  • - /health/ probes were fetching multi-MB anonymized_data JSONB blobs on every probe, list endpoints were issuing O(N) queries per page, and APIRootView was recursively running simplify_regex() over the full URL tree on every request.
  • How does this change address the issue?

  • - Avoids fetching unused JSONB blobs, collapses O(N) viewset queries into O(1) joined/annotated queries, and caches immutable URL pattern extraction per resolver. In paired benchmarks on a containerized reference deployment (RHEL 9.6, Python 3.12, Django 5.2, PostgreSQL 15, identical SHA-256 response digests):
    - /health/: 4.73× faster (3.681s → 0.779s for 100 reqs), -13.0 MiB peak RSS
    - ViewSet endpoints: 2.83× faster (3.738s → 1.319s; queries 27→2 tasks, 19→1 pending, 55→6 orgs, 81→6 teams)
    - APIRootView (/api/v1/): 3.17× faster (1.215s → 0.383s for 500 reqs)

    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/ tests/
       uv run ruff check apps/ tests/
       

    2. Run the targeted test suites:
       METRICS_SERVICE_MODE=test uv run pytest \
         apps/core/tests/test_health.py \
         apps/core/tests/test_api_root.py \
         tests/coverage/core/test_core_views.py \
         tests/unit/general/test_health_metrics.py \
         tests/unit/tasks/test_viewset_query_efficiency.py \
         tests/unit/tasks/test_tasks_api_views.py \
         tests/coverage/tasks/test_tasks_v1_views.py
       

    Expected Results


  • Ruff checks pass and all 127 tests pass, verifying anonymized_data is excluded from /health/ SQL, constant query counts and preserved ordering across task/org/team endpoints, and single-extraction caching per resolver in APIRootView.
  • Diff: 484 lines (+363 / -121) across 6 files Reviewer: redhat-chai-bot Review duration: 1224m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.47) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.47 · 484 lines diff across 6 files
    0.47
    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.370
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #554"
    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: 47.3% (MEDIUM) · Higher = larger blast radius
    47.3%
    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: 18.7% · Safe to merge
    18.7%