-
Notifications
You must be signed in to change notification settings - Fork 11.7k
refactor: optimize team booking limits query with in-app filtering #27301
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
base: main
Are you sure you want to change the base?
Conversation
Replace expensive attendee email IN clause query with eventType.teamId filtering and in-application user filtering. Changes: - Add findByTeamIdAndDateRangeIncludeAttendees to BookingRepository - Refactor _getBusyTimesFromTeamLimitsForUsers to use new method - Filter bookings in application code using Set for O(1) lookups This improves query performance for large teams by: 1. Using indexed eventType.teamId column instead of attendee email IN 2. Reducing from 2-3 queries to 1-2 queries 3. Avoiding expensive EXISTS subquery with 64+ email parameters Co-Authored-By: alex@cal.com <me@alexvanandel.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
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.
1 issue found across 2 files
Prompt for AI agents (all issues)
Check if these issues are valid — if so, understand the root cause of each and fix them.
<file name="packages/trpc/server/routers/viewer/slots/util.ts">
<violation number="1" location="packages/trpc/server/routers/viewer/slots/util.ts:659">
P2: The new attendee-email filter applies to all bookings (including managed events), but the previous query only counted managed bookings when the organizer userId matched. This changes booking-limit behavior for managed events and can over-count limits. Consider distinguishing managed bookings and only applying attendee matching to non-managed bookings to preserve prior behavior.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| return true; | ||
| } | ||
| // Check if any user is an attendee | ||
| return booking.attendees.some((attendee) => userEmailSet.has(attendee.email)); |
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.
P2: The new attendee-email filter applies to all bookings (including managed events), but the previous query only counted managed bookings when the organizer userId matched. This changes booking-limit behavior for managed events and can over-count limits. Consider distinguishing managed bookings and only applying attendee matching to non-managed bookings to preserve prior behavior.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At packages/trpc/server/routers/viewer/slots/util.ts, line 659:
<comment>The new attendee-email filter applies to all bookings (including managed events), but the previous query only counted managed bookings when the organizer userId matched. This changes booking-limit behavior for managed events and can over-count limits. Consider distinguishing managed bookings and only applying attendee matching to non-managed bookings to preserve prior behavior.</comment>
<file context>
@@ -634,16 +634,32 @@ export class AvailableSlotsService {
+ return true;
+ }
+ // Check if any user is an attendee
+ return booking.attendees.some((attendee) => userEmailSet.has(attendee.email));
+ });
+
</file context>
Devin AI is addressing Cubic AI's review feedbackA Devin session has been created to address the issues identified by Cubic AI. |
|
I reviewed the Cubic AI feedback on this PR. The issue identified (violation #1 regarding the attendee-email filter applying to managed events) has a confidence score of 7/10, which is below the 9/10 threshold required for automated fixes. Issue skipped due to low confidence score:
Since no issues meet the 9/10 confidence threshold, no changes were made to this PR. A human reviewer should evaluate whether this concern is valid and warrants a fix. |
What does this PR do?
Optimizes the team booking limits query in
_getBusyTimesFromTeamLimitsForUsersby replacing an expensiveattendee.email IN (64+ emails)SQL pattern with indexedeventType.teamIdfiltering and in-application user filtering.Problem: The original query generated an expensive EXISTS subquery scanning attendees for each booking with 64+ email parameters, which is slow for large teams.
Solution:
eventType.teamId(indexed column)Changes:
findByTeamIdAndDateRangeIncludeAttendeestoBookingRepository_getBusyTimesFromTeamLimitsForUsersto use the new method with in-app filteringTrade-off: Returns slightly more data from DB (all team bookings vs only user-relevant), but the query is significantly faster due to using indexed columns instead of expensive EXISTS subqueries.
Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
Key verification points:
userId) should be counted toward limitsincludeManagedEventsis trueChecklist
Human Review Checklist
Link to Devin run: https://app.devin.ai/sessions/65b1815aa6b74057959160047de5e8f3
Requested by: @emrysal