U
    ccvn                     @   sb   d dl mZ dZdZdZdZdZdZdZd	Z	d
Z
dZdZdZdZdZdZdZdZG dd dZdS )    )	DataErrorTS.ADDzTS.ALTERzTS.CREATERULEz	TS.CREATEz	TS.DECRBYzTS.DELETERULEzTS.DELzTS.GETz	TS.INCRBYzTS.INFOzTS.MADDzTS.MGETz	TS.MRANGEzTS.MREVRANGEzTS.QUERYINDEXzTS.RANGEzTS.REVRANGEc                   @   sP  e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdEddZdFddZdd ZdGdd ZdHd!d"Zd#d$ ZdId%d&Zd'd( Zd)d* Zed+d, ZedJd-d.Zed/d0 Zed1d2 Zed3d4 Zed5d6 Zed7d8 Zed9d: Zed;d< Z ed=d> Z!ed?d@ Z"edAdB Z#edCdD Z$dS )KTimeSeriesCommandszRedisTimeSeries Commands.c           	      K   s   | dd}| dd}| di }| dd}| dd}|g}| || | || | || | |t| | || | jtf| S )af  
        Create a new time-series.

        Args:

        key:
            time-series key
        retention_msecs:
            Maximum age for samples compared to last event time (in milliseconds).
            If None or 0 is passed then  the series is not trimmed at all.
        uncompressed:
            Since RedisTimeSeries v1.2, both timestamps and values are
            compressed by default.
            Adding this flag will keep data in an uncompressed form.
            Compression not only saves
            memory but usually improve performance due to lower number
            of memory accesses.
        labels:
            Set of label-value pairs that represent metadata labels of the key.
        chunk_size:
            Each time-serie uses chunks of memory of fixed size for
            time series samples.
            You can alter the default TSDB chunk size by passing the
            chunk_size argument (in Bytes).
        duplicate_policy:
            Since RedisTimeSeries v1.4 you can specify the duplicate sample policy
            ( Configure what to do on duplicate sample. )
            Can be one of:
            - 'block': an error will occur for any out of order sample.
            - 'first': ignore the new value.
            - 'last': override with latest value.
            - 'min': only override if the value is lower than the existing value.
            - 'max': only override if the value is higher than the existing value.
            When this is not set, the server-wide default will be used.

        For more information: https://oss.redis.com/redistimeseries/commands/#tscreate
        retention_msecsNuncompressedFlabels
chunk_sizeduplicate_policy)get_append_retention_append_uncompressed_append_chunk_size_append_duplicate_policy
CREATE_CMD_append_labelsexecute_command)	selfkeykwargsr   r   r   r   r	   params r   \/var/www/html/project/venv/lib/python3.8/site-packages/redis/commands/timeseries/commands.pycreate   s    &zTimeSeriesCommands.createc                 K   s`   | dd}| di }| dd}|g}| || | |t| | || | jtf| S )z
        Update the retention, labels of an existing key.
        For more information see

        The parameters are the same as TS.CREATE.

        For more information: https://oss.redis.com/redistimeseries/commands/#tsalter
        r   Nr   r	   )r
   r   r   	ALTER_CMDr   r   )r   r   r   r   r   r	   r   r   r   r   alterM   s    	zTimeSeriesCommands.alterc                 K   s   | dd}| dd}| di }| dd}| dd}	|||g}
| |
| | |
| | |
| | |
t|	 | |
| | jtf|
 S )a!  
        Append (or create and append) a new sample to the series.
        For more information see

        Args:

        key:
            time-series key
        timestamp:
            Timestamp of the sample. * can be used for automatic timestamp (using the system clock).
        value:
            Numeric data value of the sample
        retention_msecs:
            Maximum age for samples compared to last event time (in milliseconds).
            If None or 0 is passed then  the series is not trimmed at all.
        uncompressed:
            Since RedisTimeSeries v1.2, both timestamps and values are compressed by default.
            Adding this flag will keep data in an uncompressed form. Compression not only saves
            memory but usually improve performance due to lower number of memory accesses.
        labels:
            Set of label-value pairs that represent metadata labels of the key.
        chunk_size:
            Each time-serie uses chunks of memory of fixed size for time series samples.
            You can alter the default TSDB chunk size by passing the chunk_size argument (in Bytes).
        duplicate_policy:
            Since RedisTimeSeries v1.4 you can specify the duplicate sample policy
            (Configure what to do on duplicate sample).
            Can be one of:
            - 'block': an error will occur for any out of order sample.
            - 'first': ignore the new value.
            - 'last': override with latest value.
            - 'min': only override if the value is lower than the existing value.
            - 'max': only override if the value is higher than the existing value.
            When this is not set, the server-wide default will be used.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsadd
        r   Nr   Fr   r   r	   )r
   r   r   r   r   ADD_CMDr   r   )r   r   	timestampvaluer   r   r   r   r   r	   r   r   r   r   add`   s    &
