
    tni:                        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mZmZmZ d dlmZmZ d d	lmZ d d
lmZ  e j        e          ZddddddddZ ej        d          ZdedefdZ dedefdZ!dedede"dede#ddfdZ$d ej%        dddd d!"           ej%        d#d$d%d&d!e '           ej%        d!d(d)d*+          fd,eee          ej&        d-+          f         de"dede#ddf
d.Z'dS )/    N)datetime)Path)	AnnotatedOptional)escape)RichToolkit)	APIClientAppLogEntryStreamLogErrorTooManyRetriesError)	AppConfigget_app_config)Identity)get_rich_toolkitbluecyanyellowredmagenta)debuginfowarningwarnerrorcriticalfatalz^\d+[smhd]$valuereturnc                 b    t                               |           st          j        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   s    \/var/www/html/gpu-tools/venv/lib/python3.11/site-packages/fastapi_cloud_cli/commands/logs.py_validate_sincer%   %   s7    u%% 
 ^
 
 	
 L    logc                 V   t          j        | j                            dd                    }|                    d          dd         dz   }t
                              | j                                                  }t          | j
                  }|rd| 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colorr4   s        r$   _format_log_liner7   /   s     &s}'<'<S('K'KLLI&&'=>>ssCcIM  !2!233ES[!!G NM5MMMMmMMGMMMA-AAAAAr&   toolkit
app_configtailsincefollowc                    d}	 t                      5 }|                    |j        |||          D ])}|                     t	          |                     |dz  }*|s)|dk    r#|                     d           	 ddd           dS ddd           dS # 1 swxY w Y   dS # t
          $ r |                                  Y dS t          $ r}t          |          }	d|	v sd|	v r|                     d           n?d	|	v r|                     d
           n%|                     dt          |	                      t          j        d          dd}~wt          t          f$ r+ |                     d           t          j        d          dw xY w)z.Stream app logs and print them to the console.r   )app_idr:   r;   r<      z+No logs found for the specified time range.NzHTTP 401zHTTP 403zWThe specified token is not valid. Use [blue]`fastapi login`[/] to generate a new token.zHTTP 404z4App 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>   printr7   KeyboardInterrupt
print_liner   strr   r"   Exitr   TimeoutError)
r8   r9   r:   r;   r<   	log_countclientr'   e	error_msgs
             r$   _process_log_streamrK   >   sV    I$&[[ 	F--!(	 .     .s33444Q		 i1nnKLLL	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	     & & &FF	""jI&=&=MMi    9$$MMF    MM5&"3"355   jmm%. & & &D	
 	
 	
 jmm%	&sH   B  A'B9B  B  BB  BB   F
	F

BE?F
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                    t                      }t          d          5 }|                                s+|                    dd           t	          j        d          | pt          j                    }t          |          }|s)|                    d           t	          j        d          t          
                    d|j                   |r!|                    d	|j         d
d           n |                    d|j         dd           |                                 t          |||||           ddd           dS # 1 swxY w 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]...)r8   r9   r:   r;   r<   N)r   r   is_logged_inrA   r"   rE   r   cwdr   loggerr   r>   rC   rK   )rQ   r:   r;   r<   identityr8   app_pathr9   s           r$   rV   rV   o   s   H zzH	$	'	'	' %
7$$&& 	 MMN     *Q--%48::#H--
 	 MMW   *Q--3Z5FGGG 		MMZJ,=ZZZ     
 MMH:+<HHH     	!	
 	
 	
 	
?%
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
 %
s   DEE
E
)(loggingrer   pathlibr   typingr   r   r"   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   	getLogger__name__rY   r0   compiler    rD   r%   r7   intboolrK   OptionArgumentrV    r&   r$   <module>rn      s    				             & & & & & & & &        $ $ $ $ $ $            C B B B B B B B 1 1 1 1 1 1 8 8 8 8 8 8		8	$	$    
>**3 3    B+ B# B B B B.&.&.& .& 	.&
 .& 
.& .& .& .&n 	<   H     5<L	  -J
 J

X	
 	
 	
	J
 J
 J
, -J
8 
9J
 J
 J
 J
 J
 J
r&   