o
    -wi6)                     @  s   d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	 ddl
mZ ddlmZ ddlmZ dd	lmZ d(ddZd)ddZejd*ddZG dd dZG dd dZd+d"d#Zd,d&d'ZdS )-zCDefines an object for printing run progress at the end of a script.    )annotationsN)IterableIteratorNoReturn)wandb_internal_pb2)	interface)asyncio_compat   )printerr
   	p.Printerfinal_resultpb.PollExitResponsereturnNonec                 C  sB   |j j}|j j}|dks|dkrdS || }| d|d dS )z}Print how much W&B sync reduced the amount of uploaded data.

    Args:
        final_result: The final PollExit result.
    r   Nz"W&B sync reduced upload amount by z.1%)pusher_statsdeduped_bytestotal_bytesdisplay)r
   r   r   r   frac r   S/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/wandb/sdk/lib/progress.pyprint_sync_dedupe_stats   s   	r   progressProgressPrinterr   interface.InterfaceBasec              	     s   ddfdd}d fdd}t  4 I dH }||  ||  W d  I dH  dS 1 I dH s9w   Y  dS )	a  Poll and display ongoing tasks in the internal service process.

    This never returns and must be cancelled. This is meant to be used with
    `mailbox.wait_with_progress()`.

    Args:
        progress: The printer to update with operation stats.
        interface: The interface to use to poll for updates.

    Raises:
        HandleAbandonedError: If the mailbox associated with the interface
            becomes closed.
        Exception: Any other problem communicating with the service process.
    Nr   r   c                     s$   	 r	   tdI d H  q)NTg?)updateasynciosleepr   )r   statsr   r   loop_update_screen8   s   
z9loop_printing_operation_stats.<locals>.loop_update_screenc                    sZ   	 t  }   }|jd dI d H }|jjjt  |  }|dk r,td| I d H  q)NT)timeoutg      ?)	time	monotonicdeliver_operation_stats
wait_asyncresponseoperations_responseoperation_statsr   r   )
start_timehandleresultelapsed_time)r   r   r   r   loop_poll_stats>   s   
z6loop_printing_operation_stats.<locals>.loop_poll_stats)r   r   )r   open_task_group
start_soon)r   r   r   r,   
task_groupr   )r   r   r   r   loop_printing_operation_stats$   s   .r0   default_textstrIterator[ProgressPrinter]c                 c  sH    |   }t| ||dV  |   W d   dS 1 sw   Y  dS )zContext manager providing an object for printing run progress.

    Args:
        printer: The printer to use.
        default_text: The text to show if no information is available.
    )r1   N)dynamic_textr   progress_close)r
   r1   	text_arear   r   r   progress_printerP   s   

"r7   c                   @  sL   e Zd ZdZd d
dZd!ddZd"ddZd#ddZd$ddZd%ddZ	dS )&r   z.Displays PollExitResponse results to the user.r
   r   progress_text_areap.DynamicText | Noner1   r2   r   r   c                 C  s(   d| _ || _|| _|| _d| _d| _d S )NTr    )_show_operation_stats_printer_progress_text_area_default_text_tick_last_printed_line)selfr
   r8   r1   r   r   r   __init__g   s   
zProgressPrinter.__init__r   -list[pb.PollExitResponse] | pb.OperationStatsc                 C  sz   |sdS t |tjr| |g n#| jr!| tdd |D  nt|dkr/| |d  n| | |  j	d7  _	dS )z!Update the displayed information.Nc                 s  s    | ]}|j V  qd S )N)r'   ).0r%   r   r   r   	<genexpr>   s    z)ProgressPrinter.update.<locals>.<genexpr>r	   r   )

isinstancepbOperationStats_update_operation_statsr;   listlen_update_single_run_update_multiple_runsr?   )rA   r   r   r   r   r   t   s   
zProgressPrinter.update
stats_listlist[pb.OperationStats]c                 C  s   | j rt| j| j d| j| j| jd| d S g }d}|D ]}|jD ]}t|dk r3|	|j
 q$|d7 }q$qd|}|dkrJ|d| d7 }|| jkrU| j| || _d S )	N   )	max_linesloading_symbolr1   r      r	   z; z (+ z more))r=   _DynamicOperationStatsPrinterr<   rR   r?   r>   r   
operationsrK   appenddescjoinr@   )rA   rN   top_level_operationsextra_operationsr   opliner   r   r   rI      s.   




