o
    ưi                     @  s   d Z ddlmZ ddl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mZ dd	lmZmZ dd
lmZ G dd dZdS )z?Core export engine for Focus integrations (heavy dependencies).    )annotations)AnyDictOptionalN)verbose_logger   )FocusLiteLLMDatabase)FocusDestinationFactoryFocusTimeWindow)FocusParquetSerializerFocusSerializer)FocusTransformerc                   @  sn   e Zd ZdZddd'ddZd(ddZd)ddZd*ddZd+ddZd,ddZ	e
d-d"d#Ze
d.d%d&ZdS )/FocusExportEnginez;Engine that fetches, normalizes, and uploads Focus exports.N)destination_configproviderstrexport_formatprefixr   Optional[dict[str, Any]]returnNonec                C  sF   || _ || _|| _tj| j | j|d| _|  | _t | _	t
 | _d S )N)r   r   config)r   r   r   r	   create_destination_init_serializer_serializerr   _transformerr   	_database)selfr   r   r   r    r   _/home/app/Keep/.python/lib/python3.10/site-packages/litellm/integrations/focus/export_engine.py__init__   s   
zFocusExportEngine.__init__r   c                 C  s   | j dkr	tdt S )NZparquetz'Only parquet export supported currently)r   NotImplementedErrorr   r   r   r   r    r   (   s   
z"FocusExportEngine._init_serializerlimitOptional[int]Dict[str, Any]c                   s   | j j|dI d H }| j|}|tdt| }|tdt| }t|| |d| |d| 	|d| 	|dd}|||dS )	N)r$   2   Zspendtotal_tokensZteam_idmodel)Ztotal_recordsZtotal_spendr(   Zunique_teamsZunique_models)Z
usage_dataZnormalized_datasummary)
r   get_usage_datar   	transformheadminlenZto_dicts_sum_column_count_unique)r   r$   data
normalizedZusage_sampleZnormalized_sampler*   r   r   r    dry_run_export_usage_data-   s   



	z+FocusExportEngine.dry_run_export_usage_datawindowr
   c                  sp   | j j||j|jdI d H }| rtd| d S | j|}| r-td| d S | 	||I d H  d S )N)r$   Zstart_time_utcZend_time_utcz)Focus export: no usage data for window %sz1Focus export: normalized data empty for window %s)
r   r+   
start_timeend_timeis_emptyr   debugr   r,   _serialize_and_upload)r   r5   r$   r2   r3   r   r   r    export_windowB   s    zFocusExportEngine.export_windowframepl.DataFramec                   s@   | j |}|std d S | jj|||  dI d H  d S )Nz/Focus export: serializer returned empty payload)contentZtime_windowfilename)r   	serializer   r9   r   Zdeliver_build_filename)r   r<   r5   payloadr   r   r    r:   Z   s   
z'FocusExportEngine._serialize_and_uploadc                 C  s   | j jstdd| j j S )Nz(Serializer must declare a file extensionzusage.)r   	extension
ValueErrorr#   r   r   r    rA   g   s   z!FocusExportEngine._build_filenamecolumnfloatc                 C  sN   |   s	|| jvrdS | t| ddd }|d u r#dS t|S )Ng        sumr   )	r8   columnsselectplcolrG   aliasrowrF   r<   rE   valuer   r   r    r0   l      $zFocusExportEngine._sum_columnintc                 C  sN   |   s	|| jvrdS | t| ddd }|d u r#dS t|S )Nr   unique)	r8   rH   rI   rJ   rK   Zn_uniquerL   rM   rQ   rN   r   r   r    r1   u   rP   zFocusExportEngine._count_unique)
r   r   r   r   r   r   r   r   r   r   )r   r   )r$   r%   r   r&   )r5   r
   r$   r%   r   r   )r<   r=   r5   r
   r   r   )r   r   )r<   r=   rE   r   r   rF   )r<   r=   rE   r   r   rQ   )__name__
__module____qualname____doc__r!   r   r4   r;   r:   rA   staticmethodr0   r1   r   r   r   r    r      s    




r   )rV   
__future__r   typingr   r   r   ZpolarsrJ   Zlitellm._loggingr   Zdatabaser   destinationsr	   r
   Zserializersr   r   Ztransformerr   r   r   r   r   r    <module>   s    