feat: productionize raw history ingestion
This commit is contained in:
@@ -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