
    	]j-n                     ~   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d d	lmZmZmZ d d
lmZ d dlmZ d dlmZ d dlmZ d dlmZ d dl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z'  e jP                  e)      Z*	 ddeed      fdZ+d Z,d Z-ddZ. e	ej^                        Z0d Z1d Z2d Z3y)    N)CallableOptional)flag_set)	load_func)%load_tasks_for_async_import_streaming)settings)transaction)LabelInterface)ProjectImportProjectReimportProjectSummary)ValidationError)Task)"sanitize_prediction_import_payload)User)WebhookAction)emit_webhooks_for_instance   )
FileUpload)ImportApiSerializer)load_tasks_for_async_importrecalculate_stats_func).Nc                    t        j                         5  	 t        j                  j	                  |       }|j                  t        j                  j                  k7  r#t        j                  d|  d       	 d d d        y t        j                  j                  |_        |j                  dg       d d d        t        j                  j	                  |      }t        dd	
      r9t        j                  dj                   j"                          t%        ||       y t'        j&                         }j                   }d }t)        ||      \  }}	}
}|j*                  rQt        d|j,                  j.                  
      }t        j                  d|        t1        ||j*                  ||      }|j2                  rJt        d|j,                  j.                  
      r(g }t5        |j6                        }t9        |      D ]b  \  }}d|v st9        |d         D ]G  \  }}	 t;        |      }|j=                  |d      }|r!|D ]  }|j?                  d| d| d|         I d |rdtE        |       d}|D ]  }|d| dz  } t        d|j,                  j.                  
      r;||_        t        j                  jF                  |_        |j                  ddg       y t        j                  dtE        |       d|        |jH                  r)t        j                         5  tJ        j                  jM                         j	                  |      }tO        |dd|i      }|jQ                  d       	 |j                  |j"                         }tS        |jT                  |tV        jX                  |       tE        |      }tE        |jZ                        }tE        |j\                        }|||d!}|j_                  |d"d"d|#       t        j                  d$       |ja                  |       	 d d d        ntE        |      }d }d }t'        j&                         |z
  }xs d&|_1        xs d&|_2        xs d&|_3        ||_4        |	|_5        |
|_6        ||_7        |jp                  r|D cg c]  }|j"                   c}|_9        t        j                  jt                  |_        |j                          y # t        j
                  $ r$ t        j                  d|  d       Y d d d        y w xY w# 1 sw Y   xY w# t@        $ rH}d| d| dtC        |       }|j?                  |       t        j                  d|        Y d }~td }~ww xY w# t@        $ rZ}d%tC        |       }||_        t        j                  jF                  |_        |j                  ddg       Y d }~d d d        y d }~ww xY w# 1 sw Y   xY wc c}w )'NidzProjectImport with id $ not found, import processing failedzProcessing import with id  already startedstatusupdate_fieldsEfflag_fix_back_plt_902_async_import_background_oom_fix_22092025_shortautouserz#Using streaming import for project 1fflag_feat_utc_210_prediction_validation_15082025,Reformatting predictions with raise_errors: predictionsTreturn_errorsTask , prediction :  : Error validating prediction - Exception during validation: Prediction validation failed (
 errors):
- 
error3Prediction validation failed, not raising error - (projectr6   datamanycontextraise_exception
project_id
task_countannotation_countprediction_countFtasks_querysetmaximum_annotations_changed!overlap_cohort_percentage_changedtasks_number_changedrecalculate_stats_countsz)Tasks bulk_update finished (async import)zError creating tasks: r   );r	   atomicr   objectsgetDoesNotExistloggerr3   r   StatusCREATEDIN_PROGRESSsaver   r   infor6   r   "_async_import_background_streamingtimer   preannotated_from_fieldsorganization
created_byreformat_predictionslabel_config_is_not_defaultr
   label_config	enumerater   validate_predictionappend	ExceptionstrlenFAILEDcommit_to_projectr   select_for_updater   is_validr   active_organizationr   TASKS_CREATEDdb_annotationsdb_predictions%update_tasks_counters_and_task_statesupdate_data_columnsr@   rA   rB   durationfile_upload_idsfound_formatsdata_columnsreturn_task_idstask_ids	COMPLETED)	import_iduser_idr   kwargsproject_importr$   startr6   tasksrl   rm   rn   raise_errorsvalidation_errorsliitaskj
