-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: improve checkIsAvailable in getAvailable slots #18352
Conversation
…y check logic - Changed the booking query to use userId instead of nested user object for filtering active bookings. - Refactored availability check logic to improve readability and performance by using valueOf() for time comparisons and consolidating multiple checks into a single return statement.
Graphite Automations"Add consumer team as reviewer" took an action on this PR • (12/23/24)1 reviewer was added to this PR based on Keith Williams's automation. "Add ready-for-e2e label" took an action on this PR • (12/23/24)1 label was added to this PR based on Keith Williams's automation. "Add foundation team as reviewer" took an action on this PR • (12/23/24)1 reviewer was added to this PR based on Keith Williams's automation. |
const currentBookingsAllUsers = await monitorCallbackAsync( | ||
getExistingBookings, | ||
startTimeDate, | ||
endTimeDate, | ||
eventType, | ||
sharedQuery, | ||
usersWithCredentials, | ||
allUserIds | ||
); | ||
|
||
const outOfOfficeDaysAllUsers = await monitorCallbackAsync( | ||
getOOODates, | ||
startTimeDate, | ||
endTimeDate, | ||
allUserIds | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
both of them don't depend on each other so we can use Promise.all()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test Failing, but changes LGTM 🚀
Great improvement in readability! 👏
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Skipped Deployments
|
|
||
return busy.every((busyTime) => { | ||
const startTime = dayjs.utc(busyTime.start).utc(); | ||
const endTime = dayjs.utc(busyTime.end); | ||
const busyStartValue = dayjs.utc(busyTime.start).valueOf(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using toDate is preferred here, it's auto-cast to valueOf()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
E2E results are ready! |
const slotStartDate = time.utc().toDate(); | ||
const slotEndDate = time.add(eventLength, "minutes").utc().toDate(); | ||
|
||
return busy.every((busyTime) => { | ||
const startTime = dayjs.utc(busyTime.start).utc(); | ||
const endTime = dayjs.utc(busyTime.end); | ||
const busyStartDate = dayjs.utc(busyTime.start).toDate(); | ||
const busyEndDate = dayjs.utc(busyTime.end).toDate(); | ||
|
||
if (endTime.isBefore(slotStartTime) || startTime.isAfter(slotEndTime)) { | ||
// First check if there's any overlap at all | ||
// If busy period ends before slot starts or starts after slot ends, there's no overlap | ||
if (busyEndDate <= slotStartDate || busyStartDate >= slotEndDate) { | ||
return true; | ||
} | ||
|
||
if (slotStartTime.isBetween(startTime, endTime, null, "[)")) { | ||
return false; | ||
} else if (slotEndTime.isBetween(startTime, endTime, null, "(]")) { | ||
// Now check all possible overlap scenarios: | ||
|
||
// 1. Slot start falls within busy period (inclusive start, exclusive end) | ||
if (slotStartDate >= busyStartDate && slotStartDate < busyEndDate) { | ||
return false; | ||
} | ||
|
||
// Check if start times are the same | ||
if (time.utc().isBetween(startTime, endTime, null, "[)")) { | ||
// 2. Slot end falls within busy period (exclusive start, inclusive end) | ||
if (slotEndDate > busyStartDate && slotEndDate <= busyEndDate) { | ||
return false; | ||
} | ||
// Check if slot end time is between start and end time | ||
else if (slotEndTime.isBetween(startTime, endTime)) { | ||
|
||
// 3. Busy period completely contained within slot | ||
if (busyStartDate >= slotStartDate && busyEndDate <= slotEndDate) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have unit tests for this? We should add checkIfAvailable.timezone.test.ts
that would ensure same code is tested in different timezones
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it isn't strictly dependent(which I think is the case), we can merge other changes and merge this one after unit tests
@hariombalhara opened this new PR #18382 without |
* refactor: Update booking query to use userId and optimize availability check logic - Changed the booking query to use userId instead of nested user object for filtering active bookings. - Refactored availability check logic to improve readability and performance by using valueOf() for time comparisons and consolidating multiple checks into a single return statement. * fix: update tests * chore: use toDate() * tests: add checkavailable unit tests
* refactor: Update booking query to use userId and optimize availability check logic - Changed the booking query to use userId instead of nested user object for filtering active bookings. - Refactored availability check logic to improve readability and performance by using valueOf() for time comparisons and consolidating multiple checks into a single return statement. * fix: update tests * chore: use toDate() * tests: add checkavailable unit tests
refactor: Update booking query to use userId and optimize availability
What does this PR do?
Mandatory Tasks (DO NOT REMOVE)
How should this be tested?