perf(journey): reuse time zone lookups when grouping large galleries

This commit is contained in:
Maurice
2026-11-30 17:10:09 +01:00
parent 8319554ce7
commit 6d14eb6677
2 changed files with 42 additions and 26 deletions
+20 -9
View File
@@ -2,21 +2,32 @@ import { readEnv } from '../../app-config';
import tzlookup from 'tz-lookup';
const localFormatters = new Map<string, Intl.DateTimeFormat>();
function localFormatter(zone: string): Intl.DateTimeFormat {
const existing = localFormatters.get(zone);
if (existing) return existing;
const formatter = new Intl.DateTimeFormat('en-CA', {
timeZone: zone,
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hourCycle: 'h23',
});
if (localFormatters.size >= 1000) localFormatters.clear();
localFormatters.set(zone, formatter);
return formatter;
}
/** Local calendar date and clock time for an instant in an IANA timezone. */
export function localParts(iso: string | null, timezone: string | null): { date: string | null; time: string | null } {
if (!iso) return { date: null, time: null };
try {
const date = new Date(iso);
if (Number.isNaN(date.getTime())) return { date: null, time: null };
const parts = new Intl.DateTimeFormat('en-CA', {
timeZone: timezone || 'UTC',
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
hourCycle: 'h23',
}).formatToParts(date);
const parts = localFormatter(timezone || 'UTC').formatToParts(date);
const value = (type: Intl.DateTimeFormatPartTypes) => parts.find((part) => part.type === type)?.value ?? '';
const localDate = `${value('year')}-${value('month')}-${value('day')}`;
const localTime = `${value('hour')}:${value('minute')}`;
@@ -126,30 +126,35 @@ export function localCaptures(photos: readonly FromPhotosPhoto[], fallbackZone:
const parsed = photos
.map((photo) => ({ photo, instant: captureInstant(photo.takenAt) }))
.filter((p) => Number.isFinite(p.instant));
// The zones the photos' own positions name, for those without one to borrow.
const geoZones = parsed
.map((p) => ({
instant: p.instant,
zone: isLocated(p.photo.lat, p.photo.lng) ? resolveTimeZone(p.photo.lat, p.photo.lng) : null,
}))
.filter((z): z is { instant: number; zone: string } => z.zone !== null);
// Resolve each position once, and index geotagged times for large galleries.
const locatedCaptures = parsed.map(({ photo, instant }) => {
const located = isLocated(photo.lat, photo.lng);
return { photo, instant, located, zone: located ? resolveTimeZone(photo.lat, photo.lng) : null };
});
const geoZones = locatedCaptures.filter((capture) => capture.zone !== null).sort((a, b) => a.instant - b.instant);
const times = geoZones.filter((capture, i) => capture.instant !== geoZones[i - 1]?.instant);
const borrowed = (instant: number): string | null => {
let best: { distance: number; zone: string } | null = null;
for (const candidate of geoZones) {
const distance = Math.abs(candidate.instant - instant);
if (distance <= BORROWED_ZONE_WINDOW_MS && (!best || distance < best.distance)) {
best = { distance, zone: candidate.zone };
}
let left = 0;
let right = times.length;
while (left < right) {
const middle = Math.floor((left + right) / 2);
const candidate = times[middle];
if (candidate && candidate.instant < instant) left = middle + 1;
else right = middle;
}
return best?.zone ?? null;
const before = times[left - 1];
const after = times[left];
const earlierGap = before ? instant - before.instant : Infinity;
const laterGap = after ? after.instant - instant : Infinity;
if (Math.min(earlierGap, laterGap) > BORROWED_ZONE_WINDOW_MS) return null;
return (earlierGap <= laterGap ? before : after)?.zone ?? null;
};
return parsed.map(({ photo, instant }) => {
const located = isLocated(photo.lat, photo.lng);
return locatedCaptures.map(({ photo, instant, located, zone: positionZone }) => {
const offset = carriedOffsetMinutes(photo.takenAt);
if (offset !== null) return { ...photo, instant, ...atOffset(instant, offset), zone: offsetLabel(offset), located };
const zone = (located ? resolveTimeZone(photo.lat, photo.lng) : null) ?? borrowed(instant) ?? fallbackZone ?? 'UTC';
const zone = positionZone ?? borrowed(instant) ?? fallbackZone ?? 'UTC';
const parts = localParts(new Date(instant).toISOString(), zone);
// localParts answers nulls only for a zone the runtime does not know; UTC then.
if (!parts.date || !parts.time) return { ...photo, instant, ...atOffset(instant, 0), zone: 'UTC', located };