predictionvalidation_errors_listr3   e	error_msgerror_messagesummary
serializerr@   rA   rB   rH   rk   s                                  L/root/env/lib/python3.12/site-packages/label_studio/data_import/functions.pyasync_import_backgroundr      s    
			 
6	*2266)6DN   M$8$8$@$@@LL5i[@PQR
6 
6 !. 4 4 @ @8*5
6 <<w'DW^de9.:P:P:S:S9TUV*>4@IIKE$$GE ;VVdfj:k7E?M<..?gFZFZFeFe
 	B<.QR$UN,S,SU\^jk **x;'BVBVBaBa0 G001 ' 	RGAt$%.tM/B%C 
RMAz	R%G
%S
131G1G
bf1G1g.1)? _ 1 8 85=QRPSSUV[U\9] ^_
R	R <SAR=S<TT_`M* 02eWB/0 KRYRfRfRqRqr'4$(5(<(<(C(C%##7H2E#FI#N_J`Iaalmzl{| ''! )	$,,>>@DDWDUG -%dYX_L`aJ5!"7::>*4+C+CWmNiNikpq Z
#&z'@'@#A #&z'@'@#A 
 #-(8(8,( ==#(056;)--E >  GH++E2C)	 )	X Z
yy{U"H *aN&6&;!N#&6&;!N#&N%4N"#0N ".N%%7<"=t477"=)00::NC )) 	LL1)<`ab
6 
6	
6 
6d % R&+A3mA3>^_bcd_e^f$g	)00;'DYK%PQQRp  "8Q A'4$(5(<(<(C(C%##7H2E#FS)	 )	FG)	 )	t #>s   T6 S<AT62T6"AU)AW=;B9V1X
<,T3(T62T33T66U 	V=VV	W: AW5(W=5W::W==Xc                     | j                   d   }t        j                  j                  |      j	                  t        j
                  j                  t        j                         t        |             y Nr   r   )r   	tracebackr3   )
argsr   rJ   filterupdaterN   ra   r   
format_excr_   )job
connectiontypevalue_rr   s         r   set_import_background_failurer      sY    I  I .55##**i6J6J6LTWX]T^ 6     c                     | j                   d   }t        j                  j                  |      j	                  t        j
                  j                  t        j                         t        |             y r   )
r   r   rJ   r   r   rN   ra   r   r   r_   )r   r   r   r   r   reimport_ids         r   set_reimport_background_failurer      sX    ((1+K""k"299%%,,&&(%j : r   c           	      x   g }g }d}|r	 t        |j                        }t        |       D ]0  \  }}	d|	v r|	d   }
n|	}
g }|D ]   }||
vr|j                  d| d| d       !|
|   }|)d}d	}|ry	 |j                  |      }t        |d
      r[|j                  rOt        |j                  t              r|j                  d   n|j                  }|j                  j                         }t        |t              r|}n)|}|dk(  rd}|dv r||i}n|t        |t              s|gn|i}||||dgddd}|j                  |        |
|d}|j                  |       3 |r|rt!        d|i      |S # t        $ r0}t        j	                  d|j
                   d|        Y d}~d}~ww xY w# t        $ r Y w xY w)a  
    Transform flat task JSON objects into proper format with separate data and predictions fields.
    Also validates the predictions to ensure they are properly formatted using LabelInterface.

    Args:
        tasks: List of task data
        preannotated_from_fields: List of field names to convert to predictions
        project: Optional project instance to determine correct to_name and type from label config
    Nz,Could not create LabelInterface for project r,   r8   r*   z: Preannotated field 'z' not found in task datatextchoicesto_namer   textarea)ratingnumberdatetime)	from_namer   r   r   g      ?preannotated)resultscoremodel_version)r8   r'   preannotated_fields)r
   rZ   r^   rM   warningr   r[   r]   get_controlhasattrr   
