Fix progress tracking synchronization issues

- Pre-generate sessionId on client side before API call to avoid race conditions
- Add small delays in progress tracker to ensure EventSource connection is established
- Improve logging and error handling in progress streaming
- Add connection confirmation messages in EventSource stream
- Fix TypeScript interface to include sessionId in AnalysisProgress

This should resolve the lag between actual analysis progress and progress bar display.
This commit is contained in:
mindesbunister
2025-07-17 12:10:47 +02:00
parent 7e8f033bb2
commit 0399103f8a
4 changed files with 45 additions and 13 deletions

View File

@@ -6,13 +6,13 @@ import { progressTracker } from '../../../lib/progress-tracker'
export async function POST(request) {
try {
const body = await request.json()
const { symbol, layouts, timeframe, timeframes, selectedLayouts, analyze = true } = body
const { symbol, layouts, timeframe, timeframes, selectedLayouts, analyze = true, sessionId: providedSessionId } = body
console.log('📊 Enhanced screenshot request:', { symbol, layouts, timeframe, timeframes, selectedLayouts })
console.log('📊 Enhanced screenshot request:', { symbol, layouts, timeframe, timeframes, selectedLayouts, providedSessionId })
// Generate unique session ID for progress tracking
const sessionId = `analysis_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
console.log('🔍 Created session ID:', sessionId)
// Use provided sessionId or generate one
const sessionId = providedSessionId || `analysis_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`
console.log('🔍 Using session ID:', sessionId)
// Create progress tracking session with initial steps
const initialSteps = [

View File

@@ -12,21 +12,30 @@ export async function GET(
const stream = new ReadableStream({
start(controller) {
console.log(`🔍 Starting EventSource stream for session: ${sessionId}`)
// Send initial progress if session exists
const initialProgress = progressTracker.getProgress(sessionId)
if (initialProgress) {
console.log(`🔍 Sending initial progress for ${sessionId}:`, initialProgress.currentStep)
const data = `data: ${JSON.stringify(initialProgress)}\n\n`
controller.enqueue(encoder.encode(data))
} else {
// Send a connection established message even if no progress yet
const connectMsg = `data: ${JSON.stringify({ type: 'connected', sessionId })}\n\n`
controller.enqueue(encoder.encode(connectMsg))
}
// Listen for progress updates
const progressHandler = (progress: any) => {
console.log(`🔍 Streaming progress update for ${sessionId}:`, progress.currentStep)
const data = `data: ${JSON.stringify(progress)}\n\n`
controller.enqueue(encoder.encode(data))
}
// Listen for completion
const completeHandler = () => {
console.log(`🔍 Streaming completion for ${sessionId}`)
const data = `data: ${JSON.stringify({ type: 'complete' })}\n\n`
controller.enqueue(encoder.encode(data))
controller.close()