@@ -33,22 +33,27 @@ def get_project_top_transaction_traces_for_llm_detection(
3333 logger .exception ("Project does not exist" , extra = {"project_id" : project_id })
3434 return []
3535
36- random_offset = random .randint (1 , 8 )
3736 end_time = datetime .now (UTC )
3837 start_time = end_time - timedelta (minutes = start_time_delta_minutes )
39-
40- # use for both queries to ensure they are searching the same time window
41- snuba_params = SnubaParams (
42- start = start_time ,
43- end = end_time ,
44- projects = [project ],
45- organization = project .organization ,
46- )
4738 config = SearchResolverConfig (auto_fields = True )
4839
49- # Step 1: Get top transactions by total time in time window
40+ def _build_snuba_params (start : datetime ) -> SnubaParams :
41+ """
42+ Both queries have different start times and the same end time.
43+ """
44+ return SnubaParams (
45+ start = start ,
46+ end = end_time ,
47+ projects = [project ],
48+ organization = project .organization ,
49+ )
50+
51+ transaction_snuba_params = _build_snuba_params (start_time )
52+ random_offset = random .randint (1 , 8 )
53+ trace_snuba_params = _build_snuba_params (end_time - timedelta (minutes = 30 - random_offset ))
54+
5055 transactions_result = Spans .run_table_query (
51- params = snuba_params ,
56+ params = transaction_snuba_params ,
5257 query_string = "is_transaction:true" ,
5358 selected_columns = [
5459 "transaction" ,
@@ -74,10 +79,9 @@ def get_project_top_transaction_traces_for_llm_detection(
7479 if normalized_name in seen_names :
7580 continue
7681
77- # Step 2: Get ONE trace for this transaction from THE SAME time window
7882 escaped_transaction_name = UNESCAPED_QUOTE_RE .sub ('\\ "' , transaction_name )
7983 trace_result = Spans .run_table_query (
80- params = snuba_params ,
84+ params = trace_snuba_params ,
8185 query_string = f'is_transaction:true transaction:"{ escaped_transaction_name } "' ,
8286 selected_columns = ["trace" , "precise.start_ts" ],
8387 orderby = ["precise.start_ts" ], # First trace in the window
0 commit comments