← back to YOLO Progress Viewer

server.js

612 lines

const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const axios = require('axios');
const WebSocket = require('ws');
const http = require('http');

const app = express();
// Security headers via helmet (added 2026-05-04 overnight YOLO loop)
app.use(helmet({ contentSecurityPolicy: false }));
const PORT = 3041;

// Middleware
app.use(cors());
app.use(express.json());

// 404-guard: never serve snapshot/backup files even if accidentally dropped into static root
app.use((req, res, next) => {
  if (/\.(bak)(\.|$)|\.pre-/i.test(req.path)) {
    return res.status(404).send('Not found');
  }
  next();
});

app.use(express.static('public'));

// Create HTTP server and WebSocket server
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });

// YOLO Progress tracking
let yoloProgress = {
  isActive: false,
  currentCycle: 0,
  maxCycles: 10,
  vendorsTotal: 107,
  vendorsTested: 0,
  vendorsWorking: 0,
  vendorsWithProducts: 0,
  totalProducts: 0,
  currentVendor: null,
  recentTests: [],
  startTime: null,
  lastUpdate: null,
  cycleStartTime: null,
  estimatedCompletion: null
};

// WebSocket connections
const clients = new Set();

wss.on('connection', (ws) => {
  clients.add(ws);
  
  // Send current progress immediately
  ws.send(JSON.stringify({
    type: 'progress_update',
    data: yoloProgress
  }));
  
  ws.on('close', () => {
    clients.delete(ws);
  });
});

// Broadcast progress to all connected clients
function broadcastProgress() {
  const message = JSON.stringify({
    type: 'progress_update',
    data: yoloProgress
  });
  
  clients.forEach(client => {
    if (client.readyState === WebSocket.OPEN) {
      client.send(message);
    }
  });
}

// Fetch progress from NEW-SKU Viewer
async function fetchProgressFromViewer() {
  try {
    const response = await axios.get('http://45.61.58.125:3030/api/vendors');
    const data = response.data;
    
    // Count actual vendors in the vendors object
    const vendorCount = data.vendors ? Object.keys(data.vendors).length : 0;
    const successfulVendors = data.vendors ? Object.values(data.vendors).filter(v => v.success).length : 0;
    const vendorsWithProducts = data.vendors ? Object.values(data.vendors).filter(v => v.products > 0).length : 0;
    const totalProducts = data.vendors ? Object.values(data.vendors).reduce((sum, v) => sum + (v.products || 0), 0) : 0;
    
    console.log(`🔄 Fetching progress: ${vendorCount} vendors tested, ${successfulVendors} successful, ${totalProducts} products`);
    
    // Update progress data from actual vendor data
    yoloProgress.vendorsTested = vendorCount;
    yoloProgress.vendorsWorking = successfulVendors;
    yoloProgress.vendorsWithProducts = vendorsWithProducts;
    yoloProgress.totalProducts = totalProducts;
    yoloProgress.lastUpdate = new Date().toISOString();
    yoloProgress.isActive = data.isScanning || vendorCount > 0;
    yoloProgress.currentCycle = 1; // We're in cycle 1
    
    // Add recent successful tests if vendors data exists
    if (data.vendors && typeof data.vendors === 'object') {
      const recentSuccessful = Object.entries(data.vendors)
        .filter(([id, vendor]) => vendor.success && vendor.products > 0)
        .slice(-10)
        .map(([id, vendor]) => ({
          vendor: id,
          products: vendor.products,
          timestamp: vendor.lastTested,
          sampleProducts: vendor.sampleProducts?.slice(0, 3) || []
        }));
      
      yoloProgress.recentTests = recentSuccessful;
    }
    
    // Calculate estimated completion
    if (yoloProgress.isActive && yoloProgress.vendorsTested > 0) {
      const progress = yoloProgress.vendorsTested / yoloProgress.vendorsTotal;
      const elapsed = yoloProgress.cycleStartTime ? 
        (Date.now() - new Date(yoloProgress.cycleStartTime).getTime()) : 0;
      const estimatedTotal = elapsed / progress;
      const remaining = estimatedTotal - elapsed;
      yoloProgress.estimatedCompletion = new Date(Date.now() + remaining).toISOString();
    }
    
  } catch (error) {
    console.error('Error fetching progress:', error.message);
  }
}

