From 9cb7f8cd96338a8736686cfd1e73a72dcb401164 Mon Sep 17 00:00:00 2001 From: Baptiste Arnaud Date: Mon, 17 Oct 2022 08:19:50 +0200 Subject: [PATCH] :bug: (analytics) Fix multi usage query timeout --- .../components/shared/Graph/Edges/Edges.tsx | 138 +++++++++--------- .../[typebotId]/results/answers/count.ts | 29 ++-- 2 files changed, 84 insertions(+), 83 deletions(-) diff --git a/apps/builder/components/shared/Graph/Edges/Edges.tsx b/apps/builder/components/shared/Graph/Edges/Edges.tsx index 4204b9a8d..9609e0528 100644 --- a/apps/builder/components/shared/Graph/Edges/Edges.tsx +++ b/apps/builder/components/shared/Graph/Edges/Edges.tsx @@ -16,74 +16,72 @@ export const Edges = ({ edges, answersCounts, onUnlockProPlanClick, -}: Props) => { - return ( - ( + + + {edges.map((edge) => ( + + ))} + {answersCounts?.map((answerCount) => ( + + ))} + - - {edges.map((edge) => ( - - ))} - {answersCounts?.slice(1)?.map((answerCount) => ( - - ))} - - - - - - - - - - - ) -} + + + + + + + + + +) diff --git a/apps/builder/pages/api/typebots/[typebotId]/results/answers/count.ts b/apps/builder/pages/api/typebots/[typebotId]/results/answers/count.ts index b336e1279..6862a0d79 100644 --- a/apps/builder/pages/api/typebots/[typebotId]/results/answers/count.ts +++ b/apps/builder/pages/api/typebots/[typebotId]/results/answers/count.ts @@ -15,19 +15,22 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { where: canReadTypebot(typebotId, user), include: { publishedTypebot: true }, }) - if (!typebot) return res.status(404).send({ answersCounts: [] }) - const answersCounts: { groupId: string; totalAnswers: number }[] = - await Promise.all( - (typebot.publishedTypebot as unknown as PublicTypebot).groups.map( - async (group) => { - const totalAnswers = await prisma.answer.count({ - where: { groupId: group.id }, - }) - return { groupId: group.id, totalAnswers } - } - ) - ) - return res.status(200).send({ answersCounts }) + const publishedTypebot = + typebot?.publishedTypebot as unknown as PublicTypebot + if (!publishedTypebot) return res.status(404).send({ answersCounts: [] }) + const answersCounts = await prisma.answer.groupBy({ + by: ['groupId'], + where: { + groupId: { in: publishedTypebot.groups.map((g) => g.id) }, + }, + _count: { _all: true }, + }) + return res.status(200).send({ + answersCounts: answersCounts.map((answer) => ({ + groupId: answer.groupId, + totalAnswers: answer._count._all, + })), + }) } return methodNotAllowed(res) }