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: /router endpoint perf improvement #18366

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

hariombalhara
Copy link
Member

@hariombalhara hariombalhara commented Dec 24, 2024

What does this PR do?

  • Using possibly faster findUnique for form query instead of findFirst
  • Abstracted out handleResponse from response.handler.ts to be reused in /router - It allows already available form to be passed directly to handleResponse and avoids one sequential query to DB.
  • Passing orgId directly to findTeamMembersMatchingAttributeLogic which helps in restricting the number of records being queried in attributes related tables.
  • Completely different querying approach for getAttributesAssignmentData avoiding some queries in Prisma and using some in memory calculations.
  • Moved /router and trpc /response endpoint out of app-store as it was not allowing to use the repositories. Prisma was getting imported client side - Note: Moving /router to app-router can be done later.

Improvement seen locally:

  • With 2000 members in Acme and half of them being part of the Insights team, querying time went from 442ms(minimum) to 271ms(minimum). The improvements should be more visible with production data which has much much higher number of records.

Mandatory Tasks (DO NOT REMOVE)

  • I have self-reviewed the code (A decent size PR without self-review might be rejected).
  • 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.
  • I confirm automated tests are in place that prove my fix is effective or that my feature works.

How should this be tested?

  • Test Preview of Routing Form
  • /router endpoint should also work
  • A regular routing form submission should also work

Followup

  • A teamMembershipId column in attributeToUser could possibly save some querying time, if in-memory computation for orgMembershipId to userId doesn't work

Copy link
Contributor

github-actions bot commented Dec 24, 2024

Hey there and thank you for opening this pull request! 👋🏼

We require pull request titles to follow the Conventional Commits specification and it looks like your proposed title needs to be adjusted.

Details:

No release type found in pull request title "Add stats and allow showing embed without loader". Add a prefix to indicate what kind of release this pull request corresponds to. For reference, see https://www.conventionalcommits.org/

Available types:
 - feat: A new feature
 - fix: A bug fix
 - docs: Documentation only changes
 - style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
 - refactor: A code change that neither fixes a bug nor adds a feature
 - perf: A code change that improves performance
 - test: Adding missing tests or correcting existing tests
 - build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
 - ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, BrowserStack, SauceLabs)
 - chore: Other changes that don't modify src or test files
 - revert: Reverts a previous commit

Copy link

vercel bot commented Dec 24, 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 25, 2024 1:10pm
calcom-web-canary ⬜️ Ignored (Inspect) Visit Preview Dec 25, 2024 1:10pm

@keithwillcode keithwillcode added core area: core, team members only enterprise area: enterprise, audit log, organisation, SAML, SSO labels Dec 24, 2024
@hariombalhara hariombalhara changed the title Add stats and allow showing embed without loader perf: /router endpoint perf improvement Dec 24, 2024
@@ -178,6 +186,9 @@ export const getServerSideProps = async function getServerSideProps(
serializableForm.fields
);

// TODO: To be done using sentry tracing
console.log("Server-Timing", getServerTimingHeader(timeTaken));
Copy link
Member Author

Choose a reason for hiding this comment

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

timeTaken is already being computed, so I will just use the same approach for now for quick release. I will later move it to Sentry.

Comment on lines -134 to -138
const { createContext } = await import("@calcom/trpc/server/createContext");
const ctx = await createContext(context);

const { default: trpcRouter } = await import("@calcom/app-store/routing-forms/trpc/_router");
const caller = trpcRouter.createCaller(ctx);
Copy link
Member Author

Choose a reason for hiding this comment

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

Avoid using another trpcHandler and instead use the utility directly.

Also, needed to do this so that I can easily pass on already queried form to handleResponse and avoid repeat querying of same data

@github-actions github-actions bot added the ❗️ migrations contains migration files label Dec 24, 2024
include: {
team: {
select: {
parentId: true,
Copy link
Member Author

@hariombalhara hariombalhara Dec 24, 2024

Choose a reason for hiding this comment

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

New requirement, it was already queried in /router endpoint for some other requirement


const form = await prisma.app_RoutingForms_Form.findFirst({
async function findFormById(formId: string, prisma: AppPrisma) {
return await prisma.app_RoutingForms_Form.findUnique({
Copy link
Member Author

Choose a reason for hiding this comment

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

Using findUnique instead of findFirst

Copy link
Contributor

github-actions bot commented Dec 24, 2024

E2E results are ready!

Comment on lines -296 to -298
source: "/router/:path*",
destination: "/apps/routing-forms/router/:path*",
},
Copy link
Member Author

Choose a reason for hiding this comment

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

Rewrite not needed because /router is actually a route now

Comment on lines -25 to 27
slug: z.string(),
pages: z.array(z.string()),
})
Copy link
Member Author

Choose a reason for hiding this comment

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

Slug pages was there because it was earlier under app-store. Now it is not needed.

const { form: formId, embed, ...fieldsResponses } = queryParsed.data;
const { currentOrgDomain } = orgDomainConfig(context.req);

let timeTaken: Record<string, number | null> = {};
Copy link
Member Author

Choose a reason for hiding this comment

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

Commented at other places as well but comment here as well. Time Taken is an existing approach that I am adding more data on. This later needs to move to Sentry performance tracing

const result = await caller.public.response({
formId: form.id,
const result = await handleResponse({
form: serializableForm,
Copy link
Member Author

Choose a reason for hiding this comment

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

Passing already queried form now.

: null;

const teamMembersMatchingAttributeLogicWithResult =
formTeamId && formOrgId
Copy link
Member Author

Choose a reason for hiding this comment

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

Team Members matching logic is applicable only for organization

Comment on lines -12 to -13
import * as Router from "./router/[...appPages]";
import { getServerSideProps as getServerSidePropsRouter } from "./router/getServerSideProps";
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved outside of app-pages

@@ -6,7 +6,7 @@ import type { AppGetServerSidePropsContext, AppPrisma } from "@calcom/types/AppG
import { enrichFormWithMigrationData } from "../../enrichFormWithMigrationData";
import { getSerializableForm } from "../../lib/getSerializableForm";

export async function isAuthorizedToViewTheForm({
export function isAuthorizedToViewTheForm({
Copy link
Member Author

Choose a reason for hiding this comment

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

It was async accidentally

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 ❗️ migrations contains migration files ready-for-e2e
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants