@@ -27,10 +27,12 @@ class QueryCounter:
2727 """Database execute wrapper that counts queries and measures total DB time."""
2828
2929 def __init__ (self ):
30+ """Initialize the query counter."""
3031 self .count = 0
3132 self .total_time = 0.0
3233
3334 def __call__ (self , execute , sql , params , many , context ):
35+ """Wrap a database query execution, tracking count and time."""
3436 start = time .monotonic ()
3537 try :
3638 return execute (sql , params , many , context )
@@ -43,24 +45,28 @@ class ProfileContext:
4345 """Stores profiling data for a single request."""
4446
4547 def __init__ (self , query_counter ):
48+ """Initialize the profile context."""
4649 self .start_time = time .monotonic ()
4750 self .timings = defaultdict (lambda : {"count" : 0 , "total_ms" : 0.0 })
4851 self .counters = defaultdict (int )
4952 self .query_counter = query_counter
5053
5154 def record_timing (self , name , duration_ms ):
55+ """Record a named timing measurement in milliseconds."""
5256 entry = self .timings [name ]
5357 entry ["count" ] += 1
5458 entry ["total_ms" ] += duration_ms
5559
5660 def increment (self , name , amount = 1 ):
61+ """Increment a named counter."""
5762 self .counters [name ] += amount
5863
5964 def db_query_snapshot (self ):
6065 """Returns current query count for computing deltas."""
6166 return self .query_counter .count
6267
6368 def summary (self ):
69+ """Return a summary string of all collected profiling data."""
6470 total_ms = (time .monotonic () - self .start_time ) * 1000
6571 parts = [
6672 f"total_ms={ total_ms :.1f} " ,
@@ -120,9 +126,11 @@ class DiodeProfileMiddleware:
120126 """
121127
122128 def __init__ (self , get_response ):
129+ """Initialize the middleware."""
123130 self .get_response = get_response
124131
125132 def __call__ (self , request ):
133+ """Process a request, collecting profiling data for Diode endpoints."""
126134 if not PROFILE_ENABLED or "/plugins/diode/" not in request .path :
127135 return self .get_response (request )
128136
0 commit comments