
    5i-                    2   U d dl mZ d dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZmZ d dlmZmZmZmZmZmZmZ er"d dlmZmZmZ d d	lmZ d d
l m!Z! d dl"m#Z# d dl$m%Z%  e	e&      Z'de(d<   dZ)de(d<   dZ*de(d<   dZ+de(d<    G d dee      Z,y)    )annotationsN)TYPE_CHECKINGFinalcast)
get_logger)
AppSession)ActiveSessionInfoSessionClientSessionInfoSessionManagerSessionStorage)ACTIVE_SESSIONS_FAMILYSESSION_DURATION_FAMILYSESSION_EVENTS_FAMILYCounterStat	GaugeStatStatStatsProvider)CallableMappingSequence)
ScriptData)ScriptCache)UserInfoType)UploadedFileManagerr   _LOGGERconnect_EVENT_TYPE_CONNECT	reconnect_EVENT_TYPE_RECONNECT
disconnect_EVENT_TYPE_DISCONNECTc                      e Zd ZdZedd       Z	 	 	 	 	 	 	 	 	 	 ddZ	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZddZddZ	ddZ
dd	Zdd
ZddZddZddZ	 d	 	 	 ddZy)WebsocketSessionManagera  A SessionManager used to manage sessions with lifecycles tied to those of a
    browser tab's websocket connection.

    WebsocketSessionManagers differentiate between "active" and "inactive" sessions.
    Active sessions are those with a currently active websocket connection. Inactive
    sessions are sessions without. Eventual cleanup of inactive sessions is a detail left
    to the specific SessionStorage that a WebsocketSessionManager is instantiated with.
    c                $    t         t        t        fS N)r   r   r   selfs    u/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/streamlit/runtime/websocket_session_manager.pystats_familiesz&WebsocketSessionManager.stats_familiesB   s    %'>@VWW    c                    || _         || _        || _        || _        i | _        t        j                         | _        d| _        d| _	        d| _
        i | _        d| _        y )Nr   )_session_storage_uploaded_file_mgr_script_cache_message_enqueued_callback_active_session_info_by_id	threadingLock_stats_lock_connect_count_reconnect_count_disconnect_count_session_connect_times_total_session_duration_seconds)r(   session_storageuploaded_file_managerscript_cachemessage_enqueued_callbacks        r)   __init__z WebsocketSessionManager.__init__F   sk     !0"7)*C' IK' %>>+#$%&&' 9;#67,r+   Nc                   |r|rt        d      || j                  v rt        j                  d|       |xr+ || j                  vxr | j                  j                  |      }t        |t              r|j                  }|j                          t        |||j                        | j                  |j                  <   | j                  j                  |j                         | j                  5  | xj                  dz  c_        t!        j"                         | j$                  |j                  <   d d d        |j                  S t'        || j(                  | j*                  | j,                  ||      }t        j/                  dt        |      |j                         |j                  | j                  v rt        d|j                   d      t        ||      | j                  |j                  <   | j                  5  | xj0                  dz  c_        t!        j"                         | j$                  |j                  <   d d d        |j                  S # 1 sw Y   |j                  S xY w# 1 sw Y   |j                  S xY w)NzcOnly one of existing_session_id and session_id_override should be truthy. This should never happen.zESession with id %s is already connected! Connecting to a new session.   )script_datar;   r<   r=   	user_infosession_id_overridez1Created new session for client %s. Session ID: %szsession.id 'z6' registered multiple times. This should never happen.)RuntimeErrorr1   r   warningr-   get
isinstancer   sessionregister_file_watchersr	   script_run_countiddeleter4   r6   time	monotonicr8   r   r.   r/   r0   debugr5   )	r(   clientrA   rB   existing_session_idrC   session_infoexisting_sessionrH   s	            r)   connect_sessionz'WebsocketSessionManager.connect_session_   sI    #6, 
 $"A"AAOOW#   ?#4+J+JJ?%%))*=> 	 lK0+33335CT --DD++,<,?,?@
 !!(()9)<)<=!! T%%*%CG>>CS++,<,?,?@T $&&&#"&"9"9++&*&E&E 3
 	?FWZZ	
 ::888wzzl +, , 
 7HPW6X''

3 	G1$6:nn6FD''

3	G zz9T $&&&,	G zzs   .AI<AI(I%(I<c                   || j                   v r| j                   |   }|j                  }|j                          |j                          |j	                          | j
                  j                  t        d ||j                               | j                   |= | j                  5  | xj                  dz  c_
        | j                  |       d d d        | j                   s| j                  j                          y y # 1 sw Y   1xY w)N)rP   rH   rJ   r@   )r1   rH   request_script_stopdisconnect_file_watchersclear_session_cachesr-   saver   rJ   r4   r7   _accumulate_session_durationr/   clear)r(   
