Skip to content

Commit

Permalink
streams challenges
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Prieto committed Apr 2, 2020
1 parent 81a012e commit 72199d5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/daos/impl/redis/feed_dao_redis_impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ const insert = async (meterReading) => {
const pipeline = client.batch();

// START Challenge #6
const globalFeedKey = keyGenerator.getGlobalFeedKey();
const feedKey = keyGenerator.getFeedKey(meterReading.siteId);
pipeline.xaddAsync(globalFeedKey, '*', ...fields);
pipeline.xaddAsync(feedKey, '*', ...fields);

// END Challenge #6

await pipeline.execAsync();
Expand Down
8 changes: 3 additions & 5 deletions src/daos/impl/redis/site_geo_dao_redis_impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const findAll = async () => {
const resultSites = await pipeline.execAsync();

for (const site of resultSites) {
if (site) {
if (site) {
sites.push(remap(site));
}
}
Expand Down Expand Up @@ -177,10 +177,6 @@ const findByGeo = async (lat, lng, radius, radiusUnit) => {
* @returns {Promise} - a Promise, resolving to an array of site objects.
*/
const findByGeoWithExcessCapacity = async (lat, lng, radius, radiusUnit) => {
/* eslint-disable no-unreachable */
// Challenge #5, remove the next line...
return [];

const client = redis.getClient();

// Create a pipeline to send multiple commands in one round trip.
Expand All @@ -202,8 +198,10 @@ const findByGeoWithExcessCapacity = async (lat, lng, radius, radiusUnit) => {
// Create a key for a temporary sorted set containing sites that fell
// within the radius and their current capacities.
const sitesInRadiusCapacitySortedSetKey = keyGenerator.getTemporaryKey();
const capacityRankingKey = keyGenerator.getCapacityRankingKey();

// START Challenge #5
setOperationsPipeline.zinterstoreAsync(sitesInRadiusCapacitySortedSetKey, 2, sitesInRadiusSortedSetKey, capacityRankingKey, 'WEIGHTS', 0, 1);
// END Challenge #5

// Expire the temporary sorted sets after 30 seconds, so that we
Expand Down
6 changes: 3 additions & 3 deletions tests/feed_dao_redis_impl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,19 @@ const insertAndReadBackFromStream = async (siteId) => {
};

// This test is for Challenge #6.
test.skip(`${testSuiteName}: insert and read back from global stream`, async () => {
test(`${testSuiteName}: insert and read back from global stream`, async () => {
await insertAndReadBackFromStream();
});

// This test is for Challenge #6.
test.skip(`${testSuiteName}: read stream for site that does not exist`, async () => {
test(`${testSuiteName}: read stream for site that does not exist`, async () => {
const meterReadings = await redisFeedDAO.getRecentForSite(-1, 100);

expect(meterReadings.length).toBe(0);
});

// This test is for Challenge #6.
test.skip(`${testSuiteName}: insert and read back from site specific stream`, async () => {
test(`${testSuiteName}: insert and read back from site specific stream`, async () => {
await insertAndReadBackFromStream(998);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/site_geo_dao_redis_impl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ test(`${testSuiteName}: findByGeo no results`, async () => {
});

// This test is for Challenge #5.
test.skip(`${testSuiteName}: findByGeoWithExcessCapacity`, async () => {
test(`${testSuiteName}: findByGeoWithExcessCapacity`, async () => {
const site1 = {
id: 1,
capacity: 4.5,
Expand Down

0 comments on commit 72199d5

Please sign in to comment.