Skip to content

Commit

Permalink
Merge pull request #363 from HSLdevcom/feat/reduce-logging-for-empty-…
Browse files Browse the repository at this point in the history
…transport-mode

Reduce logging for empty transport mode
  • Loading branch information
thjarvin authored Sep 12, 2024
2 parents aeb2884 + 7d18410 commit bc6c2c0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>fi.hsl</groupId>
<artifactId>transitdata-common</artifactId>
<version>1.6.3-RC</version>
<version>1.6.4-RC</version>
<packaging>jar</packaging>
<name>Common utilities for Transitdata projects</name>
<properties>
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/fi/hsl/common/hfp/HfpParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
import java.sql.Timestamp;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.io.ByteArrayOutputStream;

public class HfpParser {
private static final Logger log = LoggerFactory.getLogger(HfpParser.class);

static final Pattern topicVersionRegex = Pattern.compile("(^v\\d+|dev)");

private static Set<String> vehiclesWithEmptyTransportMode = new HashSet<>();

// Let's use dsl-json (https://github.com/ngs-doo/dsl-json) for performance.
// Based on this benchmark: https://github.com/fabienrenaud/java-json-benchmark
Expand All @@ -32,6 +36,15 @@ public class HfpParser {

//Note! Apparently not thread safe, for per thread reuse use ThreadLocal pattern or create separate instances
final DslJson<Object> dslJson = new DslJson<>(Settings.withRuntime().allowArrayFormat(true).includeServiceLoader());

private static void foundVehicleWithEmptyTransportMode(String uniqueVehicleId) {
vehiclesWithEmptyTransportMode.add(uniqueVehicleId);

if (vehiclesWithEmptyTransportMode.size() > 100) {
log.warn("Vehicles with empty transport mode: " + vehiclesWithEmptyTransportMode);
vehiclesWithEmptyTransportMode.clear();
}
}

@NotNull
public static HfpParser newInstance() {
Expand Down Expand Up @@ -223,11 +236,12 @@ public static Hfp.Topic parseTopic(@NotNull String topic, long receivedAtMs) thr
}

final String strTransportMode = parts[index++];
boolean transportModeIsEmpty = false;
if (strTransportMode != null && !strTransportMode.isEmpty()) {
final Hfp.Topic.TransportMode transportMode = safeValueOf(Hfp.Topic.TransportMode.class, strTransportMode).orElseThrow(() -> new InvalidHfpTopicException("Unknown transport mode: " + topic));
builder.setTransportMode(transportMode);
} else {
log.info("Transport mode is empty for topic: " + topic);
transportModeIsEmpty = true;
}

final String operatorIdStr = parts[index++];
Expand All @@ -245,6 +259,9 @@ public static Hfp.Topic parseTopic(@NotNull String topic, long receivedAtMs) thr
}

builder.setUniqueVehicleId(createUniqueVehicleId(builder.getOperatorId(), builder.getVehicleNumber()));
if (transportModeIsEmpty) {
foundVehicleWithEmptyTransportMode(builder.getUniqueVehicleId());
}
if (index + 6 <= parts.length) {
HfpValidator.validateString(parts[index++]).ifPresent(builder::setRouteId);
safeParseInt(parts[index++]).ifPresent(builder::setDirectionId);
Expand Down

0 comments on commit bc6c2c0

Please sign in to comment.