o
    h                     @  s   d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	 er$d dl
mZ G dd deZdddZe	G dd deZG dd dZdS )    )annotationsN)abstractmethod)TYPE_CHECKING
NamedTupleProtocolruntime_checkable)Metricc                   @  s>   e Zd ZU dZded< ded< ded< ddd	Z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.
    strcategory_name
cache_nameintbyte_lengthreturnc                 C  s   d| j  d| j d| j S )Nzcache_memory_bytes{cache_type="z	",cache="z"} r   r   r   self r   K/var/www/vscode/kcb/lib/python3.10/site-packages/streamlit/runtime/stats.pyto_metric_str.   s   zCacheStat.to_metric_strmetricMetricProtoNonec                 C  sH   |j  }d|_| j|_|j  }d|_| j|_|j }| j|j_	dS )z-Fill an OpenMetrics `Metric` protobuf object.
cache_typecacheN)
labelsaddnamer   valuer   metric_pointsr   gauge_value	int_value)r   r   labelmetric_pointr   r   r   marshall_metric_proto1   s   


zCacheStat.marshall_metric_protoN)r   r
   )r   r   r   r   )__name__
__module____qualname____doc____annotations__r   r$   r   r   r   r   r	      s   
 
r	   statslist[CacheStat]r   c              	   C  s^   dd }g }t | |d}tj||d}|D ]\\}}}|t||tdd |D d q|S )zOGroup a list of CacheStats by category_name and cache_name and sum byte_length.c                 S  s   | j | jfS N)r   r   )individual_statr   r   r   key_functionB   s   z!group_stats.<locals>.key_function)keyc                 s  s    | ]}|j V  qd S r,   )r   ).0itemr   r   r   	<genexpr>O   s    zgroup_stats.<locals>.<genexpr>r   )sorted	itertoolsgroupbyappendr	   sum)r*   r.   resultsorted_statsgrouped_statsr   r   single_group_statsr   r   r   group_stats?   s   r<   c                   @  s   e Zd ZedddZdS )CacheStatsProviderr   r+   c                 C  s   t r,   )NotImplementedErrorr   r   r   r   	get_statsW   s   zCacheStatsProvider.get_statsNr   r+   )r%   r&   r'   r   r?   r   r   r   r   r=   U   s    r=   c                   @  s(   e Zd Zdd ZdddZdd
dZdS )StatsManagerc                 C  s
   g | _ d S r,   )_cache_stats_providersr   r   r   r   __init__]   s   
zStatsManager.__init__providerr=   r   r   c                 C  s   | j | dS )zRegister a CacheStatsProvider with the manager.
        This function is not thread-safe. Call it immediately after
        creation.
        N)rB   r6   )r   rD   r   r   r   register_provider`   s   zStatsManager.register_providerr+   c                 C  s"   g }| j D ]	}||  q|S )zAReturn a list containing all stats from each registered provider.)rB   extendr?   )r   	all_statsrD   r   r   r   r?   g   s   
zStatsManager.get_statsN)rD   r=   r   r   r@   )r%   r&   r'   rC   rE   r?   r   r   r   r   rA   \   s    
rA   )r*   r+   r   r+   )
__future__r   r4   abcr   typingr   r   r   r   *streamlit.proto.openmetrics_data_model_pb2r   r   r	   r<   r=   rA   r   r   r   r   <module>   s   
&