diff --git a/app/db.py b/app/db.py index 5f23f9b..7a0ce1e 100644 --- a/app/db.py +++ b/app/db.py @@ -251,32 +251,27 @@ def list_trades( conn: sqlite3.Connection, limit: int = 200, account_id: Optional[int] = None, + machine_id: Optional[int] = None, ) -> list[dict]: - if account_id is None: - cur = conn.execute( - """ - SELECT t.*, a.name AS account_name, m.name AS machine_name - FROM trades t - LEFT JOIN accounts a ON a.id = t.account_id - LEFT JOIN machines m ON m.id = t.machine_id - ORDER BY COALESCE(t.close_time, t.open_time, t.created_at) DESC - LIMIT ? - """, - (limit,), - ) - else: - cur = conn.execute( - """ - SELECT t.*, a.name AS account_name, m.name AS machine_name - FROM trades t - LEFT JOIN accounts a ON a.id = t.account_id - LEFT JOIN machines m ON m.id = t.machine_id - WHERE t.account_id=? - ORDER BY COALESCE(t.close_time, t.open_time, t.created_at) DESC - LIMIT ? - """, - (account_id, limit), - ) + where: list[str] = [] + params: list[Any] = [] + if account_id is not None: + where.append("t.account_id=?") + params.append(account_id) + if machine_id is not None: + where.append("t.machine_id=?") + params.append(machine_id) + sql = """ + SELECT t.*, a.name AS account_name, m.name AS machine_name + FROM trades t + LEFT JOIN accounts a ON a.id = t.account_id + LEFT JOIN machines m ON m.id = t.machine_id + """ + if where: + sql += " WHERE " + " AND ".join(where) + sql += " ORDER BY COALESCE(t.close_time, t.open_time, t.created_at) DESC LIMIT ?" + params.append(limit) + cur = conn.execute(sql, tuple(params)) rows = [] for r in cur.fetchall(): d = row_to_dict(r) diff --git a/app/routes.py b/app/routes.py index 34cfab1..aceb357 100644 --- a/app/routes.py +++ b/app/routes.py @@ -594,12 +594,25 @@ def register_routes(app: Flask) -> None: @login_required def records_page(): u = current_user() + machine_raw = (request.args.get("machine_id") or "").strip() + machine_id = int(machine_raw) if machine_raw.isdigit() else None with database.get_db() as conn: + machines = database.list_machines_for_user(conn, u) + allowed_ids = {int(m["id"]) for m in machines} + if machine_id is not None and machine_id not in allowed_ids: + machine_id = None if u["role"] == "admin": - trades = database.list_trades(conn, 300) + trades = database.list_trades(conn, 300, machine_id=machine_id) else: - trades = database.list_trades(conn, 300, account_id=u.get("account_id")) - return render_template("records.html", trades=trades) + trades = database.list_trades( + conn, 300, account_id=u.get("account_id"), machine_id=machine_id + ) + return render_template( + "records.html", + trades=trades, + machines=machines, + selected_machine_id=machine_id, + ) @app.get("/records/") @login_required diff --git a/app/templates/records.html b/app/templates/records.html index 805d228..e5874ea 100644 --- a/app/templates/records.html +++ b/app/templates/records.html @@ -2,7 +2,27 @@ {% block title %}交易复盘 · {{ app_name }}{% endblock %} {% block content %}

交易记录与复盘

-

参考 crypto_monitor 复盘流程:列表筛选 → 点开详情 → 四周期 K 线 + 入场形式标注。

+

列表可按机器筛选 → 点开详情 → 四周期 K 线 + 入场形式标注。

+ +
+
+ + + + {% if selected_machine_id %} + 清除 + {% endif %} + 共 {{ trades|length }} 条 +
+
+
@@ -29,7 +49,7 @@ {% else %} - + {% endfor %}
打开复盘
暂无数据。客户端上报成交后出现在此。
暂无数据。{% if selected_machine_id %}当前机器下没有成交。{% else %}客户端上报成交后出现在此。{% endif %}