o
    `®f|  ã                   @   sø   d dl mZ ddlmZmZmZmZ G dd„ deƒZG dd„ deƒZG dd	„ d	eƒZ	G d
d„ deƒZ
G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    )ÚUnioné   )ÚAscÚDescÚReducerÚSortDirectionc                       s*   e Zd ZdZdeddf‡ fdd„Z‡  ZS )ÚFieldOnlyReducerzHSee https://redis.io/docs/interact/search-and-query/search/aggregations/ÚfieldÚreturnNc                    s   t ƒ  |¡ || _d S ©N)ÚsuperÚ__init__Ú_field©Úselfr	   ©Ú	__class__© úW/home/ubuntu/webapp/venv/lib/python3.10/site-packages/redis/commands/search/reducers.pyr   	   s   
zFieldOnlyReducer.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr   Ú__classcell__r   r   r   r   r      s    r   c                       s&   e Zd ZdZdZd‡ fdd„Z‡  ZS )Úcountz3
    Counts the number of results in the group
    ÚCOUNTr
   Nc                    s   t ƒ  ¡  d S r   ©r   r   )r   r   r   r   r      s   zcount.__init__)r
   N)r   r   r   r   ÚNAMEr   r   r   r   r   r   r      s    r   c                       ó.   e Zd ZdZdZdeddf‡ fdd„Z‡  ZS )ÚsumzS
    Calculates the sum of all the values in the given fields within the group
    ÚSUMr	   r
   Nc                    ó   t ƒ  |¡ d S r   r   r   r   r   r   r       ó   zsum.__init__©r   r   r   r   r   r   r   r   r   r   r   r   r       ó    r    c                       r   )ÚminzK
    Calculates the smallest value in the given field within the group
    ÚMINr	   r
   Nc                    r"   r   r   r   r   r   r   r   +   r#   zmin.__init__r$   r   r   r   r   r&   $   r%   r&   c                       r   )ÚmaxzJ
    Calculates the largest value in the given field within the group
    ÚMAXr	   r
   Nc                    r"   r   r   r   r   r   r   r   6   r#   zmax.__init__r$   r   r   r   r   r(   /   r%   r(   c                       r   )ÚavgzG
    Calculates the mean value in the given field within the group
    ÚAVGr	   r
   Nc                    r"   r   r   r   r   r   r   r   A   r#   zavg.__init__r$   r   r   r   r   r*   :   r%   r*   c                       r   )Útolistz6
    Returns all the matched properties in a list
    ÚTOLISTr	   r
   Nc                    r"   r   r   r   r   r   r   r   L   r#   ztolist.__init__r$   r   r   r   r   r,   E   r%   r,   c                       r   )Úcount_distinctzs
    Calculate the number of distinct values contained in all the results in
    the group for the given field
    ÚCOUNT_DISTINCTr	   r
   Nc                    r"   r   r   r   r   r   r   r   X   r#   zcount_distinct.__init__r$   r   r   r   r   r.   P   s    r.   c                   @   s   e Zd ZdZdZdS )Úcount_distinctishzÀ
    Calculate the number of distinct values contained in all the results in the
    group for the given field. This uses a faster algorithm than
    `count_distinct` but is less accurate
    ÚCOUNT_DISTINCTISHN)r   r   r   r   r   r   r   r   r   r0   \   s    r0   c                       ó2   e Zd ZdZdZdededdf‡ fdd„Z‡  ZS )	Úquantilezp
    Return the value for the nth percentile within the range of values for the
    field within the group.
    ÚQUANTILEr	   Úpctr
   Nc                    s   t ƒ  |t|ƒ¡ || _d S r   )r   r   r   r   )r   r	   r5   r   r   r   r   n   s   
zquantile.__init__)	r   r   r   r   r   r   Úfloatr   r   r   r   r   r   r3   f   s    "r3   c                       r   )ÚstddevzG
    Return the standard deviation for the values within the group
    ÚSTDDEVr	   r
   Nc                    r"   r   r   r   r   r   r   r   z   r#   zstddev.__init__r$   r   r   r   r   r7   s   r%   r7   c                       s:   e Zd ZdZdZdedeeef ddf‡ fdd„Z	‡  Z
S )	Úfirst_valuezR
    Selects the first value within the group according to sorting parameters
    ÚFIRST_VALUEr	   Úbyfieldsr
   Nc                    s‚   g }t |ƒdkrt|d tƒrt|d tƒr|d |ƒg}|D ]
}||j|jg7 }q|g}|r6|dg| 7 }tƒ j|Ž  || _	dS )aô  
        Selects the first value of the given field within the group.

        ### Parameter

        - **field**: Source field used for the value
        - **byfields**: How to sort the results. This can be either the
            *class* of `aggregation.Asc` or `aggregation.Desc` in which
            case the field `field` is also used as the sort input.

            `byfields` can also be one or more *instances* of `Asc` or `Desc`
            indicating the sort order for these fields
        r   r   ÚBYN)
ÚlenÚ
isinstanceÚtypeÚ
issubclassr   r	   Ú	DIRSTRINGr   r   r   )r   r	   r;   Ú	fieldstrsÚfÚargsr   r   r   r   …   s   ÿþ
zfirst_value.__init__)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r9   ~   s    *r9   c                       r2   )	Úrandom_samplezT
    Returns a random sample of items from the dataset, from the given property
    ÚRANDOM_SAMPLEr	   Úsizer
   Nc                    s"   |t |ƒg}tƒ j|Ž  || _dS )z
        ### Parameter

        **field**: Field to sample from
        **size**: Return this many items (can be less)
        N)r   r   r   r   )r   r	   rG   rD   r   r   r   r   ­   s   
zrandom_sample.__init__)	r   r   r   r   r   r   Úintr   r   r   r   r   r   rE   ¦   s    "rE   N)Útypingr   Úaggregationr   r   r   r   r   r   r    r&   r(   r*   r,   r.   r0   r3   r7   r9   rE   r   r   r   r   Ú<module>   s    
(