Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support GROUPBY and REDUCE options
Browse files Browse the repository at this point in the history
sazzad16 committed Aug 2, 2021
1 parent 942cc7d commit 7d021c2
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/com/redislabs/redistimeseries/Keyword.java
Original file line number Diff line number Diff line change
@@ -15,7 +15,9 @@ public enum Keyword implements ProtocolCommand {
UNCOMPRESSED,
CHUNK_SIZE,
DUPLICATE_POLICY,
ON_DUPLICATE;
ON_DUPLICATE,
GROUPBY,
REDUCE;

private final byte[] raw;

14 changes: 14 additions & 0 deletions src/main/java/com/redislabs/redistimeseries/MultiRangeParams.java
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ public class MultiRangeParams {

private boolean withLabels;

private String[] groupByReduce;

public static MultiRangeParams multiRangeParams() {
return new MultiRangeParams();
}
@@ -41,6 +43,11 @@ public MultiRangeParams withLabels(boolean withLabels) {
return this;
}

public MultiRangeParams groupByReduce(String group, String reduce) {
this.groupByReduce = new String[] {group, reduce};
return this;
}

public byte[][] getByteParams(long from, long to, String... filters) {
List<byte[]> params = new ArrayList<>();
params.add(Protocol.toByteArray(from));
@@ -66,6 +73,13 @@ public byte[][] getByteParams(long from, long to, String... filters) {
params.add(SafeEncoder.encode(filter));
}

if (groupByReduce != null) {
params.add(Keyword.GROUPBY.getRaw());
params.add(SafeEncoder.encode(groupByReduce[0]));
params.add(Keyword.REDUCE.getRaw());
params.add(SafeEncoder.encode(groupByReduce[1]));
}

return params.toArray(new byte[params.size()][]);
}
}
Original file line number Diff line number Diff line change
@@ -409,6 +409,42 @@ public void testIncrByDecrBy() throws InterruptedException {
}
}

@Test
public void groupByReduce() {
client.create("ts1", convertMap("metric", "cpu", "metric_name", "system"));
client.create("ts2", convertMap("metric", "cpu", "metric_name", "user"));

client.add("ts1", 1L, 90.0);
client.add("ts1", 2L, 45.0);
client.add("ts2", 2L, 99.0);

Range[] range =
client.mrange(
0L,
100L,
MultiRangeParams.multiRangeParams().withLabels().groupByReduce("metric_name", "max"),
"metric=cpu");
assertEquals(2, range.length);

assertEquals("metric_name=system", range[0].getKey());
assertEquals("system", range[0].getLabels().get("metric_name"));
assertEquals("max", range[0].getLabels().get("__reducer__"));
assertEquals("ts1", range[0].getLabels().get("__source__"));

assertEquals("metric_name=user", range[1].getKey());
assertEquals("user", range[1].getLabels().get("metric_name"));
assertEquals("max", range[1].getLabels().get("__reducer__"));
assertEquals("ts2", range[1].getLabels().get("__source__"));
}

private Map<String, String> convertMap(String... array) {
Map<String, String> map = new HashMap<>(array.length / 2);
for (int i = 0; i < array.length; i += 2) {
map.put(array[i], array[i + 1]);
}
return map;
}

@Test
public void testGet() {

0 comments on commit 7d021c2

Please sign in to comment.