
    xi.                    @   U d dl mZ d dlZd dlmZmZmZmZmZ dZ	de
d<   dZde
d<   d	Zde
d
<   dZde
d<   erd dlmZmZ d dlmZ d dlmZ e G d de             Z G d de      ZddZ G d de      Z G d de      ZddZe G d de             Z G d d      Zy)     )annotationsN)TYPE_CHECKINGFinal
NamedTupleProtocolruntime_checkablecache_memory_bytesr   CACHE_MEMORY_FAMILYsession_events_totalSESSION_EVENTS_FAMILYsession_duration_seconds_totalSESSION_DURATION_FAMILYactive_sessionsACTIVE_SESSIONS_FAMILY)MappingSequence)Metric)
MetricTypec                  h    e Zd ZdZed	d       Zed	d       Zed	d       Zed	d       Zd	dZ	d
dZ
y)StatzProtocol for a stat that can be serialized to OpenMetrics format.

    All stats must have these fields to identify the metric family they belong to
    and provide metadata for OpenMetrics serialization.
    c                     y)z:The name of the metric family (e.g. 'cache_memory_bytes').N selfs    c/var/www/html/chatbot/card-advisor-bot/venv/lib/python3.12/site-packages/streamlit/runtime/stats.pyfamily_namezStat.family_name,        	    c                     y)z/The OpenMetrics type (e.g. 'gauge', 'counter').Nr   r   s    r   typez	Stat.type1   r   r   c                     y)z&The unit of the metric (e.g. 'bytes').Nr   r   s    r   unitz	Stat.unit6   r   r   c                     y)zA description of the metric.Nr   r   s    r   helpz	Stat.help;   r   r   c                     y)z5Convert this stat to an OpenMetrics-formatted string.Nr   r   s    r   to_metric_strzStat.to_metric_str@       r   c                     y-Fill an OpenMetrics `Metric` protobuf object.Nr   )r   metrics     r   marshall_metric_protozStat.marshall_metric_protoD   r'   r   Nreturnstrr+   MetricProtor.   None)__name__
__module____qualname____doc__propertyr   r    r"   r$   r&   r,   r   r   r   r   r   $   sa            r   r   c                      e Zd ZU dZded<   ded<   ded<   edd       Zedd       Zedd	       Zedd
       Z	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.
    r/   category_name
cache_nameintbyte_lengthc                    t         S N)r
   r   s    r   r   zCacheStat.family_nameb   s    ""r   c                     yNgauger   r   s    r   r    zCacheStat.typef       r   c                     y)Nbytesr   r   s    r   r"   zCacheStat.unitj   rC   r   c                     y)Nz!Total memory consumed by a cache.r   r   s    r   r$   zCacheStat.helpn   s    2r   c                l    | j                    d| j                   d| j                   d| j                   S )Nz{cache_type="z	",cache="z"} )r   r:   r;   r=   r   s    r   r&   zCacheStat.to_metric_strr   s;    ""#>$2D2D1EYtN__cdhdtdtcuvvr   c                6   |j                   j                         }d|_        | j                  |_        |j                   j                         }d|_        | j
                  |_        |j                  j                         }| j                  |j                  _	        y)r*   
