
    j&                     ,   d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZm	Z	m
Z
 d dlZddlmZmZmZmZmZmZ ddlmZmZ ddlmZ dd	lmZ  ej6                  e      Ze j<                   G d
 d             Z G d d      Z  G d dejB                        Z"y)    N)FutureCancelledError)CallableListOptional   )base_upload_managerupload_optionsfile_upload_monitorthread_poolfile_uploadertypes   )format_helperssynchronization)messages)clientc                   x    e Zd ZU eed<   ej                  ed<   ej                  ed<   de	fdZ
d	dee   de	fdZy)
UploadResultfuturemonitorr
   returnc                 6    | j                   j                         S )z7Allows to check if wrapped Future successfully finished)r   doneselfs    x/Users/manta/Documents/Projects/TheRoad-I1/backend/.venv/lib/python3.12/site-packages/opik/file_upload/upload_manager.pyreadyzUploadResult.ready   s    {{!!    Ntimeoutc           	         	 | j                   j                  |      }|y|# t        t        f$ r5}t        j                  d| j                  j                  |       Y d}~yd}~wt        $ rt}t        j                  d| j                  j                  | j                  j                  t        j                  | j                  j                        |d       Y d}~yd}~ww xY w)zHAllows to check if wrapped Future completed without raising an exceptionNTzCTimeout while waiting for the result of file '%s' upload. Error: %szMFailed to upload file with name '%s' from path [%s] with size [%s]. Error: %s)exc_infoF)r   	exceptionr   TimeoutErrorLOGGERwarningr
   	file_name	Exceptionerror	file_pathr   format_bytes	file_size)r   r    r#   es       r   
successfulzUploadResult.successful#   s    	--g6I O- 	NNU##--    	LL_##--##--++D,?,?,I,IJ    	s%   " " C!+A!!C!-A*CC!N)__name__
__module____qualname__r   __annotations__r   FileUploadMonitorr
   FileUploadOptionsboolr   r   floatr.    r   r   r   r      sB    N 222"444"t "(5/ T r   r   c                   D    e Zd Zdej                  ddfdZddZdefdZy)FileUploadManagerMonitorfile_upload_managerr   Nc                 .    || _         d| _        d | _        y )Nr   )r;   last_remaining_byteslast_remaining_uploads_display)r   r;   s     r   __init__z!FileUploadManagerMonitor.__init__?   s     $7 $%!?C+r   c           	      N   | j                   j                         }t        j                         }|j                  dk(  rt
        j                  d       n| j                  ^t
        j                  d|j                  t        j                  |j                        t        j                  |j                               nO| j                  |j                  z
  }|| j                  z
  }||z  }|dk  rx|dk  rd}t
        j                  d|j                  t        j                  |j                        t        j                  |j                        t        j                  |             nt        t        t        j                   |j                  |z                    }t
        j                  d|j                  t        j                  |j                        t        j                  |j                        t        j                  |      |       |j                  | _        || _        y )Nr   z<All assets have been sent, waiting for delivery confirmationz+Still uploading %d file(s), remaining %s/%szIStill uploading %d file(s), remaining %s/%s, Throughput %s/s, ETA unknownzFStill uploading %d file(s), remaining %s/%s, Throughput %s/s, ETA ~%ss)r;   remaining_datatime	monotonicbytesr%   infor>   uploadsr   r+   
total_sizer=   strintmathceil)r   	remainingcurrent_timeuploaded_bytestime_elapsedupload_speedremaining_times          r   log_remaining_uploadsz.FileUploadManagerMonitor.log_remaining_uploadsF   s   ,,;;=	~~'??aKKN 008KK=!!++IOO<++I,@,@A	 "66HN'$*M*MML)L8L "!##$L_%%"//	@"//	0D0DE"//= "%S9??\3Q)R%S!T\%%"//	@"//	0D0DE"//=" %.OO!.:+r   c                 6    | j                   j                         S r/   )r;   all_doner   s    r   rT   z!FileUploadManagerMonitor.all_donex   s    ''0022r   r   N)	r0   r1   r2   r	   BaseFileUploadManagerr?   rR   r6   rT   r8   r   r   r:   r:   >   s2    D#6#L#LD	D0;d3$ 3r   r:   c                      e Zd ZdZdej
                  dej                  deddfdZ	de
