Skip to content

Commit

Permalink
Use expiring cache to keep measurements.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Feb 26, 2024
1 parent e2cad4b commit 5c27fb3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void initialize(Bootstrap<VerkeersDrukteAppConfig> bootstrap) {

@Override
public void run(VerkeersDrukteAppConfig configuration, Environment environment) {
TrafficHandler ndwHandler = new TrafficHandler(configuration.getNdwConfig());
TrafficHandler ndwHandler = new TrafficHandler(configuration);
VerkeersDrukteResource resource = new VerkeersDrukteResource(ndwHandler, configuration.getTrafficConfig());
environment.healthChecks().register("ndw", new VerkeersDrukteHealthCheck(ndwHandler));
environment.jersey().register(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import nl.bertriksikken.verkeersdrukte.traffic.TrafficConfig;

@JsonIgnoreProperties(ignoreUnknown = true)
final class VerkeersDrukteAppConfig extends Configuration {
public final class VerkeersDrukteAppConfig extends Configuration {

@JsonProperty("ndw")
private NdwConfig ndwConfig = new NdwConfig();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package nl.bertriksikken.verkeersdrukte.traffic;

import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.time.Duration;

public final class MeasurementCache {

private final Instant publishedDateTime;
private Map<String, AggregateMeasurement> measurementMap = new ConcurrentHashMap<>();
private final Cache<String, AggregateMeasurement> cache;

MeasurementCache(Instant publishedDateTime) {
this.publishedDateTime = publishedDateTime;
MeasurementCache(Duration expiryDuration) {
cache = CacheBuilder.newBuilder().expireAfterWrite(expiryDuration).build();
}

public void put(String location, AggregateMeasurement measurement) {
measurementMap.put(location, measurement);
cache.put(location, measurement);
}

public AggregateMeasurement get(String location) {
return measurementMap.get(location);
return cache.getIfPresent(location);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package nl.bertriksikken.verkeersdrukte.traffic;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.Duration;
import java.time.ZoneId;
import java.util.TimeZone;

@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE)
public final class TrafficConfig {
Expand All @@ -14,6 +13,8 @@ public final class TrafficConfig {
private String timeZone = "Europe/Amsterdam";
@JsonProperty("baseUrl")
private String baseUrl = "http://stofradar.nl:9002";
@JsonProperty("expiryDurationMinutes")
private int expiryDurationMinutes = 1440;

public ZoneId getTimeZone() {
return ZoneId.of(timeZone);
Expand All @@ -22,4 +23,8 @@ public ZoneId getTimeZone() {
public String getBaseUrl() {
return baseUrl;
}

public Duration getExpiryDuration() {
return Duration.ofMinutes(expiryDurationMinutes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import nl.bertriksikken.shapefile.EShapeType;
import nl.bertriksikken.shapefile.ShapeFile;
import nl.bertriksikken.shapefile.ShapeRecord;
import nl.bertriksikken.verkeersdrukte.app.VerkeersDrukteAppConfig;
import nl.bertriksikken.verkeersdrukte.ndw.FileResponse;
import nl.bertriksikken.verkeersdrukte.ndw.NdwClient;
import nl.bertriksikken.verkeersdrukte.ndw.NdwConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -37,12 +37,13 @@ public final class TrafficHandler implements ITrafficHandler, Managed {
private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
private final ObjectMapper xmlMapper = new XmlMapper();
private final NdwClient ndwClient;
private MeasurementCache measurementCache = new MeasurementCache(Instant.now());
private final MeasurementCache measurementCache;
private FeatureCollection shapeFile;

public TrafficHandler(NdwConfig config) {
ndwClient = NdwClient.create(config);
public TrafficHandler(VerkeersDrukteAppConfig config) {
xmlMapper.findAndRegisterModules();
ndwClient = NdwClient.create(config.getNdwConfig());
measurementCache = new MeasurementCache(config.getTrafficConfig().getExpiryDuration());
}

@Override
Expand Down Expand Up @@ -89,7 +90,7 @@ private void downloadTrafficSpeed() {
Duration age = Duration.between(response.getLastModified(), Instant.now());
next = response.getLastModified().plusSeconds(65);
LOG.info("Got data, {} bytes, age {}", response.getContents().length, age);
measurementCache = decode(new ByteArrayInputStream(response.getContents()));
decode(new ByteArrayInputStream(response.getContents()));
} catch (IOException e) {
LOG.warn("Download failed", e);
next = Instant.now().plusSeconds(60);
Expand All @@ -106,15 +107,14 @@ private void downloadTrafficSpeed() {
notifyClients();
}

private MeasurementCache decode(ByteArrayInputStream inputStream) throws IOException {
private void decode(ByteArrayInputStream inputStream) throws IOException {
try (GZIPInputStream gzis = new GZIPInputStream(inputStream)) {
MeasuredDataPublication publication = MeasuredDataPublication.parse(gzis);
MeasurementCache snapshot = new MeasurementCache(publication.getPublicationTime());
LOG.info("Got data for time {}", publication.getPublicationTime());
for (SiteMeasurements measurements : publication.getSiteMeasurementsList()) {
AggregateMeasurement aggregateMeasurement = aggregateValues(measurements);
snapshot.put(measurements.reference.id, aggregateMeasurement);
measurementCache.put(measurements.reference.id, aggregateMeasurement);
}
return snapshot;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package nl.bertriksikken.verkeersdrukte.traffic;

import nl.bertriksikken.verkeersdrukte.ndw.NdwConfig;
import nl.bertriksikken.verkeersdrukte.app.VerkeersDrukteAppConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -11,7 +11,7 @@ public final class TrafficHandlerTest {
private static final Logger LOG = LoggerFactory.getLogger(TrafficHandlerTest.class);

public static void main(String[] args) throws IOException {
NdwConfig config = new NdwConfig();
VerkeersDrukteAppConfig config = new VerkeersDrukteAppConfig();
TrafficHandler trafficHandler = new TrafficHandler(config);
trafficHandler.start();
trafficHandler.subscribe("client", TrafficHandlerTest::notifyData);
Expand Down

0 comments on commit 5c27fb3

Please sign in to comment.