o
    wi'                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z d dlmZ d dlmZ d dlmZ d d	lmZ d
ZG dd deZeddG dd dZdddZG dd dZdS )    )annotationsN)	dataclass)
ServiceApi)wandb_api_pb2)asyncio_compat)new_printer)progress_printer)WandbApiFailedErrorg?c                   @  s   e Zd ZdZdS )IncompleteRunHistoryErrorzRaised when run history has incomplete history.

    Incomplete history occurs when some data has not been exported to
    parquet files yet, typically because the run is still ongoing.
    N)__name__
__module____qualname____doc__ r   r   Z/home/ubuntu/.local/lib/python3.10/site-packages/wandb/apis/public/runhistory/downloads.pyr
      s    r
   T)frozenc                   @  s.   e Zd ZU dZded< ded< dZded< dS )	DownloadHistoryResulta~  Result of downloading a run's history exports.

    Attributes:
        paths: The paths to the downloaded history files.
        errors: A dictionary mapping file paths to error messages for files that
           failed to download. None if all downloads succeeded.
        contains_live_data: Whether the run contains live data,
            not yet exported to parquet files.
    zlist[pathlib.Path]pathsboolcontains_live_dataNzdict[pathlib.Path, str] | Noneerrors)r   r   r   r   __annotations__r   r   r   r   r   r      s
   
 
r   service_apir   
request_idintr   r   returnc                   s   t | ||d I d H S )Nr   r   r   )_DownloadStatusWatcherwait_with_progressr   r   r   r   wait_for_download_with_progress+   s   r   c                   @  s>   e Z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   r   r   r   r   r   r   c                 C  s,   || _ || _|| _t | _d | _d | _d S )N)r   r   r   asyncioEvent
done_eventdownload_result_rate_limit_last_time)selfr   r   r   r   r   r   __init__8   s   

z_DownloadStatusWatcher.__init__r   r   c              	     sr   t  4 I d H }||   ||   W d   I d H  n1 I d H s(w   Y  | jd u r6td| jS )NzFailed to get download status)r   open_task_group
start_soon_wait_then_mark_done_show_progress_until_doner#   r	   )r%   groupr   r   r   r   E   s   (
z)_DownloadStatusWatcher.wait_with_progressNonec                   s   t jt jt j| jddd}| j|I d H }|jd dI d H }dd |jj	j
D }dd |jj	j D }t|| j|d	| _| j  d S )
Nr   )download_run_historyread_run_history_requesttimeoutc                 S  s   g | ]}t |qS r   pathlibPath).0	file_namer   r   r   
<listcomp>Z   s    z?_DownloadStatusWatcher._wait_then_mark_done.<locals>.<listcomp>c                 S  s   i | ]
\}}t ||qS r   r3   )r6   r7   error_messager   r   r   
<dictcomp>^   s    
z?_DownloadStatusWatcher._wait_then_mark_done.<locals>.<dictcomp>)r   r   r   )apb
ApiRequestReadRunHistoryRequestDownloadRunHistoryr   r   send_api_request_async
wait_asyncread_run_history_responser.   downloaded_filesr   itemsr   r   r#   r"   set)r%   api_requesthandleresponserB   r   r   r   r   r)   N   s,   z+_DownloadStatusWatcher._wait_then_mark_donec                   s   t  }t|dL}|  I d H sMtjtjtj| jddd}| j	|I d H }|j
d dI d H }|d ur>||jjj |  I d H rW d    d S W d    d S 1 sXw   Y  d S )NzDownloading history...r-   )download_run_history_statusr/   r1   )r   r   _rate_limit_check_doner;   r<   r=   DownloadRunHistoryStatusr   r   r?   r@   updaterA   rH   operation_stats)r%   pprogressstatus_requestrF   last_responser   r   r   r*   k   s*   "z0_DownloadStatusWatcher._show_progress_until_donec                   sV   t  }| j}|| _|r&||  }tk r&ttt| | j	 I dH  | j
 S )z4Wait for rate limit and return whether _done is set.N)time	monotonicr$   _POLL_WAIT_SECONDSr   racer    sleepr"   waitis_set)r%   now	last_timetime_since_lastr   r   r   rI   ~   s   

z-_DownloadStatusWatcher._rate_limit_check_doneN)r   r   r   r   r   r   )r   r   )r   r,   )r   r   )r   r   r   r&   r   r)   r*   rI   r   r   r   r   r   7   s    


	
r   )r   r   r   r   r   r   r   r   )
__future__r   r    r4   rQ   dataclassesr   wandb.apis.public.service_apir   wandb.protor   r;   wandb.sdk.libr   wandb.sdk.lib.printerr   wandb.sdk.lib.progressr   (wandb.sdk.lib.service.service_connectionr	   rS   	Exceptionr
   r   r   r   r   r   r   r   <module>   s"    
