-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
feat: allows forcing/skipping calendar cache serving #18224
Conversation
Signed-off-by: Omar López <zomars@me.com>
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 2 Skipped Deployments
|
E2E results are ready! |
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.
Ready for review
async getFreeBusyResult( | ||
args: FreeBusyArgs, | ||
shouldServeCache?: boolean | ||
): Promise<calendar_v3.Schema$FreeBusyResponse> { |
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.
Extracted to take advantage of early returns
args: FreeBusyArgs, | ||
shouldServeCache?: boolean | ||
): Promise<calendar_v3.Schema$FreeBusyResponse> { | ||
if (shouldServeCache === false) return await this.fetchAvailability(args); |
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 the parameter is explicitly set to false we skip cache altogether
@@ -537,7 +544,8 @@ export default class GoogleCalendarService implements Calendar { | |||
async getAvailability( | |||
dateFrom: string, | |||
dateTo: string, | |||
selectedCalendars: IntegrationCalendar[] | |||
selectedCalendars: IntegrationCalendar[], | |||
shouldServeCache?: boolean |
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.
I'm not a fan to drilling but I couldn't find a cleaner way to pass this setting around.
externalId: calendarId, | ||
}, | ||
}); | ||
const sc = await SelectedCalendarRepository.findByExternalId(credentialId, calendarId); |
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.
Moved to repo
// Populate the cache back for the remaining calendars, if any | ||
const remainingCalendars = | ||
sc?.credential?.selectedCalendars.filter((sc) => sc.externalId !== calendarId) || []; | ||
if (remainingCalendars.length > 0) { | ||
await this.fetchAvailabilityAndSetCache(remainingCalendars); | ||
} |
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.
This fixes a concern that @hariombalhara exposed
@@ -136,51 +135,6 @@ const cleanIntegrationKeys = ( | |||
return rest; | |||
}; | |||
|
|||
// here I will fetch the page json file. | |||
export const getCachedResults = async ( |
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.
Dead code
@@ -4,6 +4,7 @@ | |||
**/ | |||
export type AppFlags = { | |||
"calendar-cache": boolean; | |||
"calendar-cache-serve": boolean; |
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.
Whether to serve the cache by default or not. Override-able via URL params. @keithwillcode
@@ -48,6 +48,7 @@ export const useSchedule = ({ | |||
const searchParams = useSearchParams(); | |||
const routedTeamMemberIds = searchParams ? getRoutedTeamMemberIdsFromSearchParams(searchParams) : null; | |||
const skipContactOwner = searchParams ? searchParams.get("cal.skipContactOwner") === "true" : false; | |||
const shouldServeCache = searchParams ? searchParams.get("cal.cache") === "true" : undefined; |
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.
Undefined will fallback to the feature flag for this specific team/org
@@ -33,8 +34,7 @@ import { UserRepository } from "@calcom/lib/server/repository/user"; | |||
import getSlots from "@calcom/lib/slots"; | |||
import prisma, { availabilityUserSelect } from "@calcom/prisma"; | |||
import { PeriodType, Prisma } from "@calcom/prisma/client"; | |||
import { SchedulingType } from "@calcom/prisma/enums"; | |||
import { BookingStatus } from "@calcom/prisma/enums"; | |||
import { BookingStatus, SchedulingType } from "@calcom/prisma/enums"; |
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.
Sorted imports
const shouldServeCache = | ||
/** If we set the parameter use that, else check if the team has the feature enabled */ | ||
typeof _shouldServeCache === "boolean" || !teamId | ||
? _shouldServeCache | ||
: await featureRepo.checkIfTeamHasFeature(teamId, "calendar-cache-serve"); |
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.
Here we decide if the we should serve cache or not
@@ -11,7 +11,8 @@ const getCalendarsEvents = async ( | |||
withCredentials: CredentialPayload[], | |||
dateFrom: string, | |||
dateTo: string, | |||
selectedCalendars: SelectedCalendar[] | |||
selectedCalendars: SelectedCalendar[], | |||
shouldServeCache?: boolean |
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.
Why is this nullable? Seems like we should either just pass true or false.
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.
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.
Undefined will fallback to the DB default. If we force a boolean value the fallback will never execute.
@@ -44,6 +44,7 @@ export async function getBusyTimes(params: { | |||
})[] | |||
| null; | |||
bypassBusyCalendarTimes: boolean; | |||
shouldServeCache?: boolean; |
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.
Why is this nullable? Seems like we should either just pass true or false.
|
||
export async function getShouldServeCache(shouldServeCache?: boolean | undefined, teamId?: number) { | ||
if (typeof shouldServeCache === "boolean") return shouldServeCache; | ||
if (!teamId) return undefined; |
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.
Why not false
here?
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.
undefined has meaning in this feature.
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.
Undefined means there's no preference. So the regular default behavior will apply.
packages/prisma/migrations/20241216000000_add_calendar_cache_serve/migration.sql
Outdated
Show resolved
Hide resolved
…erve/migration.sql Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Co-authored-by: Keith Williams <keithwillcode@gmail.com>
@@ -65,4 +65,15 @@ export class FeaturesRepository implements IFeaturesRepository { | |||
throw err; | |||
} | |||
} | |||
async checkIfTeamHasFeature(teamId: number, featureId: keyof AppFlags) { | |||
try { | |||
const teamFeature = await db.teamFeatures.findUnique({ |
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.
Did you mean?
const teamFeature = await db.teamFeatures.findUnique({ | |
const teamFeature = await db.teamFeatures.findUniqueOrThrow({ |
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.
Not throwing since we're expecting a boolean return value
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.
Happy with this, let's get it merged.
* feat: allows forcing/skipping calendar cache serving Signed-off-by: Omar López <zomars@me.com> * Update features.repository.ts * Added to HNB * type fixes * Update packages/prisma/migrations/20241216000000_add_calendar_cache_serve/migration.sql Co-authored-by: Keith Williams <keithwillcode@gmail.com> * Update packages/prisma/zod-utils.ts Co-authored-by: Keith Williams <keithwillcode@gmail.com> * Update selectedCalendar.ts --------- Signed-off-by: Omar López <zomars@me.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com>
* feat: allows forcing/skipping calendar cache serving Signed-off-by: Omar López <zomars@me.com> * Update features.repository.ts * Added to HNB * type fixes * Update packages/prisma/migrations/20241216000000_add_calendar_cache_serve/migration.sql Co-authored-by: Keith Williams <keithwillcode@gmail.com> * Update packages/prisma/zod-utils.ts Co-authored-by: Keith Williams <keithwillcode@gmail.com> * Update selectedCalendar.ts --------- Signed-off-by: Omar López <zomars@me.com> Co-authored-by: Keith Williams <keithwillcode@gmail.com>
Folow up to #11928
What does this PR do?
This pull request introduces several enhancements and refactors to the calendar service, focusing on caching and availability fetching mechanisms. The key changes include the introduction of a new
shouldServeCache
parameter, refactoring of cache-related functions, and updates to various calendar service methods to incorporate these changes.Enhancements to caching and availability fetching:
packages/app-store/googlecalendar/lib/CalendarService.ts
: Introduced theshouldServeCache
parameter in multiple methods, refactoredgetCacheOrFetchAvailability
to use a new helper methodgetFreeBusyResult
, and updated cache handling inunwatchCalendar
. [1] [2] [3] [4] [5]packages/core/CalendarManager.ts
: Removed thegetCachedResults
function and added theshouldServeCache
parameter togetBusyCalendarTimes
and related functions to improve cache handling. [1] [2] [3]packages/core/getBusyTimes.ts
: Added theshouldServeCache
parameter to thegetBusyTimes
function to control cache usage. [1] [2] [3]packages/core/getCalendarsEvents.ts
: Updated thegetCalendarsEvents
function to include theshouldServeCache
parameter for better cache management. [1] [2]packages/features/calendar-cache/lib/datesForCache.ts
: Added new utility functionsgetTimeMin
andgetTimeMax
to handle date range expansions for caching purposes.Mandatory Tasks (DO NOT REMOVE)
How should this be tested?
Scenario 1
?cal.cache=false
parameter. It should skip cache altogether.Scenario 2
?cal.cache=true
parameter. It should force cache serving.Checklist