session_idactive_session_inforH   s       r)   disconnect_sessionz*WebsocketSessionManager.disconnect_session   s    888"&"A"A*"M)11G''),,.((*!!&&#%8%I%I //
;!! >&&!+&11*=> ..$$& /	> >s   $'C;;Dc                8    | j                   j                  |      S r&   )r1   rF   r(   r\   s     r)   get_active_session_infoz/WebsocketSessionManager.get_active_session_info   s    ..22:>>r+   c                    || j                   v S r&   )r1   r`   s     r)   is_active_sessionz)WebsocketSessionManager.is_active_session   s    T<<<<r+   c                H    t        | j                  j                               S r&   )listr1   valuesr'   s    r)   list_active_sessionsz,WebsocketSessionManager.list_active_sessions   s    D33::<==r+   c                r   || j                   v r| j                   |   }| j                   |= |j                  j                          | j                  5  | xj                  dz  c_        | j                  |       d d d        | j                   s| j                  j                          y | j                  j                  |      }|r\| j                  j                  |       |j                  j                          | j                  5  | j                  |       d d d        y y # 1 sw Y   xY w# 1 sw Y   y xY w)Nr@   )r1   rH   shutdownr4   r7   rZ   r/   r[   r-   rF   rL   )r(   r\   r]   rR   s       r)   close_sessionz%WebsocketSessionManager.close_session   s   888"&"A"A*"M//
;''002!! >&&!+&11*=> 22""((* ,,00<!!((4  ))+!! >11*=> > > >> >s   'D!D-!D*-D6c                    | j                   j                  |d      }|-t        j                         |z
  }| xj                  |z  c_        yy)zAccumulate the session duration for a closed session.

        This method must be called while holding self._stats_lock.
        N)r8   poprM   rN   r9   )r(   r\   connect_timedurations       r)   rZ   z4WebsocketSessionManager._accumulate_session_duration   sI    
 2266z4H#~~',6H00H<0 $r+   c                v    | j                  |      }|rt        d|      S | j                  j                  |      S )Nr   )ra   r   r-   rF   )r(   r\   rR   s      r)   get_session_infoz(WebsocketSessionManager.get_session_info   s9    33J?|44$$((44r+   c                l    t        d| j                               | j                  j                         z   S )Nlist[SessionInfo])r   rg   r-   re   r'   s    r)   list_sessionsz%WebsocketSessionManager.list_sessions   s2    $d&?&?&AB##((*+	
r+   c                n   i }|t         |v r| j                  5  | j                  }| j                  }| j                  }ddd       t        t         dt        id      t        t         dt        id      t        t         dt        id      g|t         <   |t        |v rE| j                  5  t        | j                        }ddd       t        t        dd      g|t        <   |t        |v r-t        t        t        | j                        d	      g|t        <   |S # 1 sw Y   xY w# 1 sw Y   ixY w)
zReturn session-related metrics.

        Returns session event counters (connections, reconnections, disconnections)
        and the current count of active sessions.
        Ntypez&Total count of session events by type.)family_namevaluelabelshelpsecondsz0Total time spent in active sessions, in seconds.)rv   rw   unitry   z"Current number of active sessions.)rv   rw   ry   )r   r4   r5   r6   r7   r   r   r    r"   r   intr9   r   r   lenr1   )r(   family_namesresultconnect_countreconnect_countdisconnect_counttotal_durations          r)   	get_statsz!WebsocketSessionManager.get_stats   s^    )+#8L#H!! : $ 3 3"&"7"7#'#9#9 :  5'"$78A	  5)"$9:A	  5*"$:;A	-F()* #:l#J!! K!$T%I%I!JK  7("K	/F*+ #9\#I 6d==>=.F)* a: :6K Ks   %D-D+D(+D4)returnzSequence[str])
r:   r   r;   r   r<   r   r=   zCallable[[], None] | Noner   None)NN)rP   r
   rA   r   rB   r   rQ   
str | NonerC   r   r   str)r\   r   r   r   )r\   r   r   zActiveSessionInfo | None)r\   r   r   bool)r   zlist[ActiveSessionInfo])r\   r   r   zSessionInfo | None)r   rr   r&   )r~   zSequence[str] | Noner   zMapping[str, Sequence[Stat]])__name__
__module____qualname____doc__propertyr*   r>   rT   r^   ra   rc   rg   rj   rZ   rp   rs   r    r+   r)   r$   r$   8   s     X X8'8  38 "	8
 $=8 
8< +/*.AA  A  	A
 (A (A 
AF'2?=>>0=5
 48;0;	%;r+   r$   )-
__future__r   r2   rM   typingr   r   r   streamlit.loggerr   streamlit.runtime.app_sessionr   !streamlit.runtime.session_managerr	   r
   r   r   r   streamlit.runtime.statsr   r   r   r   r   r   r   collections.abcr   r   r   streamlit.runtime.script_datar   +streamlit.runtime.scriptrunner.script_cacher   7streamlit.runtime.scriptrunner_utils.script_run_contextr   'streamlit.runtime.uploaded_file_managerr   r   r   __annotations__r   r    r"   r$   r   r+   r)   <module>r      s    #   - - ' 4    ;;8GTKH% % ' U &* u * ,  ,unm ur+   