Dashboard: show online machines and live trade feed with polling.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-17 20:35:16 +08:00
parent 44c54d6f80
commit 679b55f1db
3 changed files with 214 additions and 28 deletions
+33
View File
@@ -141,6 +141,39 @@ def list_machines(conn: sqlite3.Connection) -> list[dict]:
return [row_to_dict(r) for r in cur.fetchall()]
ONLINE_SECONDS = 180 # 与下载中心一致:3 分钟内心跳视为在线
def list_machines_for_user(conn: sqlite3.Connection, user: dict) -> list[dict]:
if user.get("role") == "admin":
items = list_machines(conn)
else:
cur = conn.execute(
"""
SELECT DISTINCT m.* FROM machines m
JOIN activations act ON act.machine_id = m.id
WHERE act.account_id=?
ORDER BY m.id DESC
""",
(user.get("account_id"),),
)
items = [row_to_dict(r) for r in cur.fetchall()]
return annotate_machines_online(items)
def annotate_machines_online(machines: list[dict], now_ts: Optional[float] = None) -> list[dict]:
ts = now_ts if now_ts is not None else now()
for m in machines:
last = m.get("last_seen_at")
try:
online = bool(last and (ts - float(last)) <= ONLINE_SECONDS)
except (TypeError, ValueError):
online = False
m["online"] = online
m["online_label"] = "在线" if online else "离线"
return machines
def create_activation(
conn: sqlite3.Connection,
account_id: int,