Skip to content

Allow to log (debug) queries in text form to dedicated log file#419

Draft
kofrezo wants to merge 8 commits intomainfrom
dk_log_queries
Draft

Allow to log (debug) queries in text form to dedicated log file#419
kofrezo wants to merge 8 commits intomainfrom
dk_log_queries

Conversation

@kofrezo
Copy link
Contributor

@kofrezo kofrezo commented Dec 3, 2025

No description provided.

@kofrezo kofrezo self-assigned this Dec 3, 2025
@kofrezo kofrezo requested a review from brainexe February 24, 2026 10:01
@kofrezo kofrezo changed the title Log Serveradmin/Shell queries to a dedicated file Allow to log (debug) queries in text form to dedicated log file Feb 24, 2026
kofrezo and others added 8 commits February 24, 2026 11:02
Introduce sql_generator_v2.py that rewrites the query generation using
Django ORM instead of raw SQL string concatenation. This provides better
security through parameterized queries and leverages Django's query
optimization.

Key changes:
- New query_builder.py facade for switching between v1/v2 implementations
- New sql_generator_v2.py with Django ORM-based query generation
- SQL_GENERATOR_VERSION setting to toggle implementations ('v1' or 'v2')
- query_executer.py updated to handle both SQL strings and QuerySets
- Comprehensive unit tests comparing v1 and v2 results

The v2 implementation supports all filter types (BaseFilter, Regexp,
comparison, containment, logical) and attribute types (special, standard,
relation, reverse, supernet, domain) with related_via traversal.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
The error "can't adapt type 'OuterRef'" occurred because:
1. OuterRef(OuterRef(...)) is invalid - Django's OuterRef doesn't nest
2. OuterRef cannot be passed as a parameter to RawSQL

Fixed by restructuring _build_supernet_lookup and _build_supernet_related_query
to reference the outer query's server table directly in the SQL string using
"server"."server_id" instead of trying to use nested OuterRef expressions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Remove defer('intern_ip') for v2 QuerySets to prevent N+1 queries.

The issue: defer() on a v2 QuerySet excludes intern_ip from the SELECT,
but QueryMaterializer later accesses intern_ip, causing Django to fetch
it individually for each server (thousands of extra queries).

The v1 raw SQL already includes intern_ip in the SELECT clause, so
defer() has no effect there. Removing defer() for v2 matches this
behavior and eliminates the N+1 problem.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
The issue: OuterRef('server_id') inside a nested Subquery incorrectly
references the immediate outer query's server_id (ServerAttribute table)
instead of the outer Server table's server_id.

For example, external_domain servers with responsible_admin configured
as "related via" were missing because the relation lookup was correlating
with the wrong table.

Fixed by using RawSQL with direct reference to "server"."server_id"
for both relation and reverse related_via types, similar to the
supernet case. This ensures proper correlation with the outer Server query.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
The query "route_network=foe-aw-int" was 14x slower (5.7s vs 400ms) because
the hostname filter was applied AFTER a correlated subquery, causing it to
be re-evaluated for each row in the outer query.

Before (slow - nested structure):
  EXISTS (
    SELECT FROM server U0
    WHERE U0.server_id IN (correlated supernet lookup)  -- per-row!
    AND U0.hostname = 'foe-aw-int'
  )

After (fast - matches v1 structure):
  EXISTS (
    SELECT FROM server supernet
    JOIN inet tables
    WHERE inet_containment
    AND supernet.server_id IN (
      SELECT server_id FROM server WHERE hostname = 'foe-aw-int'  -- once!
    )
  )

The key optimization: the hostname filter is now a NON-CORRELATED subquery
that PostgreSQL can evaluate once and cache, rather than per-row.

Also added _build_hostname_filter_sql() helper to convert Q objects to SQL
for embedding in the optimized query structure.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Add a method that takes the internal representation of a Query (dict)
and builds the text query from it.

This will be handy to log queries for debugging.
Add a configurable logger for logging Servershell and API
queries to a dedicated file.
@kofrezo kofrezo removed the request for review from brainexe February 24, 2026 10:08
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.

1 participant