#!/bin/bash
# Monitor generation progress
COMFY="http://localhost:8188"
pid=$(pgrep -f "python.*main.py" | head -1)

for i in $(seq 1 30); do
    echo "=== Check $i ($(date +%H:%M:%S)) ==="
    
    # Process status
    if [ -n "$pid" ]; then
        cpu=$(cat /proc/$pid/stat 2>/dev/null | awk '{print $14+$15}')
        rss=$(cat /proc/$pid/status 2>/dev/null | grep VmRSS | awk '{print $2}')
        swap=$(cat /proc/$pid/status 2>/dev/null | grep VmSwap | awk '{print $2}')
        vram=$(rocm-smi --showmeminfo vram 2>/dev/null | grep "Used" | awk '{print $NF}')
        echo "  CPU_ticks=$cpu RSS=${rss}kB Swap=${swap}kB VRAM=${vram}B"
    fi
    
    # Last 3 log lines
    tail -3 /home/fabian/comfyui8.log 2>/dev/null
    
    # Check queue
    queue=$(curl -s "$COMFY/api/prompt" 2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); print(f'running={len(d.get(\"exec_info\",{}).get(\"queue_running\",[]))}, pending={len(d.get(\"exec_info\",{}).get(\"queue_pending\",[]))}')" 2>/dev/null)
    echo "  Queue: $queue"
    
    # Check for output
    ls -la /home/fabian/ComfyUI/output/bc250_test* 2>/dev/null && echo "  OUTPUT FOUND!" && break
    
    echo ""
    sleep 10
done
