perf(api): eliminate N+1 queries, add query guards and missing indexes
- Notification fan-out: replace sequential for loops with Promise.all (allocation-effects, notification-broadcast, create-notification) - Public holiday batch: group resources by location combo, resolve holidays once per group, replace per-holiday delete/findFirst/create with 3 batched queries (~18K → ~5 queries) - Add take guards to unbounded findMany calls (resource-analytics: 5000, resource-marketplace: 2000, resource-capacity: 1000, chargeability-report: 2000) - auto-staffing: add select with only needed fields + take: 5000 - schema.prisma: add 5 missing indexes (ManagementLevel.groupId, Blueprint.isActive/target, Comment.parentId, Vacation.requestedById, Resource.managementLevelGroupId) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -101,24 +101,28 @@ export async function createDemandRequirementWithEffects(
|
||||
const projectName = project?.name ?? "Unknown project";
|
||||
const headcount = demandRequirement.headcount ?? 1;
|
||||
|
||||
for (const manager of managers) {
|
||||
const task = await db.notification.create({
|
||||
data: {
|
||||
userId: manager.id,
|
||||
category: "TASK",
|
||||
type: "DEMAND_FILL",
|
||||
priority: "NORMAL",
|
||||
title: `Staff demand: ${roleName} for ${projectName}`,
|
||||
body: `${headcount} ${roleName} needed for project ${projectName}`,
|
||||
taskStatus: "OPEN",
|
||||
taskAction: buildTaskAction("fill_demand", demandRequirement.id),
|
||||
entityId: demandRequirement.id,
|
||||
entityType: "demand",
|
||||
link: `/projects/${demandRequirement.projectId}`,
|
||||
channel: "in_app",
|
||||
},
|
||||
});
|
||||
emitNotificationCreated(manager.id, task.id);
|
||||
if (managers.length > 0) {
|
||||
const sharedData = {
|
||||
category: "TASK" as const,
|
||||
type: "DEMAND_FILL" as const,
|
||||
priority: "NORMAL" as const,
|
||||
title: `Staff demand: ${roleName} for ${projectName}`,
|
||||
body: `${headcount} ${roleName} needed for project ${projectName}`,
|
||||
taskStatus: "OPEN" as const,
|
||||
taskAction: buildTaskAction("fill_demand", demandRequirement.id),
|
||||
entityId: demandRequirement.id,
|
||||
entityType: "demand",
|
||||
link: `/projects/${demandRequirement.projectId}`,
|
||||
channel: "in_app" as const,
|
||||
};
|
||||
const tasks = await Promise.all(
|
||||
managers.map((manager) =>
|
||||
db.notification.create({ data: { userId: manager.id, ...sharedData } }),
|
||||
),
|
||||
);
|
||||
for (const task of tasks) {
|
||||
emitNotificationCreated(task.userId, task.id);
|
||||
}
|
||||
}
|
||||
|
||||
checkBudgetThresholdsInBackground(db, demandRequirement.projectId);
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
} from "@capakraken/engine";
|
||||
import type { PrismaClient } from "@capakraken/db";
|
||||
import type { WeekdayAvailability, PermissionKey } from "@capakraken/shared";
|
||||
import { PermissionKey as PermissionKeys } from "@capakraken/shared";
|
||||
import { PermissionKey as PermissionKeys, round1 } from "@capakraken/shared";
|
||||
import { isChargeabilityActualBooking, listAssignmentBookings } from "@capakraken/application";
|
||||
import { z } from "zod";
|
||||
import { anonymizeResources, getAnonymizationDirectory } from "../lib/anonymization.js";
|
||||
@@ -25,10 +25,6 @@ type ChargeabilityReportProcedureContext = Pick<TRPCContext, "db"> & {
|
||||
permissions?: Set<PermissionKey>;
|
||||
};
|
||||
|
||||
function round1(value: number): number {
|
||||
return Math.round(value * 10) / 10;
|
||||
}
|
||||
|
||||
function isIsoDateInMonth(isoDate: string, monthKey: string): boolean {
|
||||
return isoDate.startsWith(`${monthKey}-`);
|
||||
}
|
||||
@@ -265,6 +261,7 @@ async function queryChargeabilityReport(
|
||||
metroCity: { select: { id: true, name: true } },
|
||||
},
|
||||
orderBy: { displayName: "asc" },
|
||||
take: 2000,
|
||||
});
|
||||
|
||||
if (resources.length === 0) {
|
||||
|
||||
@@ -117,27 +117,28 @@ async function persistImmediateBroadcast(
|
||||
data: buildBroadcastCreateData(senderId, input),
|
||||
});
|
||||
|
||||
const notificationIds: BroadcastRecipientNotification[] = [];
|
||||
for (const recipientUserId of recipientIds) {
|
||||
const notificationId = await createNotification({
|
||||
db,
|
||||
userId: recipientUserId,
|
||||
type: `BROADCAST_${input.category}`,
|
||||
title: input.title,
|
||||
body: input.body,
|
||||
link: input.link,
|
||||
category: input.category,
|
||||
priority: input.priority,
|
||||
channel: input.channel,
|
||||
sourceId: broadcast.id,
|
||||
senderId,
|
||||
taskStatus: isTask ? "OPEN" : undefined,
|
||||
taskAction: input.taskAction,
|
||||
dueDate: input.dueDate,
|
||||
emit: false,
|
||||
});
|
||||
notificationIds.push({ id: notificationId, userId: recipientUserId });
|
||||
}
|
||||
const created = await Promise.all(
|
||||
recipientIds.map((recipientUserId) =>
|
||||
createNotification({
|
||||
db,
|
||||
userId: recipientUserId,
|
||||
type: `BROADCAST_${input.category}`,
|
||||
title: input.title,
|
||||
body: input.body,
|
||||
link: input.link,
|
||||
category: input.category,
|
||||
priority: input.priority,
|
||||
channel: input.channel,
|
||||
sourceId: broadcast.id,
|
||||
senderId,
|
||||
taskStatus: isTask ? "OPEN" : undefined,
|
||||
taskAction: input.taskAction,
|
||||
dueDate: input.dueDate,
|
||||
emit: false,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const notificationIds: BroadcastRecipientNotification[] = created.map((id, i) => ({ id, userId: recipientIds[i]! }));
|
||||
|
||||
const updatedBroadcast = await db.notificationBroadcast.update({
|
||||
where: { id: broadcast.id },
|
||||
|
||||
@@ -26,6 +26,7 @@ export const resourceAnalyticsProcedures = {
|
||||
const resources = await ctx.db.resource.findMany({
|
||||
where: { isActive: true },
|
||||
select: { id: true, displayName: true, chapter: true, skills: true },
|
||||
take: 5000,
|
||||
});
|
||||
|
||||
const skillMap = new Map<
|
||||
|
||||
@@ -177,6 +177,7 @@ export const resourceCapacityReadProcedures = {
|
||||
country: { select: { code: true } },
|
||||
metroCity: { select: { name: true } },
|
||||
},
|
||||
take: 1000,
|
||||
});
|
||||
const bookings = await listAssignmentBookings(ctx.db, {
|
||||
startDate: start,
|
||||
|
||||
@@ -51,6 +51,7 @@ export const resourceMarketplaceReadProcedures = {
|
||||
country: { select: { code: true } },
|
||||
metroCity: { select: { name: true } },
|
||||
},
|
||||
take: 2000,
|
||||
});
|
||||
|
||||
const assignments = await ctx.db.assignment.findMany({
|
||||
|
||||
@@ -35,64 +35,128 @@ export async function batchCreatePublicHolidayVacations(
|
||||
return { created: 0, holidays: 0, resources: 0 };
|
||||
}
|
||||
|
||||
let created = 0;
|
||||
let holidayCount = 0;
|
||||
const periodStart = new Date(`${input.year}-01-01T00:00:00.000Z`);
|
||||
const periodEnd = new Date(`${input.year}-12-31T00:00:00.000Z`);
|
||||
|
||||
// Step 1: Group resources by their holiday-resolution key so we call
|
||||
// getResolvedCalendarHolidays once per unique location combo instead of
|
||||
// once per resource (~5-10 calls vs. 500+).
|
||||
type HolidayGroup = {
|
||||
countryId: string | null;
|
||||
countryCode: string | null | undefined;
|
||||
federalState: string | null | undefined;
|
||||
metroCityId: string | null;
|
||||
metroCityName: string | null | undefined;
|
||||
resourceIds: string[];
|
||||
};
|
||||
|
||||
const groups = new Map<string, HolidayGroup>();
|
||||
for (const resource of resources) {
|
||||
const holidays = await getResolvedCalendarHolidays(asHolidayResolverDb(db), {
|
||||
periodStart: new Date(`${input.year}-01-01T00:00:00.000Z`),
|
||||
periodEnd: new Date(`${input.year}-12-31T00:00:00.000Z`),
|
||||
countryId: resource.countryId,
|
||||
countryCode: resource.country?.code,
|
||||
federalState: input.federalState ?? resource.federalState,
|
||||
metroCityId: resource.metroCityId,
|
||||
metroCityName: resource.metroCity?.name,
|
||||
});
|
||||
holidayCount += holidays.length;
|
||||
const effectiveFederalState = input.federalState ?? resource.federalState;
|
||||
const key = [
|
||||
resource.countryId ?? "",
|
||||
resource.country?.code ?? "",
|
||||
effectiveFederalState ?? "",
|
||||
resource.metroCityId ?? "",
|
||||
].join("|");
|
||||
|
||||
if (!groups.has(key)) {
|
||||
groups.set(key, {
|
||||
countryId: resource.countryId,
|
||||
countryCode: resource.country?.code,
|
||||
federalState: effectiveFederalState,
|
||||
metroCityId: resource.metroCityId,
|
||||
metroCityName: resource.metroCity?.name,
|
||||
resourceIds: [],
|
||||
});
|
||||
}
|
||||
groups.get(key)!.resourceIds.push(resource.id);
|
||||
}
|
||||
|
||||
// Step 2: Resolve holidays once per group (parallel).
|
||||
const resourceHolidays = new Map<string, Array<{ date: string; name: string }>>();
|
||||
await Promise.all(
|
||||
[...groups.values()].map(async (group) => {
|
||||
const holidays = await getResolvedCalendarHolidays(asHolidayResolverDb(db), {
|
||||
periodStart,
|
||||
periodEnd,
|
||||
countryId: group.countryId,
|
||||
countryCode: group.countryCode,
|
||||
federalState: group.federalState,
|
||||
metroCityId: group.metroCityId,
|
||||
metroCityName: group.metroCityName,
|
||||
});
|
||||
for (const resourceId of group.resourceIds) {
|
||||
resourceHolidays.set(resourceId, holidays);
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// Step 3: Build the full list of (resourceId, date) pairs.
|
||||
const allPairs: Array<{ resourceId: string; startDate: Date; endDate: Date; name: string }> = [];
|
||||
for (const resource of resources) {
|
||||
const holidays = resourceHolidays.get(resource.id) ?? [];
|
||||
for (const holiday of holidays) {
|
||||
const startDate = new Date(holiday.date);
|
||||
const endDate = new Date(holiday.date);
|
||||
|
||||
if (input.replaceExisting) {
|
||||
await db.vacation.deleteMany({
|
||||
where: {
|
||||
resourceId: resource.id,
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
startDate,
|
||||
endDate,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const exists = await db.vacation.findFirst({
|
||||
where: {
|
||||
resourceId: resource.id,
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
startDate,
|
||||
endDate,
|
||||
},
|
||||
});
|
||||
if (exists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await db.vacation.create({
|
||||
data: {
|
||||
resourceId: resource.id,
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
status: VacationStatus.APPROVED,
|
||||
startDate,
|
||||
endDate,
|
||||
note: holiday.name,
|
||||
requestedById: adminUserId,
|
||||
approvedById: adminUserId,
|
||||
approvedAt: new Date(),
|
||||
},
|
||||
});
|
||||
created++;
|
||||
const date = new Date(holiday.date);
|
||||
allPairs.push({ resourceId: resource.id, startDate: date, endDate: date, name: holiday.name });
|
||||
}
|
||||
}
|
||||
|
||||
return { created, holidays: holidayCount, resources: resources.length };
|
||||
const holidayCount = allPairs.length;
|
||||
|
||||
if (holidayCount === 0) {
|
||||
return { created: 0, holidays: 0, resources: resources.length };
|
||||
}
|
||||
|
||||
const resourceIds = resources.map((r) => r.id);
|
||||
|
||||
// Step 4: Batch delete for the whole period if replaceExisting (1 query).
|
||||
if (input.replaceExisting) {
|
||||
await db.vacation.deleteMany({
|
||||
where: {
|
||||
resourceId: { in: resourceIds },
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
startDate: { gte: periodStart, lte: periodEnd },
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Step 5: Batch fetch all existing entries for the period (1 query).
|
||||
const existing = await db.vacation.findMany({
|
||||
where: {
|
||||
resourceId: { in: resourceIds },
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
startDate: { gte: periodStart, lte: periodEnd },
|
||||
},
|
||||
select: { resourceId: true, startDate: true },
|
||||
});
|
||||
|
||||
const existingSet = new Set(
|
||||
existing.map((e) => `${e.resourceId}::${new Date(e.startDate).toISOString()}`),
|
||||
);
|
||||
|
||||
// Step 6: createMany for all new entries (1 query).
|
||||
const toCreate = allPairs.filter(
|
||||
(p) => !existingSet.has(`${p.resourceId}::${p.startDate.toISOString()}`),
|
||||
);
|
||||
|
||||
if (toCreate.length > 0) {
|
||||
const now = new Date();
|
||||
await db.vacation.createMany({
|
||||
data: toCreate.map((p) => ({
|
||||
resourceId: p.resourceId,
|
||||
type: VacationType.PUBLIC_HOLIDAY,
|
||||
status: VacationStatus.APPROVED,
|
||||
startDate: p.startDate,
|
||||
endDate: p.endDate,
|
||||
note: p.name,
|
||||
requestedById: adminUserId,
|
||||
approvedById: adminUserId,
|
||||
approvedAt: now,
|
||||
})),
|
||||
skipDuplicates: true,
|
||||
});
|
||||
}
|
||||
|
||||
return { created: toCreate.length, holidays: holidayCount, resources: resources.length };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user