Skip to content

Commit

Permalink
⚡ Fix / improve results archive crash when too many
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 23, 2023
1 parent 671c2cb commit cc9817b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 34 deletions.
99 changes: 66 additions & 33 deletions apps/builder/src/features/results/api/archiveResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,76 @@ import { deleteFiles } from '@/utils/api/storage'
import { Prisma } from 'db'
import { InputBlockType, Typebot } from 'models'

export const archiveResults = async ({
typebot,
resultsFilter,
}: {
const batchSize = 100

type Props = {
typebot: Pick<Typebot, 'groups'>
resultsFilter?: Prisma.ResultWhereInput
}) => {
resultsFilter?: Omit<Prisma.ResultWhereInput, 'typebotId'> & {
typebotId: string
}
}

export const archiveResults = async ({ typebot, resultsFilter }: Props) => {
const fileUploadBlockIds = typebot.groups
.flatMap((g) => g.blocks)
.filter((b) => b.type === InputBlockType.FILE)
.map((b) => b.id)
if (fileUploadBlockIds.length > 0) {
const filesToDelete = await prisma.answer.findMany({
where: { result: resultsFilter, blockId: { in: fileUploadBlockIds } },
.flatMap((group) => group.blocks)
.filter((block) => block.type === InputBlockType.FILE)
.map((block) => block.id)

let currentTotalResults = 0

do {
const resultsToDelete = await prisma.result.findMany({
where: {
...resultsFilter,
isArchived: false,
},
select: {
id: true,
},
take: batchSize,
})
if (filesToDelete.length > 0)
await deleteFiles({
urls: filesToDelete.flatMap((a) => a.content.split(', ')),

if (resultsToDelete.length === 0) break

currentTotalResults = resultsToDelete.length

const resultIds = resultsToDelete.map((result) => result.id)

if (fileUploadBlockIds.length > 0) {
const filesToDelete = await prisma.answer.findMany({
where: {
resultId: { in: resultIds },
blockId: { in: fileUploadBlockIds },
},
})
}
await prisma.log.deleteMany({
where: {
result: resultsFilter,
},
})
await prisma.answer.deleteMany({
where: {
result: resultsFilter,
},
})
await prisma.result.updateMany({
where: resultsFilter,
data: {
isArchived: true,
variables: [],
},
})
if (filesToDelete.length > 0)
await deleteFiles({
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
})
}

await prisma.$transaction([
prisma.log.deleteMany({
where: {
resultId: { in: resultIds },
},
}),
prisma.answer.deleteMany({
where: {
resultId: { in: resultIds },
},
}),
prisma.result.updateMany({
where: {
id: { in: resultIds },
},
data: {
isArchived: true,
variables: [],
},
}),
])
} while (currentTotalResults >= batchSize)

return { success: true }
}
2 changes: 1 addition & 1 deletion apps/builder/src/pages/api/typebots/[typebotId].ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
typebot,
resultsFilter: { typebotId },
})
if (!success) return res.status(500).send({ success: false })
if (!success) return res.status(500).send({ success: false, error: '' })
await prisma.publicTypebot.deleteMany({
where: { typebotId },
})
Expand Down

0 comments on commit cc9817b

Please sign in to comment.