diff --git a/src/main/java/redis/clients/jedis/timeseries/TSMRangeElements.java b/src/main/java/redis/clients/jedis/timeseries/TSMRangeElements.java index d6f0614c49..001b8159a0 100644 --- a/src/main/java/redis/clients/jedis/timeseries/TSMRangeElements.java +++ b/src/main/java/redis/clients/jedis/timeseries/TSMRangeElements.java @@ -8,17 +8,31 @@ public class TSMRangeElements extends KeyValue> { private final Map labels; private final List aggregators; + private final List reducers; + private final String sources; public TSMRangeElements(String key, Map labels, List value) { super(key, value); this.labels = labels; this.aggregators = null; + this.reducers = null; + this.sources = null; } public TSMRangeElements(String key, Map labels, List aggregators, List value) { super(key, value); this.labels = labels; this.aggregators = aggregators; + this.reducers = null; + this.sources = null; + } + + public TSMRangeElements(String key, Map labels, List reducers, String sources, List value) { + super(key, value); + this.labels = labels; + this.aggregators = null; + this.reducers = reducers; + this.sources = sources; } public Map getLabels() { @@ -29,16 +43,28 @@ public List getAggregators() { return aggregators; } + public List getReducers() { + return reducers; + } + + public String getSources() { + return sources; + } + public List getElements() { return getValue(); } @Override public String toString() { - return new StringBuilder().append(getClass().getSimpleName()) - .append("{key=").append(getKey()) - .append(", labels=").append(labels) - .append(", elements=").append(getElements()) - .append('}').toString(); + StringBuilder sb = new StringBuilder().append(getClass().getSimpleName()) + .append("{key=").append(getKey()).append(", labels=").append(labels); + if (aggregators != null) { + sb.append(", aggregators=").append(aggregators); + } + if (reducers != null && sources != null) { + sb.append(", reducers").append(reducers).append(", sources").append(sources); + } + return sb.append(", elements=").append(getElements()).append('}').toString(); } } diff --git a/src/main/java/redis/clients/jedis/timeseries/TimeSeriesBuilderFactory.java b/src/main/java/redis/clients/jedis/timeseries/TimeSeriesBuilderFactory.java index e7917f81bf..d360121c2b 100644 --- a/src/main/java/redis/clients/jedis/timeseries/TimeSeriesBuilderFactory.java +++ b/src/main/java/redis/clients/jedis/timeseries/TimeSeriesBuilderFactory.java @@ -1,15 +1,14 @@ package redis.clients.jedis.timeseries; -import static java.util.function.Function.identity; - import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; + import redis.clients.jedis.Builder; import redis.clients.jedis.BuilderFactory; -import redis.clients.jedis.util.SafeEncoder; public final class TimeSeriesBuilderFactory { @@ -40,7 +39,7 @@ public Map build(Object data) { .map((tsList) -> new TSMRangeElements(BuilderFactory.STRING.build(tsList.get(0)), BuilderFactory.STRING_MAP_FROM_PAIRS.build(tsList.get(1)), TIMESERIES_ELEMENT_LIST.build(tsList.get(2)))) - .collect(Collectors.toMap(TSMRangeElements::getKey, identity(), + .collect(Collectors.toMap(TSMRangeElements::getKey, Function.identity(), (x, y) -> x, LinkedHashMap::new)); } }; @@ -66,10 +65,14 @@ public Map build(Object data) { TIMESERIES_ELEMENT_LIST.build(valueList.get(2))); break; case 4: + List rdcMapObj = (List) valueList.get(1); + assert "reducers".equalsIgnoreCase(BuilderFactory.STRING.build(rdcMapObj.get(0))); + List srcMapObj = (List) valueList.get(1); + assert "sources".equalsIgnoreCase(BuilderFactory.STRING.build(srcMapObj.get(0))); elements = new TSMRangeElements(key, BuilderFactory.STRING_MAP.build(valueList.get(0)), - // TODO: valueList.get(1) - // TODO: valueList.get(2) + ((List) rdcMapObj.get(1)).stream().map(BuilderFactory.STRING::build).collect(Collectors.toList()), + BuilderFactory.STRING.build(srcMapObj.get(1)), TIMESERIES_ELEMENT_LIST.build(valueList.get(3))); break; default: @@ -89,7 +92,7 @@ public Map build(Object data) { .map((tsList) -> new TSMGetElement(BuilderFactory.STRING.build(tsList.get(0)), BuilderFactory.STRING_MAP_FROM_PAIRS.build(tsList.get(1)), TIMESERIES_ELEMENT.build(tsList.get(2)))) - .collect(Collectors.toMap(TSMGetElement::getKey, identity())); + .collect(Collectors.toMap(TSMGetElement::getKey, Function.identity())); } }; diff --git a/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java index ed38fec0c0..ecd759c392 100644 --- a/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java +++ b/src/test/java/redis/clients/jedis/modules/timeseries/TimeSeriesTest.java @@ -11,6 +11,7 @@ import java.util.*; import org.junit.BeforeClass; import org.junit.Test; +import redis.clients.jedis.RedisProtocol; import redis.clients.jedis.exceptions.JedisDataException; import redis.clients.jedis.modules.RedisModuleCommandsTestBase; @@ -565,14 +566,24 @@ public void groupByReduce() { assertEquals("metric_name=system", rangeList.get(0).getKey()); assertEquals("system", rangeList.get(0).getLabels().get("metric_name")); -// assertEquals("max", rangeList.get(0).getLabels().get("__reducer__")); -// assertEquals("ts1", rangeList.get(0).getLabels().get("__source__")); + if (protocol != RedisProtocol.RESP3) { + assertEquals("max", rangeList.get(0).getLabels().get("__reducer__")); + assertEquals("ts1", rangeList.get(0).getLabels().get("__source__")); + } else { + assertEquals(Arrays.asList("max"), rangeList.get(0).getReducers()); + assertEquals("ts1", rangeList.get(0).getSources()); + } assertEquals(Arrays.asList(new TSElement(1, 90), new TSElement(2, 45)), rangeList.get(0).getValue()); assertEquals("metric_name=user", rangeList.get(1).getKey()); assertEquals("user", rangeList.get(1).getLabels().get("metric_name")); -// assertEquals("max", rangeList.get(1).getLabels().get("__reducer__")); -// assertEquals("ts2", rangeList.get(1).getLabels().get("__source__")); + if (protocol != RedisProtocol.RESP3) { + assertEquals("max", rangeList.get(1).getLabels().get("__reducer__")); + assertEquals("ts2", rangeList.get(1).getLabels().get("__source__")); + } else { + assertEquals(Arrays.asList("max"), rangeList.get(0).getReducers()); + assertEquals("ts2", rangeList.get(0).getSources()); + } assertEquals(Arrays.asList(new TSElement(2, 99)), rangeList.get(1).getValue()); }