Fix position SSE JSON serialization for datetime fields.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-07-01 13:27:46 +08:00
parent 95156ca595
commit 5aba31f530
2 changed files with 12 additions and 5 deletions
+11 -4
View File
@@ -759,10 +759,17 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
has_commission = True
return round(total, 2) if has_commission else None
def _time_str(val) -> str:
if val is None:
return ""
if isinstance(val, str):
return val.strip()
return str(val).strip()
def _holding_duration(open_time: str, now_iso: str) -> str:
try:
from app import calc_holding_duration
open_s = (open_time or "").strip().replace("T", " ")[:19]
open_s = _time_str(open_time).replace("T", " ")[:19]
now_s = (now_iso or "").strip().replace("T", " ")[:19]
if not open_s or not now_s:
return ""
@@ -1362,7 +1369,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
lots = int(mon.get("lots") or 0)
entry = float(mon.get("entry_price") or 0)
source_label = monitor_source_label(mon.get("monitor_type"))
open_time = (mon.get("open_time") or "").strip()
open_time = _time_str(mon.get("open_time"))
open_time_source = "order"
margin = mon.get("margin")
position_pct = mon.get("position_pct")
@@ -1376,7 +1383,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
lots = int(ctp.get("lots") or 0)
entry = float(ctp.get("avg_price") or 0)
source_label = "CTP 柜台"
open_time = (ctp.get("open_time") or "").strip()
open_time = _time_str(ctp.get("open_time"))
open_time_source = "ctp"
margin = None
position_pct = None
@@ -1571,7 +1578,7 @@ def install_trading(app, *, login_required, require_nav, get_db, get_setting, se
pos_metrics = calc_position_metrics(
direction, order_price, sl or order_price, tp or order_price, lots, order_price, capital, sym,
)
open_time = (mon.get("open_time") or "").strip()
open_time = _time_str(mon.get("open_time"))
timeout_sec = get_pending_order_timeout_sec(get_setting)
remain = pending_auto_cancel_remaining(mon, timeout_sec=timeout_sec)
return {