
    g                        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e      ZddZe	 G d	 d
e             Z G d d      Zy)    )annotationsN)abstractmethod)TYPE_CHECKING
NamedTupleProtocolruntime_checkable)Metricc                  @    e Zd ZU dZded<   ded<   ded<   d
dZddZy	)	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_lengthc                T    d| j                    d| j                   d| j                   S )Nzcache_memory_bytes{cache_type="z	",cache="z"} r   r   r   selfs    L/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/stats.pyto_metric_strzCacheStat.to_metric_str.   s0    1$2D2D1EYtN__cdhdtdtcuvv    c                6   |j                   j                         }d|_        | j                  |_        |j                   j                         }d|_        | j
                  |_        |j                  j                         }| j                  |j                  _	        y)z-Fill an OpenMetrics `Metric` protobuf object.
cache_typecacheN)
labelsaddnamer   valuer   metric_pointsr   gauge_value	int_value)r   metriclabelmetric_points       r   marshall_metric_protozCacheStat.marshall_metric_proto1   st    !!#!
((!!#
oo++//1-1-=-=  *r   N)returnr   )r"   MetricProtor&   None)__name__
__module____qualname____doc____annotations__r   r%    r   r   r   r      s%      Ow>r   r   c                    d }g }t        | |      }t        j                  ||      }|D ]5  \  \  }}}|j                  t	        ||t        d |D                           7 |S )zNGroup a list of CacheStats by category_name and cache_name and sum byte_lengthc                2    | j                   | j                  fS N)r   r   )individual_stats    r   key_functionz!group_stats.<locals>.key_functionB   s    ,,o.H.HHHr   )keyc              3  4   K   | ]  }|j                     y wr1   )r   ).0items     r   	<genexpr>zgroup_stats.<locals>.<genexpr>O   s     P=OT 0 0=Os   r   )sorted	itertoolsgroupbyappendr   sum)statsr3   resultsorted_statsgrouped_statsr   r   single_group_statss           r   group_statsrC   ?   ss    I !F%\2L%%lEM;H7#
%7+%P=OPP	
 <I Mr   c                      e Zd Zedd       Zy)CacheStatsProviderc                    t         r1   )NotImplementedErrorr   s    r   	get_statszCacheStatsProvider.get_statsW   s    !!r   Nr&   list[CacheStat])r)   r*   r+   r   rH   r.   r   r   rE   rE   U   s    " "r   rE   c                  "    e Zd Zd ZddZddZy)StatsManagerc                    g | _         y r1   )_cache_stats_providersr   s    r   __init__zStatsManager.__init__]   s
    @B#r   c                :    | j                   j                  |       y)zRegister a CacheStatsProvider with the manager.
        This function is not thread-safe. Call it immediately after
        creation.
        N)rN   r<   )r   providers     r   register_providerzStatsManager.register_provider`   s    
 	##**84r   c                j    g }| j                   D ]!  }|j                  |j                                # |S )zAReturn a list containing all stats from each registered provider.)rN   extendrH   )r   	all_statsrQ   s      r   rH   zStatsManager.get_statsg   s6    %'	33HX//12 4 r   N)rQ   rE   r&   r(   rI   )r)   r*   r+   rO   rR   rH   r.   r   r   rL   rL   \   s    C5r   rL   )r>   rJ   r&   rJ   )
__future__r   r:   abcr   typingr   r   r   r   *streamlit.proto.openmetrics_data_model_pb2r	   r'   r   rC   rE   rL   r.   r   r   <module>rZ      sS    #   I IP#>
 #>L, " " " r   