|
17 | 17 | package io.github.dengliming.redismodule.redistimeseries; |
18 | 18 |
|
19 | 19 | import org.junit.Assert; |
| 20 | +import org.junit.Ignore; |
20 | 21 | import org.junit.jupiter.api.Test; |
21 | 22 | import org.redisson.client.RedisException; |
22 | 23 |
|
| 24 | +import java.time.Instant; |
| 25 | +import java.time.temporal.ChronoUnit; |
23 | 26 | import java.util.List; |
24 | 27 | import java.util.Map; |
25 | 28 |
|
@@ -152,6 +155,76 @@ public void testRange() { |
152 | 155 | assertThat(timeSeries).isEmpty(); |
153 | 156 | } |
154 | 157 |
|
| 158 | + @Test |
| 159 | + public void testAggregations() { |
| 160 | + RedisTimeSeries redisTimeSeries = getRedisTimeSeries(); |
| 161 | + long timestamp = System.currentTimeMillis(); |
| 162 | + String sensor = "temperature:sum"; |
| 163 | + |
| 164 | + assertThat(redisTimeSeries.incrBy(sensor, 10, timestamp, new TimeSeriesOptions() |
| 165 | + .retentionTime(6000L) |
| 166 | + .unCompressed()).longValue()).isEqualTo(timestamp); |
| 167 | + |
| 168 | + assertThat(redisTimeSeries.incrBy(sensor, 20, timestamp + 1).longValue()).isEqualTo(timestamp + 1); |
| 169 | + |
| 170 | + List<Value> values = redisTimeSeries.range(sensor, timestamp, timestamp + 1); |
| 171 | + assertThat(values).hasSize(2); |
| 172 | + |
| 173 | + List<Value> sum = redisTimeSeries.range(sensor, timestamp, timestamp + 10, new RangeOptions() |
| 174 | + .aggregationType(Aggregation.SUM, 60000L)); |
| 175 | + |
| 176 | + assertThat(sum).hasSize(1); |
| 177 | + // Timestamp trimmed to timeBucket (minutes) |
| 178 | + assertThat(sum.get(0).getTimestamp()).isEqualTo(Instant.ofEpochMilli(timestamp).truncatedTo(ChronoUnit.MINUTES).toEpochMilli()); |
| 179 | + assertThat(sum.get(0).getValue()).isEqualTo(40.0d); |
| 180 | + } |
| 181 | + |
| 182 | + @Ignore("Only for redis timeseries > 1.6.0") |
| 183 | + public void testAggregationsAlign() { |
| 184 | + RedisTimeSeries redisTimeSeries = getRedisTimeSeries(); |
| 185 | + long from = 1L; |
| 186 | + long to = 10000L; |
| 187 | + long timeBucket = 3000L; |
| 188 | + |
| 189 | + String sensor = "temperature:sum:align"; |
| 190 | + TimeSeriesOptions options = new TimeSeriesOptions().unCompressed(); |
| 191 | + |
| 192 | + /* |
| 193 | + TS: 1000 | 2000 | 3000 | 4000 |
| 194 | + VAL: 1 | 1 | 10 | 10 |
| 195 | + BT : -------------------|----- |
| 196 | + */ |
| 197 | + |
| 198 | + assertThat(redisTimeSeries.add(new Sample(sensor, Value.of(1000L, 1.0d)), options).longValue()).isEqualTo(1000L); |
| 199 | + assertThat(redisTimeSeries.add(new Sample(sensor, Value.of(2000L, 1.0d)), options).longValue()).isEqualTo(2000L); |
| 200 | + assertThat(redisTimeSeries.add(new Sample(sensor, Value.of(3000L, 10.0d)), options).longValue()).isEqualTo(3000L); |
| 201 | + assertThat(redisTimeSeries.add(new Sample(sensor, Value.of(4000L, 10.0d)), options).longValue()).isEqualTo(4000L); |
| 202 | + |
| 203 | + List<Value> values = redisTimeSeries.range(sensor, from, to); |
| 204 | + assertThat(values).hasSize(4); |
| 205 | + |
| 206 | + List<Value> start = redisTimeSeries.range(sensor, from, to, new RangeOptions() |
| 207 | + .aggregationType(Aggregation.SUM, timeBucket, Align.START)); |
| 208 | + |
| 209 | + assertThat(start).hasSize(2); |
| 210 | + assertThat(start.get(0).getTimestamp()).isEqualTo(from); |
| 211 | + assertThat(start.get(0).getValue()).isEqualTo(12.0d); |
| 212 | + |
| 213 | + assertThat(start.get(1).getTimestamp()).isEqualTo(from + timeBucket); |
| 214 | + assertThat(start.get(1).getValue()).isEqualTo(10.0d); |
| 215 | + |
| 216 | + List<Value> end = redisTimeSeries.range(sensor, from, to, new RangeOptions() |
| 217 | + .aggregationType(Aggregation.SUM, timeBucket, Align.END)); |
| 218 | + |
| 219 | + assertThat(end).hasSize(2); |
| 220 | + |
| 221 | + assertThat(end.get(0).getTimestamp()).isEqualTo(1000L); |
| 222 | + assertThat(end.get(0).getValue()).isEqualTo(12.0d); |
| 223 | + |
| 224 | + assertThat(end.get(1).getTimestamp()).isEqualTo(4000L); |
| 225 | + assertThat(end.get(1).getValue()).isEqualTo(10.0d); |
| 226 | + } |
| 227 | + |
155 | 228 | @Test |
156 | 229 | public void testQueryIndex() { |
157 | 230 | RedisTimeSeries redisTimeSeries = getRedisTimeSeries(); |
|
0 commit comments