o
    i܆                     @   s  d dl mZmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZmZmZmZ d dlmZmZmZmZmZmZmZmZmZ d d	lmZ d d
lm Z  d dl!m"Z" d dl#m$Z$ d dl%mZ&m'Z'm(Z(mZ)m*Z*m+Z+m,Z, d dl-m.Z. ede+e*Z/ee0Z1G dd deZ2G dd deee/ Z3G dd de3Z4G dd de3e, Z5G dd de3e( Z6G dd de3e) Z7G dd de3e) Z8G dd deZ9G d d! d!e9Z:G d"d# d#e9Z;G d$d% d%e9Z<G d&d' d'e9Z=G d(d) d)e9Z>G d*d+ d+e9Z?d,S )-    )ABCabstractmethod)bisect_left)IntEnum)	getLogger)inf)Lock)GenericListOptionalSequenceTypeVar)	AsynchronousCounter	Histogram
InstrumentObservableCounterObservableGaugeObservableUpDownCounterSynchronousUpDownCounter)Buckets)ExponentMapping)LogarithmMapping)Measurement)r   ExponentialHistogramDataPointGauger   HistogramDataPointNumberDataPointSum)
Attributes_DataPointVarTc                   @   s   e Zd ZdZdZdZdZdS )AggregationTemporalityz`
    The temporality to use when aggregating data.

    Can be one of the following values:
    r         N)__name__
__module____qualname____doc__UNSPECIFIEDDELTA
CUMULATIVE r,   r,   p/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/mysql/opentelemetry/sdk/metrics/_internal/aggregation.pyr"   >   s
    r"   c                   @   sN   e Zd ZdefddZededdfddZed	ed
e	de
e fddZdS )_Aggregation
attributesc                 C   s   t  | _|| _d | _d S N)r   _lock_attributes_previous_pointselfr/   r,   r,   r-   __init__K   s   
z_Aggregation.__init__measurementreturnNc                 C      d S r0   r,   r5   r7   r,   r,   r-   	aggregateP   s   z_Aggregation.aggregateaggregation_temporalitycollection_start_nanoc                 C   r9   r0   r,   r5   r<   r=   r,   r,   r-   collectT   s   z_Aggregation.collect)r%   r&   r'   r    r6   r   r   r;   r"   intr   r!   r?   r,   r,   r,   r-   r.   J   s    r.   c                   @   s8   e Zd ZdeddfddZdededee fdd	Z	dS )
_DropAggregationr7   r8   Nc                 C   r9   r0   r,   r:   r,   r,   r-   r;   ^   s   z_DropAggregation.aggregater<   r=   c                 C   r9   r0   r,   r>   r,   r,   r-   r?   a   s   z_DropAggregation.collect)
r%   r&   r'   r   r;   r"   r@   r   r!   r?   r,   r,   r,   r-   rA   ]   s    rA   c                       sZ   e Zd Zdedededef fddZdedd	fd
dZ	dedede
e fddZ  ZS )_SumAggregationr/   instrument_is_monotonicinstrument_temporalitystart_time_unix_nanoc                    s>   t  | || _|| _|| _| jtju rd| _d S d | _d S Nr   )superr6   _start_time_unix_nano_instrument_temporality_instrument_is_monotonicr"   r*   _value)r5   r/   rC   rD   rE   	__class__r,   r-   r6   j   s   

z_SumAggregation.__init__r7   r8   Nc                 C   sJ   | j  | jd u rd| _| j|j | _W d    d S 1 sw   Y  d S rF   )r1   rK   valuer:   r,   r,   r-   r;   |   s
   
"z_SumAggregation.aggregater<   r=   c                 C   s&  | j tju r&| j | j}| j}d| _|| _W d   n1 s w   Y  n*| j | jdu r8	 W d   dS | j}d| _| j}W d   n1 sKw   Y  t| j|||d}| jdu sc| j |u rh|| _|S |tju ry|j	| jj	 }| jj
}n|j	| jj	 }| jj}t| j||j
|d}|| _|S )zx
        Atomically return a point for the current value of the metric and
        reset the aggregation value.
        r   Nr/   rE   time_unix_nanorN   )rI   r"   r*   r1   rK   rH   r   r2   r3   rN   rP   rE   )r5   r<   r=   rN   rE   current_pointoutput_start_time_unix_nanor,   r,   r-   r?      sN   	




z_SumAggregation.collect)r%   r&   r'   r    boolr"   r@   r6   r   r;   r   r   r?   __classcell__r,   r,   rL   r-   rB   i   s$    rB   c                       sJ   e Zd Zdef fddZdefddZdeded	e	e
 fd
dZ  ZS )_LastValueAggregationr/   c                    s   t  | d | _d S r0   )rG   r6   rK   r4   rL   r,   r-   r6      s   
z_LastValueAggregation.__init__r7   c                 C   s4   | j  |j| _W d    d S 1 sw   Y  d S r0   )r1   rN   rK   r:   r,   r,   r-   r;      s   
"z_LastValueAggregation.aggregater<   r=   r8   c                 C   s`   | j  | jdu r	 W d   dS | j}d| _W d   n1 s"w   Y  t| jd||dS )P
        Atomically return a point for the current value of the metric.
        Nr   rO   )r1   rK   r   r2   )r5   r<   r=   rN   r,   r,   r-   r?      s   
