Dashboard: show online machines and live trade feed with polling.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+69
-13
@@ -179,28 +179,84 @@ def register_routes(app: Flask) -> None:
|
||||
flash(str(exc), "error")
|
||||
return redirect(url_for("users_page"))
|
||||
|
||||
@app.get("/")
|
||||
@login_required
|
||||
def dashboard():
|
||||
u = current_user()
|
||||
def _fmt_ts(value) -> str:
|
||||
if value is None or value == "":
|
||||
return "—"
|
||||
try:
|
||||
return datetime.fromtimestamp(float(value)).strftime("%Y-%m-%d %H:%M:%S")
|
||||
except (TypeError, ValueError, OSError):
|
||||
return str(value)
|
||||
|
||||
def _dashboard_payload(u: dict) -> dict:
|
||||
with database.get_db() as conn:
|
||||
if u["role"] == "admin":
|
||||
accounts = database.list_accounts(conn)
|
||||
machines = database.list_machines(conn)
|
||||
trades = database.list_trades(conn, 20)
|
||||
trades = database.list_trades(conn, 40)
|
||||
else:
|
||||
accounts = [
|
||||
a
|
||||
for a in database.list_accounts(conn)
|
||||
if a["id"] == u.get("account_id")
|
||||
]
|
||||
machines = database.list_machines(conn)
|
||||
trades = database.list_trades(conn, 20, account_id=u.get("account_id"))
|
||||
return render_template(
|
||||
"dashboard.html",
|
||||
accounts=accounts,
|
||||
machines=machines,
|
||||
trades=trades,
|
||||
trades = database.list_trades(conn, 40, account_id=u.get("account_id"))
|
||||
machines = database.list_machines_for_user(conn, u)
|
||||
online_machines = [m for m in machines if m.get("online")]
|
||||
for m in machines:
|
||||
m["last_seen_fmt"] = _fmt_ts(m.get("last_seen_at"))
|
||||
for t in trades:
|
||||
t["time_fmt"] = _fmt_ts(
|
||||
t.get("close_time") or t.get("open_time") or t.get("created_at")
|
||||
)
|
||||
return {
|
||||
"accounts": accounts,
|
||||
"machines": machines,
|
||||
"online_machines": online_machines,
|
||||
"online_count": len(online_machines),
|
||||
"machine_count": len(machines),
|
||||
"trades": trades,
|
||||
}
|
||||
|
||||
@app.get("/")
|
||||
@login_required
|
||||
def dashboard():
|
||||
data = _dashboard_payload(current_user())
|
||||
return render_template("dashboard.html", **data)
|
||||
|
||||
@app.get("/api/dashboard/live")
|
||||
@login_required
|
||||
def dashboard_live():
|
||||
"""总览轮询:在线机器 + 实时成交。"""
|
||||
data = _dashboard_payload(current_user())
|
||||
return jsonify(
|
||||
{
|
||||
"ok": True,
|
||||
"online_count": data["online_count"],
|
||||
"machine_count": data["machine_count"],
|
||||
"online_machines": [
|
||||
{
|
||||
"id": m["id"],
|
||||
"name": m.get("name") or f"#{m['id']}",
|
||||
"last_seen_at": m.get("last_seen_at"),
|
||||
"last_seen_fmt": _fmt_ts(m.get("last_seen_at")),
|
||||
"online": True,
|
||||
}
|
||||
for m in data["online_machines"]
|
||||
],
|
||||
"trades": [
|
||||
{
|
||||
"id": t["id"],
|
||||
"time_fmt": t.get("time_fmt"),
|
||||
"symbol": t.get("symbol"),
|
||||
"side": t.get("side"),
|
||||
"volume": t.get("volume"),
|
||||
"profit": t.get("profit"),
|
||||
"account_name": t.get("account_name"),
|
||||
"machine_name": t.get("machine_name"),
|
||||
"ticket": t.get("ticket"),
|
||||
}
|
||||
for t in data["trades"]
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
# ---------- 账户(仅管理员创建)----------
|
||||
|
||||
Reference in New Issue
Block a user