🗃 Improve usage queries
This commit is contained in:
@ -49,7 +49,7 @@ export const getResultsProcedure = authenticatedProcedure
|
|||||||
cursor: cursor ? { id: cursor } : undefined,
|
cursor: cursor ? { id: cursor } : undefined,
|
||||||
where: {
|
where: {
|
||||||
typebotId: typebot.id,
|
typebotId: typebot.id,
|
||||||
answers: { some: {} },
|
hasStarted: true,
|
||||||
},
|
},
|
||||||
orderBy: {
|
orderBy: {
|
||||||
createdAt: 'desc',
|
createdAt: 'desc',
|
||||||
|
@ -18,23 +18,27 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
|
|
||||||
if (!typebot) return res.status(404).send({ message: 'Typebot not found' })
|
if (!typebot) return res.status(404).send({ message: 'Typebot not found' })
|
||||||
|
|
||||||
const totalViews = await prisma.result.count({
|
const [totalViews, totalStarts, totalCompleted] = await prisma.$transaction(
|
||||||
where: {
|
[
|
||||||
typebotId: typebot.id,
|
prisma.result.count({
|
||||||
},
|
where: {
|
||||||
})
|
typebotId: typebot.id,
|
||||||
const totalStarts = await prisma.result.count({
|
},
|
||||||
where: {
|
}),
|
||||||
typebotId: typebot.id,
|
prisma.result.count({
|
||||||
answers: { some: {} },
|
where: {
|
||||||
},
|
typebotId: typebot.id,
|
||||||
})
|
hasStarted: true,
|
||||||
const totalCompleted = await prisma.result.count({
|
},
|
||||||
where: {
|
}),
|
||||||
typebotId: typebot.id,
|
prisma.result.count({
|
||||||
isCompleted: true,
|
where: {
|
||||||
},
|
typebotId: typebot.id,
|
||||||
})
|
isCompleted: true,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
const stats: Stats = {
|
const stats: Stats = {
|
||||||
totalViews,
|
totalViews,
|
||||||
|
@ -15,7 +15,12 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
now.getMonth() + 1,
|
now.getMonth() + 1,
|
||||||
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({
|
const typebots = await tx.typebot.findMany({
|
||||||
where: {
|
where: {
|
||||||
workspace: {
|
workspace: {
|
||||||
@ -24,33 +29,30 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
return tx.result.count({
|
|
||||||
where: {
|
return Promise.all([
|
||||||
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
prisma.result.count({
|
||||||
hasStarted: true,
|
where: {
|
||||||
createdAt: {
|
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
||||||
gte: firstDayOfMonth,
|
hasStarted: true,
|
||||||
lt: firstDayOfNextMonth,
|
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({
|
||||||
_sum: { storageUsed: true },
|
where: {
|
||||||
|
storageUsed: { gt: 0 },
|
||||||
|
result: {
|
||||||
|
typebotId: { in: typebots.map((typebot) => typebot.id) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
_sum: { storageUsed: true },
|
||||||
|
}),
|
||||||
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
return res.send({
|
return res.send({
|
||||||
totalChatsUsed,
|
totalChatsUsed,
|
||||||
totalStorageUsed,
|
totalStorageUsed,
|
||||||
|
@ -196,6 +196,7 @@ model Typebot {
|
|||||||
|
|
||||||
@@index([workspaceId])
|
@@index([workspaceId])
|
||||||
@@index([folderId])
|
@@index([folderId])
|
||||||
|
@@index([isArchived, createdAt(sort: Desc)])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Invitation {
|
model Invitation {
|
||||||
@ -249,7 +250,8 @@ model Result {
|
|||||||
answers Answer[]
|
answers Answer[]
|
||||||
logs Log[]
|
logs Log[]
|
||||||
|
|
||||||
@@index([typebotId])
|
@@index([typebotId, hasStarted, createdAt(sort: Desc)])
|
||||||
|
@@index([typebotId, isCompleted])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Log {
|
model Log {
|
||||||
@ -270,12 +272,13 @@ model Answer {
|
|||||||
blockId String
|
blockId String
|
||||||
groupId String
|
groupId String
|
||||||
variableId String?
|
variableId String?
|
||||||
content String
|
content String @db.Text
|
||||||
storageUsed Int?
|
storageUsed Int?
|
||||||
result Result @relation(fields: [resultId], references: [id], onDelete: Cascade)
|
result Result @relation(fields: [resultId], references: [id], onDelete: Cascade)
|
||||||
|
|
||||||
@@unique([resultId, blockId, groupId])
|
@@unique([resultId, blockId, groupId])
|
||||||
@@index([groupId])
|
@@index([groupId])
|
||||||
|
@@index([storageUsed])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Coupon {
|
model Coupon {
|
||||||
|
@ -179,6 +179,7 @@ model Typebot {
|
|||||||
isClosed Boolean @default(false)
|
isClosed Boolean @default(false)
|
||||||
|
|
||||||
@@index([workspaceId])
|
@@index([workspaceId])
|
||||||
|
@@index([isArchived, createdAt(sort: Desc)])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Invitation {
|
model Invitation {
|
||||||
@ -230,7 +231,8 @@ model Result {
|
|||||||
answers Answer[]
|
answers Answer[]
|
||||||
logs Log[]
|
logs Log[]
|
||||||
|
|
||||||
@@index([typebotId])
|
@@index([typebotId, hasStarted, createdAt(sort: Desc)])
|
||||||
|
@@index([typebotId, isCompleted])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Log {
|
model Log {
|
||||||
@ -257,6 +259,7 @@ model Answer {
|
|||||||
|
|
||||||
@@unique([resultId, blockId, groupId])
|
@@unique([resultId, blockId, groupId])
|
||||||
@@index([groupId])
|
@@index([groupId])
|
||||||
|
@@index([storageUsed])
|
||||||
}
|
}
|
||||||
|
|
||||||
model Coupon {
|
model Coupon {
|
||||||
|
Reference in New Issue
Block a user