diff --git a/src/main/java/redis/clients/jedis/timeseries/TSMRangeParams.java b/src/main/java/redis/clients/jedis/timeseries/TSMRangeParams.java index a3a9351457..ce6cccfc6b 100644 --- a/src/main/java/redis/clients/jedis/timeseries/TSMRangeParams.java +++ b/src/main/java/redis/clients/jedis/timeseries/TSMRangeParams.java @@ -1,9 +1,11 @@ package redis.clients.jedis.timeseries; +import static redis.clients.jedis.Protocol.BYTES_TILDE; import static redis.clients.jedis.Protocol.toByteArray; import static redis.clients.jedis.timeseries.TimeSeriesProtocol.MINUS; import static redis.clients.jedis.timeseries.TimeSeriesProtocol.PLUS; import static redis.clients.jedis.timeseries.TimeSeriesProtocol.TimeSeriesKeyword.*; +import static redis.clients.jedis.util.SafeEncoder.encode; import redis.clients.jedis.CommandArguments; import redis.clients.jedis.params.IParams; @@ -27,7 +29,7 @@ public class TSMRangeParams implements IParams { private AggregationType aggregationType; private long bucketDuration; - private Long bucketTimestamp; + private byte[] bucketTimestamp; private boolean empty; @@ -128,10 +130,35 @@ public TSMRangeParams aggregation(AggregationType aggregationType, long bucketDu return this; } - public TSMRangeParams aggregation(AggregationType aggregationType, long bucketDuration, long bucketTimestamp) { - this.aggregationType = aggregationType; - this.bucketDuration = bucketDuration; - this.bucketTimestamp = bucketTimestamp; + /** + * This requires AGGREGATION. + */ + public TSMRangeParams bucketTimestamp(String bucketTimestamp) { + this.bucketTimestamp = encode(bucketTimestamp); + return this; + } + + /** + * This requires AGGREGATION. + */ + public TSMRangeParams bucketTimestampLow() { + this.bucketTimestamp = MINUS; + return this; + } + + /** + * This requires AGGREGATION. + */ + public TSMRangeParams bucketTimestampHigh() { + this.bucketTimestamp = PLUS; + return this; + } + + /** + * This requires AGGREGATION. + */ + public TSMRangeParams bucketTimestampMid() { + this.bucketTimestamp = BYTES_TILDE; return this; } @@ -213,7 +240,7 @@ public void addParams(CommandArguments args) { args.add(AGGREGATION).add(aggregationType).add(toByteArray(bucketDuration)); if (bucketTimestamp != null) { - args.add(BUCKETTIMESTAMP).add(toByteArray(bucketTimestamp)); + args.add(BUCKETTIMESTAMP).add(bucketTimestamp); } if (empty) { diff --git a/src/main/java/redis/clients/jedis/timeseries/TSRangeParams.java b/src/main/java/redis/clients/jedis/timeseries/TSRangeParams.java index ce7a82bd3c..d93ee0fd6f 100644 --- a/src/main/java/redis/clients/jedis/timeseries/TSRangeParams.java +++ b/src/main/java/redis/clients/jedis/timeseries/TSRangeParams.java @@ -1,9 +1,11 @@ package redis.clients.jedis.timeseries; +import static redis.clients.jedis.Protocol.BYTES_TILDE; import static redis.clients.jedis.Protocol.toByteArray; import static redis.clients.jedis.timeseries.TimeSeriesProtocol.MINUS; import static redis.clients.jedis.timeseries.TimeSeriesProtocol.PLUS; import static redis.clients.jedis.timeseries.TimeSeriesProtocol.TimeSeriesKeyword.*; +import static redis.clients.jedis.util.SafeEncoder.encode; import redis.clients.jedis.CommandArguments; import redis.clients.jedis.params.IParams; @@ -24,7 +26,7 @@ public class TSRangeParams implements IParams { private AggregationType aggregationType; private long bucketDuration; - private Long bucketTimestamp; + private byte[] bucketTimestamp; private boolean empty; @@ -103,14 +105,38 @@ public TSRangeParams alignEnd() { public TSRangeParams aggregation(AggregationType aggregationType, long bucketDuration) { this.aggregationType = aggregationType; this.bucketDuration = bucketDuration; - this.bucketTimestamp = null; return this; } - public TSRangeParams aggregation(AggregationType aggregationType, long bucketDuration, long bucketTimestamp) { - this.aggregationType = aggregationType; - this.bucketDuration = bucketDuration; - this.bucketTimestamp = bucketTimestamp; + /** + * This requires AGGREGATION. + */ + public TSRangeParams bucketTimestamp(String bucketTimestamp) { + this.bucketTimestamp = encode(bucketTimestamp); + return this; + } + + /** + * This requires AGGREGATION. + */ + public TSRangeParams bucketTimestampLow() { + this.bucketTimestamp = MINUS; + return this; + } + + /** + * This requires AGGREGATION. + */ + public TSRangeParams bucketTimestampHigh() { + this.bucketTimestamp = PLUS; + return this; + } + + /** + * This requires AGGREGATION. + */ + public TSRangeParams bucketTimestampMid() { + this.bucketTimestamp = BYTES_TILDE; return this; } @@ -168,7 +194,7 @@ public void addParams(CommandArguments args) { args.add(AGGREGATION).add(aggregationType).add(toByteArray(bucketDuration)); if (bucketTimestamp != null) { - args.add(BUCKETTIMESTAMP).add(toByteArray(bucketTimestamp)); + args.add(BUCKETTIMESTAMP).add(bucketTimestamp); } if (empty) { diff --git a/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java index c2a64fc8cb..8ad2d61ce8 100644 --- a/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java +++ b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java @@ -889,10 +889,10 @@ public void empty() { assertNotNull(range.get(1).getValue()); // any parsable value // mrange - List mrange = client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.MAX, 5).filter("l=v")); + List mrange = client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.MIN, 5).filter("l=v")); assertEquals(1, mrange.size()); assertEquals(2, mrange.get(0).getValue().size()); - mrange = client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.MAX, 5).empty().filter("l=v")); + mrange = client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.MIN, 5).empty().filter("l=v")); assertEquals(1, mrange.size()); assertEquals(3, mrange.get(0).getValue().size()); assertNotNull(mrange.get(0).getValue().get(1).getValue()); // any parsable value @@ -906,4 +906,35 @@ public void empty() { assertEquals(3, mrange.get(0).getValue().size()); assertNotNull(mrange.get(0).getValue().get(1).getValue()); // any parsable value } + + @Test + public void bucketTimestamp() { + client.tsCreate("ts", TSCreateParams.createParams().label("l", "v")); + client.tsAdd("ts", 1, 1); + client.tsAdd("ts", 2, 3); + + // range / revrange + assertEquals(0, client.tsRange("ts", TSRangeParams.rangeParams() + .aggregation(AggregationType.FIRST, 10).bucketTimestampLow()).get(0).getTimestamp()); + assertEquals(10, client.tsRange("ts", TSRangeParams.rangeParams() + .aggregation(AggregationType.LAST, 10).bucketTimestampHigh()).get(0).getTimestamp()); + assertEquals(5, client.tsRange("ts", TSRangeParams.rangeParams() + .aggregation(AggregationType.RANGE, 10).bucketTimestampMid()).get(0).getTimestamp()); + assertEquals(5, client.tsRevRange("ts", TSRangeParams.rangeParams() + .aggregation(AggregationType.TWA, 10).bucketTimestampMid()).get(0).getTimestamp()); + assertEquals(5, client.tsRevRange("ts", TSRangeParams.rangeParams() + .aggregation(AggregationType.TWA, 10).bucketTimestamp("mid")).get(0).getTimestamp()); + + // mrange / mrevrange + assertEquals(0, client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.STD_P, 10) + .bucketTimestampLow().filter("l=v")).get(0).getValue().get(0).getTimestamp()); + assertEquals(10, client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.STD_S, 10) + .bucketTimestampHigh().filter("l=v")).get(0).getValue().get(0).getTimestamp()); + assertEquals(5, client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.TWA, 10) + .bucketTimestampMid().filter("l=v")).get(0).getValue().get(0).getTimestamp()); + assertEquals(5, client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.VAR_P, 10) + .bucketTimestampMid().filter("l=v")).get(0).getValue().get(0).getTimestamp()); + assertEquals(5, client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.VAR_S, 10) + .bucketTimestamp("~").filter("l=v")).get(0).getValue().get(0).getTimestamp()); + } }