Skip to content

Commit

Permalink
chore: add caching to js actions endpoint (formbricks#822)
Browse files Browse the repository at this point in the history
* fix: caching in actions endpoint

* refactor

---------

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
  • Loading branch information
pandeymangg and mattinannt committed Sep 16, 2023
1 parent c879119 commit 616a909
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 43 deletions.
44 changes: 6 additions & 38 deletions apps/web/app/api/v1/js/actions/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { responses } from "@/lib/api/response";
import { transformErrorToDetails } from "@/lib/api/validator";
import { prisma } from "@formbricks/database";
import { createAction } from "@formbricks/lib/services/actions";
import { ZJsActionInput } from "@formbricks/types/v1/js";
import { EventType } from "@prisma/client";
import { NextResponse } from "next/server";

export async function OPTIONS(): Promise<NextResponse> {
Expand All @@ -26,42 +25,11 @@ export async function POST(req: Request): Promise<NextResponse> {

const { environmentId, sessionId, name, properties } = inputValidation.data;

let eventType: EventType = EventType.code;
if (name === "Exit Intent (Desktop)" || name === "50% Scroll") {
eventType = EventType.automatic;
}

await prisma.event.create({
data: {
properties,
session: {
connect: {
id: sessionId,
},
},
eventClass: {
connectOrCreate: {
where: {
name_environmentId: {
name,
environmentId,
},
},
create: {
name,
type: eventType,
environment: {
connect: {
id: environmentId,
},
},
},
},
},
},
select: {
id: true,
},
createAction({
environmentId,
sessionId,
name,
properties,
});

return responses.successResponse({}, true);
Expand Down
4 changes: 3 additions & 1 deletion packages/database/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { withAccelerate } from "@prisma/extension-accelerate";
const prismaClientSingleton = () => {
return new PrismaClient({
datasources: { db: { url: process.env.DATABASE_URL } },
/* log: ["query", "info"], */
...(process.env.DEBUG === "1" && {
log: ["query", "info"],
}),
}).$extends(withAccelerate());
};

Expand Down
28 changes: 27 additions & 1 deletion packages/lib/services/actionClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import { revalidateTag, unstable_cache } from "next/cache";
import { cache } from "react";
import { validateInputs } from "../utils/validate";

const halfHourInSeconds = 60 * 30;

export const getActionClassCacheTag = (name: string, environmentId: string): string =>
`env-${environmentId}-actionClass-${name}`;
const getActionClassCacheKey = (name: string, environmentId: string): string[] => [
getActionClassCacheTag(name, environmentId),
];

const getActionClassesCacheTag = (environmentId: string): string => `env-${environmentId}-actionClasses`;
const getActionClassesCacheKey = (environmentId: string): string[] => [
getActionClassesCacheTag(environmentId),
Expand Down Expand Up @@ -52,7 +60,7 @@ export const getActionClassesCached = (environmentId: string) =>
getActionClassesCacheKey(environmentId),
{
tags: getActionClassesCacheKey(environmentId),
revalidate: 30 * 60, // 30 minutes
revalidate: halfHourInSeconds,
}
)();

Expand Down Expand Up @@ -132,10 +140,28 @@ export const updateActionClass = async (
});

// revalidate cache
revalidateTag(getActionClassCacheTag(result.name, environmentId));
revalidateTag(getActionClassesCacheTag(environmentId));

return result;
} catch (error) {
throw new DatabaseError(`Database error when updating an action for environment ${environmentId}`);
}
};

export const getActionClassCached = async (name: string, environmentId: string) =>
unstable_cache(
async () => {
return await prisma.eventClass.findFirst({
where: {
name,
environmentId,
},
});
},
getActionClassCacheKey(name, environmentId),
{
tags: getActionClassCacheKey(name, environmentId),
revalidate: halfHourInSeconds,
}
)();
82 changes: 82 additions & 0 deletions packages/lib/services/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { ZId } from "@formbricks/types/v1/environment";
import { Prisma } from "@prisma/client";
import { cache } from "react";
import { validateInputs } from "../utils/validate";
import { TJsActionInput } from "@formbricks/types/v1/js";
import { revalidateTag } from "next/cache";
import { EventType } from "@prisma/client";
import { getActionClassCacheTag, getActionClassCached } from "../services/actionClass";
import { getSessionCached } from "../services/session";

export const getActionsByEnvironmentId = cache(
async (environmentId: string, limit?: number): Promise<TAction[]> => {
Expand Down Expand Up @@ -48,3 +53,80 @@ export const getActionsByEnvironmentId = cache(
}
}
);

export const createAction = async (data: TJsActionInput) => {
const { environmentId, name, properties, sessionId } = data;

let eventType: EventType = EventType.code;
if (name === "Exit Intent (Desktop)" || name === "50% Scroll") {
eventType = EventType.automatic;
}

const session = await getSessionCached(sessionId);

if (!session) {
throw new Error("Session not found");
}

const actionClass = await getActionClassCached(name, environmentId);

if (actionClass) {
await prisma.event.create({
data: {
properties,
sessionId: session.id,
eventClassId: actionClass.id,
},
});

return;
}

// if action class does not exist, create it and then create the action
await prisma.$transaction([
prisma.eventClass.create({
data: {
name,
type: eventType,
environmentId,
},
}),

prisma.event.create({
data: {
properties,
session: {
connect: {
id: sessionId,
},
},
eventClass: {
connectOrCreate: {
where: {
name_environmentId: {
name,
environmentId,
},
},
create: {
name,
type: eventType,
environment: {
connect: {
id: environmentId,
},
},
},
},
},
},
select: {
id: true,
},
}),
]);

// revalidate cache
revalidateTag(sessionId);
revalidateTag(getActionClassCacheTag(name, environmentId));
};
10 changes: 7 additions & 3 deletions packages/lib/services/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { revalidateTag, unstable_cache } from "next/cache";
import { cache } from "react";
import { validateInputs } from "../utils/validate";

const halfHourInSeconds = 60 * 30;

const getSessionCacheKey = (sessionId: string): string[] => [sessionId];

const select = {
id: true,
createdAt: true,
Expand Down Expand Up @@ -45,10 +49,10 @@ export const getSessionCached = (sessionId: string) =>
async () => {
return await getSession(sessionId);
},
[sessionId],
getSessionCacheKey(sessionId),
{
tags: [sessionId],
revalidate: 30 * 60, // 30 minutes
tags: getSessionCacheKey(sessionId),
revalidate: halfHourInSeconds, // 30 minutes
}
)();

Expand Down
1 change: 1 addition & 0 deletions turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"outputs": ["dist/**", ".next/**"],
"env": [
"CRON_SECRET",
"DEBUG",
"PRISMA_GENERATE_DATAPROXY",
"GITHUB_ID",
"GITHUB_SECRET",
Expand Down

0 comments on commit 616a909

Please sign in to comment.