o
    d۷i                     @   s  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Zd dlm	Z	 d dl
mZ d dlmZ d dl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mZ e eZddddddddZedZ de!de!fddZ"dede!fddZ#dedede$de!de%ddfdd Z&dej'd!d"d#d$d%d&ej'd'd(d)d*d%e"d+ej'd%d,d-d.d/fd0eedB ej(d1d/f de$de!de%ddf
d2d3Z)dS )4    N)datetime)Path)	Annotated)	HTTPError)escape)RichToolkit)	APIClientAppLogEntryStreamLogErrorTooManyRetriesError)	AppConfigget_app_config)Identity)get_rich_toolkithandle_http_errorbluecyanyellowredmagenta)debuginfowarningwarnerrorcriticalfatalz^\d+[smhd]$valuereturnc                 C   s   t | s
td| S )z&Validate the --since parameter format.zPInvalid format. Use a number followed by s, m, h, or d (e.g., '5m', '1h', '2d').)SINCE_PATTERNmatchtyperBadParameter)r    r#   U/home/ubuntu/vllm_env/lib/python3.10/site-packages/fastapi_cloud_cli/commands/logs.py_validate_since&   s
   
r%   logc                 C   st   t | jdd}|ddd d }t| j }t	| j
}|r2d| d| d| d	| S d
| d	| S )z7Format a log entry for display with a colored indicatorZz+00:00z%Y-%m-%dT%H:%M:%S.%fN[u   ]┃[/z] [dim]z[/dim] u   [dim]┃[/dim] [dim])r   fromisoformat	timestampreplacestrftimeLOG_LEVEL_COLORSgetlevellowerr   message)r&   r+   timestamp_strcolorr2   r#   r#   r$   _format_log_line0   s   
r5   toolkit
app_configtailsincefollowc           
   
   C   sJ  d}zNt  @}|j|j|||dD ]}| t| |d7 }q|s4|dkr=| d 	 W d   W dS W d   W dS W d   W dS 1 sIw   Y  W dS  ty^   |   Y dS  ty } z(|jdkrmd}	nt	|j
tryt|j
}	n	dtt| }	| |	 tddd}~w ttfy   | d	 tddw )
z.Stream app logs and print them to the console.r   )app_idr8   r9   r:      z+No logs found for the specified time range.Ni  z4App not found. Make sure to use the correct account.z[red]Error:[/] z6Lost connection to log stream. Please try again later.)r   stream_app_logsr;   printr5   KeyboardInterrupt
print_liner
   status_code
isinstance	__cause__r   r   r   strr!   Exitr   TimeoutError)
r6   r7   r8   r9   r:   	log_countclientr&   er2   r#   r#   r$   _process_log_stream?   sJ   




&

rJ   d   z--tailz-tz-Number of log lines to show before streaming.T)helpshow_default5mz--sincez-sz9Show logs since a specific time (e.g., '5m', '1h', '2d').)rL   rM   callbackz--follow/--no-followz-fz=Stream logs in real-time (use --no-follow to fetch and exit).)rL   pathzEPath to the folder containing the app (defaults to current directory)c                 C   s   t  }tdd^}| s|jddd td| pt }t|}|s/|d tdt	
d|j |rE|jd	|j d
dd n|jd|j ddd |  t|||||d W d   dS 1 siw   Y  dS )a*  Stream or fetch logs from your deployed app.

    Examples:
        fastapi cloud logs                      # Stream logs in real-time
        fastapi cloud logs --no-follow          # Fetch recent logs and exit
        fastapi cloud logs --tail 50 --since 1h # Last 50 logs from the past hour
    T)minimalz<No credentials found. Use [blue]`fastapi login`[/] to login.auth)tagr<   zENo app linked to this directory. Run [blue]`fastapi deploy`[/] first.zFetching logs for app ID: %szStreaming logs for [bold]z[/bold] (Ctrl+C to exit)...logszFetching logs for [bold]z
[/bold]...)r6   r7   r8   r9   r:   N)r   r   is_logged_inr>   r!   rE   r   cwdr   loggerr   r;   r@   rJ   )rP   r8   r9   r:   identityr6   app_pathr7   r#   r#   r$   rT   o   sD   $

"rT   )*loggingrer   pathlibr   typingr   r!   httpxr   rich.markupr   rich_toolkitr   fastapi_cloud_cli.utils.apir   r	   r
   r   fastapi_cloud_cli.utils.appsr   r   fastapi_cloud_cli.utils.authr   fastapi_cloud_cli.utils.clir   r   	getLogger__name__rW   r.   compiler   rD   r%   r5   intboolrJ   OptionArgumentrT   r#   r#   r#   r$   <module>   s    




6