fix: calculate jt808 mileage from total range

This commit is contained in:
lingniu
2026-07-01 05:05:52 +08:00
parent 6016e3f99c
commit 6c9459bb64
2 changed files with 52 additions and 18 deletions

View File

@@ -63,25 +63,26 @@ public final class JdbcVehicleStatMetricRepository implements VehicleStatReposit
}
String normalizedVin = clean(vin);
Date date = Date.valueOf(statDate);
OptionalDouble existingStart = findMetric(normalizedVin, date, DAILY_MILEAGE_START_TOTAL_KEY);
double startTotalMileage = existingStart.orElse(totalMileageKm);
if (existingStart.isEmpty()) {
upsertMetric(normalizedVin, date, DAILY_MILEAGE_START_TOTAL_KEY, startTotalMileage,
DAILY_MILEAGE_UNIT, DailyMileageStrategy.JT808_TOTAL_MILEAGE_DIFF.name());
}
OptionalDouble existingLatest = findMetric(normalizedVin, date, DAILY_MILEAGE_LATEST_TOTAL_KEY);
if (existingLatest.isPresent() && totalMileageKm < existingLatest.getAsDouble()) {
return findDailyStat(normalizedVin, statDate);
}
if (totalMileageKm < startTotalMileage) {
return findDailyStat(normalizedVin, statDate);
}
String strategy = DailyMileageStrategy.JT808_TOTAL_MILEAGE_DIFF.name();
upsertMetric(normalizedVin, date, DAILY_MILEAGE_LATEST_TOTAL_KEY, totalMileageKm,
DAILY_MILEAGE_UNIT, strategy);
double dailyMileageKm = totalMileageKm - startTotalMileage;
OptionalDouble existingMin = findMetric(normalizedVin, date, DAILY_MILEAGE_START_TOTAL_KEY);
double minTotalMileage = existingMin.isPresent()
? Math.min(existingMin.getAsDouble(), totalMileageKm)
: totalMileageKm;
if (existingMin.isEmpty() || Double.compare(minTotalMileage, existingMin.getAsDouble()) != 0) {
upsertMetric(normalizedVin, date, DAILY_MILEAGE_START_TOTAL_KEY, minTotalMileage,
DAILY_MILEAGE_UNIT, strategy);
}
OptionalDouble existingMax = findMetric(normalizedVin, date, DAILY_MILEAGE_LATEST_TOTAL_KEY);
double maxTotalMileage = existingMax.isPresent()
? Math.max(existingMax.getAsDouble(), totalMileageKm)
: totalMileageKm;
if (existingMax.isEmpty() || Double.compare(maxTotalMileage, existingMax.getAsDouble()) != 0) {
upsertMetric(normalizedVin, date, DAILY_MILEAGE_LATEST_TOTAL_KEY, maxTotalMileage,
DAILY_MILEAGE_UNIT, strategy);
}
double dailyMileageKm = maxTotalMileage - minTotalMileage;
VehicleDailyStatResult result = new VehicleDailyStatResult(
normalizedVin,
statDate,