o
    1 iL.                     @   sn  d dl Z d dlZd dlZd dlZd dlZd dl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Zd dlm  mZ d dlm  mZ d dlm  mZ d dlmZ d dlmZmZ d dlmZmZ d dl m!Z! d d	l"m#Z# d d
l$m%Z% d dl&m'Z'm(Z( d dl)m*Z* d dl+m,Z, e-e.Z/ej0Z1dZ2ej34e2dkZ5e5re/6d dd Z7G dd dZ8dS )    N)floor)List)Version)ray_constants)build_addressparse_address)TagKeyrecord_extra_usage_tag)get_or_create_event_loop)DashboardPrometheusMetrics)DashboardHeadModule)aiohttphdrs)SubprocessModuleHandle)SubprocessRouteTableZ#RAY_DASHBOARD_BUILD_FOLLOW_SYMLINKS1zEnabling RAY_DASHBOARD_BUILD_FOLLOW_SYMLINKS is not recommended as it allows symlinks to directories outside the dashboard build folder. You may accidentally expose files on your system outside of the build directory.c                  C   sz   t jt jt jtdd} t jt jt}t j| s,t	t
jd| d| t j| d}tjd|td | S )NclientbuildzDashboard build directory not found. If installing from source, please follow the additional steps required to build the dashboard(cd python/ray/z#/client && npm ci && npm run build)static/static)follow_symlinks)ospathjoindirnameabspath__file__basenameisdirdashboard_utilsFrontendNotFoundErrorerrnoENOENTroutesr   FOLLOW_SYMLINKS_ENABLED)	build_dirmodule_nameZ
static_dir r'   j/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/ray/dashboard/http_server_head.pysetup_static_dir8   s   r)   c                   @   s   e Zd Zdededededededefdd	Zed
de	j
jfddZedde	j
jfddZedde	j
jfddZdd Ze	j
jdd Ze	j
jdd Ze	j
jdd Ze	j
jdd Zdee dee fd d!Zd"d# Zd$S )%HttpServerDashboardHeadip	http_host	http_porthttp_port_retriesgcs_addresssession_namemetricsc           
   
   C   s   || _ || _|| _|| _t|d | _|| _|| _d | _zt	 }t
d| W n  tjyG }	 ztjdv r;t
|	 n|	W Y d }	~	nd }	~	ww tj|  ttjtdk ratjt d| _d S t | _d S )Nr   z"Setup static dir for dashboard: %s)win32cygwinz4.0.0)loop)r+   r,   r-   r.   r   Zhead_node_ipr1   _session_namerunnerr)   loggerinfor   r    sysplatformwarningdashboard_optional_utilsDashboardHeadRouteTablebindr   r   __version__ZClientSessionr
   Zhttp_session)
