refactor: route market providers through data gateway
This commit is contained in:
@@ -180,7 +180,9 @@ class DashboardService:
|
||||
self._system_credentials,
|
||||
MENTOR_SKILLS_DIR,
|
||||
PRIVATE_MENTOR_SKILLS_DIR,
|
||||
lambda: self.token,
|
||||
)
|
||||
self.data_gateway = self.container.data_gateway
|
||||
self.ifind = self.container.ifind
|
||||
self.screener = self.container.screener
|
||||
self.strategy_tracking = self.container.strategy_tracking
|
||||
@@ -198,6 +200,13 @@ class DashboardService:
|
||||
)
|
||||
self._background_thread.start()
|
||||
|
||||
def _tushare_client(self) -> TushareClient:
|
||||
gateway = getattr(self, "data_gateway", None)
|
||||
if gateway is not None:
|
||||
return gateway.tushare()
|
||||
# Compatibility for isolated legacy unit-test service stubs.
|
||||
return TushareClient(self.token)
|
||||
|
||||
def _load_system_credentials(self, environment: dict[str, str]) -> dict[str, Any]:
|
||||
encrypted = self.database.get_system_setting("credentials")
|
||||
current = self.vault.decrypt_json(encrypted) if encrypted else {}
|
||||
@@ -1047,7 +1056,7 @@ class DashboardService:
|
||||
try:
|
||||
if not self.configured:
|
||||
raise TushareError("公共行情尚未配置")
|
||||
dashboard = TushareClient(self.token).dashboard(normalized_date)
|
||||
dashboard = self._tushare_client().dashboard(normalized_date)
|
||||
|
||||
dashboard["meta"]["source"] = source
|
||||
dashboard["meta"]["requested_date"] = self._display_compact_date(normalized_date)
|
||||
@@ -1207,7 +1216,7 @@ class DashboardService:
|
||||
ts_code = f"{raw_code}.SH"
|
||||
else:
|
||||
ts_code = f"{raw_code}.SZ"
|
||||
client = TushareClient(self.token)
|
||||
client = self._tushare_client()
|
||||
try:
|
||||
industry = client.sw_stock_industry(ts_code, actual_date)
|
||||
sector_code = str(industry.get("l2_code") or "")
|
||||
@@ -1295,7 +1304,7 @@ class DashboardService:
|
||||
raise ValueError("行情数据尚未配置。")
|
||||
return MarketInsightsService(
|
||||
self.database,
|
||||
TushareClient(self.token),
|
||||
self._tushare_client(),
|
||||
ifind=self.ifind,
|
||||
)
|
||||
|
||||
@@ -1461,7 +1470,7 @@ class DashboardService:
|
||||
notice = ""
|
||||
if self.configured:
|
||||
try:
|
||||
FactorDataService(self.database, TushareClient(self.token)).sync(
|
||||
FactorDataService(self.database, self._tushare_client()).sync(
|
||||
normalized_date, 15
|
||||
)
|
||||
except TushareError:
|
||||
@@ -1513,7 +1522,7 @@ class DashboardService:
|
||||
resolved_date = normalized_date
|
||||
if self.configured:
|
||||
try:
|
||||
client = TushareClient(self.token)
|
||||
client = self._tushare_client()
|
||||
resolved_date, _ = client.resolve_trade_context(normalized_date)
|
||||
history = self.database.watchlist_price_history(
|
||||
[str(item["code"]) for item in items], resolved_date
|
||||
@@ -1703,7 +1712,7 @@ class DashboardService:
|
||||
normalized_date = normalize_date(trade_date)
|
||||
lookback = max(25, min(260, int(lookback)))
|
||||
with self.sync_lock:
|
||||
return FactorDataService(self.database, TushareClient(self.token)).sync(
|
||||
return FactorDataService(self.database, self._tushare_client()).sync(
|
||||
normalized_date, lookback
|
||||
)
|
||||
|
||||
@@ -1759,7 +1768,7 @@ class DashboardService:
|
||||
)
|
||||
try:
|
||||
factor_sync = FactorDataService(
|
||||
self.database, TushareClient(self.token)
|
||||
self.database, self._tushare_client()
|
||||
).sync(normalized_date, 260)
|
||||
factor_dates = self.database.factor_dates(normalized_date, 300)
|
||||
if not factor_dates or factor_dates[-1] != normalized_date:
|
||||
@@ -2511,7 +2520,7 @@ class DashboardService:
|
||||
exact = [item for item in candidates if str(item.get("name") or "").casefold() == raw.casefold()]
|
||||
if not exact and self.configured:
|
||||
try:
|
||||
rows = TushareClient(self.token).query(
|
||||
rows = self._tushare_client().query(
|
||||
"stock_basic",
|
||||
{"name": raw, "list_status": "L"},
|
||||
"ts_code,symbol,name,industry,market,list_date",
|
||||
@@ -2730,7 +2739,7 @@ class DashboardService:
|
||||
if market_mode == "intraday":
|
||||
if self.configured:
|
||||
try:
|
||||
quote = TushareClient(self.token).realtime_stock_quote(
|
||||
quote = self._tushare_client().realtime_stock_quote(
|
||||
tushare_code(stock_code),
|
||||
trade_date,
|
||||
)
|
||||
@@ -3217,7 +3226,7 @@ class DashboardService:
|
||||
error = "Tushare Token 未配置"
|
||||
else:
|
||||
try:
|
||||
client = TushareClient(self.token)
|
||||
client = self._tushare_client()
|
||||
if market_mode == "intraday":
|
||||
payload = self._aggregate_index_context(trade_date)
|
||||
payload["schema_version"] = 3
|
||||
@@ -3277,7 +3286,7 @@ class DashboardService:
|
||||
"399001": "399001.SZ",
|
||||
"399006": "399006.SZ",
|
||||
}
|
||||
client = TushareClient(self.token)
|
||||
client = self._tushare_client()
|
||||
indices = []
|
||||
start_date = (
|
||||
datetime.strptime(trade_date, "%Y%m%d") - timedelta(days=20)
|
||||
@@ -3369,7 +3378,7 @@ class DashboardService:
|
||||
if not self.configured:
|
||||
return None
|
||||
try:
|
||||
payload = TushareClient(self.token).sw_sector_snapshot(
|
||||
payload = self._tushare_client().sw_sector_snapshot(
|
||||
tushare_code(identifier),
|
||||
trade_date,
|
||||
realtime_expected=market_mode == "intraday",
|
||||
@@ -3696,7 +3705,7 @@ class DashboardService:
|
||||
dashboard = self.get_dashboard(trade_date)
|
||||
if self.configured and dashboard.get("meta", {}).get("realtime"):
|
||||
try:
|
||||
realtime_snapshot = TushareClient(self.token).realtime_factor_snapshot(trade_date)
|
||||
realtime_snapshot = self._tushare_client().realtime_factor_snapshot(trade_date)
|
||||
except TushareError as exc:
|
||||
raise ValueError(f"实时选股行情不可用,已停止筛选:{exc}") from exc
|
||||
result = self.screener.screen(
|
||||
@@ -3716,7 +3725,7 @@ class DashboardService:
|
||||
return cached
|
||||
if self.configured:
|
||||
try:
|
||||
payload = TushareClient(self.token).hot_money_profiles()
|
||||
payload = self._tushare_client().hot_money_profiles()
|
||||
except TushareError:
|
||||
if cached:
|
||||
cached["meta"] = {
|
||||
@@ -3781,7 +3790,7 @@ class DashboardService:
|
||||
return cached
|
||||
if self.configured:
|
||||
try:
|
||||
payload = TushareClient(self.token).dragon_tiger(normalized_date)
|
||||
payload = self._tushare_client().dragon_tiger(normalized_date)
|
||||
except TushareError as exc:
|
||||
return {
|
||||
"meta": {
|
||||
@@ -3845,7 +3854,7 @@ class DashboardService:
|
||||
return cached_items
|
||||
|
||||
try:
|
||||
rows = TushareClient(self.token).query(
|
||||
rows = self._tushare_client().query(
|
||||
"ths_index",
|
||||
{},
|
||||
"ts_code,name,count,exchange,list_date,type",
|
||||
@@ -4023,7 +4032,7 @@ class DashboardService:
|
||||
def _ths_search_detail(
|
||||
self, basic: dict[str, Any], trade_date: str
|
||||
) -> dict[str, Any]:
|
||||
client = TushareClient(self.token)
|
||||
client = self._tushare_client()
|
||||
resolved_date, _ = client.resolve_trade_context(trade_date)
|
||||
end = datetime.strptime(resolved_date, "%Y%m%d")
|
||||
start_date = (end - timedelta(days=190)).strftime("%Y%m%d")
|
||||
@@ -4112,7 +4121,7 @@ class DashboardService:
|
||||
def _index_search_detail(
|
||||
self, basic: dict[str, Any], trade_date: str
|
||||
) -> dict[str, Any]:
|
||||
client = TushareClient(self.token)
|
||||
client = self._tushare_client()
|
||||
resolved_date, _ = client.resolve_trade_context(trade_date)
|
||||
payload = (
|
||||
client.realtime_market_indices(resolved_date)
|
||||
@@ -4200,7 +4209,7 @@ class DashboardService:
|
||||
source = "tushare"
|
||||
if self.configured:
|
||||
try:
|
||||
payload = TushareClient(self.token).stock_detail(
|
||||
payload = self._tushare_client().stock_detail(
|
||||
tushare_code(code), normalized_date
|
||||
)
|
||||
if not payload.get("prices"):
|
||||
@@ -4280,7 +4289,7 @@ class DashboardService:
|
||||
if quote and self._valid_realtime_stock_quote(quote, today):
|
||||
self._merge_realtime_stock_detail(result, quote, requested_date)
|
||||
elif self.configured and actual_date < today:
|
||||
client = TushareClient(self.token)
|
||||
client = self._tushare_client()
|
||||
try:
|
||||
resolved_date, _ = client.resolve_trade_context(requested_date)
|
||||
if resolved_date == today:
|
||||
|
||||
Reference in New Issue
Block a user