zTimeSeriesCommands.addc                 C   s2   g }|D ]}|D ]}| | qq| jtf| S )aH  
        Append (or create and append) a new `value` to series
        `key` with `timestamp`.
        Expects a list of `tuples` as (`key`,`timestamp`, `value`).
        Return value is an array with timestamps of insertions.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsmadd
        )appendr   MADD_CMD)r   Z
ktv_tuplesr   Zktvitemr   r   r   madd   s
    	zTimeSeriesCommands.maddc           
      K   s   | dd}| dd}| dd}| di }| dd}||g}	| |	| | |	| | |	| | |	| | |	| | jtf|	 S )aN  
        Increment (or create an time-series and increment) the latest
        sample's of a series.
        This command can be used as a counter or gauge that automatically gets
        history as a time series.

        Args:

        key:
            time-series key
        value:
            Numeric data value of the sample
        timestamp:
            Timestamp of the sample. None can be used for automatic timestamp (using the system clock).
        retention_msecs:
            Maximum age for samples compared to last event time (in milliseconds).
            If None or 0 is passed then  the series is not trimmed at all.
        uncompressed:
            Since RedisTimeSeries v1.2, both timestamps and values are compressed by default.
            Adding this flag will keep data in an uncompressed form. Compression not only saves
            memory but usually improve performance due to lower number of memory accesses.
        labels:
            Set of label-value pairs that represent metadata labels of the key.
        chunk_size:
            Each time-series uses chunks of memory of fixed size for time series samples.
            You can alter the default TSDB chunk size by passing the chunk_size argument (in Bytes).

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsincrbytsdecrby
        r   Nr   r   Fr   r   )r
   _append_timestampr   r   r   r   r   
INCRBY_CMD
r   r   r   r   r   r   r   r   r   r   r   r   r   incrby   s    zTimeSeriesCommands.incrbyc           
      K   s   | dd}| dd}| dd}| di }| dd}||g}	| |	| | |	| | |	| | |	| | |	| | jtf|	 S )a~  
        Decrement (or create an time-series and decrement) the
        latest sample's of a series.
        This command can be used as a counter or gauge that
        automatically gets history as a time series.

        Args:

        key:
            time-series key
        value:
            Numeric data value of the sample
        timestamp:
            Timestamp of the sample. None can be used for automatic
            timestamp (using the system clock).
        retention_msecs:
            Maximum age for samples compared to last event time (in milliseconds).
            If None or 0 is passed then  the series is not trimmed at all.
        uncompressed:
            Since RedisTimeSeries v1.2, both timestamps and values are
            compressed by default.
            Adding this flag will keep data in an uncompressed form.
            Compression not only saves
            memory but usually improve performance due to lower number
            of memory accesses.
        labels:
            Set of label-value pairs that represent metadata labels of the key.
        chunk_size:
            Each time-series uses chunks of memory of fixed size for time series samples.
            You can alter the default TSDB chunk size by passing the chunk_size argument (in Bytes).

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsincrbytsdecrby
        r   Nr   r   Fr   r   )r
   r#   r   r   r   r   r   