// Initialize YOLO mode tracking - preserve existing data
async function initializeYoloTracking() {
  // Don't reset if we already have data
  if (yoloProgress.vendorsTested > 0 || yoloProgress.totalProducts > 0) {
    console.log('📊 YOLO Progress Tracker - preserving existing data');
    console.log(`  Vendors tested: ${yoloProgress.vendorsTested}`);
    console.log(`  Products found: ${yoloProgress.totalProducts}`);
    return;
  }
  
  // Only initialize if no data exists
  console.log('🚨 YOLO Progress Tracker - loading current state');
  
  // Try to fetch existing progress first
  await fetchProgressFromViewer();
  
  // If still no data after fetching, set initial state
  if (yoloProgress.vendorsTested === 0 && yoloProgress.totalProducts === 0) {
    yoloProgress.isActive = true;
    yoloProgress.startTime = new Date().toISOString();
    yoloProgress.cycleStartTime = new Date().toISOString();
    yoloProgress.currentCycle = 1;
    console.log('🚨 YOLO Progress Tracker - starting fresh');
  }
}

// API Routes
app.get('/api/yolo-progress', (req, res) => {
  res.json(yoloProgress);
});

app.post('/api/reset-tracking', (req, res) => {
  yoloProgress = {
    isActive: false,
    currentCycle: 0,
    maxCycles: 10,
    vendorsTotal: 107,
    vendorsTested: 0,
    vendorsWorking: 0,
    vendorsWithProducts: 0,
    totalProducts: 0,
    currentVendor: null,
    recentTests: [],
    startTime: null,
    lastUpdate: null,
    cycleStartTime: null,
    estimatedCompletion: null
  };
  
  broadcastProgress();
  res.json({ success: true, message: 'Tracking reset' });
});

// Serve detailed viewer page
app.get('/detailed', (req, res) => {
  res.sendFile(__dirname + '/detailed-viewer.html');
});

// Serve main HTML page
app.get('/', (req, res) => {
  res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🚨 YOLO MODE Progress Tracker</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 50%, #fd79a8 100%);
            min-height: 100vh;
            color: white;
            overflow-x: hidden;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            position: relative;
        }

        .yolo-title {
            font-size: 4rem;
            font-weight: 900;
            background: linear-gradient(45deg, #fff, #ffeb3b, #fff);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            text-shadow: 0 0 30px rgba(255,255,255,0.5);
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.05); }
        }

        .subtitle {
            font-size: 1.2rem;
            margin-top: 10px;
            opacity: 0.9;
        }

        .status-banner {
            background: rgba(0,0,0,0.3);
            backdrop-filter: blur(10px);
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 30px;
            border: 2px solid rgba(255,255,255,0.2);
        }

        .status-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
        }

        .stat-card {
            background: rgba(255,255,255,0.1);
            border-radius: 12px;
            padding: 20px;
            text-align: center;
            border: 1px solid rgba(255,255,255,0.2);
            backdrop-filter: blur(5px);
        }

        .stat-value {
            font-size: 2.5rem;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .stat-label {
            font-size: 0.9rem;
            opacity: 0.8;
        }

        .progress-section {
            background: rgba(0,0,0,0.2);
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 30px;
        }

        .progress-bar {
            background: rgba(255,255,255,0.2);
            border-radius: 50px;
            height: 20px;
            overflow: hidden;
            margin-bottom: 15px;
            position: relative;
        }

        .progress-fill {
            background: linear-gradient(90deg, #4ade80, #22d3ee);
            height: 100%;
            border-radius: 50px;
            transition: width 0.5s ease;
            position: relative;
            overflow: hidden;
        }

        .progress-fill::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
            animation: shimmer 2s infinite;
        }

        @keyframes shimmer {
            0% { left: -100%; }
            100% { left: 100%; }
        }

        .cycle-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 15px;
            font-size: 1.1rem;
        }

        .recent-tests {
            background: rgba(0,0,0,0.2);
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 30px;
        }

        .test-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid rgba(255,255,255,0.1);
        }

        .test-item:last-child {
            border-bottom: none;
        }

        .vendor-name {
            font-weight: bold;
            color: #4ade80;
        }

        .product-count {
            background: rgba(255,255,255,0.2);
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 0.9rem;
        }

        .eta {
            background: rgba(255,193,7,0.2);
            border: 1px solid rgba(255,193,7,0.5);
            border-radius: 10px;
            padding: 15px;
            text-align: center;
            margin-bottom: 20px;
        }

        .connection-status {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 10px 15px;
            border-radius: 20px;
            font-size: 0.9rem;
            background: rgba(0,0,0,0.3);
            border: 1px solid rgba(255,255,255,0.2);
        }

        .connected {
            color: #4ade80;
            border-color: #4ade80;
        }

        .disconnected {
            color: #ef4444;
            border-color: #ef4444;
        }

        .btn {
            background: rgba(255,255,255,0.2);
            border: 2px solid rgba(255,255,255,0.3);
            color: white;
            padding: 12px 24px;
            border-radius: 25px;
            cursor: pointer;
            font-weight: 500;
            transition: all 0.3s ease;
            margin: 5px;
        }

        .btn:hover {
            background: rgba(255,255,255,0.3);
            transform: translateY(-2px);
        }
    </style>