z_LastValueAggregation.collect)r%   r&   r'   r    r6   r   r;   r"   r@   r   r!   r?   rT   r,   r,   rL   r-   rU      s    rU   c                	       sv   e Zd Z		ddededee def fddZd	e	e fd
dZ
ded	dfddZdeded	ee fddZ  ZS )#_ExplicitBucketHistogramAggregationg        g      @g      $@g      9@g      I@g     R@g      Y@g     @o@g     @@g     p@g     @@g     @g     @g     L@g     @Tr/   rE   
boundariesrecord_min_maxc                    sL   t  | t|| _|  | _t| _t | _d| _	|| _
|| _tj| _d S rF   )rG   r6   tuple_boundaries_get_empty_bucket_counts_bucket_countsr   _min_max_sum_record_min_maxrH   r"   r*   rI   )r5   r/   rE   rY   rZ   rL   r,   r-   r6      s   

z,_ExplicitBucketHistogramAggregation.__init__r8   c                 C   s   dgt | jd  S Nr   r#   )lenr\   )r5   r,   r,   r-   r]     s   z<_ExplicitBucketHistogramAggregation._get_empty_bucket_countsr7   Nc                 C   sT   |j }| jrt| j|| _t| j|| _|  j|7  _| jt| j	|  d7  < d S )Nr#   )
rN   rb   minr_   maxr`   ra   r^   r   r\   )r5   r7   rN   r,   r,   r-   r;     s   z-_ExplicitBucketHistogramAggregation.aggregater<   r=   c           	      C   s  | j 7 t| js	 W d   dS | j}| j}| j}| j}| j}|  | _|| _d| _t| _t | _W d   n1 s=w   Y  t	| j
||t||t|| j||d	}| jdu s_| j|u rd|| _|S |j}|j}|tju r| jj}|j| jj }t|j| jj}t|j| jj}dd t|j| jjD }n| jj}|j| jj }dd t|j| jjD }t	| j
||jt||t||j||d	}|| _|S )rV   Nr   )	r/   rE   rP   countsumbucket_countsexplicit_boundsre   rf   c                 S   s   g | ]\}}|| qS r,   r,   .0
curr_count
prev_countr,   r,   r-   
<listcomp>E      z?_ExplicitBucketHistogramAggregation.collect.<locals>.<listcomp>c                 S   s   g | ]\}}|| qS r,   r,   rk   r,   r,   r-   ro   O  rp   )r1   anyr^   rH   ra   r`   r_   r]   r   r   r2   rh   r[   r\   r3   rI   rf   re   r"   r+   rE   zipri   rP   rj   )	r5   r<   r=   ri   rE   sum_max_min_rQ   r,   r,   r-   r?     s   





z+_ExplicitBucketHistogramAggregation.collectrX   T)r%   r&   r'   r    r@   r   floatrS   r6   r
   r]   r   r;   r"   r   r!   r?   rT   r,   r,   rL   r-   rW      s*    %rW   c                       s   e Zd ZdZdZ	ddededef fddZd	ed
