From 4f28f8acd0e63790be97a8907ce3b29eccac09d4 Mon Sep 17 00:00:00 2001 From: Thierno Diallo Date: Mon, 14 Nov 2022 09:14:46 -0500 Subject: [PATCH] Programming Challenge #4 --- .../RU102J/dao/CapacityDaoRedisImpl.java | 106 +++++++++--------- .../RU102J/dao/CapacityDaoRedisImplTest.java | 1 - 2 files changed, 54 insertions(+), 53 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..e5e566a 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/CapacityDaoRedisImpl.java @@ -3,63 +3,65 @@ import com.redislabs.university.RU102J.api.CapacityReport; import com.redislabs.university.RU102J.api.MeterReading; import com.redislabs.university.RU102J.api.SiteCapacityTuple; -import redis.clients.jedis.*; import java.util.List; import java.util.Set; import java.util.stream.Collectors; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.Pipeline; +import redis.clients.jedis.Response; +import redis.clients.jedis.Tuple; + public class CapacityDaoRedisImpl implements CapacityDao { - private final JedisPool jedisPool; - - public CapacityDaoRedisImpl(JedisPool jedisPool) { - this.jedisPool = jedisPool; - } - - @Override - public void update(MeterReading reading) { - String capacityRankingKey = RedisSchema.getCapacityRankingKey(); - Long siteId = reading.getSiteId(); - - double currentCapacity = reading.getWhGenerated() - reading.getWhUsed(); - - try (Jedis jedis = jedisPool.getResource()) { - jedis.zadd(capacityRankingKey, currentCapacity, String.valueOf(siteId)); - } - } - - @Override - public CapacityReport getReport(Integer limit) { - CapacityReport report; - String key = RedisSchema.getCapacityRankingKey(); - - try (Jedis jedis = jedisPool.getResource()) { - Pipeline p = jedis.pipelined(); - Response> lowCapacity = p.zrangeWithScores(key, 0, limit-1); - Response> highCapacity = p.zrevrangeWithScores(key, 0, - limit-1); - p.sync(); - - List lowCapacityList = lowCapacity.get().stream() - .map(SiteCapacityTuple::new) - .collect(Collectors.toList()); - - List highCapacityList = highCapacity.get().stream() - .map(SiteCapacityTuple::new) - .collect(Collectors.toList()); - - report = new CapacityReport(highCapacityList, lowCapacityList); - } - - return report; - } - - // Challenge #4 - @Override - public Long getRank(Long siteId) { - // START Challenge #4 - return -2L; - // END Challenge #4 - } + private final JedisPool jedisPool; + + public CapacityDaoRedisImpl( JedisPool jedisPool ) { + this.jedisPool = jedisPool; + } + + @Override + public void update( MeterReading reading ) { + String capacityRankingKey = RedisSchema.getCapacityRankingKey(); + Long siteId = reading.getSiteId(); + + double currentCapacity = reading.getWhGenerated() - reading.getWhUsed(); + + try ( Jedis jedis = jedisPool.getResource() ) { + jedis.zadd( capacityRankingKey, currentCapacity, String.valueOf( siteId ) ); + } + } + + @Override + public CapacityReport getReport( Integer limit ) { + CapacityReport report; + String key = RedisSchema.getCapacityRankingKey(); + + try ( Jedis jedis = jedisPool.getResource() ) { + Pipeline p = jedis.pipelined(); + Response> lowCapacity = p.zrangeWithScores( key, 0, limit - 1 ); + Response> highCapacity = p.zrevrangeWithScores( key, 0, limit - 1 ); + p.sync(); + + List lowCapacityList = lowCapacity.get().stream().map( SiteCapacityTuple::new ).collect( Collectors.toList() ); + + List highCapacityList = highCapacity.get().stream().map( SiteCapacityTuple::new ).collect( Collectors.toList() ); + + report = new CapacityReport( highCapacityList, lowCapacityList ); + } + + return report; + } + + // Challenge #4 + @Override + public Long getRank( Long siteId ) { + // START Challenge #4 + try ( Jedis jedis = jedisPool.getResource() ) { + return jedis.zrevrank( RedisSchema.getCapacityRankingKey(), String.valueOf( siteId ) ); + } + // END Challenge #4 + } } 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..43c0898 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,6 @@ public void getReport() { } // Challenge #4 - @Ignore @Test public void getRank() { CapacityDao dao = new CapacityDaoRedisImpl(jedisPool);