perf(db): the calendar export, the journey reads and the stop reorders read and write in batches

The ICS export reads the stops and notes of all dated days in two statements instead of two per day, the journey's full read and entry list take the source trip titles in one join instead of one read per entry, and reordering a day's stops or sorting it by time writes every position in one CASE update. Results are unchanged: each day keeps its order (the id settles a tie), a missing trip still names null, and an id listed twice keeps its last position.
This commit is contained in:
Maurice
2026-10-10 12:37:51 +02:00
parent 4a3c97e500
commit e316ccf3bb
11 changed files with 163 additions and 61 deletions
@@ -461,6 +461,30 @@ export class DayAssignmentsRepository extends TrekRepository<DayAssignments> {
await this.nativeUpdate(where, { order_index });
}
/**
* {@link setOrderIndex} for many rows in one statement: `UPDATE
* day_assignments SET order_index = CASE id WHEN ? THEN ? ... END WHERE id IN
* (...) [AND day_id = ?]`. The result is what the writes one by one in the
* given order left: an id listed twice keeps its last index, and with
* `day_id` a row on another day is not touched.
*/
async setOrderIndexes(order: ReadonlyArray<{ id: number; order_index: number }>, day_id?: number): Promise<void> {
const last = new Map<number, number>();
for (const { id, order_index } of order) last.set(id, order_index);
const [first, ...rest] = [...last];
if (!first) return;
let query = this.kysely<Pick<DB, 'day_assignments'>>()
.updateTable('day_assignments')
.set((eb) => {
let index = eb.case().when('id', '=', first[0]).then(first[1]);
for (const [id, orderIndex] of rest) index = index.when('id', '=', id).then(orderIndex);
return { order_index: index.else(eb.ref('order_index')).end() };
})
.where('id', 'in', [...last.keys()]);
if (day_id !== undefined) query = query.where('day_id', '=', day_id);
await query.execute();
}
/** AS13 (`moveAssignment`'s source-day read) — `SELECT day_id FROM day_assignments WHERE id = ?`. */
async getDayId(id: number): Promise<number | undefined> {
const platform = this.getEntityManager().getPlatform();
@@ -115,6 +115,22 @@ export class JourneyEntriesRepository extends TrekRepository<JourneyEntries> {
.execute<JourneyEntry[]>('all', false);
}
/**
* JG16/JG73 — the titles of the trips a journey's entries came from, keyed by
* trip id, in one read (the journey reads used to ask once per entry). A trip
* that is gone has no entry, so its entries read a null name as before.
*/
async sourceTripTitles(journeyId: number): Promise<Map<number, string | null>> {
const rows = await this.kysely<Pick<DB, 'journey_entries' | 'trips'>>()
.selectFrom('journey_entries as je')
.innerJoin('trips as t', 't.id', 'je.source_trip_id')
.select(['t.id', 't.title'])
.distinct()
.where('je.journey_id', '=', journeyId)
.execute();
return new Map(rows.map((r) => [r.id, r.title ?? null]));
}
/**
* JS13 (Plan 4 Task 8b relocation) — the public `getPublicJourney` route's
* entry list: `SELECT je.* FROM journey_entries je WHERE je.journey_id=?
@@ -171,19 +171,16 @@ export interface CalendarReservationRow extends ReservationAllColumnsRow {
type CalendarReservationKyselyDB = Pick<DB, 'reservations' | 'places' | 'day_accommodations' | 'days'>;
/**
* CL4 (`CalendarService.buildTripCalendar`'s per-day assignment read,
* previously run once per dated day) — `SELECT da.*, p.name as place_name,
* p.address as place_address, p.lat as place_lat, p.lng as place_lng,
* CL4 (`CalendarService.buildTripCalendar`'s assignment read, for every dated
* day at once) — `SELECT da.*, p.name as place_name, p.address as
* place_address, p.lat as place_lat, p.lng as place_lng,
* COALESCE(da.assignment_time, p.place_time) as effective_time,
* COALESCE(da.assignment_end_time, p.end_time) as effective_end_time FROM
* day_assignments da JOIN places p ON da.place_id = p.id WHERE da.day_id =
* ? AND da.accommodation_id IS NULL ORDER BY da.order_index ASC,
* da.created_at ASC` — the booked-night stop (its own accommodation-linked
* `day_assignments` row) is excluded, matching the legacy comment ("that
* stop is the booking … reading it here as well would put the hotel on the
* day a second time"). Kept per-day (the caller's own loop), not batched —
* result-identical either way per the inventory's own note; per-day is the
* smaller diff against the legacy statement shape.
* day_assignments da JOIN places p ON da.place_id = p.id WHERE da.day_id IN
* (...) AND da.accommodation_id IS NULL ORDER BY da.order_index, da.created_at,
* da.id` — the booked-night stop (its own accommodation-linked row) is
* excluded: it is the booking, and reading it here would put the hotel on the
* day a second time. Each day's rows keep the per-day order; the id settles a tie.
*/
export interface CalendarStopRow extends DayAssignmentRow {
place_name: string;
@@ -994,8 +991,9 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
return rows as CalendarReservationRow[];
}
/** CL4 — see {@link CalendarStopRow}'s docstring. */
async listCalendarStops(day_id: number): Promise<CalendarStopRow[]> {
/** CL4 — see {@link CalendarStopRow}'s docstring. No days, no query. */
async listCalendarStopsForDays(day_ids: number[]): Promise<CalendarStopRow[]> {
if (day_ids.length === 0) return [];
const rows = await this.kysely<CalendarStopsKyselyDB>()
.selectFrom('day_assignments as da')
.innerJoin('places as p', 'p.id', 'da.place_id')
@@ -1008,10 +1006,11 @@ export class ReservationsRepository extends TrekRepository<Reservations> {
eb.fn.coalesce('da.assignment_time', 'p.place_time').as('effective_time'),
eb.fn.coalesce('da.assignment_end_time', 'p.end_time').as('effective_end_time'),
])
.where('da.day_id', '=', day_id)
.where('da.day_id', 'in', day_ids)
.where('da.accommodation_id', 'is', null)
.orderBy('da.order_index', 'asc')
.orderBy('da.created_at', 'asc')
.orderBy('da.id', 'asc')
.execute();
return rows as CalendarStopRow[];
}