@@ -21,12 +21,24 @@ async function loadSource(start: string, end: string, protocols: OneOsProtocol[]
|
||||
plates ? Promise.resolve([]) : fetchOneOsDailyMileage(previousMileageDate(start), undefined, protocols, refresh),
|
||||
]);
|
||||
// Plate-scoped summary requests are used only for filtered trends.
|
||||
if (plates || start === end) return { summary, yesterdayRows };
|
||||
const missing = summary.vehicles.filter(v => v.endDateTotalMileageKm === null && v.validDays + v.anomalyDays > 0);
|
||||
if (plates) return { summary, yesterdayRows };
|
||||
const missing = summary.vehicles.filter(v => v.endDateTotalMileageKm === null);
|
||||
if (!missing.length) return { summary, yesterdayRows };
|
||||
const dates = mileageDatesBetween(start, previousMileageDate(end));
|
||||
const history = await fetchOneOsMileageDates(dates, missing.map(v => v.plateNumber), protocols);
|
||||
const priorOdometers = new Map<string, { date: string; km: number }>();
|
||||
const endRows = await fetchOneOsDailyMileage(end, missing.map(v => v.plateNumber), protocols, refresh);
|
||||
const observationDate = new Intl.DateTimeFormat('en-CA', { timeZone: 'Asia/Shanghai' });
|
||||
for (const row of endRows) {
|
||||
if (row.status !== 'NORMAL' || row.dataQuality !== 'CARRIED_FORWARD' || row.totalMileageKm == null || !row.dataTime) continue;
|
||||
// OneOS observation timestamps include the Shanghai offset, even for older carried readings.
|
||||
const time = Date.parse(row.dataTime);
|
||||
if (!Number.isFinite(time)) continue;
|
||||
const date = observationDate.format(time);
|
||||
if (date <= end) priorOdometers.set(row.plateNumber.toUpperCase(), { date, km: row.totalMileageKm });
|
||||
}
|
||||
const unresolved = missing.filter(v => !priorOdometers.has(v.plateNumber) && v.validDays + v.anomalyDays > 0);
|
||||
if (start === end || !unresolved.length) return { summary, yesterdayRows, priorOdometers };
|
||||
const dates = mileageDatesBetween(start, previousMileageDate(end));
|
||||
const history = await fetchOneOsMileageDates(dates, unresolved.map(v => v.plateNumber), protocols);
|
||||
for (const date of dates.reverse()) {
|
||||
for (const row of history.get(date) || []) {
|
||||
const plate = row.plateNumber.toUpperCase();
|
||||
|
||||
Reference in New Issue
Block a user