Skip to content

Commit

Permalink
🗃️ Optimize usage queries
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 7, 2022
1 parent 855a285 commit d6f90af
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { canReadTypebot } from '@/utils/api/dbRules'
import { authenticatedProcedure } from '@/utils/server/trpc'
import { TRPCError } from '@trpc/server'
import { ResultWithAnswers, resultWithAnswersSchema } from 'models'
import { isDefined } from 'utils'
import { z } from 'zod'

const maxLimit = 200
Expand Down Expand Up @@ -44,7 +43,7 @@ export const getResultsProcedure = authenticatedProcedure
cursor: cursor ? { id: cursor } : undefined,
where: {
typebot: canReadTypebot(input.typebotId, user),
answers: { some: {} },
hasStarted: true,
},
orderBy: {
createdAt: 'desc',
Expand Down
41 changes: 21 additions & 20 deletions apps/builder/src/pages/api/typebots/[typebotId]/analytics/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') {
const typebotId = req.query.typebotId as string

const totalViews = await prisma.result.count({
where: {
typebotId,
typebot: canReadTypebot(typebotId, user),
},
})
const totalStarts = await prisma.result.count({
where: {
typebotId,
typebot: canReadTypebot(typebotId, user),
answers: { some: {} },
},
})
const totalCompleted = await prisma.result.count({
where: {
typebotId,
typebot: canReadTypebot(typebotId, user),
isCompleted: true,
},
})
const [totalViews, totalStarts, totalCompleted] = await prisma.$transaction(
[
prisma.result.count({
where: {
typebot: canReadTypebot(typebotId, user),
},
}),
prisma.result.count({
where: {
typebot: canReadTypebot(typebotId, user),
hasStarted: true,
},
}),
prisma.result.count({
where: {
typebot: canReadTypebot(typebotId, user),
isCompleted: true,
},
}),
]
)
const stats: Stats = {
totalViews,
totalStarts,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX CONCURRENTLY "Typebot_workspaceId_idx" ON "Typebot"("workspaceId");
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX CONCURRENTLY "Result_createdAt_idx" ON "Result"("createdAt");
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX CONCURRENTLY "Result_hasStarted_idx" ON "Result"("hasStarted");
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- DropIndex
DROP INDEX IF EXISTS "Result_createdAt_idx";

-- DropIndex
DROP INDEX IF EXISTS "Result_hasStarted_idx";

-- DropIndex
DROP INDEX IF EXISTS "Result_typebotId_idx";

-- CreateIndex
CREATE INDEX IF NOT EXISTS "Answer_storageUsed_idx" ON "Answer"("storageUsed")
WHERE
"storageUsed" IS NOT NULL;

-- CreateIndex
CREATE INDEX IF NOT EXISTS "Result_typebotId_createdAt_idx" ON "Result"("typebotId", "createdAt" DESC NULLS LAST)
WHERE
"hasStarted" = true;

-- CreateIndex
CREATE INDEX IF NOT EXISTS "Result_createdAt_typebotId_idx" ON "Result"("createdAt" DESC NULLS LAST, "typebotId")
WHERE
"hasStarted" = true;
6 changes: 5 additions & 1 deletion packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ model Typebot {
webhooks Webhook[]
isArchived Boolean @default(false)
isClosed Boolean @default(false)
@@index([workspaceId])
}

model Invitation {
Expand Down Expand Up @@ -222,7 +224,8 @@ model Result {
answers Answer[]
logs Log[]
@@index([typebotId])
@@index([typebotId, createdAt])
@@index([createdAt, typebotId])
}

model Log {
Expand All @@ -249,6 +252,7 @@ model Answer {
@@unique([resultId, blockId, groupId])
@@index([groupId])
@@index([storageUsed])
}

model Coupon {
Expand Down

0 comments on commit d6f90af

Please sign in to comment.