Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Govind Balaji S committed Oct 16, 2022
1 parent 3ba2146 commit ae22a0d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import java.text.DecimalFormat;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* Retain metrics using Redis sorted sets.
Expand Down Expand Up @@ -50,7 +53,8 @@ private void insertMetric(Jedis jedis, long siteId, double value, MetricUnit uni
// START Challenge #2
String metricKey = RedisSchema.getDayMetricKey(siteId, unit, dateTime);
Integer minuteOfDay = getMinuteOfDay(dateTime);
jedis.zadd(metricKey, minuteOfDay, value + ":" + minuteOfDay);
jedis.zadd(metricKey, minuteOfDay, new MeasurementMinute(value, minuteOfDay).toString());
jedis.expire(metricKey, METRIC_EXPIRATION_SECONDS);
// END Challenge #2
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ private void updateBasic(Jedis jedis, String key, MeterReading reading) {
// Challenge #3
private void updateOptimized(Jedis jedis, String key, MeterReading reading) {
// START Challenge #3
Transaction transaction = jedis.multi();
String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString();
transaction.hset(key, SiteStats.reportingTimeField, reportingTime);
transaction.hincrBy(key, SiteStats.countField, 1);
transaction.expire(key, weekSeconds);
compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxWhField, reading.getWhGenerated());
compareAndUpdateScript.updateIfLess(transaction, key, SiteStats.minWhField, reading.getWhGenerated());
compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxCapacityField, getCurrentCapacity(reading));

transaction.exec();
try (Transaction transaction = jedis.multi()) {
String reportingTime = ZonedDateTime.now(ZoneOffset.UTC).toString();
transaction.hset(key, SiteStats.reportingTimeField, reportingTime);
transaction.hincrBy(key, SiteStats.countField, 1);
transaction.expire(key, weekSeconds);
compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxWhField, reading.getWhGenerated());
compareAndUpdateScript.updateIfLess(transaction, key, SiteStats.minWhField, reading.getWhGenerated());
compareAndUpdateScript.updateIfGreater(transaction, key, SiteStats.maxCapacityField, getCurrentCapacity(reading));

transaction.exec();
}
// END Challenge #3
}

Expand Down

0 comments on commit ae22a0d

Please sign in to comment.