This commit is contained in:
Hariom Balhara authored and GitHub committed 2022-07-07 09:26:22 -06:00
1 parent f9cc71d120
commit 5db762e987
5 files changed
+58 -17

No files matched your search

+16 -3
View File
@@ -78,7 +78,7 @@ export const getConnectedCalendars = async (
const CACHING_TIME = 30_000; // 30 seconds
const getCachedResults = (
const getCachedResults = async (
withCredentials: Credential[],
dateFrom: string,
dateTo: string,
@@ -86,6 +86,8 @@ const getCachedResults = (
) => {
const calendarCredentials = withCredentials.filter((credential) => credential.type.endsWith("_calendar"));
const calendars = calendarCredentials.map((credential) => getCalendar(credential));
const startGetBusyCalendarTimes = performance.now();
const results = calendars.map(async (c, i) => {
/** Filter out nulls */
if (!c) return [];
@@ -103,15 +105,26 @@ const getCachedResults = (
.digest("hex");
/** Check if we already have cached data and return */
const cachedAvailability = cache.get(cacheKey);
if (cachedAvailability) return cachedAvailability;
if (cachedAvailability) {
log.debug(`Cache HIT: Calendar Availability for key`, { id, selectedCalendarIds, dateFrom, dateTo });
return cachedAvailability;
}
log.debug(`Cache MISS: Calendar Availability for key`, { id, selectedCalendarIds, dateFrom, dateTo });
/** If we don't then we actually fetch external calendars (which can be very slow) */
const availability = await c.getAvailability(dateFrom, dateTo, passedSelectedCalendars);
/** We save the availability to a few seconds so recurrent calls are nearly instant */
cache.put(cacheKey, availability, CACHING_TIME);
return availability;
});
const awaitedResults = await Promise.all(results);
const endGetBusyCalendarTimes = performance.now();
return Promise.all(results);
log.debug(
`getBusyCalendarTimes took ${
endGetBusyCalendarTimes - startGetBusyCalendarTimes
}ms for creds ${calendarCredentials.map((cred) => cred.id)}`
);
return awaitedResults;
};
export const getBusyCalendarTimes = async (
+4 -1
View File
@@ -1,6 +1,7 @@
import { BookingStatus, Credential, SelectedCalendar } from "@prisma/client";
import { getBusyCalendarTimes } from "@calcom/core/CalendarManager";
import logger from "@calcom/lib/logger";
// import { getBusyVideoTimes } from "@calcom/core/videoClient";
// import notEmpty from "@calcom/lib/notEmpty";
import prisma from "@calcom/prisma";
@@ -15,6 +16,7 @@ export async function getBusyTimes(params: {
selectedCalendars: SelectedCalendar[];
}) {
const { credentials, userId, eventTypeId, startTime, endTime, selectedCalendars } = params;
const startPrismaBookingGet = performance.now();
const busyTimes: EventBusyDate[] = await prisma.booking
.findMany({
where: {
@@ -32,7 +34,8 @@ export async function getBusyTimes(params: {
},
})
.then((bookings) => bookings.map(({ startTime, endTime }) => ({ end: endTime, start: startTime })));
const endPrismaBookingGet = performance.now();
logger.debug(`prisma booking get took ${endPrismaBookingGet - startPrismaBookingGet}ms`);
if (credentials.length > 0) {
const calendarBusyTimes = await getBusyCalendarTimes(credentials, startTime, endTime, selectedCalendars);
// console.log("calendarBusyTimes", calendarBusyTimes);
+5 -3
View File
@@ -1,9 +1,10 @@
import { Prisma } from "@prisma/client";
import dayjs, { Dayjs } from "@calcom/dayjs";
import { z } from "zod";
import dayjs, { Dayjs } from "@calcom/dayjs";
import { getWorkingHours } from "@calcom/lib/availability";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import prisma, { availabilityUserSelect } from "@calcom/prisma";
import { stringToDayjs } from "@calcom/prisma/zod-utils";
@@ -137,13 +138,14 @@ export async function getUserAvailability(
};
const timeZone = timezone || schedule?.timeZone || eventType?.timeZone || currentUser.timeZone;
const startGetWorkingHours = performance.now();
const workingHours = getWorkingHours(
{ timeZone },
schedule.availability ||
(eventType?.availability.length ? eventType.availability : currentUser.availability)
);
const endGetWorkingHours = performance.now();
logger.debug(`getWorkingHours took ${endGetWorkingHours - startGetWorkingHours}ms for userId ${userId}`);
return {
busy: bufferedBusyTimes,
timeZone,