fix: parse JSONB string from asyncpg + frontend parseVal fallback
This commit is contained in:
parent
7ca843ca70
commit
2e969f68b4
@ -449,7 +449,14 @@ async def get_market_indicators(user: dict = Depends(get_current_user)):
|
|||||||
ind_type,
|
ind_type,
|
||||||
)
|
)
|
||||||
if row:
|
if row:
|
||||||
indicators[ind_type] = {"value": row["value"], "ts": row["timestamp_ms"]}
|
val = row["value"]
|
||||||
|
if isinstance(val, str):
|
||||||
|
import json as _json
|
||||||
|
try:
|
||||||
|
val = _json.loads(val)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
indicators[ind_type] = {"value": val, "ts": row["timestamp_ms"]}
|
||||||
result[sym.replace("USDT", "")] = indicators
|
result[sym.replace("USDT", "")] = indicators
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|||||||
@ -115,11 +115,18 @@ function MarketIndicatorsCards({ symbol }: { symbol: Symbol }) {
|
|||||||
|
|
||||||
if (!data) return <div className="text-center text-slate-400 text-sm py-3">等待市场指标数据...</div>;
|
if (!data) return <div className="text-center text-slate-400 text-sm py-3">等待市场指标数据...</div>;
|
||||||
|
|
||||||
// value是JSONB对象,需要取具体字段
|
// value可能是JSON字符串或对象,统一解析
|
||||||
const lsVal = data.long_short_ratio?.value as Record<string, string> | undefined;
|
const parseVal = (v: unknown): Record<string, unknown> => {
|
||||||
const topVal = data.top_trader_position?.value as Record<string, string> | undefined;
|
if (!v) return {};
|
||||||
const oiVal = data.open_interest_hist?.value as Record<string, string> | undefined;
|
if (typeof v === "string") { try { return JSON.parse(v); } catch { return {}; } }
|
||||||
const premVal = data.coinbase_premium?.value as Record<string, unknown> | undefined;
|
if (typeof v === "object") return v as Record<string, unknown>;
|
||||||
|
return {};
|
||||||
|
};
|
||||||
|
|
||||||
|
const lsVal = parseVal(data.long_short_ratio?.value);
|
||||||
|
const topVal = parseVal(data.top_trader_position?.value);
|
||||||
|
const oiVal = parseVal(data.open_interest_hist?.value);
|
||||||
|
const premVal = parseVal(data.coinbase_premium?.value);
|
||||||
|
|
||||||
const longPct = Number(lsVal?.longAccount ?? 0.5) * 100;
|
const longPct = Number(lsVal?.longAccount ?? 0.5) * 100;
|
||||||
const shortPct = Number(lsVal?.shortAccount ?? 0.5) * 100;
|
const shortPct = Number(lsVal?.shortAccount ?? 0.5) * 100;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user