Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GROUPBY and REDUCE options #71

Merged
merged 3 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/main/java/com/redislabs/redistimeseries/Keyword.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public enum Keyword implements ProtocolCommand {
UNCOMPRESSED,
CHUNK_SIZE,
DUPLICATE_POLICY,
ON_DUPLICATE;
ON_DUPLICATE,
GROUPBY,
REDUCE;

private final byte[] raw;

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

private boolean withLabels;

private String[] groupByReduce;

public static MultiRangeParams multiRangeParams() {
return new MultiRangeParams();
}
Expand Down Expand Up @@ -41,17 +43,22 @@ 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<>();

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 @@ -76,6 +83,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
Expand Up @@ -17,6 +17,8 @@
public class RedisTimeSeries implements AutoCloseable {

private static final byte[] STAR = SafeEncoder.encode("*");
protected static final byte[] PLUS = SafeEncoder.encode("+");
protected static final byte[] MINUS = SafeEncoder.encode("-");

private final Pool<Jedis> pool;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,44 @@ 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__"));
assertArrayEquals(new Value[] {new Value(1, 90), new Value(2, 45)}, range[0].getValues());

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__"));
assertArrayEquals(new Value[] {new Value(2, 99)}, range[1].getValues());
}

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() {

Expand Down