From 04e047eaacbf5a3cbdde422bb2127d8f8198956d Mon Sep 17 00:00:00 2001 From: quylt Date: Tue, 7 Feb 2023 17:38:39 +0700 Subject: [PATCH] Finis challenge #4 --- .../university/RU102J/dao/CapacityDaoRedisImpl.java | 8 +++++++- .../university/RU102J/dao/MetricDaoRedisZsetImpl.java | 2 +- .../university/RU102J/dao/SiteStatsDaoRedisImpl.java | 3 +-- .../university/RU102J/dao/CapacityDaoRedisImplTest.java | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImpl.java index 8e24d22..8efb2c4 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImpl.java @@ -59,7 +59,13 @@ public CapacityReport getReport(Integer limit) { @Override public Long getRank(Long siteId) { // START Challenge #4 - return -2L; +// return -2L; // END Challenge #4 + + String capacityRankingKey = RedisSchema.getCapacityRankingKey(); + + try (Jedis jedis = jedisPool.getResource()) { + return jedis.zrevrank(capacityRankingKey, String.valueOf(siteId)); + } } } diff --git a/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java index 72d3cdf..c6e22ca 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/MetricDaoRedisZsetImpl.java @@ -54,8 +54,8 @@ private void insertMetric(Jedis jedis, long siteId, double value, MetricUnit uni // END Challenge #2 MeasurementMinute mm = new MeasurementMinute(value, minuteOfDay); - jedis.zadd(metricKey, minuteOfDay, mm.toString()); + jedis.expire(metricKey, METRIC_EXPIRATION_SECONDS); } /** diff --git a/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java index faa8be4..5361bfe 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteStatsDaoRedisImpl.java @@ -83,6 +83,7 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) { // Challenge #3 private void updateOptimized(Jedis jedis, String key, MeterReading reading) { // START Challenge #3 + // END Challenge #3 String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString(); @@ -98,8 +99,6 @@ private void updateOptimized(Jedis jedis, String key, MeterReading reading) { script.updateIfGreater(multi, key, SiteStats.maxCapacityField, getCurrentCapacity(reading)); multi.exec(); - - // END Challenge #3 } private Double getCurrentCapacity(MeterReading reading) { diff --git a/src/test/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImplTest.java b/src/test/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImplTest.java index 82917b6..f0b635e 100644 --- a/src/test/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImplTest.java +++ b/src/test/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImplTest.java @@ -71,7 +71,7 @@ public void getReport() { } // Challenge #4 - @Ignore +// @Ignore @Test public void getRank() { CapacityDao dao = new CapacityDaoRedisImpl(jedisPool);