j                  d	eej                     d
eej                     ddfdZde
j"                  d	eej                     d
eej                     ddfdZdej(                  deddfdZdefdZdej4                  fdZdefdZdee   defdZddee   dedefdZddZ y)FileUploadManagerz Manages concurrent file uploads.rest_clienthttpx_clientworker_countr   Nc                 r    || _         || _        t        j                  |      | _        g | _        d| _        y )N)r[   F)_httpx_client_rest_clientr   get_thread_pool	_executor_upload_resultsclosed)r   rY   rZ   r[   s       r   r?   zFileUploadManager.__init__   s5     *'$44,O35r   messageon_upload_successon_upload_failedc                 ~    t        |t        j                        r| j                  |||       y t	        d| d      )N
attachmentrd   re   zMessage z" is not supported for file upload.)
isinstancer   CreateAttachmentMessageupload_attachment
ValueError)r   rc   rd   re   s       r   uploadzFileUploadManager.upload   sG     gx??@""""3!1 #  xy0RSTTr   rh   c                 v    t        j                  |||      }| j                  t        j                  |       y )Nrg   )uploaderoptions)r
   #file_upload_options_from_attachment_submit_uploadr   rk   )r   rh   rd   re   rp   s        r   rk   z#FileUploadManager.upload_attachment   s>     !DD!/-

 	"44 	 	
r   rp   ro   c                    | j                   r!t        j                  d|j                         y t	        j
                         }|j                  dkD  r|j                  |_        ||| j                  | j                  d} | j                  j                  |fi |}| j                  j                  t        |||             y )Nz`The file upload manager has been already closed. No more files can be submitted for upload. (%s)r   )r   r
   rY   upload_httpx_client)r   r
   )rb   r%   r&   r'   r   r4   r,   rG   r^   r]   r`   submitra   appendr   )r   rp   ro   r   kwargsr   s         r   rr   z FileUploadManager._submit_upload   s     ;;NNr!! %779q !(!2!2G %,,#'#5#5	
 '&&x:6:##I	
r   c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr/   )r   ).0results     r   	<genexpr>z-FileUploadManager.all_done.<locals>.<genexpr>   s     E0Df6<<>0Ds   )allra   r   s    r   rT   zFileUploadManager.all_done   s    E0D0DEEEr   c                 0   d}d}d}| j                   D ]j  }|j                         du r|dz  }|j                  }|j                  |j                  @||j                  z  }||j                  |j                  z
  z  }l t        j                  |||      S )Nr   Tr   )rF   rD   rG   )ra   r   r   rG   
bytes_sentr	   RemainingUploadData)r   remaining_uploadsremaining_bytes_to_uploadrG   r{   r   s         r   rA   z FileUploadManager.remaining_data   s    $%!
**F||~%"nnG!!)W-?-?-G',,,J%););g>P>P)PP% + #66%+!
 	
r   c                 |    | j                   D cg c]  }|j                          }}|j                  d      S c c}w )zRReturns the number of remaining uploads. Non-blocking - can be called at any time.F)ra   r   count)r   r{   status_lists      r   r   z#FileUploadManager.remaining_uploads   s;    484H4HI4H&v||~4HI  '' Js   9r    c                 z    d}| j                   D ])  }|j                         r|j                  |      r%|dz  }+ |S )zvImportant - this is blocking method waiting for all remaining uploads to complete or while
        timeout is expired.r   r   )ra   r   r.   )r   r    failedr{   s       r   failed_uploadsz FileUploadManager.failed_uploads   s?     **F<<>):):7)C! + r   
sleep_timec                      t               }t        j                   fd|j                  ||        j	                         r- j                  |      }|dkD  rt        j                  d|       yy)as  Flushes all pending uploads. This is a blocking method that waits for all remaining uploads to complete,
        either until they finish or the specified timeout expires. If no timeout is set, it waits indefinitely.
        Args:
             timeout: Timeout in seconds to wait for all remaining uploads to complete.
                If None is provided, it will wait for all remaining uploads to complete.
            sleep_time: The sleep interval between checks and printing progress.
        Returns:
            The flag to indicate whether all remaining uploads are completed or not within the provided timeout.
        c                  $     j                         S r/   )rT   r   s   r   <lambda>z)FileUploadManager.flush.<locals>.<lambda>   s    4==?r   )check_functionprogress_callbackr    r   r   z4Failed to upload %d file(s). Check logs for details.TF)r:   r   wait_for_donerR   rT   r   r%   r&   )r   r    r   upload_monitorr   s   `    r   flushzFileUploadManager.flush   sl     2$7%%2,BB!		
 ==?!009N!J" r   c                 J    | j                   j                  d       d| _        y )NT)wait)r`   shutdownrb   r   s    r   closezFileUploadManager.close  s    T*r   )   rU   )!r0   r1   r2   __doc__rest_api_clientOpikApihttpxClientrI   r?   r   BaseMessager   upload_typesOnUploadSuccessCallbackOnUploadFailureCallbackrm   rj   rk   r
   r5   r   rr   r6   rT   r	   r   rA   r   r7   r   r   r   r8   r   r   rX   rX   |   sW   *$,, ll 	
 
U%%U $L$H$HIU #<#G#GH	U
 
U
44
 $L$H$HI
 #<#G#GH	

 

 
%77
CK
	
2F$ F
 3 G G 
.(3 (
huo # Xe_ # d >r   rX   )#dataclassesloggingrJ   rB   concurrent.futuresr   r   typingr   r   r   r    r	   r
   r   r   r   r   r   r   r   message_processingr   rest_apir   r   	getLoggerr0   r%   	dataclassr   r:   rV   rX   r8   r   r   <module>r      s        5 + +   / ) 0			8	$ ! ! !H;3 ;3|R+AA Rr   