cache_typecacheN)
labelsaddnamer:   valuer;   metric_pointsr=   gauge_value	int_value)r   r+   labelmetric_points       r   r,   zCacheStat.marshall_metric_protou   st    !!#!
((!!#
oo++//1-1-=-=  *r   Nr-   r0   )r3   r4   r5   r6   __annotations__r7   r   r    r"   r$   r&   r,   r   r   r   r9   r9   L   su      O # #     3 3w>r   r9   c                    dd}g }t        | |      }t        j                  ||      }|D ]5  \  \  }}}|j                  t	        ||t        d |D                           7 |S )zOGroup a list of CacheStats by category_name and cache_name and sum byte_length.c                2    | j                   | j                  fS r?   )r:   r;   )individual_stats    r   key_functionz'group_cache_stats.<locals>.key_function   s    ,,o.H.HHHr   )keyc              3  4   K   | ]  }|j                     y wr?   )r=   ).0items     r   	<genexpr>z$group_cache_stats.<locals>.<genexpr>   s     PT 0 0Ps   )r:   r;   r=   )rW   r9   r.   ztuple[str, str])sorted	itertoolsgroupbyappendr9   sum)statsrX   resultsorted_statsgrouped_statsr:   r;   single_group_statss           r   group_cache_statsrh      sw    I !F%\2L%%lEM;H 
7#
%7+%P=OPP	

 Mr   c                  r    e Zd ZU dZded<   ded<   dZded<   d	Zded
<   d	Zded<   edd       Z	ddZ
ddZy)CounterStata  A counter stat.

    Properties
    ----------
    family_name : str
        The name of the metric family (e.g. 'session_events_total').
    value : int
        The current count value.
    labels : dict[str, str] | None
        Labels to identify this specific counter (e.g. {"type": "connection"}).
    unit : str
        The unit of the metric (e.g. '' for unitless).
    help : str
        A description of the metric.
    r/   r   r<   rN   Ndict[str, str] | NonerK    r"   r$   c                     y)Ncounterr   r   s    r   r    zCounterStat.type   s    r   c                    | j                   rWdj                  d t        | j                   j                               D              }| j                   d| d| j
                   S | j                   d| j
                   S )N,c              3  2   K   | ]  \  }}| d | d  ywz=""Nr   r[   kvs      r   r]   z,CounterStat.to_metric_str.<locals>.<genexpr>         !W41aQCr!A,!W   {}  rK   joinr^   itemsr   rN   r   
labels_strs     r   r&   zCounterStat.to_metric_str   m    ;;!W6$++BSBSBU;V!WWJ&&'r*SEE""#1TZZL11r   c                ,   | j                   rSt        | j                   j                               D ]-  \  }}|j                   j                         }||_        ||_        / |j                  j                         }| j
                  |j                  _        yr)   )	rK   r^   r~   rL   rM   rN   rO   counter_valuerQ   r   r+   rM   rN   rR   rS   s         r   r,   z!CounterStat.marshall_metric_proto   sv    ;;%dkk&7&7&9: $e))+!
#$
 ++//1/3zz"",r   r-   r0   r3   r4   r5   r6   rT   rK   r"   r$   r7   r    r&   r,   r   r   r   rj   rj      sM      J$(F!(D#ND#N 2	:r   rj   c                  r    e Zd ZU dZded<   ded<   dZded<   d	Zded
<   d	Zded<   edd       Z	ddZ
ddZy)	GaugeStata  A gauge stat.

    Properties
    ----------
    family_name : str
        The name of the metric family (e.g. 'active_sessions').
    value : int
        The current gauge value.
    labels : dict[str, str] | None
        Optional labels to identify this specific gauge.
    unit : str
        The unit of the metric (e.g. '' for unitless).
    help : str
        A description of the metric.
    r/   r   r<   rN   Nrk   rK   rl   r"   r$   c                     yrA   r   r   s    r   r    zGaugeStat.type   rC   r   c                    | j                   rWdj                  d t        | j                   j                               D              }| j                   d| d| j
                   S | j                   d| j
                   S )Nrp   c              3  2   K   | ]  \  }}| d | d  ywrr   r   rt   s      r   r]   z*GaugeStat.to_metric_str.<locals>.<genexpr>   rw   rx   ry   rz   r{   r|   r   s     r   r&   zGaugeStat.to_metric_str   r   r   c                ,   | j                   rSt        | j                   j                               D ]-  \  }}|j                   j                         }||_        ||_        / |j                  j                         }| j
                  |j                  _        yr)   )	rK   r^   r~   rL   rM   rN   rO   rP   rQ   r   s         r   r,   zGaugeStat.marshall_metric_proto   sv    ;;%dkk&7&7&9: $e))+!
