Add clear-and-refetch for hub K-line cache.

Force refresh wipes the series in hub_kline.db before pulling from the exchange; add a Linux clear script and rename the UI button to 清库重拉.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-08 11:31:16 +08:00
parent 2095839fc3
commit ca6ef59a14
6 changed files with 197 additions and 2 deletions
+47
View File
@@ -196,6 +196,47 @@ def purge_1m_bar_cap(db_path: Path | None = None, *, max_bars: int | None = None
conn.close()
def clear_series_bars(
exchange_key: str,
symbol: str,
timeframe: str | None = None,
db_path: Path | None = None,
) -> int:
"""删除某交易所+币种 K 线(可指定周期);用于清库后全量重拉。"""
init_db(db_path)
ex_k = (exchange_key or "").strip().lower()
sym = (symbol or "").strip().upper()
if not ex_k or not sym:
return 0
conn = _connect(db_path)
try:
if timeframe:
tf = normalize_chart_timeframe(timeframe)
cur = conn.execute(
"DELETE FROM ohlcv_bars WHERE exchange_key=? AND symbol=? AND timeframe=?",
(ex_k, sym, tf),
)
else:
cur = conn.execute(
"DELETE FROM ohlcv_bars WHERE exchange_key=? AND symbol=?",
(ex_k, sym),
)
return int(cur.rowcount or 0)
finally:
conn.close()
def clear_all_bars(db_path: Path | None = None) -> int:
"""清空 hub K 线库全部 OHLCV 行。"""
init_db(db_path)
conn = _connect(db_path)
try:
cur = conn.execute("DELETE FROM ohlcv_bars")
return int(cur.rowcount or 0)
finally:
conn.close()
def purge_retention(db_path: Path | None = None) -> int:
"""按周期策略清理:5m/15m/1h/2h/4h 一年;1m 保留最近 N 根;1d/1w 不删。"""
n = 0
@@ -479,6 +520,7 @@ def resolve_chart_bars(
db_path: Path | None = None,
force_refresh: bool = False,
tail_refresh: bool = False,
clear_db: bool = False,
limit: int | None = None,
before_ms: int | None = None,
) -> dict[str, Any]:
@@ -488,6 +530,7 @@ def resolve_chart_bars(
"""
init_db(db_path)
purged = purge_retention(db_path)
cleared = 0
sym = (symbol or "").strip().upper()
ex_k = (exchange_key or "").strip().lower()
@@ -510,6 +553,9 @@ def resolve_chart_bars(
need = min(need, 30)
cutoff = history_cutoff_ms_for_storage(storage_tf, now_ms)
if clear_db and not is_history and not tail_refresh:
cleared = clear_series_bars(ex_k, sym, storage_tf, db_path)
def load_display_rows() -> list[dict[str, Any]]:
if is_history:
rows = load_bars_before(ex_k, sym, storage_tf, int(before_ms), need, db_path)
@@ -706,6 +752,7 @@ def resolve_chart_bars(
"candles": candles,
"from_cache": from_cache,
"fetched": fetched,
"cleared": cleared,
"purged": purged,
"price_tick": price_tick,
"stale": bool(remote_err),