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:
@@ -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 = [
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user