U
    ˜ARdä  ã                   @   sn   d dl mZ d dlmZmZ d dlmZmZ d dlm	Z
 G dd„ deƒZeG dd„ deƒƒZG d	d
„ d
ƒZdS )é    )Úabstractmethod)ÚListÚ
NamedTuple)ÚProtocolÚruntime_checkable)ÚMetricc                   @   sH   e Zd ZU dZeed< eed< eed< edœdd„Zedd	œd
d„Z	dS )Ú	CacheStatae  Describes a single cache entry.

    Properties
    ----------
    category_name : str
        A human-readable name for the cache "category" that the entry belongs
        to - e.g. "st.memo", "session_state", etc.
    cache_name : str
        A human-readable name for cache instance that the entry belongs to.
        For "st.memo" and other function decorator caches, this might be the
        name of the cached function. If the cache category doesn't have
        multiple separate cache instances, this can just be the empty string.
    byte_length : int
        The entry's memory footprint in bytes.
    Úcategory_nameÚ
cache_nameÚbyte_length©Úreturnc                 C   s   d| j | j| jf S )Nz1cache_memory_bytes{cache_type="%s",cache="%s"} %s)r	   r
   r   ©Úself© r   ú;/tmp/pip-unpacked-wheel-b9et7o5g/streamlit/runtime/stats.pyÚto_metric_str,   s
    ýzCacheStat.to_metric_strN)Úmetricr   c                 C   sH   |j  ¡ }d|_| j|_|j  ¡ }d|_| j|_|j ¡ }| j|j_	dS )z-Fill an OpenMetrics `Metric` protobuf object.Z
cache_typeÚcacheN)
ÚlabelsÚaddÚnamer	   Úvaluer
   Zmetric_pointsr   Zgauge_valueZ	int_value)r   r   ÚlabelZmetric_pointr   r   r   Úmarshall_metric_proto3   s    


zCacheStat.marshall_metric_proto)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__Úintr   ÚMetricProtor   r   r   r   r   r      s   
r   c                   @   s"   e Zd Zeee dœdd„ƒZdS )ÚCacheStatsProviderr   c                 C   s   t ‚d S ©N)ÚNotImplementedErrorr   r   r   r   Ú	get_statsC   s    zCacheStatsProvider.get_statsN)r   r   r   r   r   r   r&   r   r   r   r   r#   A   s   r#   c                   @   s6   e Zd Zdd„ Zeddœdd„Zee dœdd	„ZdS )
ÚStatsManagerc                 C   s
   g | _ d S r$   )Ú_cache_stats_providersr   r   r   r   Ú__init__I   s    zStatsManager.__init__N)Úproviderr   c                 C   s   | j  |¡ dS )zŽRegister a CacheStatsProvider with the manager.
        This function is not thread-safe. Call it immediately after
        creation.
        N)r(   Úappend)r   r*   r   r   r   Úregister_providerL   s    zStatsManager.register_providerr   c                 C   s"   g }| j D ]}| | ¡ ¡ q
|S )zAReturn a list containing all stats from each registered provider.)r(   Úextendr&   )r   Z	all_statsr*   r   r   r   r&   S   s    
zStatsManager.get_stats)	r   r   r   r)   r#   r,   r   r   r&   r   r   r   r   r'   H   s   r'   N)Úabcr   Útypingr   r   Ztyping_extensionsr   r   Z*streamlit.proto.openmetrics_data_model_pb2r   r"   r   r#   r'   r   r   r   r   Ú<module>   s   *