#!/usr/bin/env python3
"""Quick single-connection status check."""
import paramiko, json

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('192.168.178.150', username='fabian',
            key_filename=r'C:\Users\fabia\.ssh\id_ed25519', timeout=10)

def run(cmd):
    _, so, se = ssh.exec_command(cmd, timeout=15)
    return so.read().decode()

try:
    # Is ComfyUI running?
    print("=== PROCESS ===")
    print(run("bash -c 'PID=$(pgrep -f \"python3 main.py\" | head -1); "
        "if [ -n \"$PID\" ]; then "
        "  ps -p $PID -o pid,%cpu,%mem,nlwp,etime --no-headers; "
        "else echo NOT_RUNNING; fi'").strip())

    # Log
    print("\n=== LOG (last 20) ===")
    print(run("tail -20 /home/fabian/comfyui.log 2>/dev/null").strip())

    # GPU
    print("\n=== GPU ===")
    gpu = run("HSA_OVERRIDE_GFX_VERSION=10.1.0 rocm-smi 2>/dev/null | grep -E '0x|GPU%'")
    print(gpu.strip() if gpu.strip() else "no output")

    # Output files
    print("\n=== OUTPUT ===")
    print(run("ls -lah /home/fabian/ComfyUI/output/ 2>/dev/null").strip())

    # Queue
    print("\n=== QUEUE ===")
    q = run("curl -s http://localhost:8188/queue 2>/dev/null")
    if q.strip():
        qj = json.loads(q)
        print(f"Running: {len(qj.get('queue_running',[]))}, Pending: {len(qj.get('queue_pending',[]))}")
    else:
        print("Server not responding")
finally:
    ssh.close()
    print("\nSSH closed.")
