refactor: simplify jt808 mileage state

This commit is contained in:
lingniu
2026-07-01 02:10:04 +08:00
parent 7081800b84
commit 18ebd6c84f
4 changed files with 8 additions and 20 deletions

View File

@@ -18,7 +18,6 @@ public final class Jt808DailyMileageState {
private Instant lastEventTime;
private double firstTotalMileageKm = Double.NaN;
private double lastTotalMileageKm = Double.NaN;
private double odometerMileageKm;
private int acceptedPoints;
private int totalMileageSamples;
private int outOfOrderPoints;
@@ -50,7 +49,6 @@ public final class Jt808DailyMileageState {
Instant lastEventTime,
double firstTotalMileageKm,
double lastTotalMileageKm,
double odometerMileageKm,
int acceptedPoints,
int totalMileageSamples,
int outOfOrderPoints,
@@ -60,7 +58,6 @@ public final class Jt808DailyMileageState {
state.lastEventTime = lastEventTime;
state.firstTotalMileageKm = firstTotalMileageKm;
state.lastTotalMileageKm = lastTotalMileageKm;
state.odometerMileageKm = odometerMileageKm;
state.acceptedPoints = acceptedPoints;
state.totalMileageSamples = totalMileageSamples;
state.outOfOrderPoints = outOfOrderPoints;
@@ -112,11 +109,9 @@ public final class Jt808DailyMileageState {
if (current < lastTotalMileageKm) {
odometerAnomalies++;
lastTotalMileageKm = current;
odometerMileageKm = 0.0;
return;
}
lastTotalMileageKm = current;
odometerMileageKm = current - firstTotalMileageKm;
}
private OptionalDouble totalMileageDifference() {
@@ -160,10 +155,6 @@ public final class Jt808DailyMileageState {
return lastEventTime;
}
public double odometerMileageKm() {
return odometerMileageKm;
}
public double lastTotalMileageKm() {
return lastTotalMileageKm;
}

View File

@@ -66,7 +66,6 @@ public final class RedisJt808MileageStateStore implements Jt808MileageStateStore
PointSnapshot lastPoint,
Double firstTotalMileageKm,
Double lastTotalMileageKm,
double odometerMileageKm,
int acceptedPoints,
Integer totalMileageSamples,
int outOfOrderPoints,
@@ -83,7 +82,6 @@ public final class RedisJt808MileageStateStore implements Jt808MileageStateStore
null,
finiteOrNull(state.firstTotalMileageKm()),
finiteOrNull(state.lastTotalMileageKm()),
state.odometerMileageKm(),
state.acceptedPoints(),
state.totalMileageSamples(),
state.outOfOrderPoints(),
@@ -100,7 +98,6 @@ public final class RedisJt808MileageStateStore implements Jt808MileageStateStore
restoredLastEventTime(),
nanIfNull(firstTotalMileageKm),
nanIfNull(lastTotalMileageKm),
odometerMileageKm,
acceptedPoints,
restoredTotalMileageSamples(),
outOfOrderPoints,

View File

@@ -19,7 +19,6 @@ class Jt808DailyMileageStateTest {
assertThat(state.acceptedPoints()).isEqualTo(1);
assertThat(state.totalMileageSamples()).isEqualTo(1);
assertThat(state.odometerMileageKm()).isZero();
assertThat(state.firstTotalMileageKm()).isEqualTo(100.0);
assertThat(state.lastTotalMileageKm()).isEqualTo(100.0);
assertThat(state.toVehicleDailyStatResult()).isEmpty();
@@ -28,7 +27,7 @@ class Jt808DailyMileageStateTest {
}
@Test
void orderedPointUpdatesGpsSpeedAndOdometerMileage() {
void orderedPointsCalculateDailyMileageFromTotalMileageDifference() {
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));
@@ -37,7 +36,6 @@ class Jt808DailyMileageStateTest {
assertThat(state.acceptedPoints()).isEqualTo(2);
assertThat(state.totalMileageSamples()).isEqualTo(2);
assertThat(state.odometerMileageKm()).isCloseTo(1.2, offset(0.000001));
assertThat(state.firstTotalMileageKm()).isEqualTo(100.0);
assertThat(state.lastTotalMileageKm()).isEqualTo(101.2);
assertThat(state.toVehicleDailyStatResult()).isPresent();
@@ -66,7 +64,6 @@ class Jt808DailyMileageStateTest {
state.apply(point("2026-06-30T00:01:40Z", 120.010370, 30.000000, 36.0, 99.5));
assertThat(state.odometerMileageKm()).isZero();
assertThat(state.odometerAnomalies()).isEqualTo(1);
assertThat(state.toVehicleDailyStatResult()).isEmpty();
}
@@ -79,13 +76,12 @@ class Jt808DailyMileageStateTest {
state.apply(point("2026-06-30T00:01:00Z", 120.000100, 30.000000, 36.0, 200.0));
assertThat(state.odometerMileageKm()).isEqualTo(100.0);
assertThat(state.odometerAnomalies()).isZero();
assertThat(state.toVehicleDailyStatResult().orElseThrow().dailyMileageKm()).hasValue(100.0);
}
@Test
void gpsDistanceIsFallbackWhenTotalMileageIsMissing() {
void totalMileageIsRequiredForDailyMileageMetric() {
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));

View File

@@ -2,6 +2,7 @@ package com.lingniu.ingest.vehiclestat.jt808;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@@ -31,14 +32,17 @@ class RedisJt808MileageStateStoreTest {
store.save(state);
ArgumentCaptor<String> json = ArgumentCaptor.forClass(String.class);
verify(values).set(
org.mockito.ArgumentMatchers.eq("vehicle:mileage:jt808:daily:2026-06-30:VIN001"),
org.mockito.ArgumentMatchers.contains("\"vehicleKey\":\"VIN001\""),
json.capture(),
org.mockito.ArgumentMatchers.eq(Duration.ofDays(3)));
assertThat(json.getValue()).contains("\"vehicleKey\":\"VIN001\"");
assertThat(json.getValue()).doesNotContain("odometerMileageKm");
}
@Test
void loadsStateFromJson() {
void loadsStateFromLegacyJson() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
@SuppressWarnings("unchecked")
ValueOperations<String, String> values = mock(ValueOperations.class);