o
    bi                     @   s  d 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mZ ddlmZmZ ddlZddlm  mZ ddlm  mZ ddlmZmZ ddlmZmZ ddlmZm Z  dd	lm!Z!m"Z" zej#Z#W n e$y   ej%Z#Y nw e&e'Z(e Z)e Z*e	d
g dZ+ej,ej-hZ.ej/ej0eej1d fddZ2dede3fddZ4defddZ5defddZ6dS )zT
Optional utils module contains utility methods
that require optional dependencies.
    N)
namedtuple)CallableUnion)RequestResponse) RAY_INTERNAL_DASHBOARD_NAMESPACEenv_bool)aiohttphdrs)method_route_table_factoryrest_response)DashboardAgentModuleDashboardHeadModuleAiohttpCacheValue)data
expirationtaskFc                    sF   dksJ t    fdd}tr!}tj||S |S )Nr   c                    s`   r.t  dtjjf fdd}d d d}| j|7  _| j|7  _|S  S )Nreturnc                     s   | d }|j ddkr|  I d H S |jtv r|j n
|j| I d H f  }|d urK  |j rA|j	t

 krKtjjdi |jS  fdd}t|  }|| |d u rh|I d H S tjjdi |jS )Nnocache1c                    s   z|   }W n ty   ttjjt d}Y nw |jt	|j
|jd}t|t  |  <   t krB jdd |S )N)status_codemessage)statusheadersbodyF)last)result	Exceptionr   dashboard_utilsHTTPStatusCodeINTERNAL_ERROR	traceback
format_excr   dictr   r   _AiohttpCacheValuetimemove_to_endlenpopitem)r   responser   )cachekeymaxsizettl_seconds P/home/ubuntu/.local/lib/python3.10/site-packages/ray/dashboard/optional_utils.py_update_cacheV   s&   

zNaiohttp_cache.<locals>._wrapper.<locals>._cache_handler.<locals>._update_cacher/   )querygetmethod_AIOHTTP_CACHE_NOBODY_METHODSpath_qsreadr'   r   doner   r&   r	   webr   r   create_taskadd_done_callback)argsreqvaluer1   r   )r+   handlerr-   r.   )r,   r0   _cache_handler>   s$   




z7aiohttp_cache.<locals>._wrapper.<locals>._cache_handlerz[cache ttl=z, max_size=])	functoolswrapsr	   r9   r   __name____qualname__)r?   r@   suffixr+   enabler-   r.   )r?   r0   _wrapper;   s   3zaiohttp_cache.<locals>._wrapper)collectionsOrderedDictinspectiscoroutinefunctiondashboard_constsAIOHTTP_CACHE_TTL_SECONDS)r.   r-   rH   rI   target_funcr/   rG   r0   aiohttp_cache3   s   
>rQ   r=   r   c                 C   s   | j d dS )a4  Checks if a request is made by a browser like user agent.

    This heuristic is very weak, but hard for a browser to bypass- eg,
    fetch/xhr and friends cannot alter the user-agent, but requests made with
    an http library can stumble into this if they choose to user a browser like
    user agent.
    z
User-AgentMozilla)r   
startswith)r=   r/   r/   r0   is_browser_request   s   rT   c                  C      dt dt fdd} | S )z7Reject any requests that appear to be made by a browserfr   c                    s    t  dtf fdd}|S )Nr=   c                    s,   t |rtdtjjjdS  | |I d H S )NzBrowser requests not allowedtextr   )rT   r   r	   r9   HTTPMethodNotAllowedr   )selfr=   rV   r/   r0   	decorator   s   zCdeny_browser_requests.<locals>.decorator_factory.<locals>.decorator)rB   rC   r   rV   r\   r/   r[   r0   decorator_factory   s   z0deny_browser_requests.<locals>.decorator_factoryr   r^   r/   r/   r0   deny_browser_requests   s   ra   c                  C   rU   )zDDecorator to be used on methods that require being connected to Ray.rV   r   c                    s(   t  dtttf f fdd}|S )NrZ   c              
      s   zFt  s9z| j}td|  ttjtj	d< t j
|ddtdd W n ty8 } zt   |d d }~ww  | g|R i |I d H W S  tyl } ztd|  tt tjjjdW  Y d }~S d }~ww )NzConnecting to ray with address=&RAY_gcs_server_request_timeout_secondsFT)addresslog_to_driverconfigure_logging	namespace_skip_env_hookzUnexpected error in handler: rW   )rayis_initializedgcs_addressloggerinfostrrN   GCS_RPC_TIMEOUT_SECONDSosenvironinitr   r   shutdown	exceptionr   r"   r#   r	   r9   HTTPInternalServerErrorr   )rZ   r<   kwargsrc   er[   r/   r0   r\      s<   

zKinit_ray_and_catch_exceptions.<locals>.decorator_factory.<locals>.decorator)rB   rC   r   r   r   r]   r/   r[   r0   r^      s
   
 z8init_ray_and_catch_exceptions.<locals>.decorator_factoryr_   r`   r/   r/   r0   init_ray_and_catch_exceptions   s   $rw   )7__doc__asynciorJ   rB   rL   loggingro   r&   r"   r   typingr   r   aiohttp.webr   r   rh   ray.dashboard.consts	dashboardconstsrN   ray.dashboard.utilsutilsr   ray._private.ray_constantsr   r   ray.dashboard.optional_depsr	   r
   ray.dashboard.routesr   r   r   r   r:   AttributeErrorensure_future	getLoggerrD   rk   DashboardHeadRouteTableDashboardAgentRouteTabler%   METH_GETMETH_DELETEr5   rO   AIOHTTP_CACHE_MAX_SIZE%AIOHTTP_CACHE_DISABLE_ENVIRONMENT_KEYrQ   boolrT   ra   rw   r/   r/   r/   r0   <module>   sH    



N