o
    `šjY  ã                   @   s2   d dl mZmZ d dlmZ G dd„ deeƒZdS )é    )ÚABCÚabstractmethod)ÚAbstractMeasurableStatc                   @   sz   e Zd ZdZdZdd„ Zedd„ ƒZedd„ ƒZd	d
„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ dƒZdS )ÚAbstractSampledStataf  
    An AbstractSampledStat records a single scalar value measured over
    one or more samples. Each sample is recorded over a configurable
    window. The window can be defined by number of events or elapsed
    time (or both, if both are given the window is complete when
    *either* the event count or elapsed time criterion is met).

    All the samples are combined to produce the measurement. When a
    window is complete the oldest sample is cleared and recycled to
    begin recording the next sample.

    Subclasses of this class define different statistics measured
    using this basic pattern.
    ©Ú_initial_valueÚ_samplesÚ_currentc                 C   s   || _ g | _d| _d S ©Nr   r   )ÚselfÚinitial_value© r   ú^/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/kafka/metrics/stats/sampled_stat.pyÚ__init__   s   
zAbstractSampledStat.__init__c                 C   ó   d S ©Nr   )r   ÚsampleÚconfigÚvalueÚtime_msr   r   r   Úupdate   ó   zAbstractSampledStat.updatec                 C   r   r   r   )r   Úsamplesr   Únowr   r   r   Úcombine    r   zAbstractSampledStat.combinec                 C   sH   |   |¡}| ||¡r|  ||¡}|  ||t|ƒ|¡ | jd7  _d S ©Né   )ÚcurrentÚis_completeÚ_advancer   ÚfloatÚevent_count)r   r   r   r   r   r   r   r   Úrecord$   s
   
zAbstractSampledStat.recordc                 C   s   |   | j|¡S r   )ÚSampler   ©r   r   r   r   r   Ú
new_sample+   s   zAbstractSampledStat.new_samplec                 C   s    |   ||¡ t|  | j||¡ƒS r   )Úpurge_obsolete_samplesr    r   r   )r   r   r   r   r   r   Úmeasure.   s   zAbstractSampledStat.measurec                 C   s$   | j s| j  |  |¡¡ | j | j S r   )r   Úappendr%   r	   r$   r   r   r   r   2   s   zAbstractSampledStat.currentc                 C   sJ   | j s| j  |  |¡¡ | j d }| j dd … D ]
}|j|jk r"|}q|S )Nr   r   )r   r(   r%   Úlast_window_ms)r   r   Úoldestr   r   r   r   r*   7   s   
€zAbstractSampledStat.oldestc                 C   s4   |j |j }| jD ]}||j |kr| |¡ q	dS )zT
        Timeout any windows that have expired in the absence of any events
        N)r   Útime_window_msr   r)   Úreset)r   r   r   Ú
expire_ager   r   r   r   r&   @   s   

€þz*AbstractSampledStat.purge_obsolete_samplesc                 C   sT   | j d |j | _ | j t| jƒkr|  |¡}| j |¡ |S |  |¡}| |¡ |S r   )r	   r   Úlenr   r%   r(   r   r,   )r   r   r   r   r   r   r   r   I   s   


zAbstractSampledStat._advancec                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )zAbstractSampledStat.Samplec                 C   s   || _ d| _|| _|| _d S r
   )r   r!   r)   r   )r   r   r   r   r   r   r   V   s   
z#AbstractSampledStat.Sample.__init__c                 C   s   d| _ || _| j| _d S r
   )r!   r)   r   r   )r   r   r   r   r   r,   \   s   z AbstractSampledStat.Sample.resetc                 C   s   || j  |jkp| j|jkS r   )r)   r+   r!   Úevent_window)r   r   r   r   r   r   r   a   s   
ÿz&AbstractSampledStat.Sample.is_completeN)Ú__name__Ú
__module__Ú__qualname__r   r,   r   r   r   r   r   r#   T   s    r#   N)r0   r1   r2   Ú__doc__Ú	__slots__r   r   r   r   r"   r%   r'   r   r*   r&   r   r#   r   r   r   r   r      s     

		r   N)Úabcr   r   Úkafka.metrics.measurable_statr   r   r   r   r   r   Ú<module>   s    