DECRBY_CMDr%   r   r   r   decrby   s    "zTimeSeriesCommands.decrbyc                 C   s   |  t|||S )ao  
        Delete data points for a given timeseries and interval range
        in the form of start and end delete timestamps.
        The given timestamp interval is closed (inclusive), meaning start
        and end data points will also be deleted.
        Return the count for deleted items.
        For more information see

        Args:

        key:
            time-series key.
        from_time:
            Start timestamp for the range deletion.
        to_time:
            End timestamp for the range deletion.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsdel
        )r   DEL_CMD)r   r   	from_timeto_timer   r   r   delete   s    zTimeSeriesCommands.deletec                 C   s&   ||g}|  ||| | jtf| S )a  
        Create a compaction rule from values added to `source_key` into `dest_key`.
        Aggregating for `bucket_size_msec` where an `aggregation_type` can be
        [`avg`, `sum`, `min`, `max`, `range`, `count`, `first`, `last`,
        `std.p`, `std.s`, `var.p`, `var.s`]

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tscreaterule
        )_append_aggregationr   CREATERULE_CMD)r   
source_keydest_keyaggregation_typebucket_size_msecr   r   r   r   
createrule  s    	zTimeSeriesCommands.createrulec                 C   s   |  t||S )z
        Delete a compaction rule.
        For more information see

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsdeleterule
        )r   DELETERULE_CMD)r   r/   r0   r   r   r   
deleterule$  s    zTimeSeriesCommands.deleterulec                 C   sN   |||g}|  || | |||	 | || | ||
 | ||| |S )z*Create TS.RANGE and TS.REVRANGE arguments.)_append_filer_by_ts_append_filer_by_value_append_count_append_alignr-   r   r   r*   r+   countr1   r2   filter_by_tsfilter_by_min_valuefilter_by_max_valuealignr   r   r   r   Z__range_params-  s    
z!TimeSeriesCommands.__range_paramsNr   c                 C   s,   |  |||||||||	|

}| jtf| S )a  
        Query a range in forward direction for a specific time-serie.

        Args:

        key:
            Key name for timeseries.
        from_time:
            Start timestamp for the range query. - can be used to express
            the minimum possible timestamp (0).
        to_time:
            End timestamp for range query, + can be used to express the
            maximum possible timestamp.
        count:
            Optional maximum number of returned results.
        aggregation_type:
            Optional aggregation type. Can be one of
            [`avg`, `sum`, `min`, `max`, `range`, `count`,
            `first`, `last`, `std.p`, `std.s`, `var.p`, `var.s`]
        bucket_size_msec:
            Time bucket for aggregation in milliseconds.
        filter_by_ts:
            List of timestamps to filter the result by specific timestamps.
        filter_by_min_value:
            Filter result by minimum value (must mention also filter
            by_max_value).
        filter_by_max_value:
            Filter result by maximum value (must mention also filter
            by_min_value).
        align:
            Timestamp for alignment control for aggregation.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsrangetsrevrange
        )!_TimeSeriesCommands__range_paramsr   	RANGE_CMDr:   r   r   r   rangeD  s    /zTimeSeriesCommands.rangec                 C   s,   |  |||||||||	|

}| jtf| S )a*  
        Query a range in reverse direction for a specific time-series.

        **Note**: This command is only available since RedisTimeSeries >= v1.4

        Args:

        key:
            Key name for timeseries.
        from_time:
            Start timestamp for the range query. - can be used to express the minimum possible timestamp (0).
        to_time:
            End timestamp for range query, + can be used to express the maximum possible timestamp.
        count:
            Optional maximum number of returned results.
        aggregation_type:
            Optional aggregation type. Can be one of [`avg`, `sum`, `min`, `max`, `range`, `count`,
            `first`, `last`, `std.p`, `std.s`, `var.p`, `var.s`]
        bucket_size_msec:
            Time bucket for aggregation in milliseconds.
        filter_by_ts:
            List of timestamps to filter the result by specific timestamps.
        filter_by_min_value:
            Filter result by minimum value (must mention also filter_by_max_value).
        filter_by_max_value:
            Filter result by maximum value (must mention also filter_by_min_value).
        align:
            Timestamp for alignment control for aggregation.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsrangetsrevrange
        )r@   r   REVRANGE_CMDr:   r   r   r   revrange  s    ,zTimeSeriesCommands.revrangec                 C   s|   ||g}|  || | ||	|
 | || | || | ||| | ||| |dg ||7 }| ||| |S )z,Create TS.MRANGE and TS.MREVRANGE arguments.FILTER)r6   r7   r8   r9   r-   _append_with_labelsextend_append_groupby_reduce)r   r1   r2   r;   filtersr*   r+   with_labelsr<   r=   r>   groupbyreduceselect_labelsr?   r   r   r   r   Z__mrange_params  s    z"TimeSeriesCommands.__mrange_paramsFc                 C   s4   |  |||||||||	|
