#!/usr/bin/env python3
"""PAPER ONLY - serve the Kronos paper dashboard on http://127.0.0.1:8766 and re-mark open positions every N minutes.
(8765 is the cash-carry dashboard.)  Usage:  python serve_and_refresh.py   [env PORT, INTERVAL_MIN]"""
import os, subprocess, sys, threading, time, http.server, functools
BASE = os.path.dirname(os.path.abspath(__file__)); PORT = int(os.environ.get("PORT", "8766")); IV = int(os.environ.get("INTERVAL_MIN", "5"))


def loop():
    while True:
        subprocess.run([sys.executable, "-u", os.path.join(BASE, "refresh_dashboard.py")], cwd=BASE)
        time.sleep(IV * 60)


threading.Thread(target=loop, daemon=True).start()
print(f"PAPER dashboard: http://127.0.0.1:{PORT}/dashboard.html  (re-marks every {IV}m; Ctrl+C to stop)")
h = functools.partial(http.server.SimpleHTTPRequestHandler, directory=BASE)
http.server.ThreadingHTTPServer(("127.0.0.1", PORT), h).serve_forever()
