#!/bin/bash
# Monitoring rapide de l'optimisation BULL/BEAR

echo "════════════════════════════════════════════════════════════"
echo "📊 OPTIMISATION BULL/BEAR (V10 Range fixe)"
echo "════════════════════════════════════════════════════════════"
echo ""

# Check process
if pgrep -f "optimize_bull_bear.py" > /dev/null; then
    PID=$(pgrep -f "optimize_bull_bear.py")
    CPU=$(ps -p $PID -o %cpu --no-headers)
    MEM=$(ps -p $PID -o %mem --no-headers)
    ELAPSED=$(ps -p $PID -o etime --no-headers)
    echo "✅ Processus actif"
    echo "   PID: $PID | CPU: ${CPU}% | MEM: ${MEM}% | Temps: $ELAPSED"
else
    echo "❌ Processus non actif"
fi
echo ""

# Analyse résultats
if [ -f bull_bear_results.json ]; then
    python3 -c "
import json
r = json.load(open('bull_bear_results.json'))
profitable = [x for x in r if x['pnl_pct'] > 0 and x['profit_factor'] > 1.0]

print(f'Tests: {len(r)}/200 ({len(r)/2:.0f}%)')
print(f'Profitables: {len(profitable)} ({len(profitable)/len(r)*100:.1f}%)')
print()

if profitable:
    profitable.sort(key=lambda x: x['pnl_pct'], reverse=True)
    print('🏆 TOP 3:')
    for i, x in enumerate(profitable[:3], 1):
        print(f'{i}. PnL {x[\"pnl_pct\"]:+.1f}% | PF {x[\"profit_factor\"]:.2f} | Trades {x[\"trades\"]} | {x[\"trades_by_strategy\"]}')
    print()

    # Temps estimé restant
    if len(r) > 0:
        import time
        try:
            first_time = min([x.get('timestamp', '') for x in r if x.get('timestamp')])
            last_time = max([x.get('timestamp', '') for x in r if x.get('timestamp')])
            from datetime import datetime
            if first_time and last_time:
                dt1 = datetime.fromisoformat(first_time)
                dt2 = datetime.fromisoformat(last_time)
                elapsed_sec = (dt2 - dt1).total_seconds()
                if elapsed_sec > 0:
                    sec_per_test = elapsed_sec / len(r)
                    remaining_tests = 200 - len(r)
                    remaining_sec = sec_per_test * remaining_tests
                    remaining_min = remaining_sec / 60
                    print(f'⏱️  Temps restant estimé: {remaining_min:.0f} minutes')
        except:
            pass
"
else
    echo "❌ Fichier résultats introuvable"
fi

echo ""
echo "════════════════════════════════════════════════════════════"
