fix: calculate jt808 mileage from total range
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user