dfddZ	de
ded
ee fddZdd Zdd Zdd ZdefddZdd Z  ZS )&_ExponentialBucketHistogramAggregationr$   i @     r/   rE   max_sizec                    s   t  | || jk rtd| d|| jkr td| d|| _d| _d| _d| _t	| _
t	 | _t | _t | _ttj| _tj| _|| _d | _d | _d | _d | _d | _d | _d | _d S )NzBuckets max size z6 is smaller than minimum max size {self._min_max_size}z5 is larger than maximum max size {self._max_max_size}r   )rG   r6   _min_max_size
ValueError_max_max_size	_max_sizera   _count_zero_countr   r_   r`   r   	_positive	_negativer   
_max_scale_mappingr"   r*   rI   rH   _previous_scale_previous_start_time_unix_nano_previous_sum_previous_max_previous_min_previous_positive_previous_negative)r5   r/   rE   rz   rL   r,   r-   r6   s  s6   





z/_ExponentialBucketHistogramAggregation.__init__r7   r8   Nc           
      C   s  | j  |j}|| jk r|| _|| jkr|| _|  jd7  _|dkr2|  jd7  _	 W d    d S |  j|7  _|dkrA| j}n| }| j}| j	
|}d}t|dkr_||_||_||_n)||jk rt|j| | jkrtd}|}|j}n||jkr||j | jkrd}|j}|}|r| | ||| j| j | j	
|}||jk r|j| }|t|jkr||d | j ||_n||jkr||j }|t|jkr||d | j ||_||j }	|	dk r|	t|j7 }	||	 W d    d S 1 sw   Y  d S )Nr#   r   FT)r1   rN   r_   r`   r   r   ra   r   r   r   map_to_indexrd   index_start	index_end
index_baser~   
_downscale_get_scale_changecountsgrowincrement_bucket)
r5   r7   rN   bucketsindexis_rescaling_neededlowhighspanbucket_indexr,   r,   r-   r;     sl   









"z0_ExponentialBucketHistogramAggregation.aggregater<   r=   c                 C   s  | j ] | jdkr	 W d   dS | j}| j}| j}| j}| j}| j}| j}	|	t kr/d}	| j	}
|
tkr8d}
| j| jkrAd}n| j
j}t | _t | _|| _d| _d| _d| _t| _	t | _t| j||||||t|j|jdt|j|jdd|
|	d}| jdu s| j|u r|| _|| _|	| _|
| _|| _|| _|| _|W  d   S t| j|}| | j||\}}| | j||\}}t|| || || || }| | j
j| | j| j |tju r| j}|| j }t |	| j}t|
| j}| !| j|||| | !| j|||| n | j}|| j }|	}|
}| !| j|||| | !| j|||| t| j||||||t|j|jdt|j|jdd||d}|| _|| _|| _|| _|| _|W  d   S 1 sew   Y  dS )rV   r   N)offsetri   )r/   rE   rP   rg   rh   scale
zero_countpositivenegativeflagsre   rf   )"r1   r   r   r   r   rH   ra   r`   r   r_   r   r   r   r   r2   BucketsPointr   r   r   rI   r   r   r   r   r   r   re   _get_low_high_previous_currentr   r   r"   r+   rf   _merge)r5   r<   r=   current_negativecurrent_positivecurrent_zero_countcurrent_countcurrent_start_time_unix_nanocurrent_sumcurrent_maxcurrent_mincurrent_scalerQ   	min_scalelow_positivehigh_positivelow_negativehigh_negativerE   rs   rt   ru   r,   r,   r-   r?     s  





