feat: productionize raw history ingestion
This commit is contained in:
@@ -36,6 +36,10 @@
|
||||
<groupId>org.apache.kafka</groupId>
|
||||
<artifactId>kafka-clients</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.taosdata.jdbc</groupId>
|
||||
<artifactId>taos-jdbcdriver</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
|
||||
@@ -12,6 +12,10 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -27,6 +31,8 @@ import java.util.Map;
|
||||
@RequestMapping("/api/event-history")
|
||||
public class EventHistoryController {
|
||||
|
||||
private static final ZoneId DEFAULT_ZONE = ZoneId.of("Asia/Shanghai");
|
||||
|
||||
private final EventFileStore store;
|
||||
|
||||
public EventHistoryController(EventFileStore store) {
|
||||
@@ -40,8 +46,13 @@ public class EventHistoryController {
|
||||
@RequestParam String dateTo,
|
||||
@RequestParam(defaultValue = "ASC") EventFileQuery.Order order,
|
||||
@RequestParam(defaultValue = "100") int limit,
|
||||
@RequestParam(required = false) String vin) throws IOException {
|
||||
return queryRecords(protocol, dateFrom, dateTo, order, limit, vin).stream()
|
||||
@RequestParam(required = false) String vin,
|
||||
@RequestParam(required = false) String eventType,
|
||||
@RequestParam(required = false) String cursorEventTime,
|
||||
@RequestParam(required = false) String cursorIngestTime,
|
||||
@RequestParam(required = false) String cursorEventId) throws IOException {
|
||||
return queryRecords(protocol, dateFrom, dateTo, order, limit, vin, eventType,
|
||||
cursorEventTime, cursorIngestTime, cursorEventId).stream()
|
||||
.map(RecordResponse::from)
|
||||
.toList();
|
||||
}
|
||||
@@ -51,7 +62,11 @@ public class EventHistoryController {
|
||||
String dateTo,
|
||||
EventFileQuery.Order order,
|
||||
int limit,
|
||||
String vin) throws IOException {
|
||||
String vin,
|
||||
String eventType,
|
||||
String cursorEventTime,
|
||||
String cursorIngestTime,
|
||||
String cursorEventId) throws IOException {
|
||||
QueryTimeRange range = QueryTimeRange.parse(dateFrom, dateTo);
|
||||
return store.query(new EventFileQuery(
|
||||
protocol,
|
||||
@@ -62,7 +77,22 @@ public class EventHistoryController {
|
||||
order,
|
||||
limit,
|
||||
vin,
|
||||
null));
|
||||
eventType,
|
||||
instantParam(cursorEventTime),
|
||||
instantParam(cursorIngestTime),
|
||||
cursorEventId));
|
||||
}
|
||||
|
||||
private static Instant instantParam(String raw) {
|
||||
if (raw == null || raw.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
String value = raw.trim();
|
||||
try {
|
||||
return OffsetDateTime.parse(value).toInstant();
|
||||
} catch (RuntimeException ignored) {
|
||||
return LocalDateTime.parse(value).atZone(DEFAULT_ZONE).toInstant();
|
||||
}
|
||||
}
|
||||
|
||||
public record RecordResponse(
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.lingniu.ingest.eventhistory;
|
||||
|
||||
import com.google.protobuf.InvalidProtocolBufferException;
|
||||
import com.lingniu.ingest.api.consumer.EnvelopeBatchIngestor;
|
||||
import com.lingniu.ingest.api.consumer.EnvelopeIngestResult;
|
||||
import com.lingniu.ingest.api.consumer.EnvelopeIngestor;
|
||||
import com.lingniu.ingest.eventfilestore.EventFileRecord;
|
||||
import com.lingniu.ingest.eventfilestore.EventFileStore;
|
||||
import com.lingniu.ingest.sink.mq.proto.VehicleEnvelope;
|
||||
@@ -20,7 +20,7 @@ import java.util.List;
|
||||
* <p>当前 32960 本机运行配置没有启用 Kafka consumer;这个类保留给“其他服务把 envelope
|
||||
* 写回历史库”的解耦部署方式。失败时返回结构化结果,由 Kafka worker 决定是否进 DLQ。
|
||||
*/
|
||||
public final class EventHistoryEnvelopeIngestor implements EnvelopeIngestor {
|
||||
public final class EventHistoryEnvelopeIngestor implements EnvelopeBatchIngestor {
|
||||
|
||||
private final EventFileStore store;
|
||||
private final TelemetryEnvelopeRecordMapper mapper;
|
||||
@@ -33,7 +33,7 @@ public final class EventHistoryEnvelopeIngestor implements EnvelopeIngestor {
|
||||
|
||||
public EventHistoryEnvelopeIngestor(TelemetryEnvelopeRecordMapper mapper,
|
||||
TdengineHistoryWriter tdengineWriter) {
|
||||
this(mapper, tdengineWriter, true);
|
||||
this(mapper, tdengineWriter, false);
|
||||
}
|
||||
|
||||
public EventHistoryEnvelopeIngestor(TelemetryEnvelopeRecordMapper mapper,
|
||||
@@ -45,7 +45,7 @@ public final class EventHistoryEnvelopeIngestor implements EnvelopeIngestor {
|
||||
public EventHistoryEnvelopeIngestor(EventFileStore store,
|
||||
TelemetryEnvelopeRecordMapper mapper,
|
||||
TdengineHistoryWriter tdengineWriter) {
|
||||
this(store, mapper, tdengineWriter, true, true);
|
||||
this(store, mapper, tdengineWriter, true, false);
|
||||
}
|
||||
|
||||
public EventHistoryEnvelopeIngestor(EventFileStore store,
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.lingniu.ingest.eventhistory;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineHistoryReader;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineLocationQuery;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineLocationRow;
|
||||
@@ -32,8 +30,6 @@ import java.util.List;
|
||||
@Tag(name = "jt-808-location-controller", description = "JT808 位置历史分页查询接口。")
|
||||
public final class Jt808LocationHistoryController {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
private final TdengineHistoryReader reader;
|
||||
|
||||
public Jt808LocationHistoryController(TdengineHistoryReader reader) {
|
||||
@@ -127,8 +123,7 @@ public final class Jt808LocationHistoryController {
|
||||
long alarmFlag,
|
||||
long statusFlag,
|
||||
Double totalMileageKm,
|
||||
String rawUri,
|
||||
String metadataJson) {
|
||||
String rawUri) {
|
||||
|
||||
private static LocationResponse from(TdengineLocationRow row) {
|
||||
return new LocationResponse(
|
||||
@@ -147,23 +142,7 @@ public final class Jt808LocationHistoryController {
|
||||
row.alarmFlag(),
|
||||
row.statusFlag(),
|
||||
row.totalMileageKm(),
|
||||
rawUri(row),
|
||||
row.metadataJson());
|
||||
}
|
||||
|
||||
private static String rawUri(TdengineLocationRow row) {
|
||||
if (row.rawUri() != null && !row.rawUri().isBlank()) {
|
||||
return row.rawUri();
|
||||
}
|
||||
String metadataJson = row.metadataJson();
|
||||
if (metadataJson == null || metadataJson.isBlank()) {
|
||||
return row.rawUri();
|
||||
}
|
||||
try {
|
||||
return OBJECT_MAPPER.readTree(metadataJson).path("rawArchiveUri").asText(row.rawUri());
|
||||
} catch (JsonProcessingException ignored) {
|
||||
return row.rawUri();
|
||||
}
|
||||
row.rawUri());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.lingniu.ingest.eventhistory;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineHistoryReader;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineLocationQuery;
|
||||
import com.lingniu.ingest.tdenginehistory.TdengineLocationRow;
|
||||
@@ -32,8 +30,6 @@ import java.util.Locale;
|
||||
@Tag(name = "location-history-controller", description = "通用位置历史分页查询接口。")
|
||||
public final class LocationHistoryController {
|
||||
|
||||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||||
|
||||
private final TdengineHistoryReader reader;
|
||||
|
||||
public LocationHistoryController(TdengineHistoryReader reader) {
|
||||
@@ -161,7 +157,6 @@ public final class LocationHistoryController {
|
||||
long statusFlag,
|
||||
Double totalMileageKm,
|
||||
String rawUri,
|
||||
String metadataJson,
|
||||
String protocol) {
|
||||
|
||||
private static LocationResponse from(TdengineLocationRow row) {
|
||||
@@ -181,24 +176,8 @@ public final class LocationHistoryController {
|
||||
row.alarmFlag(),
|
||||
row.statusFlag(),
|
||||
row.totalMileageKm(),
|
||||
rawUri(row),
|
||||
row.metadataJson(),
|
||||
row.rawUri(),
|
||||
row.protocol());
|
||||
}
|
||||
|
||||
private static String rawUri(TdengineLocationRow row) {
|
||||
if (row.rawUri() != null && !row.rawUri().isBlank()) {
|
||||
return row.rawUri();
|
||||
}
|
||||
String metadataJson = row.metadataJson();
|
||||
if (metadataJson == null || metadataJson.isBlank()) {
|
||||
return row.rawUri();
|
||||
}
|
||||
try {
|
||||
return OBJECT_MAPPER.readTree(metadataJson).path("rawArchiveUri").asText(row.rawUri());
|
||||
} catch (JsonProcessingException ignored) {
|
||||
return row.rawUri();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +184,7 @@ public final class RawFrameHistoryController {
|
||||
String parseError,
|
||||
String peer,
|
||||
String metadataJson,
|
||||
String parsedJson,
|
||||
Map<String, Object> metadata,
|
||||
Map<String, Object> parsedFields,
|
||||
String protocol,
|
||||
@@ -209,8 +210,9 @@ public final class RawFrameHistoryController {
|
||||
row.parseError(),
|
||||
row.peer(),
|
||||
row.metadataJson(),
|
||||
row.parsedJson(),
|
||||
metadata,
|
||||
parsedFields(reader, row, metadata, includeParsedFields),
|
||||
parsedFields(reader, row, metadata, parsed(row.parsedJson()), includeParsedFields),
|
||||
row.protocol(),
|
||||
row.vehicleKey(),
|
||||
row.vin(),
|
||||
@@ -231,18 +233,23 @@ public final class RawFrameHistoryController {
|
||||
private static Map<String, Object> parsedFields(TdengineHistoryReader reader,
|
||||
TdengineRawFrameRow row,
|
||||
Map<String, Object> metadata,
|
||||
Map<String, Object> parsed,
|
||||
boolean includeParsedFields) throws IOException {
|
||||
Map<String, Object> out = new LinkedHashMap<>();
|
||||
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (key == null || key.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (key.startsWith("raw.") || key.startsWith("jt808.")) {
|
||||
out.put(key, entry.getValue());
|
||||
} else {
|
||||
out.put("raw." + key, entry.getValue());
|
||||
if (parsed.isEmpty()) {
|
||||
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (key == null || key.isBlank()) {
|
||||
continue;
|
||||
}
|
||||
if (key.startsWith("raw.") || key.startsWith("jt808.")) {
|
||||
out.put(key, entry.getValue());
|
||||
} else {
|
||||
out.put("raw." + key, entry.getValue());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
out.putAll(parsed);
|
||||
}
|
||||
if (!includeParsedFields) {
|
||||
return Map.copyOf(out);
|
||||
@@ -262,6 +269,17 @@ public final class RawFrameHistoryController {
|
||||
return Map.copyOf(out);
|
||||
}
|
||||
|
||||
private static Map<String, Object> parsed(String parsedJson) {
|
||||
if (parsedJson == null || parsedJson.isBlank()) {
|
||||
return Map.of();
|
||||
}
|
||||
try {
|
||||
return OBJECT_MAPPER.readValue(parsedJson, MAP_TYPE);
|
||||
} catch (Exception ignored) {
|
||||
return Map.of("_raw", parsedJson);
|
||||
}
|
||||
}
|
||||
|
||||
private static void addLocation(Map<String, Object> out, TdengineLocationRow row) {
|
||||
out.put("location.longitude", row.longitude());
|
||||
out.put("location.latitude", row.latitude());
|
||||
|
||||
@@ -150,6 +150,7 @@ public final class TelemetryEnvelopeRecordMapper {
|
||||
payload.put("infoType", metadata.get("infoType"));
|
||||
}
|
||||
payload.put("rawSizeBytes", rawArchive.getSizeBytes());
|
||||
putParsedJson(payload, rawArchive.getParsedJson());
|
||||
payload.put("metadata", metadata);
|
||||
try {
|
||||
return OBJECT_MAPPER.writeValueAsString(payload);
|
||||
@@ -157,4 +158,16 @@ public final class TelemetryEnvelopeRecordMapper {
|
||||
throw new IllegalArgumentException("failed to serialize raw_archive", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void putParsedJson(Map<String, Object> payload, String parsedJson) {
|
||||
if (parsedJson == null || parsedJson.isBlank()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
payload.put("parsed", OBJECT_MAPPER.readTree(parsedJson));
|
||||
} catch (JsonProcessingException ex) {
|
||||
payload.put("parsed", parsedJson);
|
||||
payload.put("parsedJsonError", ex.getMessage() == null ? ex.getClass().getSimpleName() : ex.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,11 @@ class EventHistoryControllerTest {
|
||||
"2026-06-22",
|
||||
EventFileQuery.Order.DESC,
|
||||
50,
|
||||
"VIN001");
|
||||
"VIN001",
|
||||
"RAW_ARCHIVE",
|
||||
"2026-06-22T08:00:00Z",
|
||||
"2026-06-22T08:00:01Z",
|
||||
"event-0");
|
||||
|
||||
assertThat(records).extracting(EventHistoryController.RecordResponse::eventId).containsExactly("event-1");
|
||||
assertThat(records.getFirst().eventTime()).isEqualTo("2026-06-22T08:00:00Z");
|
||||
@@ -37,6 +41,10 @@ class EventHistoryControllerTest {
|
||||
assertThat(store.lastQuery.order()).isEqualTo(EventFileQuery.Order.DESC);
|
||||
assertThat(store.lastQuery.limit()).isEqualTo(50);
|
||||
assertThat(store.lastQuery.vin()).isEqualTo("VIN001");
|
||||
assertThat(store.lastQuery.eventType()).isEqualTo("RAW_ARCHIVE");
|
||||
assertThat(store.lastQuery.cursorEventTime()).isEqualTo(Instant.parse("2026-06-22T08:00:00Z"));
|
||||
assertThat(store.lastQuery.cursorIngestTime()).isEqualTo(Instant.parse("2026-06-22T08:00:01Z"));
|
||||
assertThat(store.lastQuery.cursorEventId()).isEqualTo("event-0");
|
||||
}
|
||||
|
||||
private static EventFileRecord record(String eventId) {
|
||||
|
||||
@@ -126,7 +126,7 @@ class EventHistoryEnvelopeIngestorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryIngestWritesRawLocationAndTelemetryFactsToTdengineWhenWriterExists() throws Exception {
|
||||
void tryIngestWritesRawAndLocationFactsToTdengineWhenWriterExists() throws Exception {
|
||||
CapturingStore store = new CapturingStore();
|
||||
CapturingTdengineWriter tdengineWriter = new CapturingTdengineWriter();
|
||||
EventHistoryEnvelopeIngestor ingestor = new EventHistoryEnvelopeIngestor(
|
||||
@@ -180,10 +180,7 @@ class EventHistoryEnvelopeIngestorTest {
|
||||
.extracting(TdengineLocationRow::factId)
|
||||
.containsExactly("jt808-location-1");
|
||||
assertThat(tdengineWriter.locations.getFirst().longitude()).isEqualTo(113.12);
|
||||
assertThat(tdengineWriter.telemetryFields)
|
||||
.extracting(TdengineTelemetryFieldRow::fieldKey)
|
||||
.containsExactly("location.speedKmh");
|
||||
assertThat(tdengineWriter.telemetryFields.getFirst().valueDouble()).isEqualTo(42.5);
|
||||
assertThat(tdengineWriter.telemetryFields).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -200,16 +197,14 @@ class EventHistoryEnvelopeIngestorTest {
|
||||
.containsExactly("frame-jt808-1");
|
||||
assertThat(tdengineWriter.locations).extracting(TdengineLocationRow::factId)
|
||||
.containsExactly("jt808-location-1");
|
||||
assertThat(tdengineWriter.telemetryFields)
|
||||
.extracting(TdengineTelemetryFieldRow::fieldKey)
|
||||
.containsExactly("location.speedKmh");
|
||||
assertThat(tdengineWriter.telemetryFields).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void tryIngestCanSkipTelemetryFieldFactsForHighThroughputRuntime() {
|
||||
void tryIngestCanWriteTelemetryFieldFactsWhenExplicitlyEnabled() {
|
||||
CapturingTdengineWriter tdengineWriter = new CapturingTdengineWriter();
|
||||
EventHistoryEnvelopeIngestor ingestor = new EventHistoryEnvelopeIngestor(
|
||||
new TelemetryEnvelopeRecordMapper(), tdengineWriter, false);
|
||||
new TelemetryEnvelopeRecordMapper(), tdengineWriter, true);
|
||||
|
||||
EnvelopeIngestResult result = ingestor.tryIngest(
|
||||
jt808LocationEnvelope("jt808-location-1", "frame-jt808-1", "013800000001").toByteArray());
|
||||
@@ -219,7 +214,9 @@ class EventHistoryEnvelopeIngestorTest {
|
||||
.containsExactly("frame-jt808-1");
|
||||
assertThat(tdengineWriter.locations).extracting(TdengineLocationRow::factId)
|
||||
.containsExactly("jt808-location-1");
|
||||
assertThat(tdengineWriter.telemetryFields).isEmpty();
|
||||
assertThat(tdengineWriter.telemetryFields)
|
||||
.extracting(TdengineTelemetryFieldRow::fieldKey)
|
||||
.containsExactly("location.speedKmh");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -249,9 +246,7 @@ class EventHistoryEnvelopeIngestorTest {
|
||||
.containsExactly("frame-jt808-1", "frame-jt808-2");
|
||||
assertThat(tdengineWriter.locations).extracting(TdengineLocationRow::factId)
|
||||
.containsExactly("jt808-location-1", "jt808-location-2");
|
||||
assertThat(tdengineWriter.telemetryFields)
|
||||
.extracting(TdengineTelemetryFieldRow::fieldKey)
|
||||
.containsExactly("location.speedKmh", "location.speedKmh");
|
||||
assertThat(tdengineWriter.telemetryFields).isEmpty();
|
||||
}
|
||||
|
||||
private static VehicleEnvelope envelope(String eventId) {
|
||||
|
||||
@@ -314,6 +314,7 @@ class Gb32960DecodedFrameServiceTest {
|
||||
"",
|
||||
"127.0.0.1:32960",
|
||||
"{\"platformAccount\":\"Hyundai\"}",
|
||||
"",
|
||||
"GB32960",
|
||||
vin,
|
||||
vin,
|
||||
@@ -360,6 +361,7 @@ class Gb32960DecodedFrameServiceTest {
|
||||
"",
|
||||
"8.134.95.166:37302",
|
||||
"{\"platformAccount\":\"Hyundai\"}",
|
||||
"",
|
||||
"GB32960",
|
||||
"unknown:GB32960:platform-login",
|
||||
"",
|
||||
@@ -409,6 +411,7 @@ class Gb32960DecodedFrameServiceTest {
|
||||
"",
|
||||
"127.0.0.1:32960",
|
||||
"{\"platformAccount\":\"Hyundai\"}",
|
||||
"",
|
||||
"GB32960",
|
||||
vin,
|
||||
vin,
|
||||
|
||||
@@ -51,13 +51,12 @@ class Jt808LocationHistoryControllerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void fallsBackToRawArchiveUriInMetadataWhenLocationRawUriIsBlank() throws Exception {
|
||||
void returnsLocationRawUriFromCoreColumn() throws Exception {
|
||||
CapturingReader reader = new CapturingReader(new TdenginePage<>(
|
||||
List.of(row(
|
||||
"fact-blank-raw",
|
||||
"2026-06-29T05:00:01Z",
|
||||
"",
|
||||
"{\"rawArchiveUri\":\"archive://jt808/metadata-frame.bin\"}")),
|
||||
"archive://jt808/core-frame.bin")),
|
||||
Optional.empty()));
|
||||
Jt808LocationHistoryController controller = new Jt808LocationHistoryController(reader);
|
||||
|
||||
@@ -71,7 +70,7 @@ class Jt808LocationHistoryControllerTest {
|
||||
null);
|
||||
|
||||
assertThat(response.items().getFirst().rawUri())
|
||||
.isEqualTo("archive://jt808/metadata-frame.bin");
|
||||
.isEqualTo("archive://jt808/core-frame.bin");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -92,10 +91,10 @@ class Jt808LocationHistoryControllerTest {
|
||||
}
|
||||
|
||||
private static TdengineLocationRow row(String factId, String ts) {
|
||||
return row(factId, ts, "archive://jt808/frame-1.bin", "{\"messageId\":\"512\"}");
|
||||
return row(factId, ts, "archive://jt808/frame-1.bin");
|
||||
}
|
||||
|
||||
private static TdengineLocationRow row(String factId, String ts, String rawUri, String metadataJson) {
|
||||
private static TdengineLocationRow row(String factId, String ts, String rawUri) {
|
||||
Instant instant = Instant.parse(ts);
|
||||
return new TdengineLocationRow(
|
||||
instant,
|
||||
@@ -111,7 +110,6 @@ class Jt808LocationHistoryControllerTest {
|
||||
3L,
|
||||
null,
|
||||
rawUri,
|
||||
metadataJson,
|
||||
"JT808",
|
||||
"jt808:g7gps",
|
||||
"",
|
||||
|
||||
@@ -96,6 +96,7 @@ class Jt808RawFrameHistoryControllerTest {
|
||||
"",
|
||||
"222.66.200.68:41000",
|
||||
"{\"jt808.register.deviceId\":\"9963320\",\"jt808.register.plate\":\"沪A61559F\"}",
|
||||
"",
|
||||
"JT808",
|
||||
"jt808:13079963320",
|
||||
"unknown",
|
||||
|
||||
@@ -82,7 +82,6 @@ class LocationHistoryControllerTest {
|
||||
1,
|
||||
null,
|
||||
"",
|
||||
"{\"rawArchiveUri\":\"archive://raw.bin\"}",
|
||||
"MQTT_YUTONG",
|
||||
"LMRKH9AC2R1004087",
|
||||
"LMRKH9AC2R1004087",
|
||||
|
||||
@@ -91,7 +91,6 @@ class RawFrameHistoryControllerTest {
|
||||
524290,
|
||||
14504.446,
|
||||
raw.rawUri(),
|
||||
"{\"rawArchiveUri\":\"" + raw.rawUri() + "\"}",
|
||||
"JT808",
|
||||
"VIN-JT808-1",
|
||||
"VIN-JT808-1",
|
||||
@@ -183,6 +182,7 @@ class RawFrameHistoryControllerTest {
|
||||
"xinda",
|
||||
"{\"rawArchiveUri\":\"archive://2026/06/29/XINDA_PUSH/unknown/" + frameId
|
||||
+ ".bin\",\"plateNo\":\"粤BD12345\",\"rawJson\":\"{\\\"carName\\\":\\\"粤BD12345\\\"}\"}",
|
||||
"",
|
||||
"XINDA_PUSH",
|
||||
"LB9A32A24P0LS1270",
|
||||
"LB9A32A24P0LS1270",
|
||||
|
||||
@@ -2,9 +2,11 @@ package com.lingniu.ingest.eventhistory;
|
||||
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
import com.lingniu.ingest.eventfilestore.EventFileRecord;
|
||||
import com.lingniu.ingest.sink.mq.proto.RawArchiveRef;
|
||||
import com.lingniu.ingest.sink.mq.proto.TelemetryField;
|
||||
import com.lingniu.ingest.sink.mq.proto.TelemetrySnapshot;
|
||||
import com.lingniu.ingest.sink.mq.proto.VehicleEnvelope;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -12,6 +14,8 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
class TelemetryEnvelopeRecordMapperTest {
|
||||
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void mapsFullFieldEnvelopeToEventFileRecord() {
|
||||
VehicleEnvelope envelope = VehicleEnvelope.newBuilder()
|
||||
@@ -91,4 +95,33 @@ class TelemetryEnvelopeRecordMapperTest {
|
||||
.containsEntry("tenant", "ln")
|
||||
.containsEntry("originalSource", "FUTURE_OEM");
|
||||
}
|
||||
|
||||
@Test
|
||||
void rawArchiveRecordStoresParsedJsonInPayloadOnly() throws Exception {
|
||||
VehicleEnvelope envelope = VehicleEnvelope.newBuilder()
|
||||
.setSchemaVersion("1.0")
|
||||
.setEventId("raw-808-1")
|
||||
.setTraceId("trace-raw-1")
|
||||
.setVin("VIN808")
|
||||
.setSource("JT808")
|
||||
.setEventTimeMs(1_782_112_400_000L)
|
||||
.setIngestTimeMs(1_782_112_401_000L)
|
||||
.putMetadata("phone", "13307795425")
|
||||
.setRawArchive(RawArchiveRef.newBuilder()
|
||||
.setUri("archive://2026/06/30/JT808/VIN808/raw-808-1.bin")
|
||||
.setSizeBytes(64)
|
||||
.setParsedJson("{\"messageId\":\"0x0200\",\"body\":{\"speedKmh\":60.5}}")
|
||||
.build())
|
||||
.build();
|
||||
|
||||
EventFileRecord record = new TelemetryEnvelopeRecordMapper().toRecord(envelope);
|
||||
|
||||
assertThat(record.eventType()).isEqualTo("RAW_ARCHIVE");
|
||||
assertThat(record.metadata())
|
||||
.containsEntry("phone", "13307795425")
|
||||
.doesNotContainKey("rawArchiveParsedJson");
|
||||
var payload = objectMapper.readTree(record.payloadJson());
|
||||
assertThat(payload.path("parsed").path("messageId").asText()).isEqualTo("0x0200");
|
||||
assertThat(payload.path("parsed").path("body").path("speedKmh").asDouble()).isEqualTo(60.5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,11 +35,14 @@ class EventHistoryAutoConfigurationTest {
|
||||
@Test
|
||||
void createsHistoryBeansWhenEnabled() {
|
||||
contextRunner
|
||||
.withPropertyValues("lingniu.ingest.event-history.enabled=true")
|
||||
.withPropertyValues(
|
||||
"lingniu.ingest.event-history.enabled=true",
|
||||
"lingniu.ingest.sink.mq.consumer.enabled=true")
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(TelemetryEnvelopeRecordMapper.class);
|
||||
assertThat(context).hasSingleBean(EventHistoryEnvelopeIngestor.class);
|
||||
assertThat(context).hasSingleBean(EnvelopeConsumerProcessor.class);
|
||||
assertThat(context.getBeansOfType(EnvelopeConsumerProcessor.class))
|
||||
.containsOnlyKeys("eventHistoryEnvelopeConsumerProcessor");
|
||||
assertThat(context).hasSingleBean(EventHistoryController.class);
|
||||
assertThat(context).doesNotHaveBean(Gb32960DecodedFrameService.class);
|
||||
assertThat(context).doesNotHaveBean(Gb32960FrameController.class);
|
||||
|
||||
@@ -23,6 +23,9 @@ import com.lingniu.ingest.vehiclestat.jt808.Jt808MileageStreamProcessor;
|
||||
import com.lingniu.ingest.vehiclestat.jt808.RedisJt808MileageStateStore;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -36,7 +39,11 @@ import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.time.ZoneId;
|
||||
|
||||
@AutoConfiguration
|
||||
@AutoConfiguration(after = {
|
||||
DataSourceAutoConfiguration.class,
|
||||
JdbcTemplateAutoConfiguration.class,
|
||||
RedisAutoConfiguration.class
|
||||
})
|
||||
@EnableConfigurationProperties(VehicleStatProperties.class)
|
||||
@ConditionalOnProperty(prefix = "lingniu.ingest.vehicle-stat", name = "enabled", havingValue = "true")
|
||||
public class VehicleStatAutoConfiguration {
|
||||
|
||||
@@ -4,13 +4,16 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
|
||||
public final class JdbcJt808DailyMileageRepository implements Jt808DailyMileageRepository {
|
||||
|
||||
private final JdbcTemplate jdbc;
|
||||
|
||||
public JdbcJt808DailyMileageRepository(JdbcTemplate jdbc) {
|
||||
this.jdbc = jdbc;
|
||||
this.jdbc = Objects.requireNonNull(jdbc, "jdbc must not be null");
|
||||
initializeSchema();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -18,18 +21,23 @@ public final class JdbcJt808DailyMileageRepository implements Jt808DailyMileageR
|
||||
jdbc.update("""
|
||||
insert into vehicle_daily_mileage_jt808 (
|
||||
stat_date, vehicle_key, vin, phone, first_event_time, last_event_time,
|
||||
gps_mileage_km, speed_integral_km, odometer_mileage_km,
|
||||
daily_mileage_km, mileage_source, gps_mileage_km, speed_integral_km,
|
||||
odometer_mileage_km, first_total_mileage_km, last_total_mileage_km,
|
||||
accepted_points, bad_jump_segments, long_gap_segments, out_of_order_points,
|
||||
odometer_anomalies, data_quality
|
||||
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
on duplicate key update
|
||||
vin = values(vin),
|
||||
phone = values(phone),
|
||||
first_event_time = values(first_event_time),
|
||||
last_event_time = values(last_event_time),
|
||||
daily_mileage_km = values(daily_mileage_km),
|
||||
mileage_source = values(mileage_source),
|
||||
gps_mileage_km = values(gps_mileage_km),
|
||||
speed_integral_km = values(speed_integral_km),
|
||||
odometer_mileage_km = values(odometer_mileage_km),
|
||||
first_total_mileage_km = values(first_total_mileage_km),
|
||||
last_total_mileage_km = values(last_total_mileage_km),
|
||||
accepted_points = values(accepted_points),
|
||||
bad_jump_segments = values(bad_jump_segments),
|
||||
long_gap_segments = values(long_gap_segments),
|
||||
@@ -44,9 +52,13 @@ public final class JdbcJt808DailyMileageRepository implements Jt808DailyMileageR
|
||||
result.phone(),
|
||||
timestamp(result.firstEventTime()),
|
||||
timestamp(result.lastEventTime()),
|
||||
result.dailyMileageKm(),
|
||||
result.mileageSource(),
|
||||
result.gpsMileageKm(),
|
||||
result.speedIntegralKm(),
|
||||
result.odometerMileageKm(),
|
||||
result.firstTotalMileageKm(),
|
||||
result.lastTotalMileageKm(),
|
||||
result.acceptedPoints(),
|
||||
result.badJumpSegments(),
|
||||
result.longGapSegments(),
|
||||
@@ -55,6 +67,59 @@ public final class JdbcJt808DailyMileageRepository implements Jt808DailyMileageR
|
||||
result.dataQuality());
|
||||
}
|
||||
|
||||
private void initializeSchema() {
|
||||
jdbc.execute("""
|
||||
create table if not exists vehicle_daily_mileage_jt808 (
|
||||
stat_date date not null,
|
||||
vehicle_key varchar(64) not null,
|
||||
vin varchar(32) null,
|
||||
phone varchar(32) null,
|
||||
first_event_time timestamp null,
|
||||
last_event_time timestamp null,
|
||||
daily_mileage_km decimal(12,3) not null default 0,
|
||||
mileage_source varchar(32) not null default 'INSUFFICIENT',
|
||||
gps_mileage_km decimal(12,3) not null,
|
||||
speed_integral_km decimal(12,3) not null,
|
||||
odometer_mileage_km decimal(12,3) null,
|
||||
first_total_mileage_km decimal(12,3) null,
|
||||
last_total_mileage_km decimal(12,3) null,
|
||||
accepted_points int not null,
|
||||
bad_jump_segments int not null,
|
||||
long_gap_segments int not null,
|
||||
out_of_order_points int not null,
|
||||
odometer_anomalies int not null,
|
||||
data_quality varchar(16) not null,
|
||||
updated_at timestamp not null default current_timestamp on update current_timestamp,
|
||||
primary key (stat_date, vehicle_key),
|
||||
key idx_vehicle_daily_mileage_jt808_vehicle_date (vehicle_key, stat_date),
|
||||
key idx_vehicle_daily_mileage_jt808_vin_date (vin, stat_date),
|
||||
key idx_vehicle_daily_mileage_jt808_phone_date (phone, stat_date)
|
||||
)
|
||||
""");
|
||||
ensureColumn("daily_mileage_km", "daily_mileage_km decimal(12,3) not null default 0");
|
||||
ensureColumn("mileage_source", "mileage_source varchar(32) not null default 'INSUFFICIENT'");
|
||||
ensureColumn("first_total_mileage_km", "first_total_mileage_km decimal(12,3) null");
|
||||
ensureColumn("last_total_mileage_km", "last_total_mileage_km decimal(12,3) null");
|
||||
}
|
||||
|
||||
private void ensureColumn(String columnName, String definition) {
|
||||
if (!columnExists(columnName)) {
|
||||
jdbc.execute("alter table vehicle_daily_mileage_jt808 add column " + definition);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean columnExists(String columnName) {
|
||||
Integer count = jdbc.queryForObject("""
|
||||
select count(*)
|
||||
from information_schema.columns
|
||||
where lower(table_name) = ?
|
||||
and lower(column_name) = ?
|
||||
""", Integer.class,
|
||||
"vehicle_daily_mileage_jt808",
|
||||
columnName.toLowerCase(Locale.ROOT));
|
||||
return count != null && count > 0;
|
||||
}
|
||||
|
||||
private static Timestamp timestamp(Instant instant) {
|
||||
return instant == null ? null : Timestamp.from(instant);
|
||||
}
|
||||
|
||||
@@ -10,9 +10,13 @@ public record Jt808DailyMileageResult(
|
||||
String phone,
|
||||
Instant firstEventTime,
|
||||
Instant lastEventTime,
|
||||
double dailyMileageKm,
|
||||
String mileageSource,
|
||||
double gpsMileageKm,
|
||||
double speedIntegralKm,
|
||||
Double odometerMileageKm,
|
||||
Double firstTotalMileageKm,
|
||||
Double lastTotalMileageKm,
|
||||
int acceptedPoints,
|
||||
int badJumpSegments,
|
||||
int longGapSegments,
|
||||
|
||||
@@ -12,6 +12,7 @@ public final class Jt808DailyMileageState {
|
||||
private Instant firstEventTime;
|
||||
private Instant lastEventTime;
|
||||
private Jt808LocationPoint lastPoint;
|
||||
private double firstTotalMileageKm = Double.NaN;
|
||||
private double lastTotalMileageKm = Double.NaN;
|
||||
private double gpsMileageKm;
|
||||
private double speedIntegralKm;
|
||||
@@ -47,6 +48,7 @@ public final class Jt808DailyMileageState {
|
||||
Instant firstEventTime,
|
||||
Instant lastEventTime,
|
||||
Jt808LocationPoint lastPoint,
|
||||
double firstTotalMileageKm,
|
||||
double lastTotalMileageKm,
|
||||
double gpsMileageKm,
|
||||
double speedIntegralKm,
|
||||
@@ -60,6 +62,7 @@ public final class Jt808DailyMileageState {
|
||||
state.firstEventTime = firstEventTime;
|
||||
state.lastEventTime = lastEventTime;
|
||||
state.lastPoint = lastPoint;
|
||||
state.firstTotalMileageKm = firstTotalMileageKm;
|
||||
state.lastTotalMileageKm = lastTotalMileageKm;
|
||||
state.gpsMileageKm = gpsMileageKm;
|
||||
state.speedIntegralKm = speedIntegralKm;
|
||||
@@ -82,7 +85,7 @@ public final class Jt808DailyMileageState {
|
||||
}
|
||||
if (lastPoint == null) {
|
||||
firstEventTime = point.eventTime();
|
||||
updateOdometer(point);
|
||||
updateOdometer(point, calculator);
|
||||
accept(point);
|
||||
return;
|
||||
}
|
||||
@@ -100,7 +103,7 @@ public final class Jt808DailyMileageState {
|
||||
gpsMileageKm += segment.gpsKm();
|
||||
speedIntegralKm += segment.speedIntegralKm();
|
||||
}
|
||||
updateOdometer(point);
|
||||
updateOdometer(point, calculator);
|
||||
accept(point);
|
||||
}
|
||||
|
||||
@@ -112,9 +115,13 @@ public final class Jt808DailyMileageState {
|
||||
phone,
|
||||
firstEventTime,
|
||||
lastEventTime,
|
||||
dailyMileageKm(),
|
||||
mileageSource(),
|
||||
gpsMileageKm,
|
||||
speedIntegralKm,
|
||||
odometerMileageKm > 0.0 ? odometerMileageKm : null,
|
||||
nullable(firstTotalMileageKm),
|
||||
nullable(lastTotalMileageKm),
|
||||
acceptedPoints,
|
||||
badJumpSegments,
|
||||
longGapSegments,
|
||||
@@ -129,11 +136,14 @@ public final class Jt808DailyMileageState {
|
||||
acceptedPoints++;
|
||||
}
|
||||
|
||||
private void updateOdometer(Jt808LocationPoint point) {
|
||||
private void updateOdometer(Jt808LocationPoint point, Jt808GpsMileageCalculator calculator) {
|
||||
if (point.totalMileageKm() == null || !Double.isFinite(point.totalMileageKm())) {
|
||||
return;
|
||||
}
|
||||
double current = point.totalMileageKm();
|
||||
if (!Double.isFinite(firstTotalMileageKm)) {
|
||||
firstTotalMileageKm = current;
|
||||
}
|
||||
if (!Double.isFinite(lastTotalMileageKm)) {
|
||||
lastTotalMileageKm = current;
|
||||
return;
|
||||
@@ -143,15 +153,56 @@ public final class Jt808DailyMileageState {
|
||||
lastTotalMileageKm = current;
|
||||
return;
|
||||
}
|
||||
odometerMileageKm += current - lastTotalMileageKm;
|
||||
double delta = current - lastTotalMileageKm;
|
||||
if (lastPoint != null && !calculator.plausibleOdometerDelta(lastPoint, point, delta)) {
|
||||
odometerAnomalies++;
|
||||
lastTotalMileageKm = current;
|
||||
return;
|
||||
}
|
||||
odometerMileageKm += delta;
|
||||
lastTotalMileageKm = current;
|
||||
}
|
||||
|
||||
private double dailyMileageKm() {
|
||||
if (hasUsableTotalMileage()) {
|
||||
return odometerMileageKm;
|
||||
}
|
||||
if (gpsMileageKm > 0.0) {
|
||||
return gpsMileageKm;
|
||||
}
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
private String mileageSource() {
|
||||
if (hasUsableTotalMileage()) {
|
||||
return odometerAnomalies > 0 ? "JT808_TOTAL_MILEAGE_PARTIAL" : "JT808_TOTAL_MILEAGE";
|
||||
}
|
||||
if (gpsMileageKm > 0.0) {
|
||||
return "GPS_DISTANCE";
|
||||
}
|
||||
return "INSUFFICIENT";
|
||||
}
|
||||
|
||||
private boolean hasUsableTotalMileage() {
|
||||
if (odometerMileageKm > 0.0) {
|
||||
return true;
|
||||
}
|
||||
return acceptedPoints >= 2
|
||||
&& odometerAnomalies == 0
|
||||
&& Double.isFinite(firstTotalMileageKm)
|
||||
&& Double.isFinite(lastTotalMileageKm)
|
||||
&& lastTotalMileageKm >= firstTotalMileageKm;
|
||||
}
|
||||
|
||||
private static Double nullable(double value) {
|
||||
return Double.isFinite(value) ? value : null;
|
||||
}
|
||||
|
||||
private String dataQuality() {
|
||||
if (acceptedPoints < 2) {
|
||||
return "PARTIAL";
|
||||
}
|
||||
if (badJumpSegments > 0 || longGapSegments > 0 || vehicleKey.startsWith("jt808:")) {
|
||||
if (badJumpSegments > 0 || longGapSegments > 0 || odometerAnomalies > 0 || vehicleKey.startsWith("jt808:")) {
|
||||
return "PARTIAL";
|
||||
}
|
||||
return "GOOD";
|
||||
@@ -201,6 +252,10 @@ public final class Jt808DailyMileageState {
|
||||
return lastTotalMileageKm;
|
||||
}
|
||||
|
||||
public double firstTotalMileageKm() {
|
||||
return firstTotalMileageKm;
|
||||
}
|
||||
|
||||
public int acceptedPoints() {
|
||||
return acceptedPoints;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.time.Duration;
|
||||
public final class Jt808GpsMileageCalculator {
|
||||
|
||||
private static final double EARTH_RADIUS_KM = 6371.0088;
|
||||
private static final double ODOMETER_DELTA_TOLERANCE_KM = 0.1;
|
||||
|
||||
private final Duration maxSegmentGap;
|
||||
private final double maxImpliedSpeedKmh;
|
||||
@@ -61,6 +62,20 @@ public final class Jt808GpsMileageCalculator {
|
||||
return new SegmentResult(gpsKm, speedIntegralKm, true, false, false, false);
|
||||
}
|
||||
|
||||
public boolean plausibleOdometerDelta(Jt808LocationPoint previous,
|
||||
Jt808LocationPoint current,
|
||||
double deltaKm) {
|
||||
if (previous == null || current == null || !Double.isFinite(deltaKm) || deltaKm < 0.0) {
|
||||
return false;
|
||||
}
|
||||
Duration elapsed = Duration.between(previous.eventTime(), current.eventTime());
|
||||
if (elapsed.isZero() || elapsed.isNegative()) {
|
||||
return false;
|
||||
}
|
||||
double hours = elapsed.toMillis() / 3_600_000.0;
|
||||
return deltaKm <= maxImpliedSpeedKmh * hours + ODOMETER_DELTA_TOLERANCE_KM;
|
||||
}
|
||||
|
||||
private static double haversineKm(double lon1, double lat1, double lon2, double lat2) {
|
||||
double latRad1 = Math.toRadians(lat1);
|
||||
double latRad2 = Math.toRadians(lat2);
|
||||
|
||||
@@ -13,7 +13,8 @@ public final class Jt808LocationPointExtractor {
|
||||
public Optional<Jt808LocationPoint> extract(VehicleEnvelope envelope) {
|
||||
if (envelope == null
|
||||
|| !"JT808".equalsIgnoreCase(envelope.getSource())
|
||||
|| !envelope.hasTelemetrySnapshot()) {
|
||||
|| !envelope.hasTelemetrySnapshot()
|
||||
|| !"LOCATION".equalsIgnoreCase(envelope.getTelemetrySnapshot().getEventType())) {
|
||||
return Optional.empty();
|
||||
}
|
||||
Map<String, String> fields = fields(envelope);
|
||||
|
||||
@@ -64,7 +64,8 @@ public final class RedisJt808MileageStateStore implements Jt808MileageStateStore
|
||||
String firstEventTime,
|
||||
String lastEventTime,
|
||||
PointSnapshot lastPoint,
|
||||
double lastTotalMileageKm,
|
||||
Double firstTotalMileageKm,
|
||||
Double lastTotalMileageKm,
|
||||
double gpsMileageKm,
|
||||
double speedIntegralKm,
|
||||
double odometerMileageKm,
|
||||
@@ -83,7 +84,8 @@ public final class RedisJt808MileageStateStore implements Jt808MileageStateStore
|
||||
format(state.firstEventTime()),
|
||||
format(state.lastEventTime()),
|
||||
PointSnapshot.from(state.lastPoint()),
|
||||
state.lastTotalMileageKm(),
|
||||
finiteOrNull(state.firstTotalMileageKm()),
|
||||
finiteOrNull(state.lastTotalMileageKm()),
|
||||
state.gpsMileageKm(),
|
||||
state.speedIntegralKm(),
|
||||
state.odometerMileageKm(),
|
||||
@@ -103,7 +105,8 @@ public final class RedisJt808MileageStateStore implements Jt808MileageStateStore
|
||||
parseInstant(firstEventTime),
|
||||
parseInstant(lastEventTime),
|
||||
lastPoint == null ? null : lastPoint.toPoint(),
|
||||
lastTotalMileageKm,
|
||||
nanIfNull(firstTotalMileageKm),
|
||||
nanIfNull(lastTotalMileageKm),
|
||||
gpsMileageKm,
|
||||
speedIntegralKm,
|
||||
odometerMileageKm,
|
||||
@@ -164,4 +167,12 @@ public final class RedisJt808MileageStateStore implements Jt808MileageStateStore
|
||||
private static Instant parseInstant(String value) {
|
||||
return value == null || value.isBlank() ? null : Instant.parse(value);
|
||||
}
|
||||
|
||||
private static Double finiteOrNull(double value) {
|
||||
return Double.isFinite(value) ? value : null;
|
||||
}
|
||||
|
||||
private static double nanIfNull(Double value) {
|
||||
return value == null ? Double.NaN : value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,10 +21,13 @@ import com.lingniu.ingest.vehiclestat.jt808.RedisJt808MileageStateStore;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -117,6 +120,29 @@ class VehicleStatAutoConfigurationTest {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsJt808MileageBeansAfterJdbcAutoConfiguration() {
|
||||
new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(
|
||||
JdbcTemplateAutoConfiguration.class,
|
||||
VehicleStatAutoConfiguration.class))
|
||||
.withBean(EnvelopeDeadLetterSink.class, () -> record -> {})
|
||||
.withBean(DataSource.class, () -> new DriverManagerDataSource(
|
||||
"jdbc:h2:mem:vehicle_stat_auto;MODE=MySQL;DATABASE_TO_LOWER=TRUE;DB_CLOSE_DELAY=-1",
|
||||
"sa",
|
||||
""))
|
||||
.withPropertyValues(
|
||||
"lingniu.ingest.vehicle-stat.enabled=true",
|
||||
"lingniu.ingest.vehicle-stat.jt808.enabled=true",
|
||||
"lingniu.ingest.vehicle-stat.jt808.state-store=memory")
|
||||
.run(context -> {
|
||||
assertThat(context).hasSingleBean(JdbcTemplate.class);
|
||||
assertThat(context).hasSingleBean(Jt808DailyMileageRepository.class);
|
||||
assertThat(context).hasSingleBean(Jt808MileageStreamProcessor.class);
|
||||
assertThat(context).hasSingleBean(VehicleStatEnvelopeIngestor.class);
|
||||
});
|
||||
}
|
||||
|
||||
private static final class InMemoryVehicleStatRepository implements VehicleStatRepository {
|
||||
@Override public void appendMileagePoint(String vin, MileagePoint point) {}
|
||||
@Override public List<MileagePoint> mileagePoints(String vin, LocalDate statDate) { return List.of(); }
|
||||
|
||||
@@ -24,30 +24,20 @@ class JdbcJt808DailyMileageRepositoryTest {
|
||||
"");
|
||||
jdbc = new JdbcTemplate(dataSource);
|
||||
jdbc.execute("drop table if exists vehicle_daily_mileage_jt808");
|
||||
jdbc.execute("""
|
||||
create table vehicle_daily_mileage_jt808 (
|
||||
stat_date date not null,
|
||||
vehicle_key varchar(64) not null,
|
||||
vin varchar(32) null,
|
||||
phone varchar(32) null,
|
||||
first_event_time timestamp null,
|
||||
last_event_time timestamp null,
|
||||
gps_mileage_km decimal(12,3) not null,
|
||||
speed_integral_km decimal(12,3) not null,
|
||||
odometer_mileage_km decimal(12,3) null,
|
||||
accepted_points int not null,
|
||||
bad_jump_segments int not null,
|
||||
long_gap_segments int not null,
|
||||
out_of_order_points int not null,
|
||||
odometer_anomalies int not null,
|
||||
data_quality varchar(16) not null,
|
||||
updated_at timestamp not null default current_timestamp,
|
||||
primary key (stat_date, vehicle_key)
|
||||
)
|
||||
""");
|
||||
repository = new JdbcJt808DailyMileageRepository(jdbc);
|
||||
}
|
||||
|
||||
@Test
|
||||
void initializesTableWhenMissing() {
|
||||
Integer count = jdbc.queryForObject("""
|
||||
select count(*)
|
||||
from information_schema.tables
|
||||
where table_name = 'vehicle_daily_mileage_jt808'
|
||||
""", Integer.class);
|
||||
|
||||
assertThat(count).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void upsertsDailyMileageResult() {
|
||||
Jt808DailyMileageResult first = new Jt808DailyMileageResult(
|
||||
@@ -58,8 +48,12 @@ class JdbcJt808DailyMileageRepositoryTest {
|
||||
Instant.parse("2026-06-30T00:00:00Z"),
|
||||
Instant.parse("2026-06-30T00:01:00Z"),
|
||||
1.23456,
|
||||
"GPS_DISTANCE",
|
||||
1.23456,
|
||||
1.2,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
2,
|
||||
0,
|
||||
0,
|
||||
@@ -73,9 +67,13 @@ class JdbcJt808DailyMileageRepositoryTest {
|
||||
"13900000001",
|
||||
Instant.parse("2026-06-30T00:00:00Z"),
|
||||
Instant.parse("2026-06-30T00:02:00Z"),
|
||||
2.0,
|
||||
"JT808_TOTAL_MILEAGE",
|
||||
2.34567,
|
||||
2.3,
|
||||
2.0,
|
||||
100.0,
|
||||
102.0,
|
||||
3,
|
||||
1,
|
||||
0,
|
||||
@@ -87,14 +85,19 @@ class JdbcJt808DailyMileageRepositoryTest {
|
||||
repository.upsert(updated);
|
||||
|
||||
Map<String, Object> row = jdbc.queryForMap("""
|
||||
select vehicle_key, gps_mileage_km, odometer_mileage_km, accepted_points,
|
||||
bad_jump_segments, data_quality
|
||||
select vehicle_key, daily_mileage_km, mileage_source, gps_mileage_km,
|
||||
odometer_mileage_km, first_total_mileage_km, last_total_mileage_km,
|
||||
accepted_points, bad_jump_segments, data_quality
|
||||
from vehicle_daily_mileage_jt808
|
||||
where stat_date = '2026-06-30' and vehicle_key = 'VIN001'
|
||||
""");
|
||||
assertThat(row.get("vehicle_key")).isEqualTo("VIN001");
|
||||
assertThat(row.get("daily_mileage_km").toString()).isEqualTo("2.000");
|
||||
assertThat(row.get("mileage_source")).isEqualTo("JT808_TOTAL_MILEAGE");
|
||||
assertThat(row.get("gps_mileage_km").toString()).isEqualTo("2.346");
|
||||
assertThat(row.get("odometer_mileage_km").toString()).isEqualTo("2.000");
|
||||
assertThat(row.get("first_total_mileage_km").toString()).isEqualTo("100.000");
|
||||
assertThat(row.get("last_total_mileage_km").toString()).isEqualTo("102.000");
|
||||
assertThat(row.get("accepted_points")).isEqualTo(3);
|
||||
assertThat(row.get("bad_jump_segments")).isEqualTo(1);
|
||||
assertThat(row.get("data_quality")).isEqualTo("PARTIAL");
|
||||
|
||||
@@ -25,6 +25,10 @@ class Jt808DailyMileageStateTest {
|
||||
assertThat(state.gpsMileageKm()).isZero();
|
||||
assertThat(state.speedIntegralKm()).isZero();
|
||||
assertThat(state.odometerMileageKm()).isZero();
|
||||
assertThat(state.firstTotalMileageKm()).isEqualTo(100.0);
|
||||
assertThat(state.lastTotalMileageKm()).isEqualTo(100.0);
|
||||
assertThat(state.toResult().dailyMileageKm()).isZero();
|
||||
assertThat(state.toResult().mileageSource()).isEqualTo("INSUFFICIENT");
|
||||
assertThat(state.firstEventTime()).isEqualTo(Instant.parse("2026-06-30T00:00:00Z"));
|
||||
assertThat(state.lastEventTime()).isEqualTo(Instant.parse("2026-06-30T00:00:00Z"));
|
||||
}
|
||||
@@ -41,6 +45,12 @@ class Jt808DailyMileageStateTest {
|
||||
assertThat(state.gpsMileageKm()).isBetween(0.99, 1.01);
|
||||
assertThat(state.speedIntegralKm()).isBetween(0.99, 1.01);
|
||||
assertThat(state.odometerMileageKm()).isCloseTo(1.2, offset(0.000001));
|
||||
assertThat(state.firstTotalMileageKm()).isEqualTo(100.0);
|
||||
assertThat(state.lastTotalMileageKm()).isEqualTo(101.2);
|
||||
assertThat(state.toResult().dailyMileageKm()).isCloseTo(1.2, offset(0.000001));
|
||||
assertThat(state.toResult().mileageSource()).isEqualTo("JT808_TOTAL_MILEAGE");
|
||||
assertThat(state.toResult().firstTotalMileageKm()).isEqualTo(100.0);
|
||||
assertThat(state.toResult().lastTotalMileageKm()).isEqualTo(101.2);
|
||||
assertThat(state.badJumpSegments()).isZero();
|
||||
assertThat(state.longGapSegments()).isZero();
|
||||
}
|
||||
@@ -68,6 +78,36 @@ class Jt808DailyMileageStateTest {
|
||||
|
||||
assertThat(state.odometerMileageKm()).isZero();
|
||||
assertThat(state.odometerAnomalies()).isEqualTo(1);
|
||||
assertThat(state.toResult().dailyMileageKm()).isGreaterThan(0.9);
|
||||
assertThat(state.toResult().mileageSource()).isEqualTo("GPS_DISTANCE");
|
||||
}
|
||||
|
||||
@Test
|
||||
void odometerJumpAboveConfiguredSpeedIsIgnoredAndCounted() {
|
||||
Jt808DailyMileageState state = Jt808DailyMileageState.empty("VIN001", "VIN001", "13900000001",
|
||||
LocalDate.of(2026, 6, 30));
|
||||
state.apply(point("2026-06-30T00:00:00Z", 120.000000, 30.000000, 36.0, 100.0), calculator);
|
||||
|
||||
state.apply(point("2026-06-30T00:01:00Z", 120.000100, 30.000000, 36.0, 200.0), calculator);
|
||||
|
||||
assertThat(state.odometerMileageKm()).isZero();
|
||||
assertThat(state.odometerAnomalies()).isEqualTo(1);
|
||||
assertThat(state.toResult().dataQuality()).isEqualTo("PARTIAL");
|
||||
}
|
||||
|
||||
@Test
|
||||
void gpsDistanceIsFallbackWhenTotalMileageIsMissing() {
|
||||
Jt808DailyMileageState state = Jt808DailyMileageState.empty("VIN001", "VIN001", "13900000001",
|
||||
LocalDate.of(2026, 6, 30));
|
||||
state.apply(point("2026-06-30T00:00:00Z", 120.000000, 30.000000, 36.0, null), calculator);
|
||||
|
||||
state.apply(point("2026-06-30T00:01:40Z", 120.010370, 30.000000, 36.0, null), calculator);
|
||||
|
||||
assertThat(state.toResult().dailyMileageKm()).isBetween(0.99, 1.01);
|
||||
assertThat(state.toResult().mileageSource()).isEqualTo("GPS_DISTANCE");
|
||||
assertThat(state.toResult().odometerMileageKm()).isNull();
|
||||
assertThat(state.toResult().firstTotalMileageKm()).isNull();
|
||||
assertThat(state.toResult().lastTotalMileageKm()).isNull();
|
||||
}
|
||||
|
||||
private static Jt808LocationPoint point(String time,
|
||||
|
||||
@@ -71,6 +71,18 @@ class Jt808GpsMileageCalculatorTest {
|
||||
assertThat(result.gpsKm()).isZero();
|
||||
}
|
||||
|
||||
@Test
|
||||
void validatesPlausibleOdometerDeltaByConfiguredSpeed() {
|
||||
assertThat(calculator.plausibleOdometerDelta(
|
||||
point("2026-06-30T00:00:00Z", 120.000000, 30.000000, 36.0),
|
||||
point("2026-06-30T00:01:00Z", 120.001000, 30.000000, 36.0),
|
||||
2.0)).isTrue();
|
||||
assertThat(calculator.plausibleOdometerDelta(
|
||||
point("2026-06-30T00:00:00Z", 120.000000, 30.000000, 36.0),
|
||||
point("2026-06-30T00:01:00Z", 120.001000, 30.000000, 36.0),
|
||||
20.0)).isFalse();
|
||||
}
|
||||
|
||||
private static Jt808LocationPoint point(String time, double longitude, double latitude, Double speedKmh) {
|
||||
return new Jt808LocationPoint(
|
||||
"VIN001",
|
||||
|
||||
@@ -57,6 +57,21 @@ class Jt808LocationPointExtractorTest {
|
||||
field("longitude", "118.901034")))).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresJt808NonLocationSnapshot() {
|
||||
VehicleEnvelope envelope = envelope("JT808", "VIN001", "13900000001",
|
||||
field("longitude", "118.901034"),
|
||||
field("latitude", "31.946325"))
|
||||
.toBuilder()
|
||||
.setTelemetrySnapshot(TelemetrySnapshot.newBuilder()
|
||||
.setEventType("REGISTER")
|
||||
.addFields(field("longitude", "118.901034"))
|
||||
.addFields(field("latitude", "31.946325")))
|
||||
.build();
|
||||
|
||||
assertThat(extractor.extract(envelope)).isEmpty();
|
||||
}
|
||||
|
||||
private static VehicleEnvelope envelope(String source, String vin, String phone, TelemetryField... fields) {
|
||||
TelemetrySnapshot.Builder snapshot = TelemetrySnapshot.newBuilder().setEventType("LOCATION");
|
||||
for (TelemetryField field : fields) {
|
||||
|
||||
@@ -30,6 +30,8 @@ class Jt808MileageStreamProcessorTest {
|
||||
assertThat(state.gpsMileageKm()).isGreaterThan(0.9);
|
||||
assertThat(repository.results).hasSize(2);
|
||||
assertThat(repository.results.getLast().vehicleKey()).isEqualTo("VIN001");
|
||||
assertThat(repository.results.getLast().dailyMileageKm()).isEqualTo(1.0);
|
||||
assertThat(repository.results.getLast().mileageSource()).isEqualTo("JT808_TOTAL_MILEAGE");
|
||||
assertThat(repository.results.getLast().odometerMileageKm()).isEqualTo(1.0);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user