Files
calendar/packages/lib/slots.ts
T
devin-ai-integration[bot]GitHubDevin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>keith@cal.com <keithwillcode@gmail.com>
816df3704e perf: Optimize getSlots function to handle large dateRanges arrays efficiently (#21371)
* perf: Optimize getSlots function to handle large dateRanges arrays efficiently

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* Update packages/lib/slots.test.ts

* test: Update performance test to use 2000 date ranges

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

* test: Update performance test to process 2000 date ranges across multiple days

Co-Authored-By: keith@cal.com <keithwillcode@gmail.com>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: keith@cal.com <keithwillcode@gmail.com>
2025-05-17 04:00:13 +01:00

186 lines
5.2 KiB
TypeScript

import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs";
import type { IFromUser, IOutOfOfficeData, IToUser } from "@calcom/lib/getUserAvailability";
import { withReporting } from "@calcom/lib/sentryWrapper";
import type { DateRange } from "./date-ranges";
import { getTimeZone } from "./dayjs";
export type GetSlots = {
inviteeDate: Dayjs;
frequency: number;
dateRanges: DateRange[];
minimumBookingNotice: number;
eventLength: number;
offsetStart?: number;
datesOutOfOffice?: IOutOfOfficeData;
};
export type TimeFrame = { userIds?: number[]; startTime: number; endTime: number };
const minimumOfOne = (input: number) => (input < 1 ? 1 : input);
function buildSlotsWithDateRanges({
dateRanges,
frequency,
eventLength,
timeZone,
minimumBookingNotice,
offsetStart,
datesOutOfOffice,
}: {
dateRanges: DateRange[];
frequency: number;
eventLength: number;
timeZone: string;
minimumBookingNotice: number;
offsetStart?: number;
datesOutOfOffice?: IOutOfOfficeData;
}) {
// keep the old safeguards in; may be needed.
frequency = minimumOfOne(frequency);
eventLength = minimumOfOne(eventLength);
offsetStart = offsetStart ? minimumOfOne(offsetStart) : 0;
const orderedDateRanges = dateRanges.sort((a, b) => a.start.valueOf() - b.start.valueOf());
// there can only ever be one slot at a given start time, and based on duration also only a single length.
const slots = new Map<
string,
{
time: Dayjs;
userIds?: number[];
away?: boolean;
fromUser?: IFromUser;
toUser?: IToUser;
reason?: string;
emoji?: string;
}
>();
let interval = Number(process.env.NEXT_PUBLIC_AVAILABILITY_SCHEDULE_INTERVAL) || 1;
const intervalsWithDefinedStartTimes = [60, 30, 20, 15, 10, 5];
for (let i = 0; i < intervalsWithDefinedStartTimes.length; i++) {
if (frequency % intervalsWithDefinedStartTimes[i] === 0) {
interval = intervalsWithDefinedStartTimes[i];
break;
}
}
const startTimeWithMinNotice = dayjs.utc().add(minimumBookingNotice, "minute");
const slotBoundaries = new Map<string, true>();
orderedDateRanges.forEach((range) => {
const dateYYYYMMDD = range.start.format("YYYY-MM-DD");
let slotStartTime = range.start.utc().isAfter(startTimeWithMinNotice)
? range.start
: startTimeWithMinNotice;
slotStartTime =
slotStartTime.minute() % interval !== 0
? slotStartTime.startOf("hour").add(Math.ceil(slotStartTime.minute() / interval) * interval, "minute")
: slotStartTime;
slotStartTime = slotStartTime.add(offsetStart ?? 0, "minutes").tz(timeZone);
// Find the nearest appropriate slot boundary if this time falls within an existing slot
const slotBoundariesArray = Array.from(slotBoundaries.keys()).map((t) => dayjs(t));
if (slotBoundariesArray.length > 0) {
slotBoundariesArray.sort((a, b) => a.valueOf() - b.valueOf());
let prevBoundary = null;
for (let i = slotBoundariesArray.length - 1; i >= 0; i--) {
if (slotBoundariesArray[i].isBefore(slotStartTime)) {
prevBoundary = slotBoundariesArray[i];
break;
}
}
if (prevBoundary) {
const prevBoundaryEnd = prevBoundary.add(frequency + (offsetStart ?? 0), "minutes");
if (prevBoundaryEnd.isAfter(slotStartTime)) {
if (!prevBoundary.isBefore(range.start)) {
slotStartTime = prevBoundary;
} else {
slotStartTime = prevBoundaryEnd;
}
slotStartTime = slotStartTime.tz(timeZone);
}
}
}
while (!slotStartTime.add(eventLength, "minutes").subtract(1, "second").utc().isAfter(range.end)) {
const slotKey = slotStartTime.toISOString();
if (slots.has(slotKey)) {
slotStartTime = slotStartTime.add(frequency + (offsetStart ?? 0), "minutes");
continue;
}
slotBoundaries.set(slotKey, true);
const dateOutOfOfficeExists = datesOutOfOffice?.[dateYYYYMMDD];
let slotData: {
time: Dayjs;
userIds?: number[];
away?: boolean;
fromUser?: IFromUser;
toUser?: IToUser;
reason?: string;
emoji?: string;
} = {
time: slotStartTime,
};
if (dateOutOfOfficeExists) {
const { toUser, fromUser, reason, emoji } = dateOutOfOfficeExists;
slotData = {
time: slotStartTime,
away: true,
...(fromUser && { fromUser }),
...(toUser && { toUser }),
...(reason && { reason }),
...(emoji && { emoji }),
};
}
slots.set(slotKey, slotData);
slotStartTime = slotStartTime.add(frequency + (offsetStart ?? 0), "minutes");
}
});
return Array.from(slots.values());
}
const getSlots = ({
inviteeDate,
frequency,
minimumBookingNotice,
dateRanges,
eventLength,
offsetStart = 0,
datesOutOfOffice,
}: GetSlots): {
time: Dayjs;
userIds?: number[];
away?: boolean;
fromUser?: IFromUser;
toUser?: IToUser;
reason?: string;
emoji?: string;
}[] => {
return buildSlotsWithDateRanges({
dateRanges,
frequency,
eventLength,
timeZone: getTimeZone(inviteeDate),
minimumBookingNotice,
offsetStart,
datesOutOfOffice,
});
};
export default withReporting(getSlots, "getSlots");