z'ProgressPrinter._update_operation_statsr   c                 C  sz   |j }t|jddt|jdd}|jdkr$|dt|jdd7 }|jdkr5| ||j|j  d S | |d d S )Nz.3fz MB of z MB uploadedr   z (z MB deduped)      ?)r   
_megabytesuploaded_bytesr   r   _update_progress_text)rA   r   r   r\   r   r   r   rL      s   



z"ProgressPrinter._update_single_runprogress_listlist[pb.PollExitResponse]c              	   C  s   d}d}d}|D ]&}||j j7 }||j j7 }||j j7 }||j j7 }||jj7 }||jj7 }qdt| d| dt	|ddt	|dd	}|dkrT| 
|||  d S | 
|d d S )	Nr   zProcessing z runs with z files (z.2fz MB / z MB)r]   )file_countswandb_countmedia_countartifact_countother_countr   r_   r   rK   r^   r`   )rA   ra   total_filesr_   r   r   r\   r   r   r   rM      s&   z%ProgressPrinter._update_multiple_runstextfloatc                 C  s@   || j krd S || _ | jr| j| d S | j|d | d S )N)r@   r=   set_textr<   progress_update)rA   ri   r   r   r   r   r`      s   
z%ProgressPrinter._update_progress_textN)r
   r   r8   r9   r1   r2   r   r   )r   rC   r   r   )rN   rO   r   r   )r   r   r   r   )ra   rb   r   r   )ri   r2   r   rj   r   r   )
__name__
__module____qualname____doc__rB   r   rI   rL   rM   r`   r   r   r   r   r   d   s    




c                   @  s.   e Zd ZdZdddZdddZdddZdS )rT   z?Single-use object that writes operation stats into a text area.r
   r   r6   p.DynamicTextrQ   intrR   r2   r1   r   r   c                 C  s.   || _ || _|| _|| _|| _g | _d| _d S )Nr   )r<   
_text_area
_max_lines_loading_symbolr>   _lines
_ops_shown)rA   r
   r6   rQ   rR   r1   r   r   r   rB      s   
z&_DynamicOperationStatsPrinter.__init__rN   Iterable[pb.OperationStats]c                 C  s   d}|D ]}|j D ]
}| j|ddd q	||j7 }q| j|k rCd| j  kr-t| jkr4n n| j  || j }| jd| d t| jdkre| j	r\| j
| j	 d| j  d
S | j
| j d
S | j
d	| j d
S )z&Show the given stats in the text area.r   Fr:   
is_subtaskindentr	   z+ z more task(s) 
N)rU   _add_operationtotal_operationsrx   ru   rK   rw   poprV   rv   rt   rl   r>   rX   )rA   rN   r   r   r[   	remainingr   r   r   r      s   

 

z%_DynamicOperationStatsPrinter.displayr[   pb.Operationr{   boolr|   c           	      C  s   t | j| jkr
dS |s|  jd7  _g }|r | jjr |d | jr)|| j ||j |j	r9||j	  |dt
|jd d | j|d|  |jrw| jd}| j|j}|red	nd
}| j| | d	| d|  |jr|d	 }|jD ]}| j|d|d qdS dS )z#Add the operation to `self._lines`.Nr	   u   ↳()seconds)r}   ERRORz  r:   Trz   )rK   rw   ru   rx   r<   supports_unicoderV   rv   rW   r   _time_to_stringruntime_secondsrX   error_statuserrorsecondary_textsubtasksr   )	rA   r[   r{   r|   parts
error_word
error_descsubtask_indenttaskr   r   r   r     s>   

z,_DynamicOperationStatsPrinter._add_operationN)r
   r   r6   rr   rQ   rs   rR   r2   r1   r2   r   r   )rN   ry   r   r   )r[   r   r{   r   r|   r2   r   r   )rn   ro   rp   rq   rB   r   r   r   r   r   r   rT      s
    

rT   r   rj   c                 C  sp   | dk r
| ddS | dk r| ddS | dk r"| d }|ddS t | d }t | d d }| d| dS )	z1Returns a short string representing the duration.
   z.1fs<   z.0fi  mh)rs   )r   minuteshoursr   r   r   r   B  s   r   bytesrs   c                 C  s   | d S )z+Returns the number of megabytes in `bytes`.i   r   )r   r   r   r   r^   Q  s   r^   )r
   r   r   r   r   r   )r   r   r   r   r   r   )r
   r   r1   r2   r   r3   )r   rj   r   r2   )r   rs   r   rj   )rq   
__future__r   r   
contextlibr!   typingr   r   r   wandb.protor   rG   wandb.sdk.interfacer   wandb.sdk.libr   r:   r
   pr   r0   contextmanagerr7   r   rT   r   r^   r   r   r   r   <module>   s$    

,~
`