||||}| jtf| S )a  
        Query a range across multiple time-series by filters in forward direction.

        Args:

        from_time:
            Start timestamp for the range query. `-` can be used to
            express the minimum possible timestamp (0).
        to_time:
            End timestamp for range query, `+` can be used to express
            the maximum possible timestamp.
        filters:
            filter to match the time-series labels.
        count:
            Optional maximum number of returned results.
        aggregation_type:
            Optional aggregation type. Can be one of
            [`avg`, `sum`, `min`, `max`, `range`, `count`,
            `first`, `last`, `std.p`, `std.s`, `var.p`, `var.s`]
        bucket_size_msec:
            Time bucket for aggregation in milliseconds.
        with_labels:
            Include in the reply the label-value pairs that represent metadata
            labels of the time-series.
            If this argument is not set, by default, an empty Array will be
            replied on the labels array position.
        filter_by_ts:
            List of timestamps to filter the result by specific timestamps.
        filter_by_min_value:
            Filter result by minimum value (must mention also
            filter_by_max_value).
        filter_by_max_value:
            Filter result by maximum value (must mention also
            filter_by_min_value).
        groupby:
            Grouping by fields the results (must mention also reduce).
        reduce:
            Applying reducer functions on each group. Can be one
            of [`sum`, `min`, `max`].
        select_labels:
            Include in the reply only a subset of the key-value
            pair labels of a series.
        align:
            Timestamp for alignment control for aggregation.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsmrangetsmrevrange
        )"_TimeSeriesCommands__mrange_paramsr   
MRANGE_CMDr   r*   r+   rI   r;   r1   r2   rJ   r<   r=   r>   rK   rL   rM   r?   r   r   r   r   mrange  s"    @zTimeSeriesCommands.mrangec                 C   s4   |  |||||||||	|
||||}| jtf| S )a  
        Query a range across multiple time-series by filters in reverse direction.

        Args:

        from_time:
            Start timestamp for the range query. - can be used to express
            the minimum possible timestamp (0).
        to_time:
            End timestamp for range query, + can be used to express
            the maximum possible timestamp.
        filters:
            Filter to match the time-series labels.
        count:
            Optional maximum number of returned results.
        aggregation_type:
            Optional aggregation type. Can be one of
            [`avg`, `sum`, `min`, `max`, `range`, `count`,
            `first`, `last`, `std.p`, `std.s`, `var.p`, `var.s`]
        bucket_size_msec:
            Time bucket for aggregation in milliseconds.
        with_labels:
            Include in the reply the label-value pairs that represent
            metadata labels
            of the time-series.
            If this argument is not set, by default, an empty Array
            will be replied
            on the labels array position.
        filter_by_ts:
            List of timestamps to filter the result by specific timestamps.
        filter_by_min_value:
            Filter result by minimum value (must mention also filter
            by_max_value).
        filter_by_max_value:
            Filter result by maximum value (must mention also filter
            by_min_value).
        groupby:
            Grouping by fields the results (must mention also reduce).
        reduce:
            Applying reducer functions on each group. Can be one
            of [`sum`, `min`, `max`].
        select_labels:
            Include in the reply only a subset of the key-value pair
            labels of a series.
        align:
            Timestamp for alignment control for aggregation.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsmrangetsmrevrange
        )rN   r   MREVRANGE_CMDrP   r   r   r   	mrevrange,  s"    BzTimeSeriesCommands.mrevrangec                 C   s   |  t|S )z# noqa
        Get the last sample of `key`.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsget
        )r   GET_CMDr   r   r   r   r   r
     s    zTimeSeriesCommands.getc                 C   s4   g }|  || |dg ||7 }| jtf| S )z# noqa
        Get the last samples matching the specific `filter`.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsmget
        rE   )rF   rG   r   MGET_CMD)r   rI   rJ   r   r   r   r   mget  s
    zTimeSeriesCommands.mgetc                 C   s   |  t|S )z# noqa
        Get information of `key`.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsinfo
        )r   INFO_CMDrU   r   r   r   info  s    zTimeSeriesCommands.infoc                 C   s   | j tf| S )z# noqa
        Get all the keys matching the `filter` list.

        For more information: https://oss.redis.com/redistimeseries/master/commands/#tsqueryindex
        )r   QUERYINDEX_CMD)r   rI   r   r   r   
