Back to Redis

TS.REVRANGE

content/commands/ts.revrange.md

latest11.7 KB
Original Source

Query a range in reverse direction. Starting from Redis 8.6, NaN values are included in raw measurement reports (queries without aggregation).

Examples

Required arguments

<details open> <summary><code>key</code></summary>

is the key name for the time series.

</details> <details open> <summary><code>fromTimestamp</code></summary>

is start timestamp for the range query (integer Unix timestamp in milliseconds) or - to denote the timestamp of the earliest sample in the time series.

</details> <details open> <summary><code>toTimestamp</code></summary>

is end timestamp for the range query (integer Unix timestamp in milliseconds) or + to denote the timestamp of the latest sample in the time series.

<note><b>Note:</b> When the time series is a compaction, the last compacted value may aggregate raw values with timestamp beyond toTimestamp. That is because toTimestamp limits only the timestamp of the compacted value, which is the start time of the raw bucket that was compacted.</note>

</details>

Optional arguments

<details open> <summary><code>LATEST</code> (since RedisTimeSeries 1.8)</summary>

is used when a time series is a compaction. With LATEST, TS.REVRANGE also reports the compacted value of the latest, possibly partial, bucket, given that this bucket's start time falls within [fromTimestamp, toTimestamp]. Without LATEST, TS.REVRANGE does not report the latest, possibly partial, bucket. When a time series is not a compaction, LATEST is ignored.

The data in the latest bucket of a compaction is possibly partial. A bucket is closed and compacted only upon arrival of a new sample that opens a new latest bucket. There are cases, however, when the compacted value of the latest, possibly partial, bucket is also required. In such a case, use LATEST.

</details> <details open> <summary><code>FILTER_BY_TS ts...</code> (since RedisTimeSeries 1.6)</summary>

filters samples by a list of specific timestamps. A sample passes the filter if its exact timestamp is specified and falls within [fromTimestamp, toTimestamp].

When used together with AGGREGATION: samples are filtered before being aggregated.

</details> <details open> <summary><code>FILTER_BY_VALUE min max</code> (since RedisTimeSeries 1.6)</summary>

filters samples by minimum and maximum values. min and max cannot be NaN values.

When used together with AGGREGATION: samples are filtered before being aggregated.

</details> <details open> <summary><code>COUNT count</code></summary>

When used without AGGREGATION: limits the number of reported samples.

When used together with AGGREGATION: limits the number of reported buckets.

</details> <details open> <summary><code>ALIGN align</code> (since RedisTimeSeries 1.6)</summary>

is a time bucket alignment control for AGGREGATION. It controls the time bucket timestamps by changing the reference timestamp on which a bucket is defined. Values include:

  • start or -: The reference timestamp will be the query start interval time (fromTimestamp) which can't be -
  • end or +: The reference timestamp will be the query end interval time (toTimestamp) which can't be +
  • A specific timestamp: align the reference timestamp to a specific time

<note><b>NOTE:</b> When not provided, alignment is set to 0.</note>

</details> <details open> <summary><code>AGGREGATION aggregator bucketDuration</code></summary> aggregates samples into time buckets, where:
  • aggregator takes one of the following aggregation types:

    aggregatorDescription
    avgArithmetic mean of all non-NaN values
    sumSum of all non-NaN values
    minMinimum non-NaN value
    maxMaximum non-NaN value
    rangeDifference between the maximum and the minimum non-NaN values
    countNumber of non-NaN values
    countNaNNumber of NaN values (since Redis 8.6)
    countAllNumber of values, including NaN and non-NaN (since Redis 8.6)
    firstThe non-NaN value with the lowest timestamp in the bucket
    lastThe non-NaN value with the highest timestamp in the bucket
    std.pPopulation standard deviation of the non-NaN values
    std.sSample standard deviation of the non-NaN values
    var.pPopulation variance of the non-NaN values
    var.sSample variance of the non-NaN values
    twaTime-weighted average over the bucket's timeframe (ignores NaN values) (since RedisTimeSeries 1.8)
  • bucketDuration is duration of each bucket, in milliseconds.

Without ALIGN, bucket start times are multiples of bucketDuration.

With ALIGN align, bucket start times are multiples of bucketDuration with remainder align % bucketDuration.

The first bucket start time is less than or equal to fromTimestamp.

</details> <details open> <summary><code>[BUCKETTIMESTAMP bt]</code> (since RedisTimeSeries 1.8)</summary>

controls how bucket timestamps are reported.

btTimestamp reported for each bucket
- or startthe bucket's start time (default)
+ or endthe bucket's end time
~ or midthe bucket's mid time (rounded down if not an integer)
</details> <details open> <summary><code>[EMPTY]</code> (since RedisTimeSeries 1.8)</summary> is a flag, which, when specified, reports aggregations also for empty buckets.
aggregatorValue reported for each empty bucket
sum, count0
lastThe value of the last sample before the bucket's start. NaN when no such sample.
twaAverage value over the bucket's timeframe based on linear interpolation of the last sample before the bucket's start and the first sample after the bucket's end. NaN when no such samples.
min, max, range, avg, first, std.p, std.sNaN

Regardless of the values of fromTimestamp and toTimestamp, no data is reported for buckets that end before the earliest sample or begin after the latest sample in the time series.

</details>

Complexity

TS.REVRANGE complexity can be improved in the future by using binary search to find the start of the range, which makes this O(Log(n/m)+k*m). But, because m is small, you can disregard it and look at the operation as O(Log(n)+k).

