o
    h                     @   st   d dl mZ d dlZd dlZddlmZ d dlmZ d dlm	Z	 e	 Z
d dlmZ d dlmZmZ G d	d
 d
ZdS )    )HTTPExceptionN   )HashGenerator)UserJourney)MicrolearningDefaultsEstimator)Path)FetchResultErrorRestructureOutputErrorc                   @   s6   e Zd Zdd Zdd Zdd Zdedefd	d
ZdS )DefaultEstimatorc                 C   s.   t  | _|| _t|| _d| j d| _d S )Nz	defaults/z.md)r   crew_instance
input_datar   generate_input_hash
input_hashdefault_folder)selfr    r   /home/azureuser/microlearn/backend/user_journey_with_openai/agentic_workflow/src/user_journey_service/services/defaults_service.py__init__   s   zDefaultEstimator.__init__c              
   C   s   t d | j }z@| | j\}}|r&t d | |}dd|| jdW S | jj| jd}|j	|d | | j\}}| |}dd|| jdW S  t
yc } zd	d
| dW  Y d }~S d }~w tyy } zd	ddW  Y d }~S d }~w ty } zd	|dW  Y d }~S d }~ww )Nz&Inside user defaults estimator servicezInside if of main functionsuccesszFetched existing result.)statusmessagedefaultshashid)output_file)inputszEstimated the defaults.failedzError in fetching the result: )r   r   z!Error in restructuring the result)printr   dict_fetch_existing_resultr   _restructure_outputr   r   defaults_crewkickoffr   r	   	Exception)r   r   existsresultoutputcrewer   r   r   run   s.   


zDefaultEstimator.runc              
   C   s   t d z;tj|r=t d t|d}| }t d|  t dt|  W d    n1 s3w   Y  d|fW S W dS  tyR } zt|t	|d }~ww )NzInside fetch result functionzInside if conditionrzThe fetched result is : The type of result is : T)F )
r   ospathr#   openreadtyper"   r   str)r   	file_pathfiler$   r'   r   r   r   r      s   
z'DefaultEstimator._fetch_existing_resultr$   returnc              
   C   sv   z(t d t dt|  t dt|  td|}t d dd |D W S  ty: } zt | td }~ww )Nz"Inside restructure output functionr*   zResult content: z##\s*([\w\s]+?)\s*-\s*(\d+)%zmatch foundc                 S   s   i | ]\}}|  t|qS r   )stripint).0skillpercentr   r   r   
<dictcomp>   s    z8DefaultEstimator._restructure_output.<locals>.<dictcomp>)r   r0   reprrefindallr"   r	   )r   r$   matchesr'   r   r   r   r      s   z$DefaultEstimator._restructure_outputN)	__name__
__module____qualname__r   r(   r   r1   r   r   r   r   r   r   r
      s
    r
   )fastapir   r,   r<   utils.hashingr   user_journey_service.crewr   2user_journey_service.processors.defaults_estimatorr   defaults_estimatorpathlibr   $user_journey_service.core.exceptionsr   r	   r
   r   r   r   r   <module>   s    t