mirror of
https://github.com/kjanat/livedash-node.git
synced 2026-01-16 15:32:10 +01:00
Broken shit
This commit is contained in:
@ -2,6 +2,133 @@
|
||||
import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { fetchAndParseCsv } from "../../../lib/csvFetcher";
|
||||
import { prisma } from "../../../lib/prisma";
|
||||
import { processUnprocessedSessions } from "../../../lib/processingSchedulerNoCron";
|
||||
import { exec } from "child_process";
|
||||
import { promisify } from "util";
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
|
||||
/**
|
||||
* Triggers the complete workflow: fetch transcripts + process all sessions
|
||||
*/
|
||||
async function triggerCompleteWorkflow(): Promise<{ message: string }> {
|
||||
try {
|
||||
// Step 1: Fetch missing transcripts
|
||||
const sessionsWithoutMessages = await prisma.session.count({
|
||||
where: {
|
||||
messages: { none: {} },
|
||||
fullTranscriptUrl: { not: null }
|
||||
}
|
||||
});
|
||||
|
||||
if (sessionsWithoutMessages > 0) {
|
||||
console.log(`[Complete Workflow] Fetching transcripts for ${sessionsWithoutMessages} sessions`);
|
||||
|
||||
// Get sessions that have fullTranscriptUrl but no messages
|
||||
const sessionsToProcess = await prisma.session.findMany({
|
||||
where: {
|
||||
AND: [
|
||||
{ fullTranscriptUrl: { not: null } },
|
||||
{ messages: { none: {} } },
|
||||
],
|
||||
},
|
||||
include: {
|
||||
company: true,
|
||||
},
|
||||
take: 20, // Process in batches
|
||||
});
|
||||
|
||||
for (const session of sessionsToProcess) {
|
||||
try {
|
||||
if (!session.fullTranscriptUrl) continue;
|
||||
|
||||
// Fetch transcript content
|
||||
const transcriptContent = await fetchTranscriptContent(
|
||||
session.fullTranscriptUrl,
|
||||
session.company.csvUsername || undefined,
|
||||
session.company.csvPassword || undefined
|
||||
);
|
||||
|
||||
if (!transcriptContent) {
|
||||
console.log(`No transcript content for session ${session.id}`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Parse transcript into messages
|
||||
const lines = transcriptContent.split("\n").filter((line) => line.trim());
|
||||
const messages: Array<{
|
||||
sessionId: string;
|
||||
role: string;
|
||||
content: string;
|
||||
timestamp: Date;
|
||||
order: number;
|
||||
}> = [];
|
||||
let messageOrder = 0;
|
||||
const currentTimestamp = new Date();
|
||||
|
||||
for (const line of lines) {
|
||||
// Try format: [DD-MM-YYYY HH:MM:SS] Role: Content
|
||||
const timestampMatch = line.match(/^\[([^\]]+)\]\s*([^:]+):\s*(.+)$/);
|
||||
|
||||
if (timestampMatch) {
|
||||
const [, timestamp, role, content] = timestampMatch;
|
||||
const dateMatch = timestamp.match(/^(\d{1,2})-(\d{1,2})-(\d{4}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/);
|
||||
let parsedTimestamp = new Date();
|
||||
|
||||
if (dateMatch) {
|
||||
const [, day, month, year, hour, minute, second] = dateMatch;
|
||||
parsedTimestamp = new Date(
|
||||
parseInt(year),
|
||||
parseInt(month) - 1,
|
||||
parseInt(day),
|
||||
parseInt(hour),
|
||||
parseInt(minute),
|
||||
parseInt(second)
|
||||
);
|
||||
}
|
||||
|
||||
messages.push({
|
||||
sessionId: session.id,
|
||||
role: role.trim().toLowerCase(),
|
||||
content: content.trim(),
|
||||
timestamp: parsedTimestamp,
|
||||
order: messageOrder++,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (messages.length > 0) {
|
||||
// Save messages to database
|
||||
await prisma.message.createMany({
|
||||
data: messages as any, // Type assertion needed due to Prisma types
|
||||
});
|
||||
console.log(`Added ${messages.length} messages for session ${session.id}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error processing session ${session.id}:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Process all unprocessed sessions
|
||||
const unprocessedWithMessages = await prisma.session.count({
|
||||
where: {
|
||||
processed: false,
|
||||
messages: { some: {} }
|
||||
}
|
||||
});
|
||||
|
||||
if (unprocessedWithMessages > 0) {
|
||||
console.log(`[Complete Workflow] Processing ${unprocessedWithMessages} sessions`);
|
||||
await processUnprocessedSessions();
|
||||
}
|
||||
|
||||
return { message: `Complete workflow finished successfully` };
|
||||
} catch (error) {
|
||||
console.error('[Complete Workflow] Error:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
interface SessionCreateData {
|
||||
id: string;
|
||||
@ -166,7 +293,21 @@ export default async function handler(
|
||||
});
|
||||
}
|
||||
|
||||
res.json({ ok: true, imported: sessions.length });
|
||||
// After importing sessions, automatically trigger complete workflow (fetch transcripts + process)
|
||||
// This runs in the background without blocking the response
|
||||
triggerCompleteWorkflow()
|
||||
.then((result) => {
|
||||
console.log(`[Refresh Sessions] Complete workflow finished: ${result.message}`);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(`[Refresh Sessions] Complete workflow failed:`, error);
|
||||
});
|
||||
|
||||
res.json({
|
||||
ok: true,
|
||||
imported: sessions.length,
|
||||
message: "Sessions imported and complete processing workflow started automatically"
|
||||
});
|
||||
} catch (e) {
|
||||
const error = e instanceof Error ? e.message : "An unknown error occurred";
|
||||
res.status(500).json({ error });
|
||||
|
||||
@ -2,7 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
|
||||
import { getServerSession } from "next-auth";
|
||||
import { authOptions } from "../auth/[...nextauth]";
|
||||
import { prisma } from "../../../lib/prisma";
|
||||
import { processUnprocessedSessions } from "../../../lib/processingScheduler";
|
||||
import { processUnprocessedSessions } from "../../../lib/processingSchedulerNoCron";
|
||||
|
||||
interface SessionUser {
|
||||
email: string;
|
||||
@ -50,8 +50,10 @@ export default async function handler(
|
||||
const { batchSize, maxConcurrency } = req.body;
|
||||
|
||||
// Validate parameters
|
||||
const validatedBatchSize = batchSize && batchSize > 0 ? parseInt(batchSize) : null;
|
||||
const validatedMaxConcurrency = maxConcurrency && maxConcurrency > 0 ? parseInt(maxConcurrency) : 5;
|
||||
const validatedBatchSize =
|
||||
batchSize && batchSize > 0 ? parseInt(batchSize) : null;
|
||||
const validatedMaxConcurrency =
|
||||
maxConcurrency && maxConcurrency > 0 ? parseInt(maxConcurrency) : 5;
|
||||
|
||||
// Check how many unprocessed sessions exist
|
||||
const unprocessedCount = await prisma.session.count({
|
||||
@ -76,12 +78,17 @@ export default async function handler(
|
||||
|
||||
// Note: We're calling the function but not awaiting it to avoid timeout
|
||||
// The processing will continue in the background
|
||||
processUnprocessedSessions(validatedBatchSize, validatedMaxConcurrency)
|
||||
processUnprocessedSessions(validatedBatchSize || undefined, validatedMaxConcurrency)
|
||||
.then(() => {
|
||||
console.log(`[Manual Trigger] Processing completed for company ${user.companyId}`);
|
||||
console.log(
|
||||
`[Manual Trigger] Processing completed for company ${user.companyId}`
|
||||
);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(`[Manual Trigger] Processing failed for company ${user.companyId}:`, error);
|
||||
console.error(
|
||||
`[Manual Trigger] Processing failed for company ${user.companyId}:`,
|
||||
error
|
||||
);
|
||||
});
|
||||
|
||||
return res.json({
|
||||
@ -92,7 +99,6 @@ export default async function handler(
|
||||
maxConcurrency: validatedMaxConcurrency,
|
||||
startedAt: new Date().toISOString(),
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error("[Manual Trigger] Error:", error);
|
||||
return res.status(500).json({
|
||||
|
||||
@ -45,7 +45,7 @@ export default async function handler(
|
||||
if (startDate && endDate) {
|
||||
whereClause.startTime = {
|
||||
gte: new Date(startDate as string),
|
||||
lte: new Date(endDate as string + 'T23:59:59.999Z'), // Include full end date
|
||||
lte: new Date((endDate as string) + "T23:59:59.999Z"), // Include full end date
|
||||
};
|
||||
}
|
||||
|
||||
@ -100,10 +100,12 @@ export default async function handler(
|
||||
// Calculate date range from sessions
|
||||
let dateRange: { minDate: string; maxDate: string } | null = null;
|
||||
if (prismaSessions.length > 0) {
|
||||
const dates = prismaSessions.map(s => new Date(s.startTime)).sort((a, b) => a.getTime() - b.getTime());
|
||||
const dates = prismaSessions
|
||||
.map((s) => new Date(s.startTime))
|
||||
.sort((a, b) => a.getTime() - b.getTime());
|
||||
dateRange = {
|
||||
minDate: dates[0].toISOString().split('T')[0], // First session date
|
||||
maxDate: dates[dates.length - 1].toISOString().split('T')[0] // Last session date
|
||||
minDate: dates[0].toISOString().split("T")[0], // First session date
|
||||
maxDate: dates[dates.length - 1].toISOString().split("T")[0], // Last session date
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user