数据统计页:多机 KPI 对比与余额曲线采样。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -209,6 +209,66 @@ def pull_all_enabled() -> list[dict[str, Any]]:
|
||||
return results
|
||||
|
||||
|
||||
def fetch_one_stats(inst: dict[str, Any], *, window: str = "7d") -> dict[str, Any]:
|
||||
"""拉取单机窗口化期权统计."""
|
||||
base = (inst.get("base_url") or "").rstrip("/")
|
||||
token = (inst.get("bridge_token") or "").strip()
|
||||
out: dict[str, Any] = {
|
||||
"id": inst.get("id"),
|
||||
"name": inst.get("name"),
|
||||
"online": False,
|
||||
"error": "",
|
||||
"stats": None,
|
||||
}
|
||||
if not base or not token:
|
||||
out["error"] = "缺少 URL 或 Token"
|
||||
return out
|
||||
url = f"{base}/api/hub/options/stats"
|
||||
try:
|
||||
r = requests.get(
|
||||
url,
|
||||
headers={"X-Hub-Token": token, "Accept": "application/json"},
|
||||
params={"window": window},
|
||||
timeout=max(_timeout(), 15.0),
|
||||
)
|
||||
data = {}
|
||||
try:
|
||||
data = r.json()
|
||||
except Exception:
|
||||
data = {}
|
||||
if r.status_code != 200 or not isinstance(data, dict) or not data.get("ok"):
|
||||
out["error"] = (data.get("msg") if isinstance(data, dict) else None) or f"HTTP {r.status_code}"
|
||||
out["stats"] = data if isinstance(data, dict) else None
|
||||
return out
|
||||
out["online"] = True
|
||||
out["stats"] = data
|
||||
return out
|
||||
except requests.Timeout:
|
||||
out["error"] = "超时"
|
||||
return out
|
||||
except Exception as e:
|
||||
out["error"] = str(e) or "拉取失败"
|
||||
return out
|
||||
|
||||
|
||||
def pull_all_stats(window: str = "7d") -> list[dict[str, Any]]:
|
||||
instances = list_instances(enabled_only=True)
|
||||
if not instances:
|
||||
return []
|
||||
results: list[dict[str, Any]] = []
|
||||
with ThreadPoolExecutor(max_workers=min(8, len(instances))) as pool:
|
||||
futs = {
|
||||
pool.submit(fetch_one_stats, inst, window=window): inst["id"] for inst in instances
|
||||
}
|
||||
by_id: dict[Any, dict[str, Any]] = {}
|
||||
for fut in as_completed(futs):
|
||||
row = fut.result()
|
||||
by_id[row.get("id")] = row
|
||||
for inst in instances:
|
||||
results.append(by_id.get(inst["id"]) or fetch_one_stats(inst, window=window))
|
||||
return results
|
||||
|
||||
|
||||
def dispatch_cond_order_to_ids(instance_ids: list[int], payload: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
results = []
|
||||
for iid in instance_ids:
|
||||
|
||||
Reference in New Issue
Block a user