⚡ Fix / improve results archive crash when too many
This commit is contained in:
@ -3,43 +3,76 @@ import { deleteFiles } from '@/utils/api/storage'
|
|||||||
import { Prisma } from 'db'
|
import { Prisma } from 'db'
|
||||||
import { InputBlockType, Typebot } from 'models'
|
import { InputBlockType, Typebot } from 'models'
|
||||||
|
|
||||||
export const archiveResults = async ({
|
const batchSize = 100
|
||||||
typebot,
|
|
||||||
resultsFilter,
|
type Props = {
|
||||||
}: {
|
|
||||||
typebot: Pick<Typebot, 'groups'>
|
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
|
const fileUploadBlockIds = typebot.groups
|
||||||
.flatMap((g) => g.blocks)
|
.flatMap((group) => group.blocks)
|
||||||
.filter((b) => b.type === InputBlockType.FILE)
|
.filter((block) => block.type === InputBlockType.FILE)
|
||||||
.map((b) => b.id)
|
.map((block) => block.id)
|
||||||
|
|
||||||
|
let currentTotalResults = 0
|
||||||
|
|
||||||
|
do {
|
||||||
|
const resultsToDelete = await prisma.result.findMany({
|
||||||
|
where: {
|
||||||
|
...resultsFilter,
|
||||||
|
isArchived: false,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
},
|
||||||
|
take: batchSize,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (resultsToDelete.length === 0) break
|
||||||
|
|
||||||
|
currentTotalResults = resultsToDelete.length
|
||||||
|
|
||||||
|
const resultIds = resultsToDelete.map((result) => result.id)
|
||||||
|
|
||||||
if (fileUploadBlockIds.length > 0) {
|
if (fileUploadBlockIds.length > 0) {
|
||||||
const filesToDelete = await prisma.answer.findMany({
|
const filesToDelete = await prisma.answer.findMany({
|
||||||
where: { result: resultsFilter, blockId: { in: fileUploadBlockIds } },
|
where: {
|
||||||
|
resultId: { in: resultIds },
|
||||||
|
blockId: { in: fileUploadBlockIds },
|
||||||
|
},
|
||||||
})
|
})
|
||||||
if (filesToDelete.length > 0)
|
if (filesToDelete.length > 0)
|
||||||
await deleteFiles({
|
await deleteFiles({
|
||||||
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
|
urls: filesToDelete.flatMap((a) => a.content.split(', ')),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
await prisma.log.deleteMany({
|
|
||||||
|
await prisma.$transaction([
|
||||||
|
prisma.log.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
result: resultsFilter,
|
resultId: { in: resultIds },
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
await prisma.answer.deleteMany({
|
prisma.answer.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
result: resultsFilter,
|
resultId: { in: resultIds },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
prisma.result.updateMany({
|
||||||
|
where: {
|
||||||
|
id: { in: resultIds },
|
||||||
},
|
},
|
||||||
})
|
|
||||||
await prisma.result.updateMany({
|
|
||||||
where: resultsFilter,
|
|
||||||
data: {
|
data: {
|
||||||
isArchived: true,
|
isArchived: true,
|
||||||
variables: [],
|
variables: [],
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
|
])
|
||||||
|
} while (currentTotalResults >= batchSize)
|
||||||
|
|
||||||
return { success: true }
|
return { success: true }
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|||||||
typebot,
|
typebot,
|
||||||
resultsFilter: { typebotId },
|
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({
|
await prisma.publicTypebot.deleteMany({
|
||||||
where: { typebotId },
|
where: { typebotId },
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user