Examples

<details open> <summary><b>Filter results by timestamp or sample value</b></summary>

Consider a metric where acceptable values are between -100 and 100, and the value 9999 is used as an indication of bad measurement.

{{< highlight bash >}} 127.0.0.1:6379> TS.CREATE temp:TLV LABELS type temp location TLV OK 127.0.0.1:6379> TS.MADD temp:TLV 1000 30 temp:TLV 1010 35 temp:TLV 1020 9999 temp:TLV 1030 40

  1. (integer) 1000
  2. (integer) 1010
  3. (integer) 1020
  4. (integer) 1030 {{< / highlight >}}

Now, retrieve all values except out-of-range values.

{{< highlight bash >}} TS.REVRANGE temp:TLV - + FILTER_BY_VALUE -100 100

    1. (integer) 1030
    2. 40
    1. (integer) 1010
    2. 35
    1. (integer) 1000
    2. 30 {{< / highlight >}}

Now, retrieve the average value, while ignoring out-of-range values.

{{< highlight bash >}} TS.REVRANGE temp:TLV - + FILTER_BY_VALUE -100 100 AGGREGATION avg 1000

    1. (integer) 1000
    2. 35 {{< / highlight >}}
</details> <details open> <summary><b>Align aggregation buckets</b></summary>

To demonstrate alignment, let’s create a stock and add prices at three different timestamps.

{{< highlight bash >}} 127.0.0.1:6379> TS.CREATE stock:A LABELS type stock name A OK 127.0.0.1:6379> TS.MADD stock:A 1000 100 stock:A 1010 110 stock:A 1020 120

  1. (integer) 1000
  2. (integer) 1010
  3. (integer) 1020 127.0.0.1:6379> TS.MADD stock:A 2000 200 stock:A 2010 210 stock:A 2020 220
  4. (integer) 2000
  5. (integer) 2010
  6. (integer) 2020 127.0.0.1:6379> TS.MADD stock:A 3000 300 stock:A 3010 310 stock:A 3020 320
  7. (integer) 3000
  8. (integer) 3010
  9. (integer) 3020 {{< / highlight >}}

Next, aggregate without using ALIGN, defaulting to alignment 0.

{{< highlight bash >}} 127.0.0.1:6379> TS.REVRANGE stock:A - + AGGREGATION min 20

    1. (integer) 3020
    2. 320
    1. (integer) 3000
    2. 300
    1. (integer) 2020
    2. 220
    1. (integer) 2000
    2. 200
    1. (integer) 1020
    2. 120
    1. (integer) 1000
    2. 100 {{< / highlight >}}

And now set ALIGN to 10 to have a bucket start at time 10, and align all the buckets with a 20 milliseconds duration.

{{< highlight bash >}} 127.0.0.1:6379> TS.REVRANGE stock:A - + ALIGN 10 AGGREGATION min 20

    1. (integer) 3010
    2. 310
    1. (integer) 2990
    2. 300
    1. (integer) 2010
    2. 210
    1. (integer) 1990
    2. 200
    1. (integer) 1010
    2. 110
    1. (integer) 990
    2. 100 {{< / highlight >}}

When the start timestamp for the range query is explicitly stated (not -), you can set ALIGN to that time by setting align to - or to start.

{{< highlight bash >}} 127.0.0.1:6379> TS.REVRANGE stock:A 5 + ALIGN - AGGREGATION min 20

    1. (integer) 3005
    2. 310
    1. (integer) 2985
    2. 300
    1. (integer) 2005
    2. 210
    1. (integer) 1985
    2. 200
    1. (integer) 1005
    2. 110
    1. (integer) 985
    2. 100 {{< / highlight >}}

Similarly, when the end timestamp for the range query is explicitly stated, you can set ALIGN to that time by setting align to + or to end.

</details>

Redis Software and Redis Cloud compatibility

| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Supported</span> | <span title="Supported">✅ Flexible & Annual</span> <span title="Supported">✅ Free & Fixed</nobr></span> | |

Return information

{{< multitabs id="ts-revrange-return-info" tab1="RESP2" tab2="RESP3" >}}

One of the following:

  • [Array reply]({{< relref "/develop/reference/protocol-spec#arrays" >}}) of ([Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}), [Simple string reply]({{< relref "/develop/reference/protocol-spec#simple-strings" >}})) pairs representing (timestamp, value) in reverse chronological order.
  • [Simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}) in these cases: invalid filter value, wrong key type, key does not exist, etc.

-tab-sep-

One of the following:

  • [Array reply]({{< relref "/develop/reference/protocol-spec#arrays" >}}) of ([Integer reply]({{< relref "/develop/reference/protocol-spec#integers" >}}), [Double reply]({{< relref "/develop/reference/protocol-spec#doubles" >}})) pairs representing (timestamp, value) in reverse chronological order.
  • [Simple error reply]({{< relref "/develop/reference/protocol-spec#simple-errors" >}}) in these cases: invalid filter value, wrong key type, key does not exist, etc.

{{< /multitabs >}}

See also

[TS.RANGE]({{< relref "commands/ts.range/" >}}) | [TS.MRANGE]({{< relref "commands/ts.mrange/" >}}) | [TS.MREVRANGE]({{< relref "commands/ts.mrevrange/" >}})

[RedisTimeSeries]({{< relref "/develop/data-types/timeseries/" >}})