feat: API performance improvements - gzip compression, N+1 detection, query caching, DB explain endpoint - #749
Merged
DokaIzk merged 2 commits intoJun 25, 2026
Conversation
…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)
|
@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! 🚀 |
…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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR addresses four performance and debugging issues for the SoroScan API:
Closes #487 — Gzip Compression for API Responses
django.middleware.gzip.GZipMiddlewareis active in the middleware stack (positioned afterCommonMiddlewarefor proper content-type detection)Accept-Encoding: gzipCloses #488 — Query Result Caching Layer
GET /api/ingest/contracts/endpoint viastable_cache_key+get_or_set_jsonregister_contract,update_contract,set_contract_metadata,delete_contract_metadata)CacheBustingMiddlewaresupportingCache-Control: no-cacheandX-Cache-Bust: 1headersGET /api/cache/stats/endpoint for monitoring cache status and configuration@cache_resultdecorator, cache stats endpoint, and contracts list caching behaviorCloses #490 — N+1 Query Detection in GraphQL
N1QueryDetectorExtension— a Strawberry schema extension that counts DB queries per resolverGRAPHQL_N1_DETECTION_ENABLEDsetting (defaults toDEBUGmode, zero overhead in production)Closes #491 — EXPLAIN ANALYZE Endpoint for Query Debugging
POST /api/admin/db/explain/endpoint returning query execution plans{"query": "SELECT ...", "analyze": true}— falls back toEXPLAINon SQLiteSELECT,WITH, andEXPLAINstatements allowed via regex whitelistIsAuthenticated+ staff checkUserRateThrottleTest Results
mainremain unchangedFiles Changed
django-backend/soroscan/settings.py— N+1 detection config + logger setupdjango-backend/soroscan/middleware.py—CacheBustingMiddlewareclassdjango-backend/soroscan/urls.py— new endpoint routesdjango-backend/soroscan/ingest/views.py— DB explain view, cache stats view, contracts list cachingdjango-backend/soroscan/ingest/schema.py— N+1 extension, cache invalidation on mutationsdjango-backend/soroscan/graphql_n1_detector.py— new N+1 detection extension