diff --git a/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts b/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts index a8bf5d1fa..88901cb42 100644 --- a/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts +++ b/apps/builder/src/features/results/api/procedures/getResultsProcedure.ts @@ -49,7 +49,7 @@ export const getResultsProcedure = authenticatedProcedure cursor: cursor ? { id: cursor } : undefined, where: { typebotId: typebot.id, - answers: { some: {} }, + hasStarted: true, }, orderBy: { createdAt: 'desc', diff --git a/apps/builder/src/pages/api/typebots/[typebotId]/analytics/stats.ts b/apps/builder/src/pages/api/typebots/[typebotId]/analytics/stats.ts index 88d1279ed..dab92c499 100644 --- a/apps/builder/src/pages/api/typebots/[typebotId]/analytics/stats.ts +++ b/apps/builder/src/pages/api/typebots/[typebotId]/analytics/stats.ts @@ -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, diff --git a/apps/builder/src/pages/api/workspaces/[workspaceId]/usage.ts b/apps/builder/src/pages/api/workspaces/[workspaceId]/usage.ts index 510ef9dc8..bb9b5e0c1 100644 --- a/apps/builder/src/pages/api/workspaces/[workspaceId]/usage.ts +++ b/apps/builder/src/pages/api/workspaces/[workspaceId]/usage.ts @@ -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: { @@ -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, - }, - }, - }) - }) - const { - _sum: { storageUsed: totalStorageUsed }, - } = await prisma.answer.aggregate({ - where: { - storageUsed: { gt: 0 }, - result: { - typebot: { - workspace: { - id: workspaceId, - members: { some: { userId: user.id } }, + + return Promise.all([ + prisma.result.count({ + where: { + typebotId: { in: typebots.map((typebot) => typebot.id) }, + hasStarted: true, + createdAt: { + gte: firstDayOfMonth, + lt: firstDayOfNextMonth, }, }, - }, - }, - _sum: { storageUsed: true }, + }), + prisma.answer.aggregate({ + where: { + storageUsed: { gt: 0 }, + result: { + typebotId: { in: typebots.map((typebot) => typebot.id) }, + }, + }, + _sum: { storageUsed: true }, + }), + ]) }) + return res.send({ totalChatsUsed, totalStorageUsed, diff --git a/packages/db/mysql/schema.prisma b/packages/db/mysql/schema.prisma index f7f09a983..664e3d4dc 100644 --- a/packages/db/mysql/schema.prisma +++ b/packages/db/mysql/schema.prisma @@ -196,6 +196,7 @@ model Typebot { @@index([workspaceId]) @@index([folderId]) + @@index([isArchived, createdAt(sort: Desc)]) } model Invitation { @@ -249,7 +250,8 @@ model Result { answers Answer[] logs Log[] - @@index([typebotId]) + @@index([typebotId, hasStarted, createdAt(sort: Desc)]) + @@index([typebotId, isCompleted]) } model Log { @@ -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 { diff --git a/packages/db/postgresql/schema.prisma b/packages/db/postgresql/schema.prisma index 2dbbe1313..9dd5a1c79 100644 --- a/packages/db/postgresql/schema.prisma +++ b/packages/db/postgresql/schema.prisma @@ -179,6 +179,7 @@ model Typebot { isClosed Boolean @default(false) @@index([workspaceId]) + @@index([isArchived, createdAt(sort: Desc)]) } model Invitation { @@ -230,7 +231,8 @@ model Result { answers Answer[] logs Log[] - @@index([typebotId]) + @@index([typebotId, hasStarted, createdAt(sort: Desc)]) + @@index([typebotId, isCompleted]) } model Log { @@ -257,6 +259,7 @@ model Answer { @@unique([resultId, blockId, groupId]) @@index([groupId]) + @@index([storageUsed]) } model Coupon {