77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
|
|
#!/usr/bin/env python3
|
|||
|
|
"""既有研究单表只读守卫的本地副本,供fetch.py拼接remote.py。
|
|||
|
|
|
|||
|
|
在155一次性研究容器从stdin运行,不调用业务服务。
|
|||
|
|
凭证仅从容器环境取得;异常只输出类型和数值错误码。
|
|||
|
|
"""
|
|||
|
|
import datetime as dt
|
|||
|
|
import decimal
|
|||
|
|
import json
|
|||
|
|
import re
|
|||
|
|
import signal
|
|||
|
|
import sys
|
|||
|
|
import time
|
|||
|
|
|
|||
|
|
import pymysql
|
|||
|
|
from sqlalchemy.engine import make_url
|
|||
|
|
from config.settings import settings
|
|||
|
|
from app.db.session import assert_single_table
|
|||
|
|
|
|||
|
|
TABLES = {
|
|||
|
|
"proxy": ["zs_day_data", "intraday_index_qrs", "gp_day_data", "gp_day_basic",
|
|||
|
|
"gp_fina_indicator", "gp_stock_basic", "gp_stock_st", "gp_index_weight",
|
|||
|
|
"gp_adj_factor", "gp_trade_cal", "gp_hybk", "gp_fx_daily", "gp_shibor"],
|
|||
|
|
"index": ["zs_day_data", "gp_day_data", "gp_hybk"],
|
|||
|
|
}
|
|||
|
|
INDEXES = ["000300.SH", "000905.SH", "000852.SH", "399006.SZ", "000688.SH", "000001.SH"]
|
|||
|
|
CUTOFF = "2026-10-23 23:59:59"
|
|||
|
|
START = "2005-01-01"
|
|||
|
|
CONNECTIONS = {}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def safe_error(exc):
|
|||
|
|
args = getattr(exc, "args", ())
|
|||
|
|
return {"type": type(exc).__name__, "code": args[0] if args and isinstance(args[0], int) else None}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def encode(value):
|
|||
|
|
if isinstance(value, (dt.datetime, dt.date)):
|
|||
|
|
return value.isoformat()
|
|||
|
|
if isinstance(value, decimal.Decimal):
|
|||
|
|
return float(value)
|
|||
|
|
raise TypeError(type(value).__name__)
|
|||
|
|
|
|||
|
|
|
|||
|
|
def connection(source):
|
|||
|
|
if source not in CONNECTIONS:
|
|||
|
|
raw = settings.PROXY_DB_URL if source == "proxy" else settings.DB_MYSQL_URL
|
|||
|
|
url = make_url(raw)
|
|||
|
|
if url.get_backend_name() != "mysql":
|
|||
|
|
raise ValueError("仅支持预定的MySQL源")
|
|||
|
|
CONNECTIONS[source] = pymysql.connect(
|
|||
|
|
host=url.host, port=url.port or 3306, user=url.username,
|
|||
|
|
password=url.password, database=url.database, charset="utf8mb4",
|
|||
|
|
connect_timeout=5, read_timeout=20, write_timeout=5, autocommit=True,
|
|||
|
|
cursorclass=pymysql.cursors.DictCursor,
|
|||
|
|
)
|
|||
|
|
return CONNECTIONS[source]
|
|||
|
|
|
|||
|
|
|
|||
|
|
def read(source, sql, params=None, cap=10000):
|
|||
|
|
if not re.match(r"^(SELECT|SHOW)\s", sql, re.I) or ";" in sql:
|
|||
|
|
raise ValueError("只允许单条SELECT或SHOW")
|
|||
|
|
if re.search(r"\b(INTO|OUTFILE|DUMPFILE|FOR\s+UPDATE|LOCK|SLEEP|BENCHMARK)\b", sql, re.I):
|
|||
|
|
raise ValueError("不允许具有副作用的读取")
|
|||
|
|
assert_single_table(sql)
|
|||
|
|
begin = time.monotonic()
|
|||
|
|
try:
|
|||
|
|
with connection(source).cursor() as cur:
|
|||
|
|
cur.execute(sql, params)
|
|||
|
|
rows = cur.fetchmany(cap + 1)
|
|||
|
|
if len(rows) > cap:
|
|||
|
|
raise ValueError("读取超过固定行数上限")
|
|||
|
|
return {"ok": True, "rows": rows, "seconds": round(time.monotonic() - begin, 3)}
|
|||
|
|
except Exception as exc:
|
|||
|
|
# 错误信息可能包含连接串,禁止直接输出str(exc)。
|
|||
|
|
return {"ok": False, "error": safe_error(exc), "seconds": round(time.monotonic() - begin, 3)}
|