
    	]jP                     t    d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
  ej                  e      Zd Zd Zy)	z
FSM utility functions for backfilling and managing state transitions.

This module contains reusable functions for FSM state management that are
used across different parts of the codebase.
    NCurrentContext)iterate_queryset)get_or_infer_state)get_or_initialize_stateis_fsm_enabledc                 d   |dk  ryt               sy	 ddlm} t        |j                  j                  |       j                  d      d| j                  dd            }|syt        j                  d	t        |       d
       t        j                         }|j                  j                  |      }t        |      D ]  }t        |      }t        |||        t        j                  dt        |       d
       y# t         $ r$}	t        j#                  d|	 d       Y d}	~	yd}	~	ww xY w)a|  
    Backfill initial FSM states for tasks created during storage sync.

    This function creates initial CREATED state records for all tasks that were
    created during a storage sync operation. It's designed to be called after
    tasks have been successfully created and linked to storage.

    Args:
        storage_id: The ID of the storage that created the tasks
        tasks_created: Number of tasks that were created
        link_class: The link model class (e.g., S3ImportStorageLink) to query tasks

    Note:
        - CurrentContext must be available before calling this function
        - This function is safe to call in both LSO and LSE environments
        - Failures are logged but don't propagate to prevent breaking storage sync
        - FSM feature flag is checked before processing
        - Tasks are processed in chunks via iterate_queryset to avoid OOM issues
    r   N)Task)storagez-created_attask_idT)flatz.Storage sync: creating initial FSM states for z tasks)id__in)userinferred_statez%Storage sync: FSM states created for z$FSM sync after storage sync failed: )exc_info)r   tasks.modelsr
   listobjectsfilterorder_byvalues_listloggerinfolenr   get_userr   r   r   	Exceptionerror)

storage_idtasks_created
link_classr
   task_idsr   tasks_qstaskr   es
             D/root/env/lib/python3.12/site-packages/label_studio/fsm/functions.pybackfill_fsm_states_for_tasksr&      s&   (  P% %%j%9Xm$^m5[[.
 DS]OSYZ[ &&(<<&&h&7$X. 	TD/5N#DtNS	T 	;CM?&QR P;A3?$OOPs   AD $BD 	D/D**D/c                    ddl m} ddlm} ddlm} ddlm} ddlm	}  |j                         } ||      sy	  |       }|j                  |       }	| j                  r|j                  n|j                  }
|	@| j                  r|j                  | d	|
       n|j                  | d|
        |||       y|	|
k7  rC|
|j                  k(  r|j                  | d|
       n|j                  | d	|
        |||       yy# t         $ rD}t"        j%                  dt'        |       | j(                  |j(                  d       Y d}~yd}~ww xY w)a+  
    Update task FSM state after an annotation has been deleted.

    This function ensures that the task's FSM state reflects its current labeled status
    after an annotation has been deleted. It will:
    1. Check if FSM is enabled
    2. Get the current task state
    3. Determine the expected state based on task.is_labeled
    4. Execute appropriate transition if state doesn't match
    5. Update project state if task state was changed

    Args:
        task: The Task instance whose annotation was deleted
        project: The Project instance containing the task

    Note:
        - Requires CurrentContext to be set with a valid user
        - Failures are logged but don't propagate to prevent breaking annotation deletion
        - Will initialize state if task has no FSM state record yet
    r   r   )&update_project_state_after_task_change)TaskStateChoices)get_state_manager)r   )r   Ntask_completed)entitytransition_namer   task_in_progressz4FSM state update failed during annotation deletion: )r   
project_id)extra)core.current_requestr   fsm.project_transitionsr(   fsm.state_choicesr)   fsm.state_managerr*   	fsm.utilsr   r   get_current_state_value
is_labeled	COMPLETEDIN_PROGRESSexecute_transitionr   r   warningstrid)r#   projectr   r(   r)   r*   r   r   StateManagercurrent_task_stateexpected_stater$   s               r%   +update_task_state_after_annotation_deletionrB   J   sN   * 4N23( #>""$Dt$ 
(* *AA$G 8<)33L\LhLh %//tM]dh/i//tM_fj/k27F>1!1!=!==//tM_fj/k//tM]dh/i27F 2  
B3q6(K"ggWZZ@ 	 	
 	

s   A=D 9AD 	E:E

E)__doc__loggingr1   r   core.utils.iteratorsr   fsm.state_inferencer   r5   r   r   	getLogger__name__r   r&   rB        r%   <module>rK      s:     / 1 2 =			8	$5PpA
rJ   