U
    Œàôg|  ã                   @   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eddœ‡ fdd„Z‡  ZS )ÚFieldOnlyReducerzHSee https://redis.io/docs/interact/search-and-query/search/aggregations/N©ÚfieldÚreturnc                    s   t ƒ  |¡ || _d S ©N)ÚsuperÚ__init__Ú_field©Úselfr
   ©Ú	__class__© úB/tmp/pip-unpacked-wheel-f3sx1i9r/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dœ‡ fdd„Z‡  ZS )Úcountz3
    Counts the number of results in the group
    ZCOUNTN)r   c                    s   t ƒ  ¡  d S r   ©r   r   )r   r   r   r   r      s    zcount.__init__)r   r   r   r   ÚNAMEr   r   r   r   r   r   r      s   r   c                       s,   e Zd ZdZdZeddœ‡ fdd„Z‡  ZS )ÚsumzS
    Calculates the sum of all the values in the given fields within the group
    ZSUMNr	   c                    s   t ƒ  |¡ d S r   r   r   r   r   r   r       s    zsum.__init__©r   r   r   r   r   r   r   r   r   r   r   r   r      s   r   c                       s,   e Zd ZdZdZeddœ‡ fdd„Z‡  ZS )ÚminzK
    Calculates the smallest value in the given field within the group
    ZMINNr	   c                    s   t ƒ  |¡ d S r   r   r   r   r   r   r   +   s    zmin.__init__r    r   r   r   r   r!   $   s   r!   c                       s,   e Zd ZdZdZeddœ‡ fdd„Z‡  ZS )ÚmaxzJ
    Calculates the largest value in the given field within the group
    ÚMAXNr	   c                    s   t ƒ  |¡ d S r   r   r   r   r   r   r   6   s    zmax.__init__r    r   r   r   r   r"   /   s   r"   c                       s,   e Zd ZdZdZeddœ‡ fdd„Z‡  ZS )ÚavgzG
    Calculates the mean value in the given field within the group
    ZAVGNr	   c                    s   t ƒ  |¡ d S r   r   r   r   r   r   r   A   s    zavg.__init__r    r   r   r   r   r$   :   s   r$   c                       s,   e Zd ZdZdZeddœ‡ fdd„Z‡  ZS )Útolistz6
    Returns all the matched properties in a list
    ZTOLISTNr	   c                    s   t ƒ  |¡ d S r   r   r   r   r   r   r   L   s    ztolist.__init__r    r   r   r   r   r%   E   s   r%   c                       s,   e Zd ZdZdZeddœ‡ fdd„Z‡  ZS )Úcount_distinctzs
    Calculate the number of distinct values contained in all the results in
    the group for the given field
    ZCOUNT_DISTINCTNr	   c                    s   t ƒ  |¡ d S r   r   r   r   r   r   r   X   s    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
    ZCOUNT_DISTINCTISHN)r   r   r   r   r   r   r   r   r   r'   \   s   r'   c                       s.   e Zd ZdZdZeeddœ‡ 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.
    ZQUANTILEN)r
   Úpctr   c                    s   t ƒ  |t|ƒ¡ || _d S r   )r   r   r   r   )r   r
   r)   r   r   r   r   n   s    zquantile.__init__)	r   r   r   r   r   r   Úfloatr   r   r   r   r   r   r(   f   s   r(   c                       s,   e Zd ZdZdZeddœ‡ fdd„Z‡  ZS )ÚstddevzG
    Return the standard deviation for the values within the group
    ZSTDDEVNr	   c                    s   t ƒ  |¡ d S r   r   r   r   r   r   r   z   s    zstddev.__init__r    r   r   r   r   r+   s   s   r+   c                       s6   e Zd ZdZdZeeeef ddœ‡ fdd„Z	‡  Z
S )Úfirst_valuezR
    Selects the first value within the group according to sorting parameters
    ZFIRST_VALUEN)r
   Úbyfieldsr   c                    s‚   g }t |ƒdkr:t|d tƒr:t|d tƒr:|d |ƒg}|D ]}||j|jg7 }q>|g}|rl|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   ZBYN)
ÚlenÚ
isinstanceÚtypeÚ
issubclassr   r
   Z	DIRSTRINGr   r   r   )r   r
   r-   Z	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   r,   ~   s   r,   c                       s.   e Zd ZdZdZeeddœ‡ fdd„Z‡  ZS )Úrandom_samplezT
    Returns a random sample of items from the dataset, from the given property
    ZRANDOM_SAMPLEN)r
   Úsizer   c                    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
   r5   r3   r   r   r   r   ­   s    zrandom_sample.__init__)	r   r   r   r   r   r   Úintr   r   r   r   r   r   r4   ¦   s   r4   N)Útypingr   Zaggregationr   r   r   r   r   r   r   r!   r"   r$   r%   r&   r'   r(   r+   r,   r4   r   r   r   r   Ú<module>   s   
(