Skip to content

Commit

Permalink
Allow +0000 timestamps (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
grafnu authored Dec 4, 2023
1 parent b59c5bc commit 40c2db3
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
16 changes: 14 additions & 2 deletions common/src/main/java/com/google/udmi/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,25 @@ public static <T> T fromStringStrict(Class<T> targetClass, String messageString)
}

/**
* Get a date object parsed from a string representation.
* Get a Date object parsed from a string representation.
*
* @param timestamp string representation
* @return Date object
*/
public static Date getDate(String timestamp) {
return timestamp == null ? null : Date.from(Instant.parse(timestamp));
return timestamp == null ? null : Date.from(getInstant(timestamp));
}

/**
* Get an Instant object parsed from a string representation. Also perform some munging on the
* input string to handle standard-yet-not-supported formats.
*
* @param timestamp string representation
* @return Instant object
*/
public static Instant getInstant(String timestamp) {
String replaced = timestamp.replaceFirst("\\+0000$", "Z");
return timestamp == null ? null : Instant.parse(replaced);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/traces/simple/devices/AHU-1/002_event_system.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
"mem_total_mb": 25164.88,
"store_total_mb": null
},
"timestamp" : "2022-07-19T05:04:36Z",
"timestamp" : "2022-07-19T05:04:36+0000",
"version" : "1.3.14"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"deviceNumId" : "00000062256946",
"publishTime" : "1999-10-20T01:02:03Z",
"publishTime" : "2022-07-19T05:04:36+0000",
"subFolder" : "system",
"deviceRegistryId" : "ZZ-TRI-FECTA",
"subType" : "event",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"metrics" : {
"mem_total_mb" : 25164.88
},
"timestamp" : "1999-10-20T01:02:03Z",
"timestamp" : "2022-07-19T05:04:36+0000",
"version" : "1.3.14"
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static com.google.udmi.util.GeneralUtils.ifNotNullThen;
import static com.google.udmi.util.JsonUtil.JSON_SUFFIX;
import static com.google.udmi.util.JsonUtil.OBJECT_MAPPER;
import static com.google.udmi.util.JsonUtil.getInstant;
import static com.google.udmi.util.JsonUtil.getTimestamp;
import static java.lang.String.format;
import static java.util.Optional.ofNullable;
Expand Down Expand Up @@ -118,7 +119,8 @@ public class Validator {
public static final String STATE_PREFIX = "state_";
public static final String PROJECT_PROVIDER_PREFIX = "//";
public static final String TIMESTAMP_ZULU_SUFFIX = "Z";
public static final String TIMESTAMP_UTC_SUFFIX = "+00:00";
public static final String TIMESTAMP_UTC_SUFFIX_1 = "+00:00";
public static final String TIMESTAMP_UTC_SUFFIX_2 = "+0000";
private static final String ERROR_FORMAT_INDENT = " ";
private static final String SCHEMA_VALIDATION_FORMAT = "Validating %d schemas";
private static final String TARGET_VALIDATION_FORMAT = "Validating %d files against %s";
Expand Down Expand Up @@ -527,7 +529,7 @@ private void validateMessage(
}

if (simulatedMessages) {
mockNow = Instant.parse((String) message.get(TIMESTAMP_KEY));
mockNow = getInstant((String) message.get(TIMESTAMP_KEY));
ReportingDevice.setMockNow(mockNow);
}
ReportingDevice reportingDevice = validateMessageCore(message, attributes);
Expand Down Expand Up @@ -614,9 +616,9 @@ public void validateDeviceMessage(ReportingDevice device, Map<String, Object> me
upgradeMessage(schemaName, message);

String timestampRaw = (String) message.get("timestamp");
Instant timestamp = ifNotNullGet(timestampRaw, Instant::parse);
Instant timestamp = ifNotNullGet(timestampRaw, JsonUtil::getInstant);
String publishRaw = attributes.get(PUBLISH_TIME_KEY);
Instant publishTime = ifNotNullGet(publishRaw, Instant::parse);
Instant publishTime = ifNotNullGet(publishRaw, JsonUtil::getInstant);
try {
// TODO: Validate message contests to make sure state sub-blocks don't also have timestamp.

Expand All @@ -632,7 +634,8 @@ public void validateDeviceMessage(ReportingDevice device, Map<String, Object> me
}
if (publishTime != null) {
if (!timestampRaw.endsWith(TIMESTAMP_ZULU_SUFFIX)
&& !timestampRaw.endsWith(TIMESTAMP_UTC_SUFFIX)) {
&& !timestampRaw.endsWith(TIMESTAMP_UTC_SUFFIX_1)
&& !timestampRaw.endsWith(TIMESTAMP_UTC_SUFFIX_2)) {
throw new RuntimeException("Invalid timestamp timezone " + timestampRaw);
}
long between = Duration.between(publishTime, timestamp).getSeconds();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.google.daq.mqtt.validator;

import static com.google.udmi.util.Common.TIMESTAMP_KEY;
import static com.google.udmi.util.JsonUtil.getInstant;
import static com.google.udmi.util.JsonUtil.getTimestamp;
import static com.google.udmi.util.JsonUtil.safeSleep;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -145,7 +146,7 @@ public void lastSeenUpdate() {
ValidationState report = getValidationReport();
DeviceValidationEvent deviceValidationEvent = report.devices.get(TestCommon.DEVICE_ID);
Date lastSeen = deviceValidationEvent.last_seen;
Instant parse = Instant.parse((String) eventBundle.message.get(TIMESTAMP_KEY));
Instant parse = getInstant((String) eventBundle.message.get(TIMESTAMP_KEY));
assertEquals("device last seen", getTimestamp(Date.from(parse)), getTimestamp(lastSeen));
}

Expand Down

0 comments on commit 40c2db3

Please sign in to comment.