</head>
<body>
    <div class="connection-status" id="connectionStatus">🔴 Connecting...</div>
    
    <div class="container">
        <div class="header">
            <h1 class="yolo-title">🚨 YOLO MODE 🚨</h1>
            <p class="subtitle">Overnight Vendor Testing Marathon</p>
        </div>

        <div class="status-banner">
            <div class="status-grid">
                <div class="stat-card">
                    <div class="stat-value" id="currentCycle">0</div>
                    <div class="stat-label">Current Cycle</div>
                </div>
                <div class="stat-card">
                    <div class="stat-value" id="vendorsTested">0</div>
                    <div class="stat-label">Vendors Tested</div>
                </div>
                <div class="stat-card">
                    <div class="stat-value" id="vendorsWorking">0</div>
                    <div class="stat-label">Working</div>
                </div>
                <div class="stat-card">
                    <div class="stat-value" id="totalProducts">0</div>
                    <div class="stat-label">Products Found</div>
                </div>
            </div>
        </div>

        <div class="progress-section">
            <div class="cycle-info">
                <span>Progress: <span id="progressPercent">0%</span></span>
                <span>Status: <span id="statusText">Initializing...</span></span>
            </div>
            <div class="progress-bar">
                <div class="progress-fill" id="progressFill" style="width: 0%"></div>
            </div>
            <div class="eta" id="eta" style="display: none;">
                <strong>Estimated Completion:</strong> <span id="etaTime">Calculating...</span>
            </div>
        </div>

        <div class="recent-tests">
            <h3 style="margin-bottom: 20px;">🎯 Recent Successful Tests</h3>
            <div id="recentTestsList">
                <div style="text-align: center; opacity: 0.7;">No tests completed yet...</div>
            </div>
        </div>

        <div style="text-align: center;">
            <button class="btn" onclick="window.open('http://45.61.58.125:3030', '_blank', 'noopener,noreferrer')">
                📊 View Full Dashboard
            </button>
            <button class="btn" onclick="resetTracking()">
                🔄 Reset Tracking
            </button>
        </div>
    </div>

    <script>
        let ws;
        let reconnectInterval;

        function connectWebSocket() {
            ws = new WebSocket('ws://45.61.58.125:3041');
            
            ws.onopen = function() {
                console.log('🚨 YOLO WebSocket connected');
                document.getElementById('connectionStatus').innerHTML = '🟢 Connected';
                document.getElementById('connectionStatus').className = 'connection-status connected';
                clearInterval(reconnectInterval);
            };

            ws.onmessage = function(event) {
                const message = JSON.parse(event.data);
                if (message.type === 'progress_update') {
                    updateProgress(message.data);
                }
            };

            ws.onclose = function() {
                console.log('🚨 YOLO WebSocket disconnected');
                document.getElementById('connectionStatus').innerHTML = '🔴 Disconnected';
                document.getElementById('connectionStatus').className = 'connection-status disconnected';
                
                // Reconnect every 3 seconds
                reconnectInterval = setInterval(() => {
                    connectWebSocket();
                }, 3000);
            };

            ws.onerror = function(error) {
                console.error('WebSocket error:', error);
            };
        }

        function updateProgress(data) {
            // Update stats
            document.getElementById('currentCycle').textContent = data.currentCycle || 0;
            document.getElementById('vendorsTested').textContent = data.vendorsTested || 0;
            document.getElementById('vendorsWorking').textContent = data.vendorsWorking || 0;
            document.getElementById('totalProducts').textContent = data.totalProducts || 0;

            // Update progress bar
            const progress = data.vendorsTotal > 0 ? (data.vendorsTested / data.vendorsTotal) * 100 : 0;
            document.getElementById('progressPercent').textContent = Math.round(progress) + '%';
            document.getElementById('progressFill').style.width = progress + '%';

            // Update status
            let statusText = data.isActive ? '🔥 ACTIVE' : '⏸️ PAUSED';
            if (data.currentVendor) {
                statusText += ' - Testing ' + data.currentVendor;
            }
            document.getElementById('statusText').textContent = statusText;

            // Update ETA
            if (data.estimatedCompletion) {
                const eta = new Date(data.estimatedCompletion);
                document.getElementById('etaTime').textContent = eta.toLocaleString();
                document.getElementById('eta').style.display = 'block';
            }

            // Update recent tests
            updateRecentTests(data.recentTests || []);
        }

        function updateRecentTests(tests) {
            const list = document.getElementById('recentTestsList');
            
            if (tests.length === 0) {
                list.innerHTML = '<div style="text-align: center; opacity: 0.7;">No tests completed yet...</div>';
                return;
            }

            list.innerHTML = tests.map(test => \`
                <div class="test-item">
                    <div>
                        <div class="vendor-name">\${test.vendor}</div>
                        <div style="font-size: 0.8rem; opacity: 0.7;">
                            \${test.sampleProducts?.map(p => p.title || p.name).slice(0, 2).join(', ')}
                        </div>
                    </div>
                    <div class="product-count">\${test.products} products</div>
                </div>
            \`).join('');
        }

        async function resetTracking() {
            try {
                await fetch('/api/reset-tracking', { method: 'POST' });
                alert('🔄 Tracking reset successfully');
            } catch (error) {
                alert('❌ Error resetting tracking');
            }
        }

        // Initialize
        connectWebSocket();
        
        // Fallback polling every 30 seconds
        setInterval(async () => {
            try {
                const response = await fetch('/api/yolo-progress');
                const data = await response.json();
                updateProgress(data);
            } catch (error) {
                console.error('Error fetching progress:', error);
            }
        }, 30000);
    </script>
</body>
</html>
  `);
});

// Start server
server.listen(PORT, '0.0.0.0', () => {
  console.log(`🚨 YOLO Progress Viewer running on http://45.61.58.125:${PORT}`);
  console.log(`📊 Real-time WebSocket updates enabled`);
  console.log(`🔗 Progress API: http://45.61.58.125:${PORT}/api/yolo-progress`);
  
  // Initialize tracking
  initializeYoloTracking();
  
  // Fetch progress every 10 seconds
  setInterval(fetchProgressFromViewer, 10000);
  
  // Broadcast progress every 5 seconds
  setInterval(broadcastProgress, 5000);
});

// Error handling
process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

process.on('uncaughtException', (error) => {
  console.error('Uncaught Exception:', error);
  process.exit(1);
});