isLoggedIn()) { throw new Exception('Unauthorized'); } $db = Database::getInstance(); // Get current analysis state (no caching) $stateQuery = $db->query("SELECT * FROM optimization_analysis_state ORDER BY id DESC LIMIT 1"); $state = $stateQuery ? $stateQuery->fetch_assoc() : null; // Get FRESH real-time statistics - force fresh database query $stats = getCurrentStatsFresh($db); // Calculate processing rate $processingRate = calculateProcessingRate($db); // Estimate time remaining $timeRemaining = estimateTimeRemaining($stats, $processingRate); // Check for recent activity $recentActivity = checkRecentActivity($db); // Add debug timestamp to verify fresh data $debug_info = [ 'query_time' => date('Y-m-d H:i:s'), 'microtime' => microtime(true), 'cache_buster' => time() ]; echo json_encode([ 'success' => true, 'is_running' => $state ? (bool)$state['is_running'] : false, 'should_pause' => $state ? (bool)$state['should_pause'] : false, 'stats' => $stats, 'processing_rate' => $processingRate, 'time_remaining' => $timeRemaining, 'recent_activity' => $recentActivity, 'started_at' => $state ? $state['started_at'] : null, 'last_updated' => $state ? $state['last_updated'] : null, 'debug' => $debug_info ]); } catch (Exception $e) { http_response_code(500); echo json_encode([ 'success' => false, 'message' => $e->getMessage(), 'debug' => [ 'error_time' => date('Y-m-d H:i:s'), 'error_file' => __FILE__ ] ]); } // ENHANCED: Force fresh statistics with better queries function getCurrentStatsFresh($db) { $stats = []; try { // Force fresh queries by using NOW() in query to prevent any MySQL query caching // Total combinations $totalQuery = $db->query("SELECT COUNT(*) as count FROM panel_directives WHERE created_at <= NOW()"); $stats['total'] = $totalQuery ? (int)$totalQuery->fetch_assoc()['count'] : 0; // Processed combinations (force fresh read) $processedQuery = $db->query("SELECT COUNT(*) as count FROM panel_directives WHERE llm_checked = 1 AND updated_at <= NOW()"); $stats['processed'] = $processedQuery ? (int)$processedQuery->fetch_assoc()['count'] : 0; // Impossible combinations (force fresh read) $impossibleQuery = $db->query("SELECT COUNT(*) as count FROM panel_directives WHERE llm_checked = 1 AND is_impossible = 1 AND updated_at <= NOW()"); $stats['impossible'] = $impossibleQuery ? (int)$impossibleQuery->fetch_assoc()['count'] : 0; // Progress percentage $stats['progress'] = $stats['total'] > 0 ? ($stats['processed'] / $stats['total']) * 100 : 0; // Optimization rate $stats['optimization_rate'] = $stats['total'] > 0 ? ($stats['impossible'] / $stats['total']) * 100 : 0; // Remaining combinations $stats['remaining'] = $stats['total'] - $stats['processed']; // Add timestamp for debugging $stats['fetched_at'] = date('Y-m-d H:i:s'); return $stats; } catch (Exception $e) { error_log("Error getting fresh stats: " . $e->getMessage()); return [ 'total' => 0, 'processed' => 0, 'impossible' => 0, 'progress' => 0, 'optimization_rate' => 0, 'remaining' => 0, 'fetched_at' => date('Y-m-d H:i:s'), 'error' => $e->getMessage() ]; } } function calculateProcessingRate($db) { try { // Calculate combinations processed in the last hour $hourlyQuery = $db->query(" SELECT COUNT(*) as count FROM panel_directives WHERE llm_checked = 1 AND updated_at >= DATE_SUB(NOW(), INTERVAL 1 HOUR) "); $hourlyProcessed = $hourlyQuery ? $hourlyQuery->fetch_assoc()['count'] : 0; // Calculate rate per minute $rate = $hourlyProcessed / 60; return max(0, $rate); } catch (Exception $e) { return 0; } } function estimateTimeRemaining($stats, $processingRate) { try { if ($processingRate <= 0 || $stats['remaining'] <= 0) { return null; } $minutesRemaining = $stats['remaining'] / $processingRate; if ($minutesRemaining < 60) { return round($minutesRemaining) . ' minutes'; } else if ($minutesRemaining < 1440) { // Less than 24 hours return round($minutesRemaining / 60, 1) . ' hours'; } else { return round($minutesRemaining / 1440, 1) . ' days'; } } catch (Exception $e) { return null; } } function checkRecentActivity($db) { try { // Check if any combinations were processed in the last 5 minutes $recentQuery = $db->query(" SELECT COUNT(*) as count FROM panel_directives WHERE llm_checked = 1 AND updated_at >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) "); $recentCount = $recentQuery ? $recentQuery->fetch_assoc()['count'] : 0; return $recentCount > 0; } catch (Exception $e) { return false; } } ?>