1717from kernelCI_app .models import Issues , Checkouts , Builds , Tests , Incidents
1818
1919from kernelCI_app .management .commands .helpers .process_submissions import (
20+ TableNames ,
2021 build_instances_from_submission ,
2122)
2223
@@ -293,6 +294,38 @@ def prepare_file_data(filename, trees_name, spool_dir):
293294 }
294295
295296
297+ # TODO: MOVE THESE TYPES AND CONSTS
298+ type TableModels = Issues | Checkouts | Builds | Tests | Incidents
299+
300+ MODEL_MAP : dict [TableNames , TableModels ] = {
301+ "issues" : Issues ,
302+ "checkouts" : Checkouts ,
303+ "builds" : Builds ,
304+ "tests" : Tests ,
305+ "incidents" : Incidents ,
306+ }
307+
308+
309+ def consume_buffer (buffer : list [TableModels ], item_type : TableNames ) -> None :
310+ """
311+ Consume a buffer of items and insert them into the database.
312+ This function is called by the db_worker thread.
313+ """
314+ if not buffer :
315+ return
316+
317+ model = MODEL_MAP [item_type ]
318+
319+ t0 = time .time ()
320+ model .objects .bulk_create (
321+ buffer ,
322+ batch_size = INGEST_BATCH_SIZE ,
323+ ignore_conflicts = True ,
324+ )
325+ _out ("bulk_create %s: n=%d in %.3fs" % (item_type , len (buffer ), time .time () - t0 ))
326+
327+
328+ # TODO: lower the complexity of this function
296329def db_worker (stop_event : threading .Event ): # noqa: C901
297330 """
298331 Worker thread that processes the database queue.
@@ -303,11 +336,11 @@ def db_worker(stop_event: threading.Event): # noqa: C901
303336 """
304337
305338 # Local buffers for batching
306- issues_buf = []
307- checkouts_buf = []
308- builds_buf = []
309- tests_buf = []
310- incidents_buf = []
339+ issues_buf : list [ Issues ] = []
340+ checkouts_buf : list [ Checkouts ] = []
341+ builds_buf : list [ Builds ] = []
342+ tests_buf : list [ Tests ] = []
343+ incidents_buf : list [ Incidents ] = []
311344
312345 last_flush_ts = time .time ()
313346
@@ -331,55 +364,11 @@ def flush_buffers():
331364 try :
332365 # Single transaction for all tables in the flush
333366 with transaction .atomic ():
334- if issues_buf :
335- t0 = time .time ()
336- Issues .objects .bulk_create (
337- issues_buf , batch_size = INGEST_BATCH_SIZE , ignore_conflicts = True
338- )
339- _out (
340- "bulk_create issues: n=%d in %.3fs"
341- % (len (issues_buf ), time .time () - t0 )
342- )
343- if checkouts_buf :
344- t0 = time .time ()
345- Checkouts .objects .bulk_create (
346- checkouts_buf ,
347- batch_size = INGEST_BATCH_SIZE ,
348- ignore_conflicts = True ,
349- )
350- _out (
351- "bulk_create checkouts: n=%d in %.3fs"
352- % (len (checkouts_buf ), time .time () - t0 )
353- )
354- if builds_buf :
355- t0 = time .time ()
356- Builds .objects .bulk_create (
357- builds_buf , batch_size = INGEST_BATCH_SIZE , ignore_conflicts = True
358- )
359- _out (
360- "bulk_create builds: n=%d in %.3fs"
361- % (len (builds_buf ), time .time () - t0 )
362- )
363- if tests_buf :
364- t0 = time .time ()
365- Tests .objects .bulk_create (
366- tests_buf , batch_size = INGEST_BATCH_SIZE , ignore_conflicts = True
367- )
368- _out (
369- "bulk_create tests: n=%d in %.3fs"
370- % (len (tests_buf ), time .time () - t0 )
371- )
372- if incidents_buf :
373- t0 = time .time ()
374- Incidents .objects .bulk_create (
375- incidents_buf ,
376- batch_size = INGEST_BATCH_SIZE ,
377- ignore_conflicts = True ,
378- )
379- _out (
380- "bulk_create incidents: n=%d in %.3fs"
381- % (len (incidents_buf ), time .time () - t0 )
382- )
367+ consume_buffer (issues_buf , "issues" )
368+ consume_buffer (checkouts_buf , "checkouts" )
369+ consume_buffer (builds_buf , "builds" )
370+ consume_buffer (tests_buf , "tests" )
371+ consume_buffer (incidents_buf , "incidents" )
383372 except Exception as e :
384373 logger .error ("Error during bulk_create flush: %s" , e )
385374 finally :
0 commit comments