selfr+   r,   r-   r.   r/   r0   r1   r%   exr'   r'   r(   __init__O   s,   

z HttpServerDashboardHead.__init__/returnc              
      s|   zt tjd W n ty# } ztd|  W Y d }~nd }~ww tjt	j
t	j
t	j
td}d|jd< |S )NTruez`Failed to record the dashboard usage. This error message is harmless and can be ignored. Error: zclient/build/index.htmlzno-storeCache-Control)r	   r   ZDASHBOARD_USED	Exceptionr7   r;   r   webFileResponser   r   r   r   r   r   headers)r@   reqerespr'   r'   r(   	get_indexy   s$   
z!HttpServerDashboardHead.get_indexz/favicon.icoc              	      s(   t jtjtjtjtdS )Nzclient/build/favicon.ico)	r   rH   rI   r   r   r   r   r   r   )r@   rK   r'   r'   r(   get_favicon   s   z#HttpServerDashboardHead.get_faviconz	/timezonec              
      sf   zt  }tj|W S  ty2 } ztd|  tjjddt	| dW  Y d }~S d }~ww )NzError getting timezone: i  zInternal Server Error:statustext)
timezone_utilsZget_current_timezone_infor   rH   Zjson_responserG   r7   errorResponsestr)r@   rK   Zcurrent_timezonerL   r'   r'   r(   get_timezone   s   z$HttpServerDashboardHead.get_timezonec                 C   s   | j r| jsJ | j | jfS N)r,   r-   r@   r'   r'   r(   get_address   s   z#HttpServerDashboardHead.get_addressc                    s   |j ds|j dr<t|j drdnd}tt|j }||kr<||jvr<td|d| t	j
 ||I d H S )Nr   z/logszRejecting request_path=z& because it is not relative to parent=)r   
startswithpathlibPurePosixPath	posixpathrealpathparentsr7   r8   r   rH   ZHTTPForbidden)r@   requesthandlerparentrequest_pathr'   r'   r(   path_clean_middleware   s   
z-HttpServerDashboardHead.path_clean_middlewarec                    s<   t |r|jtjtjfv rtjjdddS ||I d H S )Ni  z'Method Not Allowed for browser traffic.rP   )	r<   Zis_browser_requestmethodr   Z	METH_POSTZMETH_PUTr   rH   rU   )r@   ra   rb   r'   r'   r(   browsers_no_post_put_middleware   s   z7HttpServerDashboardHead.browsers_no_post_put_middlewarec                    sp  t  }zjz\||I d H }t|jd  d}|W W t  | }z'| jjj|j|tj	| j
dd| | jjj|j|j|tj	| j
dd  W S  tyb } ztd|  W Y d }~S d }~ww  ttjfyo   d} w t  | }z'| jjj|j|tj	| j
dd| | jjj|j|j|tj	| j
dd  W w  ty } ztd|  W Y d }~w d }~ww )Nd   xx	dashboard)endpointhttp_statusr   SessionName	Component)rf   rk   rl   r   rm   rn   zError emitting api metrics: Z5xx)time	monotonicr   rQ   r1   Zmetrics_request_durationlabels__name__rayr?   r5   ZobserveZmetrics_request_countrf   incrG   r7   	exceptionasyncioCancelledError)r@   ra   rb   
start_timeresponseZ
status_tagZ	resp_timerL   r'   r'   r(   metrics_middleware   sp   

z*HttpServerDashboardHead.metrics_middlewarec                    s8   |j dr||I d H }d|jd< |S ||I d H S )Nr   zmax-age=31536000rF   )r   r[   rJ   )r@   ra   rb   ry   r'   r'   r(   cache_control_static_middleware   s   
z7HttpServerDashboardHead.cache_control_static_middlewaredashboard_head_modulessubprocess_module_handlesc                    s  |D ]}t j| q|D ]}t| qtjjtj| j	| j
| j| jgd}|jt d |jt d tjj|dd| _| j I d H  d }td| j D ]:}ztj| j| j| j}| I d H  W  n. ty }	 z|	}|  jd7  _td| j|	 W Y d }	~	qQd }	~	ww td| j d| |jjd	  ^| _| _}
t !| jj"r| j#n| j| _t$d
t%| j| j dd |j& D }|D ]}t$| qt$dt'| d S )N)Zclient_max_sizeZmiddlewares)r#   z;%a %t '%r' %s %b bytes %D us '%{Referer}i' '%{User-Agent}i')Zaccess_log_format   zTry to use port %s: %sz0Failed to find a valid port for dashboard after z
 retries: r   zDashboard head http address: %sc                 S   s   g | ]
}|j tjkr|qS r'   )rf   r   Z	METH_HEAD).0rr'   r'   r(   
<listcomp>(  s    z/HttpServerDashboardHead.run.<locals>.<listcomp>zRegistered %s routes.)(r<   r=   r>   r   r   rH   Applicationr   ZDASHBOARD_CLIENT_MAX_SIZErz   re   rg   r{   Z
add_routesr#   Zbound_routesZ	AppRunnerr6   setupranger.   ZTCPSiter,   r-   startOSErrorr7   r;   rG   _serversocketsgetsockname	ipaddress
ip_addressis_unspecifiedr+   r8   r   Zrouterlen)r@   r|   r}   mhappZlast_exisiterL   _Zdump_routesr   r'   r'   r(   run   sh   	zHttpServerDashboardHead.runc                    s   | j  I d H  d S rX   )r6   cleanuprY   r'   r'   r(   r   -  s   zHttpServerDashboardHead.cleanupN)rr   
__module____qualname__rV   intr   rB   r#   getr   rH   rI   rN   rO   rU   rW   rZ   Z
middlewarere   rg   rz   r{   r   r   r   r   r   r'   r'   r'   r(   r*   N   sJ    
*




@r*   )9rv   r!   r   loggingr   r\   r^   r9   ro   mathr   typingr   Zpackaging.versionr   rs   Zray.dashboard.optional_utilsrj   Zoptional_utilsr<   Zray.dashboard.timezone_utilsrS   Zray.dashboard.utilsutilsr   r   Zray._common.network_utilsr   r   Zray._common.usage.usage_libr   r	   Zray._common.utilsr
   Zray.dashboard.dashboard_metricsr   Zray.dashboard.headr   Zray.dashboard.optional_depsr   r   Z!ray.dashboard.subprocesses.handler   Z!ray.dashboard.subprocesses.routesr   	getLoggerrr   r7   r=   r#   ZENV_VAR_FOLLOW_SYMLINKSenvironr   r$   r;   r)   r*   r'   r'   r'   r(   <module>   sF    
