perf: Avoid useless dependencies in calendar cache page (#9290)
* increases the time range to cover all time zones * it isolates the getCachedResults and rename to getCalendarsEvents * remove unrelated changes * remove unrelated changes --------- Co-authored-by: zomars <zomars@me.com>pull/8355/head
parent
893b67b6c4
commit
585679969a
|
@ -6,13 +6,13 @@
|
||||||
import type { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next";
|
import type { GetStaticPaths, GetStaticProps, InferGetStaticPropsType } from "next";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
import { getCachedResults } from "@calcom/core";
|
import getCalendarsEvents from "@calcom/core/getCalendarsEvents";
|
||||||
import dayjs from "@calcom/dayjs";
|
import dayjs from "@calcom/dayjs";
|
||||||
import prisma from "@calcom/prisma";
|
import prisma from "@calcom/prisma";
|
||||||
|
|
||||||
const paramsSchema = z.object({ user: z.string(), month: z.string() });
|
const paramsSchema = z.object({ user: z.string(), month: z.string() });
|
||||||
export const getStaticProps: GetStaticProps<
|
export const getStaticProps: GetStaticProps<
|
||||||
{ results: Awaited<ReturnType<typeof getCachedResults>> },
|
{ results: Awaited<ReturnType<typeof getCalendarsEvents>> },
|
||||||
{ user: string }
|
{ user: string }
|
||||||
> = async (context) => {
|
> = async (context) => {
|
||||||
const { user: username, month } = paramsSchema.parse(context.params);
|
const { user: username, month } = paramsSchema.parse(context.params);
|
||||||
|
@ -33,7 +33,7 @@ export const getStaticProps: GetStaticProps<
|
||||||
const endDate = dayjs.utc(month, "YYYY-MM").endOf("month").add(14, "hours").format();
|
const endDate = dayjs.utc(month, "YYYY-MM").endOf("month").add(14, "hours").format();
|
||||||
try {
|
try {
|
||||||
const results = userWithCredentials?.credentials
|
const results = userWithCredentials?.credentials
|
||||||
? await getCachedResults(
|
? await getCalendarsEvents(
|
||||||
userWithCredentials?.credentials,
|
userWithCredentials?.credentials,
|
||||||
startDate,
|
startDate,
|
||||||
endDate,
|
endDate,
|
||||||
|
|
|
@ -18,6 +18,8 @@ import type {
|
||||||
import type { CredentialPayload, CredentialWithAppName } from "@calcom/types/Credential";
|
import type { CredentialPayload, CredentialWithAppName } from "@calcom/types/Credential";
|
||||||
import type { EventResult } from "@calcom/types/EventManager";
|
import type { EventResult } from "@calcom/types/EventManager";
|
||||||
|
|
||||||
|
import getCalendarsEvents from "./getCalendarsEvents";
|
||||||
|
|
||||||
const log = logger.getChildLogger({ prefix: ["CalendarManager"] });
|
const log = logger.getChildLogger({ prefix: ["CalendarManager"] });
|
||||||
let coldStart = true;
|
let coldStart = true;
|
||||||
|
|
||||||
|
@ -236,7 +238,7 @@ export const getBusyCalendarTimes = async (
|
||||||
const startDate = dayjs(dateFrom).subtract(11, "hours").format();
|
const startDate = dayjs(dateFrom).subtract(11, "hours").format();
|
||||||
// Add 14 hours from the start date to avoid problems in UTC+ time zones.
|
// Add 14 hours from the start date to avoid problems in UTC+ time zones.
|
||||||
const endDate = dayjs(dateTo).endOf("month").add(14, "hours").format();
|
const endDate = dayjs(dateTo).endOf("month").add(14, "hours").format();
|
||||||
results = await getCachedResults(withCredentials, startDate, endDate, selectedCalendars);
|
results = await getCalendarsEvents(withCredentials, startDate, endDate, selectedCalendars);
|
||||||
logger.info("Generating calendar cache in background");
|
logger.info("Generating calendar cache in background");
|
||||||
// on cold start the calendar cache page generated in the background
|
// on cold start the calendar cache page generated in the background
|
||||||
Promise.all(months.map((month) => createCalendarCachePage(username, month)));
|
Promise.all(months.map((month) => createCalendarCachePage(username, month)));
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
import type { SelectedCalendar } from "@prisma/client";
|
||||||
|
|
||||||
|
import { getCalendar } from "@calcom/app-store/_utils/getCalendar";
|
||||||
|
import { performance } from "@calcom/lib/server/perfObserver";
|
||||||
|
import type { EventBusyDate } from "@calcom/types/Calendar";
|
||||||
|
import type { CredentialPayload } from "@calcom/types/Credential";
|
||||||
|
|
||||||
|
const getCalendarsEvents = async (
|
||||||
|
withCredentials: CredentialPayload[],
|
||||||
|
dateFrom: string,
|
||||||
|
dateTo: string,
|
||||||
|
selectedCalendars: SelectedCalendar[]
|
||||||
|
): Promise<EventBusyDate[][]> => {
|
||||||
|
const calendarCredentials = withCredentials.filter((credential) => credential.type.endsWith("_calendar"));
|
||||||
|
const calendars = await Promise.all(calendarCredentials.map((credential) => getCalendar(credential)));
|
||||||
|
performance.mark("getBusyCalendarTimesStart");
|
||||||
|
const results = calendars.map(async (c, i) => {
|
||||||
|
/** Filter out nulls */
|
||||||
|
if (!c) return [];
|
||||||
|
/** We rely on the index so we can match credentials with calendars */
|
||||||
|
const { type, appId } = calendarCredentials[i];
|
||||||
|
/** We just pass the calendars that matched the credential type,
|
||||||
|
* TODO: Migrate credential type or appId
|
||||||
|
*/
|
||||||
|
const passedSelectedCalendars = selectedCalendars.filter((sc) => sc.integration === type);
|
||||||
|
if (!passedSelectedCalendars.length) return [];
|
||||||
|
/** We extract external Ids so we don't cache too much */
|
||||||
|
const selectedCalendarIds = passedSelectedCalendars.map((sc) => sc.externalId);
|
||||||
|
/** If we don't then we actually fetch external calendars (which can be very slow) */
|
||||||
|
performance.mark("eventBusyDatesStart");
|
||||||
|
const eventBusyDates = await c.getAvailability(dateFrom, dateTo, passedSelectedCalendars);
|
||||||
|
performance.mark("eventBusyDatesEnd");
|
||||||
|
performance.measure(
|
||||||
|
`[getAvailability for ${selectedCalendarIds.join(", ")}][$1]'`,
|
||||||
|
"eventBusyDatesStart",
|
||||||
|
"eventBusyDatesEnd"
|
||||||
|
);
|
||||||
|
|
||||||
|
return eventBusyDates.map((a) => ({ ...a, source: `${appId}` }));
|
||||||
|
});
|
||||||
|
const awaitedResults = await Promise.all(results);
|
||||||
|
performance.mark("getBusyCalendarTimesEnd");
|
||||||
|
performance.measure(
|
||||||
|
`getBusyCalendarTimes took $1 for creds ${calendarCredentials.map((cred) => cred.id)}`,
|
||||||
|
"getBusyCalendarTimesStart",
|
||||||
|
"getBusyCalendarTimesEnd"
|
||||||
|
);
|
||||||
|
return awaitedResults;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default getCalendarsEvents;
|
Loading…
Reference in New Issue