
    	]j
                     x    d dl Z d dlmZ d dlmZ d dlmZ  e j                  e      Z	dee
   fdZdee
   fdZy)    N)Optional)	load_func)settingsreturnc                    ddl m}m}m} | j                  j
                  j                         }|dk(  r?| j                  r|j                  S | j                  dkD  r|j                  S |j                  S |dk(  r| j                  j                         }|j                         s|j                  S |j                         }|j!                  d      j                         }|dk(  r|j                  S ||k(  r|j                  S |j                  S |dk(  r|j                  S t"        j%                  d| d	|| j&                  d
       y)a  
    Infer what the FSM state should be based on entity's current data.

    This is used for "cold start" scenarios where entities exist in the database
    but don't have FSM state records yet (e.g., after FSM deployment to production
    with pre-existing data).

    Args:
        entity: The entity to infer state for (Task, Project, or Annotation)

    Returns:
        Inferred state value, or None if entity type not supported

    Examples:
        >>> task = Task.objects.get(id=123)
        >>> task.is_labeled = True
        >>> _get_or_infer_state(task)
        'COMPLETED'

        >>> project = Project.objects.get(id=456)
        >>> _get_or_infer_state(project)
        'CREATED'
    r   )AnnotationStateChoicesProjectStateChoicesTaskStateChoicestaskprojectT)
is_labeled
annotationz,Cannot infer state for unknown entity type: zfsm.infer_state_unknown_type)evententity_type	entity_id)extraN)fsm.state_choicesr   r	   r
   _meta
model_namelowerr   	COMPLETEDtotal_annotationsIN_PROGRESSCREATEDtasksallexistscountfilterloggerwarningpk)entityr   r	   r
   r   r   total_taskslabeled_taskss           J/root/env/lib/python3.12/site-packages/label_studio/fsm/state_inference.py_get_or_infer_stater'   
   s7   0 `_,,))//1Kf#---%%)#///#+++			!
   "||~&... kkm5;;=A&...k)&000&222		$%---:;-H7*#YY 	 	
     c                 D    t        t        j                        } ||       S )N)r   r   FSM_INFERENCE_FUNCTION)r#   funcs     r&   get_or_infer_stater,   P   s    X445D<r(   )loggingtypingr   core.utils.commonr   django.confr   	getLogger__name__r    strr'   r,    r(   r&   <module>r5      sF      '  			8	$C8C= CL(3- r(   