Skip to content

Commit 2ce8921

Browse files
committed
Support GROUPBY and REDUCE options
1 parent 942cc7d commit 2ce8921

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

src/main/java/com/redislabs/redistimeseries/Keyword.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public enum Keyword implements ProtocolCommand {
1515
UNCOMPRESSED,
1616
CHUNK_SIZE,
1717
DUPLICATE_POLICY,
18-
ON_DUPLICATE;
18+
ON_DUPLICATE,
19+
GROUPBY,
20+
REDUCE;
1921

2022
private final byte[] raw;
2123

src/main/java/com/redislabs/redistimeseries/MultiRangeParams.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public class MultiRangeParams {
1414

1515
private boolean withLabels;
1616

17+
private String[] groupByReduce;
18+
1719
public static MultiRangeParams multiRangeParams() {
1820
return new MultiRangeParams();
1921
}
@@ -41,6 +43,11 @@ public MultiRangeParams withLabels(boolean withLabels) {
4143
return this;
4244
}
4345

46+
public MultiRangeParams groupByReduce(String group, String reduce) {
47+
this.groupByReduce = new String[] {group, reduce};
48+
return this;
49+
}
50+
4451
public byte[][] getByteParams(long from, long to, String... filters) {
4552
List<byte[]> params = new ArrayList<>();
4653
params.add(Protocol.toByteArray(from));
@@ -66,6 +73,13 @@ public byte[][] getByteParams(long from, long to, String... filters) {
6673
params.add(SafeEncoder.encode(filter));
6774
}
6875

76+
if (groupByReduce != null) {
77+
params.add(Keyword.GROUPBY.getRaw());
78+
params.add(SafeEncoder.encode(groupByReduce[0]));
79+
params.add(Keyword.REDUCE.getRaw());
80+
params.add(SafeEncoder.encode(groupByReduce[1]));
81+
}
82+
6983
return params.toArray(new byte[params.size()][]);
7084
}
7185
}

src/test/java/com/redislabs/redistimeseries/RedisTimeSeriesTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.redislabs.redistimeseries;
22

3+
import static org.junit.Assert.assertArrayEquals;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertTrue;
56
import static org.junit.Assert.fail;
@@ -409,6 +410,44 @@ public void testIncrByDecrBy() throws InterruptedException {
409410
}
410411
}
411412

413+
@Test
414+
public void groupByReduce() {
415+
client.create("ts1", convertMap("metric", "cpu", "metric_name", "system"));
416+
client.create("ts2", convertMap("metric", "cpu", "metric_name", "user"));
417+
418+
client.add("ts1", 1L, 90.0);
419+
client.add("ts1", 2L, 45.0);
420+
client.add("ts2", 2L, 99.0);
421+
422+
Range[] range =
423+
client.mrange(
424+
0L,
425+
100L,
426+
MultiRangeParams.multiRangeParams().withLabels().groupByReduce("metric_name", "max"),
427+
"metric=cpu");
428+
assertEquals(2, range.length);
429+
430+
assertEquals("metric_name=system", range[0].getKey());
431+
assertEquals("system", range[0].getLabels().get("metric_name"));
432+
assertEquals("max", range[0].getLabels().get("__reducer__"));
433+
assertEquals("ts1", range[0].getLabels().get("__source__"));
434+
assertArrayEquals(new Value[] {new Value(1, 90), new Value(2, 45)}, range[0].getValues());
435+
436+
assertEquals("metric_name=user", range[1].getKey());
437+
assertEquals("user", range[1].getLabels().get("metric_name"));
438+
assertEquals("max", range[1].getLabels().get("__reducer__"));
439+
assertEquals("ts2", range[1].getLabels().get("__source__"));
440+
assertArrayEquals(new Value[] {new Value(2, 99)}, range[1].getValues());
441+
}
442+
443+
private Map<String, String> convertMap(String... array) {
444+
Map<String, String> map = new HashMap<>(array.length / 2);
445+
for (int i = 0; i < array.length; i += 2) {
446+
map.put(array[i], array[i + 1]);
447+
}
448+
return map;
449+
}
450+
412451
@Test
413452
public void testGet() {
414453

0 commit comments

Comments
 (0)