o
    `j                     @   sJ   d dl mZ d dlmZ G dd dZG dd deZG dd deZd	S )
    )AbstractMeasurableStat)AbstractSampledStatc                   @   sd   e Zd ZddddddddZed	 Zed
 Zed Zed Zed Zed Z	ed Z
edd ZdS )TimeUnitr                     )
nanosecondmicrosecondmillisecondsecondminutehourdayr   r   r   r   r   r   r   c                 C   s
   t j|  S N)r   _names)	time_unit r   V/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/metrics/stats/rate.pyget_name   s   
zTimeUnit.get_nameN)__name__
__module____qualname__r   NANOSECONDSMICROSECONDSMILLISECONDSSECONDSMINUTESHOURSDAYSstaticmethodr   r   r   r   r   r      s$    
r   c                   @   sL   e Zd ZdZdZejdfddZdd Zdd	 Z	d
d Z
dd Zdd ZdS )Ratea  
    The rate of the given quantity. By default this is the total observed
    over a set of samples from a sampled statistic divided by the elapsed
    time over the sample windows. Alternative AbstractSampledStat
    implementations can be provided, however, to record the rate of
    occurrences (e.g. the count of values measured over the time interval)
    or other such values.
    )_stat_unitNc                 C   s   |pt  | _|| _d S r   )SampledTotalr$   r%   )selfr   sampled_statr   r   r   __init__(   s   
zRate.__init__c                 C   s   t | jS r   )r   r   r%   )r'   r   r   r   	unit_name,   s   zRate.unit_namec                 C   s   | j ||| d S r   )r$   record)r'   configvaluetime_msr   r   r   r+   /   s   zRate.recordc                 C   s(   | j ||}t|| | || S r   )r$   measurefloatconvertwindow_size)r'   r,   nowr-   r   r   r   r/   2   s   zRate.measurec                 C   sX   | j || 	 || j |j }t||j }|jd }||k r*||| |j 7 }|S )Nr   )r$   purge_obsolete_samplesoldestlast_window_msinttime_window_mssamples)r'   r,   r3   total_elapsed_time_msnum_full_windowsmin_full_windowsr   r   r   r2   6   s   
zRate.window_sizec                 C   s   | j tjkr|d d S | j tjkr|d S | j tjkr|S | j tjkr(|d S | j tjkr2|d S | j tjkr<|d S | j tjkrF|d S t	d| j f )Ng     @@g     L@g    @wKAg    pAzUnknown unit: %s)
r%   r   r   r   r   r   r   r    r!   
ValueError)r'   r.   r   r   r   r1   X   s   zRate.convert)r   r   r   __doc__	__slots__r   r   r)   r*   r+   r/   r2   r1   r   r   r   r   r#      s    "r#   c                       s2   e Zd ZdZd	 fdd	Zdd Zdd Z  ZS )
r&   )_initial_value_samples_currentNc                    s    |d urt dt d d S )Nz+initial_value cannot be set on SampledTotalg        )r=   superr)   )r'   initial_value	__class__r   r   r)   m   s   zSampledTotal.__init__c                 C   s   | j |7  _ d S r   r-   )r'   sampler,   r-   r.   r   r   r   updater   s   zSampledTotal.updatec                 C   s   t tdd |D S )Nc                 s   s    | ]}|j V  qd S r   rG   ).0rH   r   r   r   	<genexpr>v   s    z'SampledTotal.combine.<locals>.<genexpr>)r0   sum)r'   r9   r,   r3   r   r   r   combineu   s   zSampledTotal.combiner   )r   r   r   r?   r)   rI   rM   __classcell__r   r   rE   r   r&   k   s
    r&   N)kafka.metrics.measurable_statr    kafka.metrics.stats.sampled_statr   r   r#   r&   r   r   r   r   <module>   s
    N