Skip to content

feat: API performance improvements - gzip compression, N+1 detection, query caching, DB explain endpoint - #749

Merged
DokaIzk merged 2 commits into
SoroScan:mainfrom
trinnode:feat/performance-api-improvements
Jun 25, 2026
Merged

feat: API performance improvements - gzip compression, N+1 detection, query caching, DB explain endpoint#749
DokaIzk merged 2 commits into
SoroScan:mainfrom
trinnode:feat/performance-api-improvements

Conversation

@trinnode

Copy link
Copy Markdown
Contributor

Summary

This PR addresses four performance and debugging issues for the SoroScan API:

Closes #487 — Gzip Compression for API Responses

  • Verified django.middleware.gzip.GZipMiddleware is active in the middleware stack (positioned after CommonMiddleware for proper content-type detection)
  • Added 8 comprehensive tests covering: compression activation, Accept-Encoding handling, small response skip, double-compression prevention, and JSON API response compression
  • Responses >500 bytes are automatically compressed when client sends Accept-Encoding: gzip

Closes #488 — Query Result Caching Layer

  • Added 30s TTL caching for GET /api/ingest/contracts/ endpoint via stable_cache_key + get_or_set_json
  • Cache automatically invalidated on all GraphQL mutations (register_contract, update_contract, set_contract_metadata, delete_contract_metadata)
  • Added CacheBustingMiddleware supporting Cache-Control: no-cache and X-Cache-Bust: 1 headers
  • Added GET /api/cache/stats/ endpoint for monitoring cache status and configuration
  • 16 tests covering cache utilities, @cache_result decorator, cache stats endpoint, and contracts list caching behavior

Closes #490 — N+1 Query Detection in GraphQL

  • Added N1QueryDetectorExtension — a Strawberry schema extension that counts DB queries per resolver
  • Logs warnings when a resolver executes >5 queries (potential N+1 pattern)
  • Enabled via GRAPHQL_N1_DETECTION_ENABLED setting (defaults to DEBUG mode, zero overhead in production)
  • Integrated into the GraphQL schema extensions alongside existing rate limiting and logging extensions
  • 8 tests covering enable/disable logic, resolver skipping, and warning emission

Closes #491 — EXPLAIN ANALYZE Endpoint for Query Debugging

  • Added POST /api/admin/db/explain/ endpoint returning query execution plans
  • Accepts {"query": "SELECT ...", "analyze": true} — falls back to EXPLAIN on SQLite
  • SQL injection prevention: only SELECT, WITH, and EXPLAIN statements allowed via regex whitelist
  • Secured to admin (staff) users only with IsAuthenticated + staff check
  • Rate limited via UserRateThrottle
  • 9 tests covering admin access, regular user rejection, anonymous rejection, dangerous query blocking, and WITH clause support

Test Results

  • 46 new tests added across 5 test files, all passing
  • Zero regressions — all 17 pre-existing test failures on main remain unchanged

Files Changed

  • django-backend/soroscan/settings.py — N+1 detection config + logger setup
  • django-backend/soroscan/middleware.pyCacheBustingMiddleware class
  • django-backend/soroscan/urls.py — new endpoint routes
  • django-backend/soroscan/ingest/views.py — DB explain view, cache stats view, contracts list caching
  • django-backend/soroscan/ingest/schema.py — N+1 extension, cache invalidation on mutations
  • django-backend/soroscan/graphql_n1_detector.py — new N+1 detection extension
  • 5 new test files covering all features

…ion, query caching, and DB explain endpoint

- Gzip compression: verified GZipMiddleware is active, added comprehensive tests for compression behavior, Accept-Encoding handling, and edge cases (issue SoroScan#487)
- N+1 query detection: added GraphQL extension that identifies lazy-loading patterns and logs warnings in dev mode with zero production overhead (issue SoroScan#490)
- Query result caching: added 30s TTL caching for GET /contracts endpoint, cache invalidation on GraphQL mutations, cache busting headers via Cache-Control/X-Cache-Bust, and cache stats endpoint (issue SoroScan#488)
- DB explain endpoint: added /api/admin/db/explain/ for admins to run EXPLAIN ANALYZE on SQL queries with SQL injection prevention (SELECT-only) and rate limiting (issue SoroScan#491)
@drips-wave

drips-wave Bot commented Jun 25, 2026

Copy link
Copy Markdown

@trinnode Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

…x URL routing, cache invalidation, and lint

- Added GZipMiddleware and CacheBustingMiddleware to settings_test.py for CI parity
- Added GRAPHQL_N1_DETECTION_ENABLED=False to test settings
- Added db_explain and cache_stats endpoints to urls_test.py
- Fixed contracts list cache invalidation on create/update/delete to prevent stale responses
- Fixed all ruff lint issues (unused imports, lambda assignments, E402)
- Moved import re to top-level in views.py
- Removed unused backend_name variable in cache_stats_view
- All 744 tests pass with settings_test.py (CI configuration)
@DokaIzk
DokaIzk merged commit 3bacec5 into SoroScan:main Jun 25, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Database Query Execution Plans N+1 Query Detection in GraphQL Query Result Caching Layer API Response Compression (gzip)

3 participants