C

	
 &z._ExponentialBucketHistogramAggregation.collectc           
      C   sl   |  ||\}}|  ||\}}||kr|}|}	||	fS ||kr(|}|}	||	fS t||}t||}	||	fS r0   )_get_low_highre   rf   )
r5   previous_point_bucketscurrent_point_bucketsr   previous_point_lowprevious_point_highcurrent_point_lowcurrent_point_highr   r   r,   r,   r-   r     s"   


zE_ExponentialBucketHistogramAggregation._get_low_high_previous_currentc                 C   s0   |j dgkrdS | jj| }|j|? |j|? fS )Nr   )r   )r   r   _scaler   r   )r5   r   r   shiftr,   r,   r-   r     s   z4_ExponentialBucketHistogramAggregation._get_low_highc                 C   s<   d}|| | j kr|d? }|d? }|d7 }|| | j ks	|S rc   r~   )r5   r   r   changer,   r,   r-   r     s   z8_ExponentialBucketHistogramAggregation._get_scale_changer   c                 C   s`   |dkrd S |dk rt d| jj| }|| || |dkr't|}nt|}|| _d S )Nr   zInvalid change of scale)	Exceptionr   r   	downscaler   r   )r5   r   r   r   	new_scalemappingr,   r,   r-   r     s   



z1_ExponentialBucketHistogramAggregation._downscalec                 C   s  || }t |jD ]{\}}|dkrq	|j| |? }	|	|jk r?|j|	 }
|
| jkr,td|
t|jkr<||
d | j |	|_|	|jkre|	|j }
|
| jkrRtd|
t|jkrb||
d | j |	|_|	|j	 }|dk ru|t|j7 }|t
ju r}| }|j||d q	d S )Nr   zIncorrect merge scaler#   )	increment)	enumerater   r   r   r   r~   r   rd   r   r   r"   r*   r   )r5   previous_bucketscurrent_bucketsr   r   r<   current_changecurrent_bucket_indexcurrent_bucketr   r   r   r,   r,   r-   r     s4   







z-_ExponentialBucketHistogramAggregation._mergery   )r%   r&   r'   r{   r}   r    r@   r6   r   r;   r"   r   r!   r?   r   r   r   r   r   rT   r,   r,   rL   r-   rx   g  s2    
@j
 .rx   c                	   @   s.   e Zd ZdZededededefddZ	dS )	Aggregationz/
    Base class for all aggregation types.
    
instrumentr/   rE   r8   c                 C   s   dS )zCreates an aggregationNr,   r5   r   r/   rE   r,   r,   r-   _create_aggregationB  s    zAggregation._create_aggregationN)
r%   r&   r'   r(   r   r   r    r@   r.   r   r,   r,   r,   r-   r   =  s    r   c                   @   *   e Zd ZdZdedededefddZdS )	DefaultAggregationa  
    The default aggregation to be used in a `View`.

    This aggregation will create an actual aggregation depending on the
    instrument type, as specified next:

    ==================================================== ====================================
    Instrument                                           Aggregation
    ==================================================== ====================================
    `mysql.opentelemetry.sdk.metrics.Counter`                  `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.UpDownCounter`            `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.ObservableCounter`        `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.ObservableUpDownCounter`  `SumAggregation`
    `mysql.opentelemetry.sdk.metrics.Histogram`                `ExplicitBucketHistogramAggregation`
    `mysql.opentelemetry.sdk.metrics.ObservableGauge`          `LastValueAggregation`
    ==================================================== ====================================
    r   r/   rE   r8   c                 C   s   t |trt|dtj|dS t |trt|dtj|dS t |tr*t|dtj|dS t |tr8t|dtj|dS t |t	rBt
||S t |trKt|S tdt| d)NT)rC   rD   rE   FzInvalid instrument type z found)
isinstancer   rB   r"   r*   r   r   r+   r   r   rW   r   rU   r   typer   r,   r,   r-   r   _  sB   






z&DefaultAggregation._create_aggregationN	r%   r&   r'   r(   r   r    r@   r.   r   r,   r,   r,   r-   r   L  s    r   c                   @   s8   e Zd Z	ddefddZdedededefd	d
ZdS )%ExponentialBucketHistogramAggregationry   rz   c                 C   s
   || _ d S r0   r   )r5   rz   r,   r,   r-   r6     s   
z.ExponentialBucketHistogramAggregation.__init__r   r/   rE   r8   c                 C   s   t ||| jdS )N)rz   )rx   r~   r   r,   r,   r-   r     s
   z9ExponentialBucketHistogramAggregation._create_aggregationNr   )	r%   r&   r'   r@   r6   r   r    r.   r   r,   r,   r,   r-   r     s    
r   c                   @   sJ   e Zd ZdZ		ddee deddfdd	Zd
ede	de
defddZdS )"ExplicitBucketHistogramAggregationaG  This aggregation informs the SDK to collect:

    - Count of Measurement values falling within explicit bucket boundaries.
    - Arithmetic sum of Measurement values in population. This SHOULD NOT be collected when used with instruments that record negative measurements, e.g. UpDownCounter or ObservableGauge.
    - Min (optional) Measurement value in population.
    - Max (optional) Measurement value in population.


    Args:
        boundaries: Array of increasing values representing explicit bucket boundary values.
        record_min_max: Whether to record min and max.
    rX   TrY   rZ   r8   Nc                 C   s   || _ || _d S r0   )r\   rb   )r5   rY   rZ   r,   r,   r-   r6     s   
z+ExplicitBucketHistogramAggregation.__init__r   r/   rE   c                 C   s   t ||| j| jS r0   )rW   r\   rb   r   r,   r,   r-   r     s   z6ExplicitBucketHistogramAggregation._create_aggregationrv   )r%   r&   r'   r(   r   rw   rS   r6   r   r    r@   r.   r   r,   r,   r,   r-   r     s(    
r   c                   @   r   )	SumAggregationzbThis aggregation informs the SDK to collect:

    - The arithmetic sum of Measurement values.
    r   r/   rE   r8   c                 C   s@   t j}t|trt j}nt|trt j}t|t|tt	f||S r0   )
r"   r)   r   r   r*   r   r+   rB   r   r   )r5   r   r/   rE   temporalityr,   r,   r-   r     s   

z"SumAggregation._create_aggregationNr   r,   r,   r,   r-   r     s    r   c                   @   r   )	LastValueAggregationz
    This aggregation informs the SDK to collect:

    - The last Measurement.
    - The timestamp of the last Measurement.
    r   r/   rE   r8   c                 C      t |S r0   )rU   r   r,   r,   r-   r        z(LastValueAggregation._create_aggregationNr   r,   r,   r,   r-   r     s    r   c                   @   r   )	DropAggregationz=Using this aggregation will make all measurements be ignored.r   r/   rE   r8   c                 C   r   r0   )rA   r   r,   r,   r-   r     r   z#DropAggregation._create_aggregationNr   r,   r,   r,   r-   r     s    r   N)@abcr   r   bisectr   enumr   loggingr   mathr   	threadingr   typingr	   r
   r   r   r   mysql.opentelemetry.metricsr   r   r   r   r   r   r   r   r   Gmysql.opentelemetry.sdk.metrics._internal.exponential_histogram.bucketsr   Xmysql.opentelemetry.sdk.metrics._internal.exponential_histogram.mapping.exponent_mappingr   Ymysql.opentelemetry.sdk.metrics._internal.exponential_histogram.mapping.logarithm_mappingr   5mysql.opentelemetry.sdk.metrics._internal.measurementr   /mysql.opentelemetry.sdk.metrics._internal.pointr   r   r   HistogramPointr   r   r   mysql.opentelemetry.util.typesr    r!   r%   _loggerr"   r.   rA   rB   rU   rW   rx   r   r   r   r   r   r   r   r,   r,   r,   r-   <module>   sD   ,$	V 
   YB4