Skip to content
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

Merged
merged 5 commits into from
Dec 27, 2024

Conversation

Udit-takkar
Copy link
Contributor

@Udit-takkar Udit-takkar commented Dec 23, 2024

refactor: Update booking query to use userId and optimize availability

  • 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.

What does this PR do?

  • Fixes #XXXX (GitHub issue number)
  • Fixes CAL-XXXX (Linear issue number - should be visible at the bottom of the GitHub issue description)

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • N/A I have updated the developer docs in /docs if this PR makes changes that would require a documentation change. If N/A, write N/A here and check the checkbox.
  • N/A I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

…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-app graphite-app bot requested a review from a team December 23, 2024 08:31
@Udit-takkar Udit-takkar changed the title refactor: Update booking query to use userId and optimize availabilit… perf: improve getAvailable slots Dec 23, 2024
@keithwillcode keithwillcode added core area: core, team members only enterprise area: enterprise, audit log, organisation, SAML, SSO labels Dec 23, 2024
Copy link

graphite-app bot commented Dec 23, 2024

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.

Comment on lines 989 to 1004
const currentBookingsAllUsers = await monitorCallbackAsync(
getExistingBookings,
startTimeDate,
endTimeDate,
eventType,
sharedQuery,
usersWithCredentials,
allUserIds
);

const outOfOfficeDaysAllUsers = await monitorCallbackAsync(
getOOODates,
startTimeDate,
endTimeDate,
allUserIds
);
Copy link
Contributor Author

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()

@Udit-takkar Udit-takkar added the performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive label Dec 23, 2024
Copy link
Member

@alishaz-polymath alishaz-polymath left a 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! 👏

packages/lib/server/repository/booking.ts Outdated Show resolved Hide resolved
@Udit-takkar Udit-takkar added the high-risk Requires approval by Foundation team label Dec 23, 2024
@graphite-app graphite-app bot requested a review from a team December 23, 2024 09:09
Copy link

vercel bot commented Dec 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cal ⬜️ Ignored (Inspect) Visit Preview Dec 27, 2024 2:17pm
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview Dec 27, 2024 2:17pm


return busy.every((busyTime) => {
const startTime = dayjs.utc(busyTime.start).utc();
const endTime = dayjs.utc(busyTime.end);
const busyStartValue = dayjs.utc(busyTime.start).valueOf();
Copy link
Contributor

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()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor

github-actions bot commented Dec 23, 2024

E2E results are ready!

Comment on lines +65 to +91
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) {
Copy link
Member

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

Copy link
Member

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

@Udit-takkar
Copy link
Contributor Author

@hariombalhara opened this new PR #18382 without checkIsAvailable changes

@Udit-takkar Udit-takkar added this to the v4.9 milestone Dec 27, 2024
@Udit-takkar Udit-takkar changed the title perf: improve getAvailable slots perf: improve checkIsAvailable in getAvailable slots Dec 27, 2024
@emrysal emrysal merged commit e2f45bd into main Dec 27, 2024
40 checks passed
@emrysal emrysal deleted the perf/get-slots-improv branch December 27, 2024 16:01
joeauyeung pushed a commit that referenced this pull request Jan 5, 2025
* 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
MuhammadAimanSulaiman pushed a commit to hit-pay/cal.com that referenced this pull request Jan 10, 2025
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core area: core, team members only enterprise area: enterprise, audit log, organisation, SAML, SSO High priority Created by Linear-GitHub Sync high-risk Requires approval by Foundation team performance area: performance, page load, slow, slow endpoints, loading screen, unresponsive ready-for-e2e 💻 refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants