Skip to content

Commit

Permalink
🗃 Improve usage queries
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 12, 2023
1 parent c175ade commit e9a1d16
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const getResultsProcedure = authenticatedProcedure
cursor: cursor ? { id: cursor } : undefined,
where: {
typebotId: typebot.id,
answers: { some: {} },
hasStarted: true,
},
orderBy: {
createdAt: 'desc',
Expand Down
38 changes: 21 additions & 17 deletions apps/builder/src/pages/api/typebots/[typebotId]/analytics/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {

if (!typebot) return res.status(404).send({ message: 'Typebot not found' })

const totalViews = await prisma.result.count({
where: {
typebotId: typebot.id,
},
})
const totalStarts = await prisma.result.count({
where: {
typebotId: typebot.id,
answers: { some: {} },
},
})
const totalCompleted = await prisma.result.count({
where: {
typebotId: typebot.id,
isCompleted: true,
},
})
const [totalViews, totalStarts, totalCompleted] = await prisma.$transaction(
[
prisma.result.count({
where: {
typebotId: typebot.id,
},
}),
prisma.result.count({
where: {
typebotId: typebot.id,
hasStarted: true,
},
}),
prisma.result.count({
where: {
typebotId: typebot.id,
isCompleted: true,
},
}),
]
)

const stats: Stats = {
totalViews,
Expand Down
50 changes: 26 additions & 24 deletions apps/builder/src/pages/api/workspaces/[workspaceId]/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
now.getMonth() + 1,
1
)
const totalChatsUsed = await prisma.$transaction(async (tx) => {
const [
totalChatsUsed,
{
_sum: { storageUsed: totalStorageUsed },
},
] = await prisma.$transaction(async (tx) => {
const typebots = await tx.typebot.findMany({
where: {
workspace: {
Expand All @@ -24,33 +29,30 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
},
},
})
return tx.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,

return Promise.all([
prisma.result.count({
where: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,
},
},
},
})
})
const {
_sum: { storageUsed: totalStorageUsed },
} = await prisma.answer.aggregate({
where: {
storageUsed: { gt: 0 },
result: {
typebot: {
workspace: {
id: workspaceId,
members: { some: { userId: user.id } },
}),
prisma.answer.aggregate({
where: {
storageUsed: { gt: 0 },
result: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
},
},
},
},
_sum: { storageUsed: true },
_sum: { storageUsed: true },
}),
])
})

return res.send({
totalChatsUsed,
totalStorageUsed,
Expand Down
7 changes: 5 additions & 2 deletions packages/db/mysql/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ model Typebot {
@@index([workspaceId])
@@index([folderId])
@@index([isArchived, createdAt(sort: Desc)])
}

model Invitation {
Expand Down Expand Up @@ -249,7 +250,8 @@ model Result {
answers Answer[]
logs Log[]
@@index([typebotId])
@@index([typebotId, hasStarted, createdAt(sort: Desc)])
@@index([typebotId, isCompleted])
}

model Log {
Expand All @@ -270,12 +272,13 @@ model Answer {
blockId String
groupId String
variableId String?
content String
content String @db.Text
storageUsed Int?
result Result @relation(fields: [resultId], references: [id], onDelete: Cascade)
@@unique([resultId, blockId, groupId])
@@index([groupId])
@@index([storageUsed])
}

model Coupon {
Expand Down
5 changes: 4 additions & 1 deletion packages/db/postgresql/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ model Typebot {
isClosed Boolean @default(false)
@@index([workspaceId])
@@index([isArchived, createdAt(sort: Desc)])
}

model Invitation {
Expand Down Expand Up @@ -230,7 +231,8 @@ model Result {
answers Answer[]
logs Log[]
@@index([typebotId])
@@index([typebotId, hasStarted, createdAt(sort: Desc)])
@@index([typebotId, isCompleted])
}

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

model Coupon {
Expand Down

0 comments on commit e9a1d16

Please sign in to comment.