新增实例数据统计页:复盘 KPI 上、资金曲线下,系统设置可关。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
"""实例数据统计:复盘 KPI + 资金曲线 API,以及后台现金采样."""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Callable
|
||||
|
||||
from flask import Flask, jsonify, request
|
||||
|
||||
|
||||
def install_instance_data_stats(app: Flask, app_module: Any) -> None:
|
||||
get_db: Callable = app_module.get_db
|
||||
login_required = app_module.login_required
|
||||
|
||||
def _fetch_balances() -> dict[str, Any]:
|
||||
ex = getattr(app_module, "exchange_options", None)
|
||||
if ex is None:
|
||||
return {}
|
||||
from lib.exchange.okx_options_lib import fetch_options_balances
|
||||
|
||||
return fetch_options_balances(ex, force=False) or {}
|
||||
|
||||
@app.route("/api/instance/cash_curve")
|
||||
@login_required
|
||||
def api_instance_cash_curve():
|
||||
from lib.instance.instance_cash_sample_lib import (
|
||||
query_cash_series_with_db,
|
||||
sample_once,
|
||||
)
|
||||
|
||||
# 打开页面时尽量补一笔最新点(失败忽略)
|
||||
try:
|
||||
sample_once(get_db, _fetch_balances)
|
||||
except Exception:
|
||||
pass
|
||||
range_key = (request.args.get("range") or "7d").strip()
|
||||
conn = get_db()
|
||||
try:
|
||||
return jsonify(query_cash_series_with_db(conn, range_key=range_key))
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "msg": f"加载资金曲线失败: {e}"}), 500
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# 后台采样(daemon);无密钥时会空转跳过
|
||||
try:
|
||||
from lib.instance.instance_cash_sample_lib import start_cash_sample_loop
|
||||
|
||||
start_cash_sample_loop(get_db, _fetch_balances)
|
||||
except Exception as e:
|
||||
print(f"[instance_cash_sample] start skipped: {e}", flush=True)
|
||||
Reference in New Issue
Block a user