99
1010import httpx
1111import pandas as pd
12+ import redis
1213
1314from data_retrieval_app .external_data_retrieval .config import settings
1415from data_retrieval_app .external_data_retrieval .data_retrieval .utils import (
@@ -102,7 +103,7 @@ def _json_to_df(self, stashes: list) -> pd.DataFrame | None:
102103
103104 return df
104105
105- def _check_stashes (self , stashes : list ) -> pd .DataFrame :
106+ def _detector_filter (self , stashes : list ) -> pd .DataFrame :
106107 """
107108 Parameters:
108109 :param stashes: (list) A list of stash objects
@@ -176,11 +177,7 @@ def _get_latest_change_id(self) -> str:
176177
177178 return next_change_id
178179
179- def _follow_stream (
180- self ,
181- client : httpx .Client ,
182- stop_event : threading .Event ,
183- ):
180+ def _follow_stream (self , client : httpx .Client , stop_event : threading .Event ):
184181 local_pending = []
185182 change_id = self .initial_change_id
186183 weird_errors = 0
@@ -246,7 +243,7 @@ def _follow_stream(
246243 self .pending_queue .put (None )
247244
248245 @sync_timing_tracker
249- def _process_stream (self ) -> pd .DataFrame | None :
246+ def _process_stream (self , cache : redis . Redis ) -> pd .DataFrame | None :
250247 i = 0
251248 stashes = []
252249 while i < self .mini_batch_size :
@@ -259,26 +256,25 @@ def _process_stream(self) -> pd.DataFrame | None:
259256 if pending is None :
260257 return stashes
261258
262- # try:
263259 obj = json .loads (pending .response .decode ("utf-8" ))
264260 stashes .extend (obj ["stashes" ])
265- # finally:
266- # pending.response.close( )
261+
262+ cache . set ( "next_change_id" , pending .next_change_id )
267263
268264 i += 1
269265
270266 logger .info ("Stashes are ready for processing" )
271- wanted_df = self ._check_stashes (stashes )
267+ wanted_df = self ._detector_filter (stashes )
272268 logger .info ("Finished processing the data, waiting for more" )
273269 if wanted_df .empty :
274270 return None
275271 return wanted_df
276272
277- def _gather_n_checkpoints (self , n : int ) -> pd .DataFrame | None :
273+ def _gather_n_checkpoints (self , cache : redis . Redis , n : int ) -> pd .DataFrame | None :
278274 df = None
279275 for _ in range (n ):
280276 start_time = time .perf_counter ()
281- wanted_df = self ._process_stream ()
277+ wanted_df = self ._process_stream (cache )
282278 end_time = time .perf_counter ()
283279
284280 time_per_mini_batch = end_time - start_time
@@ -299,9 +295,16 @@ def _gather_n_checkpoints(self, n: int) -> pd.DataFrame | None:
299295 return df
300296
301297 def initialize_data_stream_threads (
302- self , executor : ThreadPoolExecutor , stop_event : threading .Event
298+ self ,
299+ executor : ThreadPoolExecutor ,
300+ stop_event : threading .Event ,
301+ cache : redis .Redis ,
303302 ):
304- self .initial_change_id = self ._get_latest_change_id ()
303+ self .initial_change_id = cache .get ("next_change_id" )
304+ if self .initial_change_id is None or settings .MANUAL_NEXT_CHANGE_ID :
305+ logger .info ("Using manually set change id" )
306+ self .initial_change_id = self ._get_latest_change_id ()
307+
305308 self .rate_limiter = RateLimiter ()
306309 self .mini_batch_size = settings .MINI_BATCH_SIZE
307310 self .pending_queue = Queue (maxsize = self .mini_batch_size )
@@ -318,11 +321,12 @@ def initialize_data_stream_threads(
318321 logging .getLogger ("httpx" ).setLevel (logging .WARNING )
319322 return executor .submit (self ._follow_stream , client , stop_event )
320323
321- def dump_stream (self ) -> Iterator [pd .DataFrame ]:
324+ def dump_stream (self , cache : redis . Redis ) -> Iterator [pd .DataFrame ]:
322325 time .sleep (5 ) # Waits for the listening threads to have time to start up.
323326 while True :
324327 logger .info ("Waiting for data from the stream" )
325328 df = self ._gather_n_checkpoints (
329+ cache ,
326330 n = settings .N_CHECKPOINTS_PER_TRANSFORMATION ,
327331 )
328332 if df is None :
0 commit comments