o
    Lj                     @   s  d dl mZmZ ddlmZmZmZmZ dedefdd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G d"d# d#eZd$S )%    )IterableUnion   )AscDescReducerSortDirectionnamereturnc                 C   s   |  dr| S d|  S )z3Return ``name`` with a single leading ``@`` prefix.@)
startswith)r	    r   \/home/djax/ivt_ai_plugin/venv/lib/python3.10/site-packages/redis/commands/search/reducers.py_ensure_at_prefix   s   r   c                       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/fieldr
   Nc                    s   t  | || _d S N)super__init___fieldselfr   	__class__r   r   r      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   0   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   ;   r(   zmax.__init__r)   r   r   r   r   r-   4   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   F   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   Q   r(   ztolist.__init__r)   r   r   r   r   r1   J   r*   r1   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   ]   r(   zcount_distinct.__init__r)   r   r   r   r   r3   U   s    r3   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   r5   a   s    r5   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   r:   r   r   r   r   s   s   
zquantile.__init__)	r   r   r   r   r#   r   floatr   r   r   r   r   r   r8   k   s    "r8   c                       r$   )stddevzG
    Return the standard deviation for the values within the group
    STDDEVr   r
   Nc                    r'   r   r"   r   r   r   r   r      r(   zstddev.__init__r)   r   r   r   r   r<   x   r*   r<   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   r>      s    *r>   c                       r7   )	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   rL   rI   r   r   r   r      s   
zrandom_sample.__init__)	r   r   r   r   r#   r   intr   r   r   r   r   r   rJ      s    "rJ   c                       sl   e Zd ZdZdZ				ddeee B dedee	B eee	B  B dB d	e
eef dB d
df
 fddZ  ZS )collecta  
    Gathers the rows of each ``GROUPBY`` group, projects a chosen set of
    fields from every row, optionally deduplicates, sorts, and limits them,
    and returns them as an array of per-entry maps under the reducer alias.

    ``COLLECT`` is a preview feature gated behind
    ``search-enable-unstable-features``. See
    `FT.AGGREGATE <https://redis.io/commands/ft.aggregate>`_.
    COLLECT*FNfieldsdistinctsort_bylimitr
   c                    s   g }|dkr|ddg7 }n.t |tr|gnt|}|r$tdd |D r(tddd |D }|dtt|g| 7 }|rB|dg7 }|d	urut |ttfrP|gn|}g }|D ]}	|t|	j	|	j
g7 }qV|sitd
|dtt|g| 7 }|d	ur|\}
}|dt|
t|g7 }t j|  d	S )a  
        ### Parameters

        - **fields**: The fields to project from each collected row. Either the
            literal ``"*"`` (project every field present in the pipeline at this
            stage) or a field name / iterable of field names. Names are
            normalized to a single leading ``@`` on the wire; output map keys
            are the bare names.
        - **distinct**: When ``True``, emit ``DISTINCT`` to deduplicate entries
            with identical projected fields. Forward-compatible: this option is
            not yet implemented by the server and currently produces a server
            error when sent.
        - **sort_by**: An ``Asc``/``Desc`` instance or an iterable of them, used
            to order the collected entries within each group.
        - **limit**: An ``(offset, count)`` pair. Returns at most ``count``
            entries per group after skipping ``offset``. With ``sort_by`` this
            acts as a top-N selection.
        rP   FIELDSc                 s   s    | ]}|   V  qd S r   )strip.0nr   r   r   	<genexpr>   s    z#collect.__init__.<locals>.<genexpr>z7collect fields must be '*' or a non-empty list of namesc                 S   s   g | ]}t |qS r   )r   rW   r   r   r   
<listcomp>   s    z$collect.__init__.<locals>.<listcomp>DISTINCTNz/collect sort_by must contain at least one fieldSORTBYLIMIT)rC   r   listany
ValueErrorrB   r   r   r   r   rF   r   r   )r   rQ   rR   rS   rT   rI   namessort_fields	sort_argsrH   offsetr    r   r   r   r      s0   
zcollect.__init__)rP   FNN)r   r   r   r   r#   r   r   boolr   r   tuplerM   r   r   r   r   r   r   rN      s$    

rN   N)typingr   r   aggregationr   r   r   r   r   r   r   r    r%   r+   r-   r/   r1   r3   r5   r8   r<   r>   rJ   rN   r   r   r   r   <module>   s"    
(