o
    ưi                     @  s   d Z ddlmZ ddlZddlmZmZmZ ddlmZm	Z	m
Z
mZmZmZ ddlZddlmZ ddlmZ dd	lmZ erKdd
lmZ ddlmZ ne	ZdZdZG dd deZdgZdS )z;Focus export logger orchestrating DB pull/transform/upload.    )annotationsN)datetime	timedeltatimezone)TYPE_CHECKINGAnyDictListOptionalcast)verbose_logger)CustomLogger   )FocusTimeWindow)AsyncIOSchedulerFocusExportEngineZfocus_export_usage_datai  c                      s   e Zd ZdZddddddddd4 fddZd5ddZddddd6ddZefd7dd Zd8d!d"Z	e
d9d%d&Zd:d(d)Zd8d*d+Zd;d.d/Zd<d2d3Z  ZS )=FocusLoggerzOCoordinates Focus export jobs across transformer/serializer/destination layers.N)providerexport_format	frequencycron_offset_minuteinterval_secondsprefixdestination_configr   Optional[str]r   r   r   Optional[int]r   r   r   Optional[dict[str, Any]]kwargsr   returnNonec                  s   t  jdi | |ptdpd | _|ptdpd | _|p)tdp)d | _|d ur3|nttdd| _	|d urB|ntd	}	|	d urOt|	nd | _
td
}
|d ur]|n|
ra|
nd| _|| _d | _d S )NZFOCUS_PROVIDERZs3ZFOCUS_FORMATZparquetZFOCUS_FREQUENCYhourlyZFOCUS_CRON_OFFSET5ZFOCUS_INTERVAL_SECONDSZFOCUS_PREFIXZfocus_exports )super__init__osgetenvlowerr   r   r   intr   r   r   _destination_config_engine)selfr   r   r   r   r   r   r   r   Zraw_intervalZ
env_prefix	__class__r#   ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/focus/focus_logger.pyr%      s(   

zFocusLogger.__init__'FocusExportEngine'c                 C  s6   | j du rddlm} || j| j| j| jd| _ | j S )z+Instantiate the heavy export engine lazily.Nr   r   )r   r   r   r   )r+   export_enginer   r   r   r   r*   )r,   r   r#   r#   r/   _ensure_engineA   s   
zFocusLogger._ensure_engine)limitstart_time_utcend_time_utcr3   r4   Optional[datetime]r5   c                  s^   t |t |A rtd|r|rt||| jd}n	| ttj}| j	||dI dH  dS )z*Public hook to trigger export immediately.z9start_time_utc and end_time_utc must be provided together
start_timeend_timer   windowr3   N)
bool
ValueErrorr   r   _compute_time_windowr   nowr   utc_export_window)r,   r3   r4   r5   r;   r#   r#   r/   export_usage_dataN   s   zFocusLogger.export_usage_datadict[str, Any]c                   s   |   }|j|dI dH S )z*Return transformed data without uploading.)r3   N)r2   dry_run_export_usage_data)r,   r3   enginer#   r#   r/   rD   e   s   z%FocusLogger.dry_run_export_usage_datac              	     s   ddl m} d}|durt|dd}|durt|dd}|rR|jrR|jtdI dH }|s4td dS z|  I dH  W |j	tdI dH  dS |j	tdI dH  w |  I dH  dS )z@Entry point for scheduler jobs to run export cycle with locking.r   )proxy_logging_objNZdb_spend_update_writerpod_lock_manager)Z
cronjob_idz(Focus export: unable to acquire pod lock)
Zlitellm.proxy.proxy_serverrF   getattrredis_cacheacquire_lockFOCUS_USAGE_DATA_JOB_NAMEr   debug_run_scheduled_exportrelease_lock)r,   rF   rG   writerZacquiredr#   r#   r/   initialize_focus_export_jobl   s,   

z'FocusLogger.initialize_focus_export_job	schedulerr   c                   sP   t jjtd}|std dS tt|d }| }| j|j	fi | dS )zBRegister the export cron/interval job with the provided scheduler.)Zcallback_typez5No Focus export logger registered; skipping schedulerNr   )
litellmZlogging_callback_managerZget_custom_loggers_for_typer   r   rL   r   _build_scheduler_triggerZadd_jobrP   )rQ   Zfocus_loggersZfocus_loggerZtrigger_kwargsr#   r#   r/    init_focus_export_background_job   s"   
z,FocusLogger.init_focus_export_background_jobDict[str, Any]c                 C  s   | j dkr| jp	d}d|dS | j dkr#tdtd| j}d|ddS | j d	krCtd| j}td
|d }td|d }d||ddS td| j  )z:Return scheduler configuration for the selected frequency.interval<   )triggersecondsr!   r   ;   cron)rX   minuteseconddaily   )rX   hourr\   r]   Unsupported frequency: )r   r   maxminr   r=   )r,   rY   r\   Ztotal_minutesr`   r#   r#   r/   rS      s   




z$FocusLogger._build_scheduler_triggerc                   s,   |  ttj}| j|ddI dH  dS )z7Execute the scheduled export for the configured window.Nr:   )r>   r   r?   r   r@   rA   )r,   r;   r#   r#   r/   rM      s   z!FocusLogger._run_scheduled_exportr;   r   c                  s"   |   }|j||dI d H  d S )Nr:   )r2   Zexport_window)r,   r;   r3   rE   r#   r#   r/   rA      s   zFocusLogger._export_windowr?   r   c                 C  s   | tj}| jdkr|jdddd}|tdd }n2| jdkr1|jddddd}|tdd }n| jd	krEt| jp;d
d}|}|| }ntd| j t||| jdS )z?Derive the time window to export based on configured frequency.r!   r   )r\   r]   microsecondr   )hoursr^   )r`   r\   r]   rd   )daysrV   rW   )rY   ra   r7   )	
astimezoner   r@   r   replacer   r   r=   r   )r,   r?   Znow_utcr9   r8   rV   r#   r#   r/   r>      s"   



z FocusLogger._compute_time_window)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    )r   r0   )r3   r   r4   r6   r5   r6   r   r    )r3   r   r   rC   )r   r    )rQ   r   r   r    )r   rU   )r;   r   r3   r   r   r    )r?   r   r   r   )__name__
__module____qualname____doc__r%   r2   rB   DEFAULT_DRY_RUN_LIMITrD   rP   staticmethodrT   rS   rM   rA   r>   __classcell__r#   r#   r-   r/   r      s0    
%



	r   )rl   
__future__r   r&   r   r   r   typingr   r   r   r	   r
   r   rR   Zlitellm._loggingr   Z"litellm.integrations.custom_loggerr   destinationsr   Zapscheduler.schedulers.asyncior   r1   r   rK   rm   r   __all__r#   r#   r#   r/   <module>   s$      
;