perf(api): eliminate 3 N+1 query patterns
- timeline-holiday-load-support: deduplicate getResolvedCalendarHolidays by location key so resources sharing the same country/state/city resolve holidays once instead of per-resource - rate-card-lookup: add lookupRatesBatch that loads rate card lines once and scores locally per demand line, replacing per-line DB round-trips in estimate-demand-lines autoFillDemandLineRates - config-readmodels: include _count in utilization-category list query instead of calling getById per category for project counts Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -57,36 +57,57 @@ export async function loadTimelineHolidayOverlaysForReadModel(
|
||||
},
|
||||
});
|
||||
|
||||
// Group resources by location key to deduplicate holiday resolution.
|
||||
// Resources sharing the same (countryId, federalState, metroCityId) get
|
||||
// identical holidays, so we resolve once per location instead of once per resource.
|
||||
const locationGroups = new Map<
|
||||
string,
|
||||
{ locationResource: (typeof resources)[0]; resourceIds: string[] }
|
||||
>();
|
||||
for (const resource of resources) {
|
||||
const key = `${resource.countryId ?? ""}:${resource.federalState ?? ""}:${resource.metroCityId ?? ""}`;
|
||||
const existing = locationGroups.get(key);
|
||||
if (existing) {
|
||||
existing.resourceIds.push(resource.id);
|
||||
} else {
|
||||
locationGroups.set(key, { locationResource: resource, resourceIds: [resource.id] });
|
||||
}
|
||||
}
|
||||
|
||||
const resolverDb = asHolidayResolverDb(db);
|
||||
const overlays = await Promise.all(
|
||||
resources.map(async (resource) => {
|
||||
const holidays = await getResolvedCalendarHolidays(asHolidayResolverDb(db), {
|
||||
[...locationGroups.values()].map(async ({ locationResource, resourceIds }) => {
|
||||
const holidays = await getResolvedCalendarHolidays(resolverDb, {
|
||||
periodStart: input.startDate,
|
||||
periodEnd: input.endDate,
|
||||
countryId: resource.countryId,
|
||||
countryCode: resource.country?.code ?? null,
|
||||
federalState: resource.federalState,
|
||||
metroCityId: resource.metroCityId,
|
||||
metroCityName: resource.metroCity?.name ?? null,
|
||||
countryId: locationResource.countryId,
|
||||
countryCode: locationResource.country?.code ?? null,
|
||||
federalState: locationResource.federalState,
|
||||
metroCityId: locationResource.metroCityId,
|
||||
metroCityName: locationResource.metroCity?.name ?? null,
|
||||
});
|
||||
|
||||
return holidays.map((holiday) => {
|
||||
const holidayDate = new Date(`${holiday.date}T00:00:00.000Z`);
|
||||
return {
|
||||
id: `calendar-holiday:${resource.id}:${holiday.date}`,
|
||||
resourceId: resource.id,
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
status: "APPROVED" as const,
|
||||
startDate: holidayDate,
|
||||
endDate: holidayDate,
|
||||
note: holiday.name,
|
||||
scope: holiday.scope,
|
||||
calendarName: holiday.calendarName,
|
||||
sourceType: holiday.sourceType,
|
||||
countryCode: resource.country?.code ?? null,
|
||||
countryName: resource.country?.name ?? null,
|
||||
federalState: resource.federalState ?? null,
|
||||
metroCityName: resource.metroCity?.name ?? null,
|
||||
};
|
||||
return resourceIds.flatMap((resourceId) => {
|
||||
const resource = resources.find((r) => r.id === resourceId)!;
|
||||
return holidays.map((holiday) => {
|
||||
const holidayDate = new Date(`${holiday.date}T00:00:00.000Z`);
|
||||
return {
|
||||
id: `calendar-holiday:${resourceId}:${holiday.date}`,
|
||||
resourceId,
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
status: "APPROVED" as const,
|
||||
startDate: holidayDate,
|
||||
endDate: holidayDate,
|
||||
note: holiday.name,
|
||||
scope: holiday.scope,
|
||||
calendarName: holiday.calendarName,
|
||||
sourceType: holiday.sourceType,
|
||||
countryCode: resource.country?.code ?? null,
|
||||
countryName: resource.country?.name ?? null,
|
||||
federalState: resource.federalState ?? null,
|
||||
metroCityName: resource.metroCity?.name ?? null,
|
||||
};
|
||||
});
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user