Skip to content

Commit

Permalink
Fix and test BUCKETTIMESTAMP
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Jul 20, 2022
1 parent 094ff20 commit 89ece4e
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 15 deletions.
39 changes: 33 additions & 6 deletions src/main/java/redis/clients/jedis/timeseries/TSMRangeParams.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,7 +29,7 @@ public class TSMRangeParams implements IParams {

private AggregationType aggregationType;
private long bucketDuration;
private Long bucketTimestamp;
private byte[] bucketTimestamp;

private boolean empty;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
40 changes: 33 additions & 7 deletions src/main/java/redis/clients/jedis/timeseries/TSRangeParams.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,7 +26,7 @@ public class TSRangeParams implements IParams {

private AggregationType aggregationType;
private long bucketDuration;
private Long bucketTimestamp;
private byte[] bucketTimestamp;

private boolean empty;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -889,10 +889,10 @@ public void empty() {
assertNotNull(range.get(1).getValue()); // any parsable value

// mrange
List<TSKeyedElements> mrange = client.tsMRange(TSMRangeParams.multiRangeParams().aggregation(AggregationType.MAX, 5).filter("l=v"));
List<TSKeyedElements> 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
Expand All @@ -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());
}
}

0 comments on commit 89ece4e

Please sign in to comment.