isinstancelisttaglowerdictr   )rw   rU   r6   rx   	new_tasksry   rz   r   
task_indexr|   	task_datar'   fieldr   r   prediction_typecontrol_tagprediction_value	value_keyr~   new_tasks                        r   rX   rX      sA    I 
B	] 4 45B &e, F#
DT>VII- ;	/EI%!((5<RSXRYYq)rse$E  "+&(nnU&;";	:{?R?R $.k.A.A4#H !, 3 3A 6%0%8%8 $
 /:oo.C.C.EO eT*',$ !0I&*4$*	 '*JJ,5u+=( -6jQVX\F]wch+i(
 */'.$3%5	 !%3
 "":.w;	/| &kB"MF#R \46GHII_  	]NNI'**UWXYWZ[\\	]B % s*   E1 1A8F-1	F*:%F%%F*-	F98F9c                 "   	 t         j                  }d}d}d}i }t               }	g }
t        j                         5  |j                  | j                         ddd       d}t        j                  || j                  | j                  |      D ]  \  }}}|s#t        j                  d| j                          -|dz  }t        j                  d| dt        |       d| j                          t        j                         5  t        j                  j!                         j#                  |	      }t%        |d
||d      }|j'                  d
       |j)                  |j                        }|
j+                  |D cg c]  }|j                   c}       t        |      }t        |j,                        }t        |j.                        }||z  }||z  }||z  }|j1                  |       |r
|	s|}	n|	|z  }	|j3                  |       ddd       t        j                  d| d d d d	        |
rt        j                  dt        |
       d       t5        ||t6        j8                  |
       t:        j                  j=                  |
      }|||d}|j?                  |ddd