#$
 ++//1-1ZZ  *r   r-   r0   r   r   r   r   r   r      sM      J$(F!(D#ND#N 2	8r   r   c                b    ddl m}m}m}m}m}m}m}m} |||||||d}	|	j                  | |      S )zCConvert a metric type string to its corresponding proto enum value.r   )COUNTERGAUGEGAUGE_HISTOGRAM	HISTOGRAMINFO	STATE_SETSUMMARYUNKNOWN)rB   rn   	state_setinfo	histogramgauge_histogramsummary)
*streamlit.proto.openmetrics_data_model_pb2r   r   r   r   r   r   r   r   get)
type_stringr   r   r   r   r   r   r   r   type_maps
             r   metric_type_string_to_protor      sB    	 	 	 *H <<W--r   c                  0    e Zd Zedd       Z	 d	 	 	 ddZy)StatsProviderc                     y)zThe metric family names that this provider supports.

        The StatsManager uses this property to determine which providers to call
        when specific metric families are requested.
        Nr   r   s    r   stats_familieszStatsProvider.stats_families  s     	r   Nc                    t         )aY  Return stats for the specified metric families.

        Parameters
        ----------
        family_names : Sequence[str] | None
            If provided, only stats for these metric families should be computed
            and returned. If None, stats for all families this provider supports
            should be returned. Providers should check this parameter and skip
            computing expensive stats for families that aren't requested.

        Returns
        -------
        Mapping[str, Sequence[Stat]]
            A mapping from metric family names to sequences of Stat objects.
        )NotImplementedError)r   family_namess     r   	get_statszStatsProvider.get_stats  s
    $ "!r   )r.   zSequence[str]r?   r   zSequence[str] | Noner.   zMapping[str, Sequence[Stat]])r3   r4   r5   r7   r   r   r   r   r   r   r     s.      48"0"	%"r   r   c                  .    e Zd ZddZddZ	 d	 	 	 ddZy)	StatsManagerc                    i | _         y r?   )_providers_by_familyr   s    r   __init__zStatsManager.__init__-  s
    DF!r   c                    |j                   D ]=  }|| j                  vrg | j                  |<   | j                  |   j                  |       ? y)ac  Register a StatsProvider with the manager.

        This function is not thread-safe. Call it immediately after creation.

        Parameters
        ----------
        provider : StatsProvider
            The stats provider to register. The provider's `stats_families`
            property determines which metric families it will be called for.
        N)r   r   ra   )r   providerfamilys      r   register_providerzStatsManager.register_provider0  sP     -- 	?FT66646))&1%%f-44X>	?r   Nc                   i }|r|n"t        | j                  j                               }t               }|D ]  }| j                  j	                  |g       D ]r  }t        |      }||v r|j                  |       |j                  |      }|j                         D ]+  \  }	}
|	|vrg ||	<   t        ||	         t        |
      z   ||	<   - t  |S )a  Return registered stats grouped by metric family name.

        Parameters
        ----------
        family_names : Sequence[str] | None
            If provided, only stats for these metric families will be returned.
            If None, stats for all registered families are returned.

        Returns
        -------
        Mapping[str, Sequence[Stat]]
            A mapping from metric family names to sequences of Stat objects
            from all providers.
        )	listr   keyssetr   idrL   r   r~   )r   r   rd   families_to_queryqueried_providersr   r   provider_idprovider_statsfnamerc   s              r   r   zStatsManager.get_stats@  s    " -/ )Ld43L3L3Q3Q3S.T 	 '*e' 	FF 5599&"E F l"33!%%k2!)!3!3L!A$2$8$8$: FLE5F*(*u %)$7$u+$EF5MFF	F  r   )r.   r2   )r   r   r.   r2   r?   r   )r3   r4   r5   r   r   r   r   r   r   r   r   ,  s'    G?" 48-0-	%-r   r   )rc   list[CacheStat]r.   r   )r   r/   r.   zMetricType.ValueType)
__future__r   r_   typingr   r   r   r   r   r
   rT   r   r   r   collections.abcr   r   r   r   r1   r   r   r9   rh   rj   r   r   r   r   r   r   r   <module>r      s    #  P P1 U 15 u 5!A  A 1  11
 !8 ! !N4>
 4>n,*:* *:Z*8
 *8Z.2 "H " ">A Ar   