@@ -36,19 +36,26 @@ def get_project_top_transaction_traces_for_llm_detection(
3636 random_offset = random .randint (1 , 8 )
3737 end_time = datetime .now (UTC )
3838 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- )
4739 config = SearchResolverConfig (auto_fields = True )
4840
41+ def _build_snuba_params (start : datetime ) -> SnubaParams :
42+ """
43+ Both queries have different start times and the same end time.
44+ """
45+ return SnubaParams (
46+ start = start ,
47+ end = end_time ,
48+ projects = [project ],
49+ organization = project .organization ,
50+ )
51+
52+ transaction_snuba_params = _build_snuba_params (start_time )
53+ random_offset = random .randint (1 , 8 )
54+ trace_snuba_params = _build_snuba_params (end_time - timedelta (minutes = 30 - random_offset ))
55+
4956 # Step 1: Get top transactions by total time in time window
5057 transactions_result = Spans .run_table_query (
51- params = snuba_params ,
58+ params = transaction_snuba_params ,
5259 query_string = "is_transaction:true" ,
5360 selected_columns = [
5461 "transaction" ,
@@ -77,7 +84,7 @@ def get_project_top_transaction_traces_for_llm_detection(
7784 # Step 2: Get ONE trace for this transaction from THE SAME time window
7885 escaped_transaction_name = UNESCAPED_QUOTE_RE .sub ('\\ "' , transaction_name )
7986 trace_result = Spans .run_table_query (
80- params = snuba_params ,
87+ params = trace_snuba_params ,
8188 query_string = f'is_transaction:true transaction:"{ escaped_transaction_name } "' ,
8289 selected_columns = ["trace" , "precise.start_ts" ],
8390 orderby = ["precise.start_ts" ], # First trace in the window
0 commit comments