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

SELECTED_LABELS option #70

Merged
merged 6 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
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 @@ -11,6 +11,7 @@ public enum Keyword implements ProtocolCommand {
RETENTION,
TIMESTAMP,
WITHLABELS,
SELECTED_LABELS,
COUNT,
UNCOMPRESSED,
CHUNK_SIZE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class MultiRangeParams {
private long timeBucket;

private boolean withLabels;
private String[] selectedLabels;

private String[] groupByReduce;

Expand Down Expand Up @@ -56,6 +57,12 @@ public MultiRangeParams withLabels(boolean withLabels) {
return this;
}

/** NOTE: SELECTED_LABELS and WITHLABELS are mutually exclusive. */
public MultiRangeParams selectedLabels(String... labels) {
this.selectedLabels = labels;
return this;
}

public MultiRangeParams groupByReduce(String group, String reduce) {
this.groupByReduce = new String[] {group, reduce};
return this;
Expand Down Expand Up @@ -101,7 +108,12 @@ public byte[][] getByteParams(Long from, Long to, String... filters) {
params.add(Protocol.toByteArray(timeBucket));
}

if (withLabels) {
if (selectedLabels != null) {
params.add(Keyword.SELECTED_LABELS.getRaw());
for (String label : selectedLabels) {
params.add(SafeEncoder.encode(label));
}
} else if (withLabels) {
params.add(Keyword.WITHLABELS.getRaw());
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/redislabs/redistimeseries/Range.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ protected static Value[] parseRange(List<Object> range) {
private static Map<String, String> getLabelsStringStringMap(List<?> resLabels) {
Map<String, String> rangeLabels = new HashMap<>(resLabels.size());
for (Object resLabel : resLabels) {
List<byte[]> label = (List<byte[]>) resLabel;
rangeLabels.put(SafeEncoder.encode(label.get(0)), SafeEncoder.encode(label.get(1)));
List<byte[]> pair = (List<byte[]>) resLabel;
if (pair.get(0) != null && pair.get(1) != null) {
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
rangeLabels.put(SafeEncoder.encode(pair.get(0)), SafeEncoder.encode(pair.get(1)));
}
}
return rangeLabels;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,48 @@ public void mrangeFilterBy() {
assertArrayEquals(new Value[] {rawValues[0]}, range[0].getValues());
}

@Test
public void mrangeSelectedLabels() {

client.create("ts1", convertMap("l1", "v1", "l2", "v2"));
client.create("ts2", convertMap("l1", "v1", "l3", "v3"));

Value[] rawValues =
new Value[] {
new Value(1000L, 1.0),
new Value(2000L, 0.9),
new Value(3200L, 1.1),
new Value(4500L, -1.1)
};

client.add("ts1", rawValues[0].getTime(), rawValues[0].getValue());
client.add("ts2", rawValues[1].getTime(), rawValues[1].getValue());
client.add("ts2", rawValues[2].getTime(), rawValues[2].getValue());
client.add("ts1", rawValues[3].getTime(), rawValues[3].getValue());

Range[] range =
client.mrange(0L, 5000L, MultiRangeParams.multiRangeParams().withLabels(), "l1=v1");
assertEquals(2, range.length);
assertEquals("ts1", range[0].getKey());
assertEquals(2, range[0].getLabels().size());
assertEquals("ts2", range[1].getKey());
assertEquals(2, range[1].getLabels().size());

range =
client.mrange(
0L, 5000L, MultiRangeParams.multiRangeParams().selectedLabels("l1", "l2"), "l1=v1");
assertEquals(2, range.length);
assertEquals("ts1", range[0].getKey());
assertEquals(2, range[0].getLabels().size());
assertEquals("ts2", range[1].getKey());
assertEquals(1, range[1].getLabels().size());

range =
client.mrange(0L, 5000L, MultiRangeParams.multiRangeParams().selectedLabels("l3"), "l2=v2");
assertEquals(1, range.length);
assertEquals(0, range[0].getLabels().size());
}

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