feat(mileage): add statistic-time sorting and refresh control
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
@@ -104,8 +104,11 @@ function normalizeRange(startQuery: string, endQuery: string): { start: string;
|
||||
}
|
||||
|
||||
app.get('/', async (c) => {
|
||||
const sortBy = c.req.query('sortBy') || 'today';
|
||||
const sortOrder = c.req.query('sortOrder') || 'desc';
|
||||
const requestedSortBy = c.req.query('sortBy');
|
||||
const sortBy = requestedSortBy === 'total' || requestedSortBy === 'statisticTime'
|
||||
? requestedSortBy
|
||||
: 'today';
|
||||
const sortOrder = c.req.query('sortOrder') === 'asc' ? 'asc' : 'desc';
|
||||
const limit = Number(c.req.query('limit')) || 50;
|
||||
const page = Number(c.req.query('page')) || 1;
|
||||
const date = c.req.query('date') || '';
|
||||
@@ -189,9 +192,26 @@ app.get('/', async (c) => {
|
||||
};
|
||||
|
||||
const sorted = [...filtered].sort((a, b) => {
|
||||
if (sortBy === 'statisticTime') {
|
||||
const rawTimeA = a.dataTime || a.updatedAt || a.calculatedAt;
|
||||
const rawTimeB = b.dataTime || b.updatedAt || b.calculatedAt;
|
||||
const parsedTimeA = rawTimeA ? Date.parse(rawTimeA) : Number.NaN;
|
||||
const parsedTimeB = rawTimeB ? Date.parse(rawTimeB) : Number.NaN;
|
||||
const timeA = Number.isFinite(parsedTimeA) ? parsedTimeA : null;
|
||||
const timeB = Number.isFinite(parsedTimeB) ? parsedTimeB : null;
|
||||
|
||||
// Missing/invalid statistic times always stay at the end for both directions.
|
||||
if (timeA == null && timeB == null) return a.plate.localeCompare(b.plate, 'zh-CN');
|
||||
if (timeA == null) return 1;
|
||||
if (timeB == null) return -1;
|
||||
const timeDiff = sortOrder === 'desc' ? timeB - timeA : timeA - timeB;
|
||||
return timeDiff || a.plate.localeCompare(b.plate, 'zh-CN');
|
||||
}
|
||||
|
||||
const valA = sortBy === 'today' ? a.dailyKm : (a.totalKm || 0);
|
||||
const valB = sortBy === 'today' ? b.dailyKm : (b.totalKm || 0);
|
||||
return sortOrder === 'desc' ? valB - valA : valA - valB;
|
||||
const mileageDiff = sortOrder === 'desc' ? valB - valA : valA - valB;
|
||||
return mileageDiff || a.plate.localeCompare(b.plate, 'zh-CN');
|
||||
});
|
||||
|
||||
const offset = (page - 1) * limit;
|
||||
|
||||
Reference in New Issue
Block a user