Skip to content

Commit

Permalink
ALIGN option (#69)
Browse files Browse the repository at this point in the history
* ALIGN option

* fix due to master

* use constant

Co-authored-by: Guy Korland <gkorland@gmail.com>
  • Loading branch information
sazzad16 and gkorland authored Aug 3, 2021
1 parent 3c30148 commit 0ef81fc
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/redislabs/redistimeseries/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum Keyword implements ProtocolCommand {
CHUNK_SIZE,
DUPLICATE_POLICY,
ON_DUPLICATE,
ALIGN,
FILTER_BY_TS,
FILTER_BY_VALUE,
GROUPBY,
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/com/redislabs/redistimeseries/RangeParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class RangeParams {

private Integer count;

private byte[] align;

private Aggregation aggregationType;
private long timeBucket;

Expand All @@ -34,6 +36,23 @@ public RangeParams count(int count) {
return this;
}

private RangeParams align(byte[] raw) {
this.align = raw;
return this;
}

public RangeParams align(long timestamp) {
return align(Protocol.toByteArray(timestamp));
}

public RangeParams alignStart() {
return align(RedisTimeSeries.MINUS);
}

public RangeParams alignEnd() {
return align(RedisTimeSeries.PLUS);
}

public RangeParams aggregation(Aggregation aggregation, long timeBucket) {
this.aggregationType = aggregation;
this.timeBucket = timeBucket;
Expand All @@ -45,13 +64,13 @@ public byte[][] getByteParams(String key, Long from, Long to) {
params.add(SafeEncoder.encode(key));

if (from == null) {
params.add("-".getBytes());
params.add(RedisTimeSeries.MINUS);
} else {
params.add(Protocol.toByteArray(from));
}

if (to == null) {
params.add("+".getBytes());
params.add(RedisTimeSeries.PLUS);
} else {
params.add(Protocol.toByteArray(to));
}
Expand All @@ -75,6 +94,11 @@ public byte[][] getByteParams(String key, Long from, Long to) {
params.add(Protocol.toByteArray(count));
}

if (align != null) {
params.add(Keyword.ALIGN.getRaw());
params.add(align);
}

if (aggregationType != null) {
params.add(Keyword.AGGREGATION.getRaw());
params.add(aggregationType.getRaw());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,43 @@ public void testIncrByDecrBy() throws InterruptedException {
}
}

@Test
public void align() {
client.add("align", 1, 10d);
client.add("align", 3, 5d);
client.add("align", 11, 10d);
client.add("align", 25, 11d);

Value[] values =
client.range(
"align", 1L, 30L, RangeParams.rangeParams().aggregation(Aggregation.COUNT, 10));
assertArrayEquals(new Value[] {new Value(1, 2), new Value(11, 1), new Value(21, 1)}, values);

values =
client.range(
"align",
1L,
30L,
RangeParams.rangeParams().alignStart().aggregation(Aggregation.COUNT, 10));
assertArrayEquals(new Value[] {new Value(1, 2), new Value(11, 1), new Value(21, 1)}, values);

values =
client.range(
"align",
1L,
30L,
RangeParams.rangeParams().alignEnd().aggregation(Aggregation.COUNT, 10));
assertArrayEquals(new Value[] {new Value(1, 2), new Value(11, 1), new Value(21, 1)}, values);

values =
client.range(
"align",
1L,
30L,
RangeParams.rangeParams().align(5).aggregation(Aggregation.COUNT, 10));
assertArrayEquals(new Value[] {new Value(1, 2), new Value(11, 1), new Value(21, 1)}, values);
}

@Test
public void rangeFilterBy() {

Expand Down

0 comments on commit 0ef81fc

Please sign in to comment.