🔧 Improve logging in daily job

This commit is contained in:
Baptiste Arnaud 2026-02-02 17:41:46 +01:00
parent 884ec72bcc
commit 30ddd69a9c
No known key found for this signature in database
4 changed files with 44 additions and 41 deletions

View File

@ -7,6 +7,7 @@ export const cleanExpiredData = async () => {
return { totalDeletedChatSessions };
};
const CHAT_SESSIONS_BATCH_SIZE = 80000;
const deleteOldChatSessions = async () => {
const twoDaysAgo = new Date();
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
@ -14,6 +15,7 @@ const deleteOldChatSessions = async () => {
let totalDeletedChatSessions = 0;
let deletingChatSessions: number;
do {
console.log(`🔨 Deleting ${CHAT_SESSIONS_BATCH_SIZE} old chat sessions...`);
const chatSessions = await prisma.chatSession.findMany({
where: {
updatedAt: {
@ -23,7 +25,7 @@ const deleteOldChatSessions = async () => {
select: {
id: true,
},
take: 80000,
take: CHAT_SESSIONS_BATCH_SIZE,
});
deletingChatSessions = chatSessions.length;
@ -31,7 +33,6 @@ const deleteOldChatSessions = async () => {
const chunkSize = 500;
for (let i = 0; i < chatSessions.length; i += chunkSize) {
console.log(`Deleting ${i}/${deletingChatSessions} chat sessions...`);
const chunk = chatSessions.slice(i, i + chunkSize);
await prisma.chatSession.deleteMany({
where: {
@ -41,7 +42,7 @@ const deleteOldChatSessions = async () => {
},
});
}
} while (deletingChatSessions === 80000);
} while (deletingChatSessions === CHAT_SESSIONS_BATCH_SIZE);
return { totalDeletedChatSessions };
};

View File

@ -9,13 +9,14 @@ export const trackAndReportYesterdaysResults = async () => {
yesterdayMidnight.setDate(yesterdayMidnight.getDate() - 1);
yesterdayMidnight.setUTCHours(0, 0, 0, 0);
console.log(`Fetching recently active workspaces...`);
console.log(`🔍 Fetching recently active workspaces...`);
const recentWorkspaces = await prisma.workspace.findMany({
where: {
lastActivityAt: {
gte: yesterdayMidnight,
},
isSuspended: { not: true },
},
select: {
id: true,
@ -37,53 +38,53 @@ export const trackAndReportYesterdaysResults = async () => {
},
});
console.log("🔍 Found", recentWorkspaces.length, "workspaces");
console.log(" Found", recentWorkspaces.length, "workspaces");
let resultsSum = 0;
const newResultsCollectedEvents: TelemetryEvent[] = [];
let index = 1;
console.log("🔨 Processing workspaces...");
for (const workspace of recentWorkspaces) {
console.log(
"Processing workspace",
index + 1,
"/",
recentWorkspaces.length,
);
index += 1;
const results = await prisma.result.groupBy({
by: ["typebotId"],
_count: {
_all: true,
},
where: {
typebotId: { in: workspace.typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: yesterdayMidnight,
lt: todayMidnight,
try {
const results = await prisma.result.groupBy({
by: ["typebotId"],
_count: {
_all: true,
},
},
});
const olderAdmin = workspace.members
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
.at(0);
if (!olderAdmin) continue;
for (const result of results) {
if (result._count._all === 0) continue;
newResultsCollectedEvents.push({
name: "New results collected",
typebotId: result.typebotId,
workspaceId: workspace.id,
userId: olderAdmin.user.id,
data: {
total: result._count._all,
where: {
typebotId: { in: workspace.typebots.map((typebot) => typebot.id) },
hasStarted: true,
createdAt: {
gte: yesterdayMidnight,
lt: todayMidnight,
},
},
});
resultsSum += result._count._all;
const olderAdmin = workspace.members
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime())
.at(0);
if (!olderAdmin) continue;
for (const result of results) {
if (result._count._all === 0) continue;
newResultsCollectedEvents.push({
name: "New results collected",
typebotId: result.typebotId,
workspaceId: workspace.id,
userId: olderAdmin.user.id,
data: {
total: result._count._all,
},
});
resultsSum += result._count._all;
}
} catch (error) {
console.error("❌ Error processing workspace", workspace.id);
throw error;
}
}
console.log("💾 Saving events to PostHog...");
await trackEvents(newResultsCollectedEvents);
console.log("✅ Done!");
return {
totalWorkspaces: recentWorkspaces.length,

View File

@ -20,6 +20,7 @@ const inspectWorkspace = async () => {
},
include: {
typebots: {
take: 30,
orderBy: {
updatedAt: "desc",
},

View File

@ -42,6 +42,6 @@ export const trackEvents = async (events: TelemetryEvent[]) => {
try {
await client.shutdown();
} catch (err) {
console.error("ERROR while shutting down PostHog client", err);
console.error("❌ Failed to shut down PostHog client", err);
}
};