Skip to content

Commit

Permalink
Implement Challenge redislabs-training#3
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkec committed May 5, 2023
1 parent 0430b6c commit c067487
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private void insertMetric(Jedis jedis, long siteId, double value, MetricUnit uni
String metricKey = RedisSchema.getDayMetricKey(siteId, unit, dateTime);
Integer minuteOfDay = getMinuteOfDay(dateTime);
jedis.zadd(metricKey, minuteOfDay, new MeasurementMinute(value, minuteOfDay).toString());
jedis.expire(metricKey, METRIC_EXPIRATION_SECONDS);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void update(MeterReading reading) {
ZonedDateTime day = reading.getDateTime();
String key = RedisSchema.getSiteStatsKey(siteId, day);

updateBasic(jedis, key, reading);
updateOptimized(jedis, key, reading);
}
}

Expand Down Expand Up @@ -80,8 +80,19 @@ 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();
try(Transaction transaction = jedis.multi()) {
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();
}

}

private Double getCurrentCapacity(MeterReading reading) {
Expand Down

0 comments on commit c067487

Please sign in to comment.