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,21 +190,21 @@ 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 [ const typebots = await prisma.typebot.findMany({
totalChatsUsed,
{
_sum: { storageUsed: totalStorageUsed },
},
] = await prisma.$transaction(async (tx) => {
const typebots = await tx.typebot.findMany({
where: { where: {
workspace: { workspace: {
id: workspaceId, id: workspaceId,
}, },
}, },
select: { id: true },
}) })
return Promise.all([ const [
totalChatsUsed,
{
_sum: { storageUsed: totalStorageUsed },
},
] = await Promise.all([
prisma.result.count({ prisma.result.count({
where: { where: {
typebotId: { in: typebots.map((typebot) => typebot.id) }, typebotId: { in: typebots.map((typebot) => typebot.id) },
@@ -225,7 +225,6 @@ const getUsage = async (workspaceId: string) => {
_sum: { storageUsed: true }, _sum: { storageUsed: true },
}), }),
]) ])
})
return { return {
totalChatsUsed, totalChatsUsed,