2
0

(scripts) Improve result digest db queries

This commit is contained in:
Baptiste Arnaud
2023-04-28 09:09:08 +02:00
parent e827da7b6b
commit 5845e1cb8c
2 changed files with 27 additions and 28 deletions

View File

@@ -106,7 +106,7 @@ export const ConversationContainer = (props: Props) => {
setHasError(true) setHasError(true)
props.onNewLogs?.([ props.onNewLogs?.([
{ {
description: 'Error while sending message', description: 'Failed to send the reply',
details: error, details: error,
status: 'error', status: 'error',
}, },

View File

@@ -33,7 +33,7 @@ export const sendTotalResultsDigest = async () => {
console.log("Generating total results yesterday's digest...") console.log("Generating total results yesterday's digest...")
const todayMidnight = new Date() const todayMidnight = new Date()
todayMidnight.setHours(0, 0, 0, 0) todayMidnight.setUTCHours(0, 0, 0, 0)
const yesterday = new Date(todayMidnight) const yesterday = new Date(todayMidnight)
yesterday.setDate(yesterday.getDate() - 1) yesterday.setDate(yesterday.getDate() - 1)
@@ -190,42 +190,41 @@ const getUsage = async (workspaceId: string) => {
const now = new Date() const now = new Date()
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1) const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1)
const firstDayOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1) const firstDayOfNextMonth = new Date(now.getFullYear(), now.getMonth() + 1, 1)
const typebots = await prisma.typebot.findMany({
where: {
workspace: {
id: workspaceId,
},
},
select: { id: true },
})
const [ const [
totalChatsUsed, totalChatsUsed,
{ {
_sum: { storageUsed: totalStorageUsed }, _sum: { storageUsed: totalStorageUsed },
}, },
] = await prisma.$transaction(async (tx) => { ] = await Promise.all([
const typebots = await tx.typebot.findMany({ prisma.result.count({
where: { where: {
workspace: { typebotId: { in: typebots.map((typebot) => typebot.id) },
id: workspaceId, hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,
}, },
}, },
}) }),
prisma.answer.aggregate({
return Promise.all([ where: {
prisma.result.count({ storageUsed: { gt: 0 },
where: { result: {
typebotId: { in: typebots.map((typebot) => typebot.id) }, typebotId: { in: typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: firstDayOfMonth,
lt: firstDayOfNextMonth,
},
}, },
}), },
prisma.answer.aggregate({ _sum: { storageUsed: true },
where: { }),
storageUsed: { gt: 0 }, ])
result: {
typebotId: { in: typebots.map((typebot) => typebot.id) },
},
},
_sum: { storageUsed: true },
}),
])
})
return { return {
totalChatsUsed, totalChatsUsed,