queryindex  s    zTimeSeriesCommands.queryindexc                 C   s   |r|  dg dS )z"Append UNCOMPRESSED tag to params.ZUNCOMPRESSEDNrG   )r   r   r   r   r   r     s    z'TimeSeriesCommands._append_uncompressedc                 C   s6   |r|rt d|r | dg |r2| d| dS )z!Append labels behavior to params.z:with_labels and select_labels cannot be provided together.Z
WITHLABELSSELECTED_LABELSN)r]   )r   rG   )r   rJ   rM   r   r   r   rF     s    z&TimeSeriesCommands._append_with_labelsc                 C   s*   |dk	r&|dk	r&|  d|d| g dS )z)Append GROUPBY REDUCE property to params.NZGROUPBYREDUCE)rG   upper)r   rK   rL   r   r   r   rH     s    z)TimeSeriesCommands._append_groupby_reducec                 C   s   |dk	r|  d|g dS )z$Append RETENTION property to params.NZ	RETENTIONr\   )r   Z	retentionr   r   r   r     s    z$TimeSeriesCommands._append_retentionc                 C   s2   |r.|  d | D ]\}}| ||g qdS )z!Append LABELS property to params.ZLABELSN)r   itemsrG   )r   r   kvr   r   r   r     s    
z!TimeSeriesCommands._append_labelsc                 C   s   |dk	r|  d|g dS )z Append COUNT property to params.NZCOUNTr\   )r   r;   r   r   r   r8     s    z TimeSeriesCommands._append_countc                 C   s   |dk	r|  d|g dS )z$Append TIMESTAMP property to params.N	TIMESTAMPr\   )r   r   r   r   r   r#     s    z$TimeSeriesCommands._append_timestampc                 C   s   |dk	r|  d|g dS )z Append ALIGN property to params.NZALIGNr\   )r   r?   r   r   r   r9     s    z TimeSeriesCommands._append_alignc                 C   s$   |dk	r |  d | ||g dS )z&Append AGGREGATION property to params.NZAGGREGATION)r   rG   )r   r1   r2   r   r   r   r-     s    
z&TimeSeriesCommands._append_aggregationc                 C   s   |dk	r|  d|g dS )z%Append CHUNK_SIZE property to params.NZ
CHUNK_SIZEr\   )r   r   r   r   r   r     s    z%TimeSeriesCommands._append_chunk_sizec                 C   s2   |dk	r.|dkr |  d|g n|  d|g dS )z^Append DUPLICATE_POLICY property to params on CREATE
        and ON_DUPLICATE on ADD.
        Nr   ZON_DUPLICATEZDUPLICATE_POLICYr\   )r   commandr	   r   r   r   r     s    z+TimeSeriesCommands._append_duplicate_policyc                 C   s   |dk	r|  d| dS )z'Append FILTER_BY_TS property to params.NFILTER_BY_TS)re   r\   )r   Zts_listr   r   r   r6     s    z&TimeSeriesCommands._append_filer_by_tsc                 C   s$   |dk	r |dk	r |  d||g dS )z*Append FILTER_BY_VALUE property to params.NZFILTER_BY_VALUEr\   )r   Z	min_valueZ	max_valuer   r   r   r7     s    z)TimeSeriesCommands._append_filer_by_value)NNr   NNNN)NNr   NNNN)NNr   FNNNNNNN)NNr   FNNNNNNN)F)N)%__name__
__module____qualname____doc__r   r   r   r"   r&   r(   r,   r3   r5   r@   rB   rD   rN   rQ   rS   r
   rW   rY   r[   staticmethodr   rF   rH   r   r   r8   r#   r9   r-   r   r   r6   r7   r   r   r   r   r      s   44,0	       
B       
:#           
X           
U












r   N)Zredis.exceptionsr   r   r   r.   r   r'   r4   r)   rT   r$   rX   r    rV   rO   rR   rZ   rA   rC   r   r   r   r   r   <module>   s$   