|       t        j                  d       || _         || _!        || _"        || _#        tI        |	      | _%        tL        jN                  jP                  | _)        | j)                          t        j                  d| j                   d| d       tU        |        y# 1 sw Y   xY wc c}w # 1 sw Y   _xY w# tV        $ r}t        jY                  d| j                   dt[        |       d
        tL        jN                  j\                  | _)        t_        j`                         | _/        t[        |      | _,        | j)                           d}~ww xY w)!zTStreaming version of reimport that processes tasks in batches to reduce memory usager   N)files_as_tasks_list
batch_sizez"Empty batch received for reimport r   Processing batch  with z tasks for reimport r5   Tr6   r$   r7   r;   r=   Batch  processed successfully: z tasks, z annotations, z predictionszDFinalizing reimport: emitting webhooks and updating task states for  tasksid__inr?   FrC   z5Tasks bulk_update finished (async streaming reimport)zStreaming reimport  completed:  tasks importedzError in streaming reimport r,   exc_info)1r   REIMPORT_BATCH_SIZEsetr	   rI   remove_tasks_by_file_uploadsrl   r   (load_tasks_from_uploaded_files_streamingr   rM   rR   r   r`   r   rJ   rc   rK   r   rd   rQ   extendrg   rh   r   rj   r   r   rf   r   r   ri   r@   rA   rB   rm   r   rn   r   rN   rq   r   post_process_reimportr^   r3   r_   ra   r   r   )reimportr6   organization_idr$   r   total_task_counttotal_annotation_counttotal_prediction_countall_found_formatsall_data_columnsall_created_task_idsbatch_numberbatch_tasksbatch_formatsbatch_columnsr   r   batch_db_taskstbatch_task_countbatch_annotation_countbatch_prediction_countall_tasks_querysetrH   r   s                            r   $_async_reimport_background_streamingr     s   r11
 !"!"5! ! 	K001I1IJ	K 9C9l9lX--8C_C_lv:
 0	5K @NOALKK+L>K@P?QQefnfqfqerst ##%  <(00BBDHHQXHY 1$4WVZ9[
 ##D#9!+GJJ!G %++>,JaQTT,JK $'~#6 ),Z-F-F)G&),Z-F-F)G& $44 &*@@&&*@@& "((7 ++8((M9( ++N;A <D KK&?@P?QQY)*.9O8PP\^[0	f  KKVWZ[oWpVqqwx
 'A\A\^rs "&!4!4<P!4!Q.$:$:($ 991,127%))A :  KKOP /$:!$:!!2 $%5 6)00::)(++lCSBTTcde 	h'{	K 	K8 -K <  <^  3HKK=3q6(KVZ[)0077&113Qs]   8M7 MB.M7 A=M*M%A9M*E
M7 M"M7 %M**M4	/M7 7	P B	P		Pc                    	 t         j                  }d}d}d}g }| j                  }t        j                         }d}	t	        | ||      }
g }i }t               }|
D ]  \  }}}}|s#t        j                  d| j                          .|	dz  }	t        j                  d|	 dt        |       d| j                          |r||vr|}|j                  |       |j                  |       | j                  rQt        d|j                  j                        }t        j                  d	|        t        || j                  ||      }|j                   rNt        d|j                  j                        r,g }t#        |j$                        }t'        |      D ]e  \  }}d
|v st'        |d
         D ]J  \  }}	 t)        |      }|j+                  |d      }|r$|D ]  }|j-                  d||z    d| d|        ! L g |rdt        |       d}|D ]  }|d| dz  } t        d|j                  j                        r<|| _        t4        j6                  j8                  | _        | j=                  ddg        y t        j3                  dt        |       d|        | j>                  rtA        jB                         5  tD        jF                  jI                         jK                  |      }tM        |dd|i      }|jO                  d       |j=                  |j                        } |jQ                  | D !cg c]  }!|!j                   c}!       t        |       }"t        |jR                        }#t        |jT                        }$||"z  }||#z  }||$z  }|jW                  |        d d d        n|t        |      z  }t        j                  d|	 d t        |       d!        tY        |      }| j>                  r|rt        j                  d"t        |       d!       t[        |j\                  |t^        j`                  |       |||d#}%tb        jF                  je                  |$      }&|jg                  |&d%d%d|%&       t        j                  d'       t        j                         |z
  }'|xs d| _4        |xs d| _5        |xs d| _6        |'| _7        || _8        || _9        || _:        | jv                  r|| _<        t4        j6                  jz                  | _        | j=                          t        j                  d(| j                   d)| d*       y # t.        $ rK}d||z    d| dt1        |       }|j-                  |       t        j3                  d|        Y d }~d }~ww xY wc c}!w # 1 sw Y   xY w# t.        $ r}t        j3                  d+| j                   dt1        |       d,       t4        j6                  j8                  | _        t}        j~                         | _>        t1        |      | _        | j=                           d }~ww xY w)-Nr   z Empty batch received for import r   r   r   z tasks for import r%   r#   r&   r'   Tr(   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r   r   r4   r5   r6   r7   r;   r=   r   r   r   zBFinalizing import: emitting webhooks and updating task states for r?   r   FrC   z3Tasks bulk_update finished (async streaming import)zStreaming import r   r   zError in streaming import r   )@r   IMPORT_BATCH_SIZEr6   rT   r   r   rM   rR   r   r`   r   rU   r   rV   rW   rX   rY   r
   rZ   r[   r   r\   r]   r^   r_   r3   r   rN   ra   r   rQ   rb   r	   rI   r   rJ   rc   rK   r   rd   r   rg   rh   rj   r   r   re   r   rf   r   r   ri   r@   rA   rB   rk   rl   rm   rn   ro   rp   rq   r   r   )(ru   r$   r   r   r   r   r   r6   rv   r   streaming_generatorfinal_file_upload_idsfinal_found_formatsfinal_data_columnsr   rl   rm   rn   rx   ry   rz   r{   r|   r}   r~   r   r3   r   r   r   r   r   r   r   r   r   r   rH   r   rk   s(                                           r   rS   rS     s   \//
!"!"! ((		CNTXZde "  UI\ V	bEK->~?P?P>QRSALKK#L>K8H7II[\j\m\m[no ?:O#O(7%&&}5%%l366'GgNbNbNmNm  J<.YZ2!H!H'S_ 22xC'J^J^JiJi8 %'!#G$8$89(5 ZGAt$,-6tM7J-K ZMAzZ-OPZ-[
9;9O9OPZjn9O9o 6#91G %*(9(@(@.34Dq4H3IWXVYY[\a[b,c)*%*ZZ  %&DSIZE[D\\g$hM!2 8%2eWB78  KRYRfRfRqRq 0=,0=0D0D0K0K-&++7H:M+NQRUVgRhQiit  vC  uD  E // '') @,44FFHLLU\L]G!4+D[dfmZn!oJ'''=%/__

_%KN(//~0N!0NO'*>':$-01J1J-K*-01J1J-K*$(88$*.DD**.DD*//?#@ @( !C$44 KK&.GKHXGYY_`amV	bp ""45++0DKKTUXYmUnToouv '(('=3N3NPd
 /$:$:($ "&!4!4<P!4!Q991,127%))A :  KKMN99;&$4$9!*@*EA'*@*EA'"*)>&':$&8#))&:N# - 4 4 > >'(9(9':,GWFXXghis $- Z.34Dq4H3IWXVYYyz}~  {A  zB  -C	 1 8 8 C &/LYK-X Y YZ< 1O@ @H  1.2C2C1DBs1vhOZ^_ - 4 4 ; ;#,#7#7#9 "1vs   FW  W 5AU9BW ;AW  A<V5<V0AV5+F-W 	V-	"A V(	"W (V-	-W 0V55V?	:W 	YB	YYc                 b   t        j                         5  	 t        j                  j	                  |       }|j                  t        j                  j                  k7  r#t        j                  d|  d       	 d d d        y t        j                  j                  |_        |j                  dg       d d d        j                  }t        dd	
      r1t        j                  d|j                           t#        ||||       y t%        j&                  |j                  |j(                  |j*                        \  }}}t        j                         5  t,        j                  j/                         j	                  |      }	|j1                  |j(                         t3        |d||d      }
|
j5                  d       |
j                  |j                         }t7        ||t8        j:                  |       t=        |      }t=        |
j>                        }t=        |
j@                        }|||d}|jC                  |ddd|       t        j                  d       |	jE                  |       d d d        |_#        |_$        |_%        ||_&        tO        |      |_(        t        j                  jR                  |_        |j                          tU        |       y # t        j
                  $ r$ t        j                  d|  d       Y d d d        y w xY w# 1 sw Y   gxY w# 1 sw Y   xY w)Nr   zProjectReimport with id r   zProcessing reimport with id r   r   r   Afflag_fix_back_plt_838_reimport_memory_improvement_05082025_shortr"   r#   z%Using streaming reimport for project )r   r5   Tr   r7   r;   r=   r?   FrC   z+Tasks bulk_update finished (async reimport))+r	   rI   r   rJ   rK   rL   rM   r3   r   rN   rO   rP   rQ   r6   r   rR   r   r   r   load_tasks_from_uploaded_filesrl   r   r   rc   r   r   rd   r   r   rf   r`   rg   rh   ri   rj   r@   rA   rB   rm   r   rn   rq   r   )r   r   r$   rt   r   r6   rw   rm   rn   r   r   r@   rA   rB   rH   s                  r   async_reimport_backgroundr   3  s   				 
0	&..22k2BH ??o44<<<LL7}DTUV
0 
0 *00<<XJ/
0 G SZ`a;GJJ<HI,XwQUV .8-V-Vh66HLhLh.
*}l ! 	/$,,>>@DDWDUG001I1IJ,%dX_imLnoJ5OOwzzO:E&A\A\^cdUJ":#<#<=":#<#<= )$4$4($ 99$,127%))A :  KKEF''.?	/D )$4!$4!!. $\ 2)00::h' ++ 	LL3K=@def
0 
0	
0 
00	/ 	/sA   L KAL2LDL%,L
LLLL"%L.)N)NF)4loggingrT   r   typingr   r   core.feature_flagsr   core.utils.commonr   data_import.uploaderr   django.confr   	django.dbr	    label_studio_sdk.label_interfacer
   projects.modelsr   r   r   rest_framework.exceptionsr   tasks.modelsr   tasks.serializersr   users.modelsr   webhooks.modelsr   webhooks.utilsr   modelsr   serializersr   uploaderr   	getLogger__name__rM   r   r   r   rX   POST_PROCESS_REIMPORTr   r   rS   r    r   r   <module>r      s       % ' ' F   ! ; J J 5  @  ) 5  , 1			8	$ QUG08)9L0MGTaH "("@"@A tn]@D(r   