o
    *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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e ej)d1d/f de%de"de&ddf
d2d3Z*dS )4    N)datetime)Path)	AnnotatedOptional)	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$   \/home/ubuntu/veenaModal/venv/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colorr3   r$   r$   r%   _format_log_line0   s   
r6   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_idr9   r:   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<   printr6   KeyboardInterrupt
print_liner   status_code
isinstance	__cause__r   r   r   strr"   Exitr   TimeoutError)
r7   r8   r9   r:   r;   	log_countclientr'   er3   r$   r$   r%   _process_log_stream?   sJ   




&

rK   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').)rM   rN   callbackz--follow/--no-followz-fz=Stream logs in real-time (use --no-follow to fetch and exit).)rM   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]...)r7   r8   r9   r:   r;   N)r   r   is_logged_inr?   r"   rF   r   cwdr   loggerr   r<   rA   rK   )rQ   r9   r:   r;   identityr7   app_pathr8   r$   r$   r%   rU   o   sD   $

"rU   )+loggingrer   pathlibr   typingr   r   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__rX   r/   compiler    rE   r&   r6   intboolrK   OptionArgumentrU   r$   r$   r$   r%   <module>   s    


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