每日计划:冻结输入并发布完整版本,贯通排序与复盘(台账 064)
一、修复同版不一致。固定输入与参数装配,按内容指纹追加发布;查询只读已发布版本,盘中修订仅更新评析和区制。 二、修复确定性与历史边界。同强度证据稳定排序,无主题不共用限额;复盘分别读取首版和实际消费记录。合成输入离线回归 32 组通过。 三、部署约束。保留现有准入与运行阈值,新增五分钟修订入口及日检;首个新版晨版验证后才启用自动修订。
This commit is contained in:
parent
ca46a956ad
commit
5eac6f68eb
|
|
@ -190,7 +190,7 @@ docker compose exec akg-factor-bridge python run.py freeze --date 2026-07-24
|
|||
| `GET /health` | 存活探针;2026-09-03 起带 `pool_top`(POOL_TOP)与 `pool_max`(POOL_MAX),供 PMS 池深探针比对 |
|
||||
| `GET /plan/dates?limit=30` | 有计划文件的日期清单 |
|
||||
| `GET /plan?date=&format=json\|md&top=&obs_top=` | 当日选股计划(基座今日页 `/plan/today` 与 PMS 下单候选都读它)。顶层 `regime`(区制)与 `market`(两市成交额、广度、融资、恐贪四项)都只从当日快照读,快照缺失为空;每行的判决类字段 2026-09-03 起多 `basis`(判决依据)与 `logic`(因果论断带出处);2026-09-17 起名次改按质地带优先,每行多 `quality_band`(质地带:好 3 中 2 无评析 1 差 0)、`rank_axis`(quality/transmission)、`pointed`(是否被传导指向)、`rank_old`(旧序名次,供周报四周对照),开关 `PLAN_RANK_AXIS` 设 transmission 即逐字回旧 |
|
||||
| `POST /plan/refresh?date=` | 重生成当日计划文件 |
|
||||
| `POST /plan/refresh?date=&key=` | 使用既有 XXL key 排入完整修订;晨版缺失不代造 |
|
||||
| `GET /plan/verdict?codes=300750,SH600438&date=` | 逐票『计划判决』(2026-08-18 PMS 对接加):decision(main/observe/reject/absent)+ 与命令行 `plan_reconcile` 逐字同口径的 verdict_text + score/rank/tier/赛道/图谱证据。单票数据异常或代码形态认不出只报该票 error,不崩整批 |
|
||||
| `GET\|POST /api/v1/xxl/daily-build?key=&steps=&date=` | XXL-JOB 平台触发盘前链(build→plan→push-pool,白名单步骤、单实例互斥、回调结案);`.env` 不配 `XXL_TRIGGER_KEY` 则整组禁用 |
|
||||
| `GET /review/latest` | 最新一份复盘周报的只读读数(2026-09-16 加,配合数据基座判收读数包):文件名、日期、大小、前二十行、三个标记行计数(`公司质地=`、`五日头`、`二十日头`)。没有周报回 404。纯文件读取,不连库 |
|
||||
|
|
|
|||
167
api.py
167
api.py
|
|
@ -8,7 +8,7 @@ cron 的 docker exec 构建/出计划照旧,互不影响。局域网内部服
|
|||
GET /plan?date=2026-07-30 指定日期
|
||||
GET /plan?format=md Markdown 原文(浏览器直接可读)
|
||||
GET /plan/dates 可用日期列表
|
||||
POST /plan/refresh?date=... 重新生成该日计划文件(data/plan/*.md)
|
||||
POST /plan/refresh?date=...&key=... 按 XXL 鉴权排入计划修订,不在请求中重装配
|
||||
|
||||
统一任务调度平台(XXL-JOB)触发入口挂在 /api/v1/xxl/*(见 xxl.py,2026-08-03):
|
||||
盘前链(build → plan → push-pool)可由平台拉起并回调结案,.env 配 XXL_TRIGGER_KEY 才启用。
|
||||
|
|
@ -27,6 +27,7 @@ import sources
|
|||
import config
|
||||
import db
|
||||
import logic_state_daily
|
||||
import plan_versions
|
||||
import plan
|
||||
import plan_reconcile
|
||||
import regime
|
||||
|
|
@ -44,6 +45,9 @@ def health():
|
|||
"""存活探针。2026-09-03 起带 pool_top 与 pool_max:PMS 的池深探针拿它与自身的计划深度
|
||||
比较(池深不变式,台账 008),库连不上时也照样返回这两项。"""
|
||||
depth = {"pool_top": config.POOL_TOP, "pool_max": config.POOL_MAX}
|
||||
import plan_health
|
||||
plan_status, plan_detail = plan_health.inspect(config.PLAN_SNAPSHOT_DIR)
|
||||
depth.update(plan_status=plan_status, plan=plan_detail)
|
||||
try:
|
||||
d = plan._latest_date("t_factor_akg_score") # noqa: SLF001 —— 桥内自用
|
||||
except Exception as e: # noqa: BLE001 —— 库连不上也要能回答"我还活着"
|
||||
|
|
@ -53,13 +57,9 @@ def health():
|
|||
|
||||
@app.get("/plan/dates")
|
||||
def plan_dates(limit: int = 30):
|
||||
df = db.read_mysql(
|
||||
"factor", "SELECT DISTINCT trade_date FROM t_factor_akg_score "
|
||||
"ORDER BY trade_date DESC LIMIT %s", (int(limit),))
|
||||
if df.empty:
|
||||
return {"dates": []}
|
||||
return {"dates": [pd.Timestamp(x).date().isoformat()
|
||||
for x in df["trade_date"]]}
|
||||
from pathlib import Path
|
||||
days = sorted((p.stem[5:] for p in Path(config.PLAN_SNAPSHOT_DIR).glob('plan_????-??-??.json')), reverse=True)
|
||||
return {'dates': days[:max(0, min(int(limit), 3650))]}
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
|
@ -81,43 +81,28 @@ _PLAN_CACHE_LOCK = threading.Lock()
|
|||
PLAN_CACHE_SEC = int(os.environ.get("PLAN_CACHE_SEC", "43200"))
|
||||
# 2026-09-10(台账 057):接口优先读当日全量快照、按请求参数截取,不再每次现场装配。
|
||||
# 装配那三十三秒本来就不该在请求里花 —— 计划是日频的,出计划时已经落了一份全量底本。
|
||||
# 关掉这个开关就回到每次现算的老路(缓存仍在),是这条路出问题时的一键回退。
|
||||
# 保留旧配置名称;完整版本上线后不再允许查询回落现算。
|
||||
PLAN_FROM_SNAPSHOT = os.environ.get("PLAN_FROM_SNAPSHOT", "1").lower() not in ("0", "false", "no")
|
||||
_PLAN_CACHE_MAX = 8 # 不同参数组合最多留几份,防内存慢涨
|
||||
_PLAN_BUILD_LOCKS: dict = {} # 每个缓存键一把「正在算」的锁,见 _plan_cached 里的说明
|
||||
|
||||
|
||||
def _plan_build(date, top, obs_top, theme_cap) -> dict:
|
||||
"""出一份计划。优先读当日全量快照(毫秒级),读不到就现场装配(三十几秒)。
|
||||
|
||||
回落是自动的、无声的对下游而言,但**一定会记一行警告**说明为什么没走快照 ——
|
||||
如果快照那条路悄悄失效了,只会表现成"页面偶尔变慢",不记原因就查不出来。
|
||||
返回里的 plan_source 说明这一份是哪来的,snapshot 或 live。
|
||||
"""
|
||||
if PLAN_FROM_SNAPSHOT:
|
||||
try:
|
||||
return plan.from_snapshot(date, top, obs_top, theme_cap)
|
||||
except Exception as e: # noqa: BLE001 —— 任何原因读不到快照都回落,不能让接口挂掉
|
||||
_access.warning("plan 快照读取失败, 本次回落到实时装配 (date=%s): %s: %s",
|
||||
date or "最新", type(e).__name__, e)
|
||||
data = plan.collect(date, top, obs_top, theme_cap)
|
||||
data.pop("_full", None)
|
||||
data["plan_source"] = "live"
|
||||
return data
|
||||
def _plan_build(date, top, obs_top, theme_cap, decision_id=None) -> dict:
|
||||
"""只读已发布版本。缺档明确报错,禁止用当前研究重构历史。"""
|
||||
return plan.from_snapshot(date, top, obs_top, theme_cap, decision_id=decision_id)
|
||||
|
||||
|
||||
def _plan_cached(date, top, obs_top, theme_cap, nocache: bool):
|
||||
def _plan_cached(date, top, obs_top, theme_cap, nocache: bool, decision_id=None):
|
||||
"""返回 (data, 命中与否)。data 是 plan.collect 的结果(已去掉 _full)。
|
||||
|
||||
键里放**真实数据日**而不是请求里的 date:不传 date 时数据日随构建更新,明早换日后
|
||||
旧那一份必须立刻失效,不能等缓存到期。取最新数据日是一条毫秒级查询。"""
|
||||
ds_key = date
|
||||
if not ds_key:
|
||||
try:
|
||||
ds_key = plan._latest_date("t_factor_akg_score") # noqa: SLF001 —— 同仓自用
|
||||
except Exception: # noqa: BLE001 —— 取不到就退回按请求参数缓存
|
||||
ds_key = ""
|
||||
key = (ds_key or "", int(top), int(obs_top), int(theme_cap))
|
||||
import plan_versions as pv
|
||||
published = pv.load(config.PLAN_SNAPSHOT_DIR, date, decision_id)
|
||||
ds_key = published['date']
|
||||
pinned = (published.get('decision') or {}).get('id')
|
||||
state = pv.health(config.PLAN_SNAPSHOT_DIR, ds_key) if not decision_id else {}
|
||||
key = (pinned or pv.digest(published), pv.digest(state), int(top), int(obs_top), int(theme_cap))
|
||||
now = time.time()
|
||||
if not nocache and PLAN_CACHE_SEC > 0:
|
||||
with _PLAN_CACHE_LOCK:
|
||||
|
|
@ -141,7 +126,8 @@ def _plan_cached(date, top, obs_top, theme_cap, nocache: bool):
|
|||
hit = _PLAN_CACHE.get(key)
|
||||
if hit and time.time() - hit[0] < PLAN_CACHE_SEC:
|
||||
return hit[1], True
|
||||
data = _plan_build(date, top, obs_top, theme_cap)
|
||||
data = _plan_build(ds_key, top, obs_top, theme_cap, pinned)
|
||||
data['refresh_status'] = state
|
||||
if PLAN_CACHE_SEC > 0:
|
||||
with _PLAN_CACHE_LOCK:
|
||||
_PLAN_CACHE[key] = (time.time(), data)
|
||||
|
|
@ -152,73 +138,28 @@ def _plan_cached(date, top, obs_top, theme_cap, nocache: bool):
|
|||
return data, False
|
||||
|
||||
|
||||
def _attach_fresh_reviews(data: dict, ds: str) -> int:
|
||||
"""把个股深度评析按请求实时补一次,返回补了几只。
|
||||
|
||||
为什么非补不可。评析是全天在跑的(数据基座每小时一批),而计划一天只装配
|
||||
一次、这个接口又带十二小时缓存。于是装配那一刻还没出报告的票,当天就再也
|
||||
补不上了 —— 2026-09-10 实测:早上七点十分那份计划里 154 行只有 7 只带评析;
|
||||
中午十二点二十三到三十五分之间新跑出五份报告,而这五只正是持仓管理系统当天
|
||||
出提议的票,它当天一份都看不到,卡片上写的全是「数据基座还没出这家的报告」。
|
||||
|
||||
三层原因叠在一起:评析的批次时刻晚于出计划、计划一天只装配一次、接口缓存
|
||||
十二小时。这一段一次解掉后两层 —— 评析什么时候跑完都行,跑完下一次请求就带上。
|
||||
|
||||
做法与上面 regime、market 两个键同一个路子:按请求补、不进缓存。代价是每次
|
||||
请求多一条数据库查询(一次批量取,不是逐票取)。
|
||||
|
||||
注意浅拷贝:get_plan 里的 dict(data) 只拷了顶层,main / observe 两个列表里的
|
||||
行对象与缓存里那一份是同一批。要改行,必须先把那一行拷出来,否则会把当次
|
||||
请求的结果写进缓存、污染全天。
|
||||
"""
|
||||
rows = list(data.get("main") or []) + list(data.get("observe") or [])
|
||||
codes = [r.get("code") for r in rows if isinstance(r, dict) and r.get("code")]
|
||||
if not codes:
|
||||
return 0
|
||||
try:
|
||||
fresh = sources.company_reviews(codes, ds)
|
||||
except Exception as e: # noqa: BLE001 —— 评析读不到绝不拖垮计划接口
|
||||
_access.warning("plan 评析实时补充失败, 本次沿用装配时那一份: %r", e)
|
||||
return 0
|
||||
if not fresh:
|
||||
return 0
|
||||
n = 0
|
||||
for key in ("main", "observe"):
|
||||
out, changed = [], False
|
||||
for r in (data.get(key) or []):
|
||||
cr = fresh.get(r.get("code")) if isinstance(r, dict) else None
|
||||
if cr:
|
||||
r = dict(r) # 先拷贝再改, 别碰缓存里那一份
|
||||
r["company_review"] = cr
|
||||
r["company_review_text"] = card.company_review_view(cr)
|
||||
changed = True
|
||||
n += 1
|
||||
out.append(r)
|
||||
if changed:
|
||||
data[key] = out
|
||||
return n
|
||||
|
||||
|
||||
@app.get("/plan")
|
||||
def get_plan(request: Request, date: str | None = None, format: str = "json",
|
||||
top: int = 20, obs_top: int = 10, theme_cap: int = 5, nocache: int = 0):
|
||||
top: int = 20, obs_top: int = 10, theme_cap: int = 5, nocache: int = 0,
|
||||
decision_id: str | None = None):
|
||||
"""向下兼容承诺(2026-09-02 方案 2.7):main / observe 的装配、排序、裁剪与既有字段
|
||||
一字不动,每行只多联入判决类字段;顶层只新增 generated_at、plan_version、regime、
|
||||
card_counts、candidates、watch、segments_pointed、snapshot,2026-09-03 再加 market
|
||||
(环境段市场四项,与 regime 一样只从当日快照读,快照缺失为空字典)。PMS 按字段名取值、忽略未知键。"""
|
||||
t0 = time.time()
|
||||
try:
|
||||
data, cached = _plan_cached(date, top, obs_top, theme_cap, bool(nocache))
|
||||
except RuntimeError as e:
|
||||
data, cached = _plan_cached(date, top, obs_top, theme_cap, bool(nocache), decision_id)
|
||||
except (RuntimeError, ValueError, OSError) as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
data = dict(data) # 下面几个键按请求写,不污染缓存里那一份
|
||||
ds = data["date"]
|
||||
reg = regime.read_from_snapshot(ds)
|
||||
data["snapshot"] = "present" if os.path.exists(regime.snapshot_path(ds)) else "missing"
|
||||
data["regime"] = reg or {"status": regime.UNKNOWN, "weak_day": None,
|
||||
data["snapshot"] = "present"
|
||||
data["regime"] = data.get('regime') or {"status": regime.UNKNOWN, "weak_day": None,
|
||||
"source": "当日快照无环境段(08:45 追加未跑或快照缺失)"}
|
||||
data["market"] = regime.read_section(ds, "market") or {}
|
||||
n_cr = _attach_fresh_reviews(data, ds)
|
||||
data["market"] = data.get('market') or {}
|
||||
n_cr = 0 # 评析只在完整版本发布时变化。
|
||||
_access.info("plan client=%s date=%s regime=%s generated_at=%s version=%s "
|
||||
"top=%s obs_top=%s theme_cap=%s cache=%s source=%s 评析补%s只 耗时=%.1fs",
|
||||
request.client.host if request.client else "-", ds,
|
||||
|
|
@ -233,19 +174,36 @@ def get_plan(request: Request, date: str | None = None, format: str = "json",
|
|||
|
||||
|
||||
@app.post("/plan/refresh")
|
||||
def refresh(date: str | None = None):
|
||||
def refresh(date: str | None = None, key: str | None = None):
|
||||
from xxl import trigger_daily_build
|
||||
return trigger_daily_build(callbackUrl=None, steps='plan-refresh', date=date,
|
||||
key=key, x_job_key=None)
|
||||
|
||||
|
||||
def _published_stock_rows(want, date, decision_id):
|
||||
try:
|
||||
out = plan.generate(date)
|
||||
except SystemExit as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
with _PLAN_CACHE_LOCK: # 重新生成了计划,实时接口的缓存整体作废
|
||||
_PLAN_CACHE.clear()
|
||||
return {"ok": True, "file": out}
|
||||
published = plan_versions.load(config.PLAN_SNAPSHOT_DIR, date, decision_id)
|
||||
except (OSError, ValueError):
|
||||
if decision_id or date:
|
||||
raise HTTPException(status_code=404, detail='指定计划版本不可用')
|
||||
return {}, {}
|
||||
rows = {r['code']: dict(r, decision=bucket, source='published',
|
||||
traceability=published.get('traceability') or 'legacy_unverified',
|
||||
decision_version=published.get('decision'), date=published['date'],
|
||||
verdict_text=r.get('basis') or r.get('verdict'))
|
||||
for bucket in ('main', 'observe') for r in published.get(bucket, [])}
|
||||
wanted = []
|
||||
for c in want:
|
||||
try:
|
||||
wanted.append(plan_reconcile._norm_code(c))
|
||||
except SystemExit:
|
||||
continue
|
||||
return {c: rows[c] for c in wanted if c in rows}, published
|
||||
|
||||
|
||||
@app.get("/plan/verdict")
|
||||
def plan_verdict(codes: str | None = None, code: str | None = None,
|
||||
date: str | None = None):
|
||||
date: str | None = None, decision_id: str | None = None):
|
||||
"""逐票『计划判决』(只读)——今日页 / 机会线索页对齐用。
|
||||
|
||||
GET /plan/verdict?codes=300750,600438,SH688041 多只(逗号分隔)
|
||||
|
|
@ -260,6 +218,10 @@ def plan_verdict(codes: str | None = None, code: str | None = None,
|
|||
want = [c.strip() for c in raw.split(",") if c.strip()]
|
||||
if not want:
|
||||
raise HTTPException(status_code=400, detail="缺少 code / codes 参数")
|
||||
frozen, published = _published_stock_rows(want, date, decision_id)
|
||||
if decision_id or date or len(frozen) == len(want):
|
||||
return {'date': published.get('date'), 'decision_version': published.get('decision'),
|
||||
'verdicts': list(frozen.values()), 'count': len(frozen), 'stale': ''}
|
||||
d = date or plan_reconcile.latest_date()
|
||||
if not d:
|
||||
raise HTTPException(status_code=404, detail="档位表为空——先跑当日构建")
|
||||
|
|
@ -280,6 +242,7 @@ def plan_verdict(codes: str | None = None, code: str | None = None,
|
|||
verdicts.append({"input": c, "code": k, # 与上面认不出形态的处理对称
|
||||
"error": f"{type(e).__name__}: {e}"})
|
||||
continue
|
||||
v = frozen.get(k, dict(v, observation_at=plan_versions.now(), observation_scope='independent'))
|
||||
v["input"] = c
|
||||
verdicts.append(v)
|
||||
return {"date": d, "stale": L.get("stale", ""),
|
||||
|
|
@ -288,7 +251,7 @@ def plan_verdict(codes: str | None = None, code: str | None = None,
|
|||
|
||||
@app.get("/logic_state")
|
||||
def logic_state_lookup(codes: str | None = None, code: str | None = None,
|
||||
date: str | None = None):
|
||||
date: str | None = None, decision_id: str | None = None):
|
||||
"""逐票逻辑状态四态(只读)——给 PMS 早上拉完计划后查在持票用(2026-09-07 第三件桥侧前置)。
|
||||
|
||||
GET /logic_state?codes=300750,SH688041 多只(逗号分隔)
|
||||
|
|
@ -304,6 +267,11 @@ def logic_state_lookup(codes: str | None = None, code: str | None = None,
|
|||
want = [c.strip() for c in raw.split(",") if c.strip()]
|
||||
if not want:
|
||||
raise HTTPException(status_code=400, detail="缺少 code / codes 参数")
|
||||
frozen, published = _published_stock_rows(want, date, decision_id)
|
||||
if decision_id or date or len(frozen) == len(want):
|
||||
return {'date': published.get('date'), 'decision_version': published.get('decision'),
|
||||
'states': [dict(r, **(r.get('logic_state') or {})) for r in frozen.values()],
|
||||
'count': len(frozen)}
|
||||
d = date or plan._latest_date("t_factor_akg_score") # noqa: SLF001 —— 桥内自用
|
||||
if not d:
|
||||
raise HTTPException(status_code=404, detail="档位表为空——先跑当日构建")
|
||||
|
|
@ -345,13 +313,16 @@ def logic_state_lookup(codes: str | None = None, code: str | None = None,
|
|||
_access.warning("logic_state valuation failed date=%s err=%r", d, e)
|
||||
# 公司深度两键随状态一起回(2026-09-09 接入方案):持仓页"证据还在不在"旁显示质地与失效条件。读不到整批不带。
|
||||
try:
|
||||
crs = sources.company_reviews(list(keyed), d)
|
||||
crs = sources.company_reviews(list(keyed), d, cutoff_at=plan_versions.now() if not date else None)
|
||||
for item in out:
|
||||
cr = crs.get(item["code"])
|
||||
item["company_review"] = cr
|
||||
item["company_review_text"] = card.company_review_view(cr)
|
||||
except Exception as e: # noqa: BLE001
|
||||
_access.warning("logic_state company_review failed date=%s err=%r", d, e)
|
||||
out = [dict(frozen[r['code']], **(frozen[r['code']].get('logic_state') or {}))
|
||||
if r['code'] in frozen else dict(r, observation_at=plan_versions.now(), observation_scope='independent')
|
||||
for r in out]
|
||||
return {"date": d, "count": len(out) + len(bad), "states": out + bad}
|
||||
|
||||
|
||||
|
|
|
|||
4
card.py
4
card.py
|
|
@ -268,7 +268,7 @@ def logic_lines(claims, limit: int = 3) -> list[str]:
|
|||
return out
|
||||
|
||||
|
||||
def sort_key(row: dict[str, Any]) -> tuple:
|
||||
def sort_key(row: dict[str, Any], sort_by_quality=None) -> tuple:
|
||||
"""卡内序:先判决(候选 < 关注 < 仅展示),2026-09-17 起在判决与涨幅之间插入质地带降序
|
||||
(开关 CARD_SORT_BY_QUALITY,默认开),最后按数据日涨幅降序。开关关掉时元组与旧实现
|
||||
(先判决再涨幅)逐字相同,逐字回旧。不算总分。"""
|
||||
|
|
@ -276,7 +276,7 @@ def sort_key(row: dict[str, Any]) -> tuple:
|
|||
v = _VERDICT_ORDER.get(row.get("verdict"), 9)
|
||||
pct0 = _num(row.get("pct0"))
|
||||
pct_key = -(pct0 if pct0 is not None else -1e9)
|
||||
if config.CARD_SORT_BY_QUALITY:
|
||||
if config.CARD_SORT_BY_QUALITY if sort_by_quality is None else sort_by_quality:
|
||||
return (v, -int(row.get("quality_band") or 0), pct_key)
|
||||
return (v, pct_key)
|
||||
|
||||
|
|
|
|||
|
|
@ -619,3 +619,13 @@
|
|||
- **验证读数(2026-09-24 08:53)。** 新版本 177 行,带评议 139、带定性 138(ok 134、half 4,half 为上限提高前截断锁住的四家,随 09-29 升版本重跑),`gave_up` 0,09-23 09:41 之后没有一行评议或定性被写回假;16:05 到 02:05 每批 10 家全成。判收读数:目标 120 家有报告 110、评议成功 84(76%)、报告不落后 0.92、评议不落后 0.70。
|
||||
|
||||
- **复核日期。** 09-30 判收读数(评议不落后到 0.80);长假后总评审复核云端定性的费用与"回本地"条件。
|
||||
|
||||
## 064 · 2026-10-15 · 每日计划完整版本与顺序贯通:固定晨版、盘中修订、按实际消费复盘
|
||||
|
||||
- **日期。** 2026-10-15,北京时间;本条为用户批准方案的本地实现决定,尚未部署。
|
||||
- **改动。** 保留候选准入与有效排序开关;同强度证据及股票补稳定次序,无主题不合成限额桶。计划冻结输入、参数与研究,发布不可变版本;07:20—15:10 五分钟独立修订仅响应评析及现有区制。取消查询时局部覆盖及缺历史现场重构。PMS 继承消费顺序,信号优先记原因;提议采纳前按新版核验,仅名次变化不撤销。基座提供不可变报告引用,不加选股规则。
|
||||
- **依据。** 用户批准《每日选股计划完善整体方案》三项决定;离线回归覆盖单名额错序、同强度证据不确定、无主题限额、评析与判决不同步、历史读取未来资料,并补发布恢复及并发采纳。代码与实测见同日规格书及交接。
|
||||
- **预期。** 同版研究、判决、质地带及排序一致;历史内容不变;缺档不伪造;消费参数、提议与执行可追溯。健康传播目标十二分钟;旧配置及准入门槛保持,未自动切换任何线上开关。
|
||||
- **复核日期。** 模拟实例获准部署后的首个交易日开始,连续五个交易日核验一致性、顺序、时延、故障恢复和重复指令;五/十/二十日效果仍按原观察期限复核,日期待部署日确定。
|
||||
|
||||
补充说明:本条取代 054/056/057 中“查询时补最新评析”“旧快照回落现算”的运行语义;历史条目及当时证据保留。
|
||||
|
|
|
|||
94
plan.py
94
plan.py
|
|
@ -33,6 +33,15 @@ import logic_state_daily
|
|||
import db
|
||||
import sources
|
||||
import version
|
||||
import plan_inputs
|
||||
import plan_versions
|
||||
|
||||
# 来源代理只在输入录制/重放上下文内生效。普通独立研究调用保持原行为。
|
||||
sources = plan_inputs.Readers(sources)
|
||||
db = plan_inputs.Readers(db, {'read_pg', 'read_mysql'})
|
||||
judgement = plan_inputs.Readers(judgement, {'load_previous', 'recent_rows'})
|
||||
logic_state_daily = plan_inputs.Readers(logic_state_daily, {'history'})
|
||||
config = plan_inputs.Config(config)
|
||||
|
||||
|
||||
# 分数编码(与 factors.build_score 一致):主榜 = 200 + 传导档位×20 + 组内分,
|
||||
|
|
@ -93,7 +102,10 @@ def _evidence(ds: str):
|
|||
tr["n_sources"] = pd.to_numeric(tr["n_sources"], errors="coerce").fillna(0)
|
||||
tr["moved_ratio"] = pd.to_numeric(tr["moved_ratio"], errors="coerce").fillna(0)
|
||||
tr["strength"] = tr["n_sources"] * (1.0 - tr["moved_ratio"])
|
||||
tr = tr.sort_values("strength", ascending=False).drop_duplicates("k")
|
||||
tr = tr.sort_values(["strength", "k", "target", "n_sources", "moved_ratio",
|
||||
"members_total", "moved", "mkt_trade_date"],
|
||||
ascending=[False, True, True, False, True, False, False, False],
|
||||
kind="mergesort").drop_duplicates("k")
|
||||
# 元组前三项是既有口径(多处按下标取),第四、五项是环节的成员数与已动数(关注环节表用)
|
||||
ev = {r.k: (str(r.target), int(r.n_sources), float(r.moved_ratio),
|
||||
None if pd.isna(r.members_total) else int(r.members_total),
|
||||
|
|
@ -178,7 +190,7 @@ def take_by_theme(items, n, theme_cap, theme_of, uncapped_no_theme):
|
|||
out, cnt = [], {}
|
||||
for it in items:
|
||||
th = theme_of(it)
|
||||
exempt = uncapped_no_theme and not th
|
||||
exempt = not th # 无主题不代表同一行业;两种排序轴都不把它们合成一桶。
|
||||
if not exempt:
|
||||
bucket = th or "(无传导)"
|
||||
if theme_cap and cnt.get(bucket, 0) >= theme_cap:
|
||||
|
|
@ -426,7 +438,8 @@ def _assemble_cards(ds: str, codes: list, ev: dict, upside: pd.Series,
|
|||
# 抗抖动(2026-09-07 第三件桥侧前置):配上这只票在逐票日频表里的近日行,落定今天的状态。
|
||||
# 进入逻辑存疑即刻成立,退出要连续几天不再存疑,其余迁移要连续几天同向;表里没有
|
||||
# 这只票时落定态就是原始态。落定态进 state,原始态另存 raw_state,两个都发给下游。
|
||||
state = logic_state_daily.settle_one(k, raw_state, hist.get(k))
|
||||
state = logic_state_daily.settle_one(k, raw_state, hist.get(k),
|
||||
confirm_days=config.LOGIC_SETTLE_CONFIRM_DAYS, exit_days=config.LOGIC_SETTLE_EXIT_DAYS)
|
||||
j = card.judge(evd, start_pct=config.CARD_START_PCT,
|
||||
accum_max_age=config.CARD_ACCUM_MAX_AGE,
|
||||
neg_tol=config.UPSIDE_NEG_TOLERANCE,
|
||||
|
|
@ -453,7 +466,7 @@ def _assemble_cards(ds: str, codes: list, ev: dict, upside: pd.Series,
|
|||
"pressure": n.get("pressure"), "date": n.get("conclusion_date")}
|
||||
if n else None),
|
||||
}
|
||||
for i, (k, c) in enumerate(sorted(cards.items(), key=lambda kv: card.sort_key(kv[1])), 1):
|
||||
for i, (k, c) in enumerate(sorted(cards.items(), key=lambda kv: (*card.sort_key(kv[1], config.CARD_SORT_BY_QUALITY), kv[0])), 1):
|
||||
c["card_rank"] = i
|
||||
|
||||
segs: dict = {}
|
||||
|
|
@ -506,7 +519,7 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
if upside.empty:
|
||||
# 当日 upside 表为空时现算兜底(as-of 口径不变:consensus<=当日、当日收盘价)
|
||||
import factors
|
||||
df_up = factors.build_upside(ds, ds)
|
||||
df_up = plan_inputs.factor_read('build_upside', ds, ds)
|
||||
if df_up is not None and not df_up.empty:
|
||||
x = df_up.copy()
|
||||
x["k"] = x["stock_code"].map(common.to_prefix)
|
||||
|
|
@ -516,12 +529,12 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
names = _names()
|
||||
ev, mkt_days = _evidence(ds)
|
||||
|
||||
main = score[score >= _MAIN_MIN].sort_values(ascending=False)
|
||||
obs = score[score < _MAIN_MIN].sort_values(ascending=False)
|
||||
main = score[score >= _MAIN_MIN].sort_index().sort_values(ascending=False, kind='mergesort')
|
||||
obs = score[score < _MAIN_MIN].sort_index().sort_values(ascending=False, kind='mergesort')
|
||||
generated_at = dt.datetime.now().isoformat(timespec="seconds")
|
||||
try: # 风险名单只读一次:候选卡与升降档原因共用
|
||||
import factors
|
||||
risk = factors._risk_set() or set() # noqa: SLF001 —— 同仓自用
|
||||
risk = plan_inputs.factor_read('_risk_set') or set()
|
||||
except Exception: # noqa: BLE001
|
||||
risk = set()
|
||||
cards, segments_pointed = _assemble_cards(
|
||||
|
|
@ -648,7 +661,7 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
if prev_up.empty:
|
||||
try:
|
||||
import factors
|
||||
dfu = factors.build_upside(prev_ds, prev_ds)
|
||||
dfu = plan_inputs.factor_read('build_upside', prev_ds, prev_ds)
|
||||
if dfu is not None and not dfu.empty:
|
||||
x = dfu.copy()
|
||||
x["k"] = x["stock_code"].map(common.to_prefix)
|
||||
|
|
@ -662,7 +675,7 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
tset = None
|
||||
try:
|
||||
import factors
|
||||
tset = factors._track_set() # noqa: SLF001 —— 同仓自用
|
||||
tset = plan_inputs.factor_read('_track_set')
|
||||
except Exception: # noqa: BLE001 —— 拿不到就退化为通用文案
|
||||
pass
|
||||
|
||||
|
|
@ -1021,7 +1034,7 @@ def snapshot_fresh_enough(snap: dict) -> tuple:
|
|||
g = dt.datetime.fromisoformat(gen)
|
||||
except ValueError:
|
||||
return False, f"快照的生成时刻认不出: {gen}"
|
||||
age_h = (dt.datetime.now() - g).total_seconds() / 3600.0
|
||||
age_h = (dt.datetime.now(g.tzinfo) - g).total_seconds() / 3600.0
|
||||
if age_h > max_age_h:
|
||||
return False, (f"快照出于 {gen},距今 {age_h:.1f} 小时,超过 {max_age_h:g} 小时的界;"
|
||||
f"证据线可能已经变了,本次按实时装配")
|
||||
|
|
@ -1049,7 +1062,7 @@ def pick_rows(rows, n: int, theme_cap: int, uncapped_no_theme=None) -> list:
|
|||
|
||||
|
||||
def from_snapshot(date: str | None = None, top: int = 20, obs_top: int = 10,
|
||||
theme_cap: int = 5) -> dict:
|
||||
theme_cap: int = 5, decision_id=None) -> dict:
|
||||
"""从当日 JSON 快照读计划,按请求参数截取。**不做任何装配、不查任何库。**
|
||||
|
||||
为什么要这条路(2026-09-10,台账 057): collect 每次请求都现场装配整池,实测三十三秒,
|
||||
|
|
@ -1061,30 +1074,24 @@ def from_snapshot(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
· main / observe 按本次请求的参数从全量截取,名次重编成连续的
|
||||
· theme_cap 按本次请求写,不用落盘时那个
|
||||
· 多两个键 plan_source 与 snapshot_generated_at,让人一眼看出这份是哪来的、多新
|
||||
快照不存在、读不动、或数据日对不上,一律抛异常,由调用方回落到 collect。
|
||||
快照不存在、读不动、或数据日对不上,一律抛异常;查询不得回落当前研究。
|
||||
"""
|
||||
ds = date or _latest_date("t_factor_akg_score")
|
||||
if not ds:
|
||||
raise RuntimeError("t_factor_akg_score 还没有数据——先 build akg_score。")
|
||||
ds = date or (decision_id[:10] if decision_id else plan_versions.latest_date(config.PLAN_SNAPSHOT_DIR))
|
||||
path = os.path.join(config.PLAN_SNAPSHOT_DIR, f"plan_{ds}.json")
|
||||
if not os.path.exists(path):
|
||||
raise FileNotFoundError(f"{ds} 的计划快照不存在: {path}")
|
||||
with open(path, encoding="utf-8") as f:
|
||||
snap = json.load(f)
|
||||
snap = plan_versions.load(config.PLAN_SNAPSHOT_DIR, ds, decision_id)
|
||||
if str(snap.get("date") or "") != str(ds):
|
||||
raise RuntimeError(f"快照里的数据日 {snap.get('date')} 与请求的 {ds} 对不上: {path}")
|
||||
full_main, full_obs = snap.get("main"), snap.get("observe")
|
||||
if not isinstance(full_main, list) or not isinstance(full_obs, list):
|
||||
raise RuntimeError(f"快照缺全量主榜或观察档: {path}")
|
||||
if not date: # 只有「要今天该用的那份」才谈新鲜度
|
||||
if not date and not decision_id and not snap.get('decision'):
|
||||
ok, why = snapshot_fresh_enough(snap)
|
||||
if not ok:
|
||||
raise StaleSnapshot(why)
|
||||
|
||||
data = {k: v for k, v in snap.items()
|
||||
# 前三个是落盘那次的裁剪结果与参数,与本次请求无关;
|
||||
# 后两个由接口每次实时读,留在这里会被覆盖,删掉免得看的人以为快照说了算。
|
||||
if k not in ("main_shown", "observe_shown", "shown_params", "market", "regime")}
|
||||
# 落盘那次的裁剪参数与本次请求无关,研究及环境仍来自同一版本。
|
||||
if k not in ("main_shown", "observe_shown", "shown_params")}
|
||||
data["main"] = [{**r, "rank": i}
|
||||
for i, r in enumerate(pick_rows(full_main, top, theme_cap), 1)]
|
||||
data["observe"] = [{**r, "rank": i}
|
||||
|
|
@ -1092,23 +1099,30 @@ def from_snapshot(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
data["theme_cap"] = theme_cap
|
||||
data["plan_source"] = "snapshot"
|
||||
data["snapshot_generated_at"] = snap.get("generated_at")
|
||||
data['traceability'] = snap.get('traceability') or 'legacy_unverified'
|
||||
data['refresh_status'] = (plan_versions.health(config.PLAN_SNAPSHOT_DIR, ds)
|
||||
if not decision_id else {})
|
||||
return data
|
||||
|
||||
|
||||
def generate(date: str | None = None, top: int = 20, obs_top: int = 10,
|
||||
theme_cap: int = 5) -> str:
|
||||
with plan_versions.writer(config.PLAN_SNAPSHOT_DIR):
|
||||
return _generate(date, top, obs_top, theme_cap)
|
||||
|
||||
|
||||
def _generate(date, top, obs_top, theme_cap):
|
||||
ds = date or _latest_date('t_factor_akg_score')
|
||||
bundle = plan_inputs.Bundle(params=plan_inputs.effective_config(config.module))
|
||||
try:
|
||||
data = collect(date, top, obs_top, theme_cap)
|
||||
with bundle.use():
|
||||
data = collect(ds, top, obs_top, theme_cap)
|
||||
data['market'] = sources.market_context(data['date'])
|
||||
except RuntimeError as e:
|
||||
raise SystemExit(str(e))
|
||||
# 环境段的市场四项(2026-09-03):出计划时读一次落进快照,接口 /plan 只从快照读,盘中不再取数;
|
||||
# 每项读失败为空并把原因记在 market.errors,不阻断。区制段仍由 08:45 的追加步骤写入。
|
||||
data["market"] = sources.market_context(data["date"])
|
||||
text = render_md(data)
|
||||
os.makedirs(config.PLAN_SNAPSHOT_DIR, exist_ok=True)
|
||||
out = os.path.join(config.PLAN_SNAPSHOT_DIR, f"plan_{data['date']}.md")
|
||||
with open(out, "w", encoding="utf-8") as f:
|
||||
f.write(text + "\n")
|
||||
# ---- 当日 JSON 快照(2026-09-02 方案第 2.2 节第一项):主榜与观察档全部行、全部证据线,
|
||||
# 不裁剪、不设主题限额;带生成时刻与代码版本。它是复盘与对账的唯一底本;
|
||||
# 08:45 的 regime-append 步骤会往里追加 regime 段,/plan 的 regime 段只读这份。----
|
||||
|
|
@ -1117,10 +1131,10 @@ def generate(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
"main": full.get("main", []), "observe": full.get("observe", []),
|
||||
"shown_params": {"top": top, "obs_top": obs_top, "theme_cap": theme_cap}}
|
||||
jpath = os.path.join(config.PLAN_SNAPSHOT_DIR, f"plan_{data['date']}.json")
|
||||
tmp = jpath + ".tmp"
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(snap, f, ensure_ascii=False, indent=1, default=str)
|
||||
os.replace(tmp, jpath)
|
||||
snap = plan_versions.publish(config.PLAN_SNAPSHOT_DIR, snap,
|
||||
{'records': bundle.records, 'market': data['market'], 'regime': data.get('regime')},
|
||||
bundle.params, cutoff_at=bundle.cutoff_at, reason='晨版生成')
|
||||
text = write_published_md(snap)
|
||||
# ---- 逐票日频逻辑状态表(2026-09-07 第三件桥侧前置):只在这一次生成里写,/plan 的实时重算
|
||||
# 只读不写。写在快照落盘之后:表写失败只打印原因,计划文件与快照照出,不断产。----
|
||||
try:
|
||||
|
|
@ -1140,3 +1154,15 @@ def generate(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
f"(主榜 {len(snap['main'])} 行、观察档 {len(snap['observe'])} 行、"
|
||||
f"候选 {len(data.get('candidates') or [])} 只,版本 {data.get('plan_version')})")
|
||||
return out
|
||||
|
||||
|
||||
def write_published_md(snap):
|
||||
"""兼容 Markdown 是已发布 JSON 的派生物,带版本号;失败不回滚完整版本。"""
|
||||
shown = dict(snap, main=snap.get('main_shown', snap['main']),
|
||||
observe=snap.get('observe_shown', snap['observe']))
|
||||
text = '决策版本:' + snap['decision']['id'] + '\n\n' + render_md(shown) + '\n'
|
||||
try:
|
||||
plan_versions.atomic(os.path.join(config.PLAN_SNAPSHOT_DIR, f"plan_{snap['date']}.md"), text, text=True)
|
||||
except OSError as e:
|
||||
print(f'兼容 Markdown 更新失败,已发布 JSON 仍有效:{type(e).__name__}')
|
||||
return text
|
||||
|
|
|
|||
|
|
@ -0,0 +1,37 @@
|
|||
"""桥机容器:python plan_health.py。只读本地发布文件,不连库,不调用模型。"""
|
||||
import json
|
||||
import datetime as dt
|
||||
import config
|
||||
import plan_versions as pv
|
||||
|
||||
|
||||
def inspect(root, now=None):
|
||||
try:
|
||||
p = pv.load(root)
|
||||
state = pv.health(root, p['date'])
|
||||
result = {'data_date': p['date'], 'decision': p.get('decision'),
|
||||
'generated_at': p.get('generated_at'), 'refresh': state,
|
||||
'rows': sum(len(p.get(k) or []) for k in ('main', 'observe'))}
|
||||
if not p.get('decision'):
|
||||
return 1, dict(result, error='旧快照,追溯不完整')
|
||||
archived = pv.load(root, decision_id=p['decision']['id'])
|
||||
if archived != p:
|
||||
return 2, dict(result, error='最新入口与不可变版本不一致')
|
||||
from plan_refresh import in_window, BJ
|
||||
current = now or dt.datetime.now(dt.timezone.utc)
|
||||
if in_window(current):
|
||||
cutoff = dt.datetime.fromisoformat(p['decision']['cutoff_at'])
|
||||
checked = dt.datetime.fromisoformat(state.get('checked_at') or p['generated_at'])
|
||||
if cutoff.astimezone(BJ).date() != current.astimezone(BJ).date():
|
||||
return 1, dict(result, error='今日晨版尚未发布')
|
||||
if (current - checked).total_seconds() > 12 * 60:
|
||||
return 1, dict(result, error='修订检查超过十二分钟未更新')
|
||||
return (1 if state.get('error') or state.get('pending_codes') else 0), result
|
||||
except (OSError, ValueError, KeyError, ImportError, TypeError) as e:
|
||||
return 2, {'error': str(e)}
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
status, result = inspect(config.PLAN_SNAPSHOT_DIR)
|
||||
print(json.dumps(result, ensure_ascii=False, indent=2))
|
||||
raise SystemExit(status)
|
||||
|
|
@ -0,0 +1,154 @@
|
|||
"""计划输入的录制与重放。仅捕获显式来源边界,重放绝不回退到数据库。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import contextvars
|
||||
import datetime as dt
|
||||
import decimal
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
_active = contextvars.ContextVar('plan_inputs', default=None)
|
||||
|
||||
|
||||
def pack(v):
|
||||
import pandas as pd
|
||||
if v is pd.NA or v is pd.NaT:
|
||||
return None
|
||||
if isinstance(v, pd.DataFrame):
|
||||
return {'$frame': pack(v.to_dict('split')), 'dtypes': {str(k): str(t) for k, t in v.dtypes.items()}}
|
||||
if isinstance(v, pd.Series):
|
||||
return {'$series': pack({'index': list(v.index), 'data': list(v), 'name': v.name}), 'dtype': str(v.dtype)}
|
||||
if isinstance(v, dict):
|
||||
return {'$dict': [[pack(k), pack(x)] for k, x in sorted(v.items(), key=lambda p: str(p[0]))]}
|
||||
if isinstance(v, (list, tuple, set)):
|
||||
xs = sorted(v, key=str) if isinstance(v, set) else v
|
||||
return {'$' + type(v).__name__: [pack(x) for x in xs]}
|
||||
if isinstance(v, dt.datetime):
|
||||
return {'$datetime': v.isoformat()}
|
||||
if isinstance(v, dt.date):
|
||||
return {'$date': v.isoformat()}
|
||||
if isinstance(v, decimal.Decimal):
|
||||
return {'$decimal': str(v)}
|
||||
if isinstance(v, float) and not math.isfinite(v):
|
||||
return {'$float': str(v)}
|
||||
if v is None or isinstance(v, (str, int, float, bool)):
|
||||
return v
|
||||
if hasattr(v, 'item'):
|
||||
return pack(v.item())
|
||||
raise TypeError(f'计划输入不能序列化: {type(v).__name__}')
|
||||
|
||||
|
||||
def unpack(v):
|
||||
if not isinstance(v, dict):
|
||||
return v
|
||||
if '$frame' in v:
|
||||
import pandas as pd
|
||||
p = unpack(v['$frame'])
|
||||
out = pd.DataFrame(p['data'], columns=p['columns'], index=p['index'])
|
||||
for k, dtype in v.get('dtypes', {}).items():
|
||||
if dtype != 'object' and k in out:
|
||||
out[k] = out[k].astype(dtype)
|
||||
return out
|
||||
if '$series' in v:
|
||||
import pandas as pd
|
||||
p = unpack(v['$series'])
|
||||
return pd.Series(p['data'], index=p['index'], name=p['name'], dtype=v['dtype'])
|
||||
if '$dict' in v:
|
||||
return {unpack(k): unpack(x) for k, x in v['$dict']}
|
||||
for kind, ctor in (('list', list), ('tuple', tuple), ('set', set)):
|
||||
if '$' + kind in v:
|
||||
return ctor(unpack(x) for x in v['$' + kind])
|
||||
for kind, ctor in (('date', dt.date.fromisoformat), ('datetime', dt.datetime.fromisoformat),
|
||||
('decimal', decimal.Decimal), ('float', float)):
|
||||
if '$' + kind in v:
|
||||
return ctor(v['$' + kind])
|
||||
raise ValueError('未知计划输入编码')
|
||||
|
||||
|
||||
def digest(v):
|
||||
return hashlib.sha256(json.dumps(v, ensure_ascii=False, sort_keys=True,
|
||||
separators=(',', ':'), allow_nan=False).encode()).hexdigest()
|
||||
|
||||
|
||||
def effective_config(module):
|
||||
names = set()
|
||||
for name in ('plan.py', 'card.py', 'logic_state_daily.py'):
|
||||
names.update(re.findall(r'config\.([A-Z][A-Z_0-9]+)', Path(__file__).with_name(name).read_text()))
|
||||
return {k: getattr(module, k) for k in sorted(names) if hasattr(module, k)
|
||||
and k not in {'PLAN_SNAPSHOT_DIR'}}
|
||||
|
||||
|
||||
class Bundle:
|
||||
def __init__(self, *, records=None, params=None, cutoff_at=None):
|
||||
self.replay = records is not None
|
||||
self.records = {} if records is None else dict(records)
|
||||
self.params = params or {}
|
||||
self.cutoff_at = cutoff_at or dt.datetime.now(dt.timezone.utc).isoformat()
|
||||
self.missing = []
|
||||
|
||||
@contextlib.contextmanager
|
||||
def use(self):
|
||||
token = _active.set(self)
|
||||
try:
|
||||
yield self
|
||||
if self.missing:
|
||||
raise RuntimeError('冻结输入缺失,禁止回源: ' + ', '.join(self.missing))
|
||||
finally:
|
||||
_active.reset(token)
|
||||
|
||||
def call(self, name, fn, args, kw):
|
||||
key = name + ':' + digest(pack([args, kw]))
|
||||
if key in self.records:
|
||||
rec = self.records[key]
|
||||
if 'error' in rec:
|
||||
raise RuntimeError(rec['error'])
|
||||
return unpack(rec['value'])
|
||||
if self.replay:
|
||||
self.missing.append(name)
|
||||
raise RuntimeError('冻结输入缺失: ' + name)
|
||||
try:
|
||||
actual = dict(kw)
|
||||
if name == 'sources.company_reviews':
|
||||
actual.update(cutoff_at=self.cutoff_at, strict=True)
|
||||
result = fn(*args, **actual)
|
||||
except Exception as e:
|
||||
self.records[key] = {'name': name, 'error': type(e).__name__, 'args': pack([args, kw])}
|
||||
raise
|
||||
self.records[key] = {'name': name, 'args': pack([args, kw]), 'value': pack(result)}
|
||||
return unpack(self.records[key]['value'])
|
||||
|
||||
|
||||
def read(name, fn, *args, **kw):
|
||||
bundle = _active.get()
|
||||
return bundle.call(name, fn, args, kw) if bundle is not None else fn(*args, **kw)
|
||||
|
||||
|
||||
class Readers:
|
||||
def __init__(self, module, names=None):
|
||||
self.module, self.names = module, names
|
||||
|
||||
def __getattr__(self, name):
|
||||
fn = getattr(self.module, name)
|
||||
if not callable(fn) or (self.names is not None and name not in self.names):
|
||||
return fn
|
||||
return lambda *a, **kw: read(self.module.__name__ + '.' + name, fn, *a, **kw)
|
||||
|
||||
|
||||
class Config:
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
def __getattr__(self, name):
|
||||
bundle = _active.get()
|
||||
if bundle is not None and name in bundle.params:
|
||||
return bundle.params[name]
|
||||
return getattr(self.module, name)
|
||||
|
||||
|
||||
def factor_read(name, *args):
|
||||
import factors
|
||||
return read('factors.' + name, getattr(factors, name), *args)
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
"""本地:仅离线测试。桥机容器:python run.py plan-refresh [--date 数据日]。
|
||||
|
||||
统一调度每五分钟触发独立 plan-refresh 步骤;北京时间 07:20—15:10 的
|
||||
交易日才工作。只替换评析/区制,重放其余输入,绝不调用模型或写日频状态表。
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
import datetime as dt
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import config
|
||||
import plan_inputs as pi
|
||||
import plan_versions as pv
|
||||
|
||||
BJ = ZoneInfo('Asia/Shanghai')
|
||||
|
||||
|
||||
def in_window(now):
|
||||
from chinese_calendar import is_holiday
|
||||
local = now.astimezone(BJ)
|
||||
return (local.weekday() < 5 and not is_holiday(local.date())
|
||||
and dt.time(7, 20) <= local.time().replace(tzinfo=None) <= dt.time(15, 10))
|
||||
|
||||
|
||||
def refresh(day=None, *, now=None, regime_value=None):
|
||||
import plan
|
||||
import sources
|
||||
now = now or dt.datetime.now(dt.timezone.utc)
|
||||
if not in_window(now):
|
||||
return {'status': 'outside_window'}
|
||||
root = config.PLAN_SNAPSHOT_DIR
|
||||
with pv.writer(root):
|
||||
try:
|
||||
old = pv.load(root, day)
|
||||
morning = pv.load(root, old['date'], morning=True)
|
||||
except (FileNotFoundError, ValueError):
|
||||
return {'status': 'no_morning'}
|
||||
# 行情日常是昨天;晨版截止时刻才界定属于哪一个决策日。
|
||||
if dt.datetime.fromisoformat(morning['decision']['cutoff_at']).astimezone(BJ).date() != now.astimezone(BJ).date():
|
||||
return {'status': 'no_morning_today'}
|
||||
day = old['date']
|
||||
pending = set(pv.health(root, day).get('pending_codes') or [])
|
||||
try:
|
||||
env = pv.envelope(root, old['decision']['id'])
|
||||
inputs = copy.deepcopy(env['inputs'])
|
||||
for rec in inputs['records'].values():
|
||||
if rec['name'] != 'sources.company_reviews':
|
||||
continue
|
||||
args, kw = pi.unpack(rec['args'])
|
||||
before = pi.unpack(rec['value']) if 'value' in rec else {}
|
||||
# 读取失败时无法确定哪些股票变化,保守暂停整个本次观察范围。
|
||||
pending.update(args[0])
|
||||
pv.health(root, day, pending_codes=sorted(pending), error=None)
|
||||
after = sources.company_reviews(*args, **dict(kw, cutoff_at=now.isoformat(), strict=True))
|
||||
pending.difference_update(args[0])
|
||||
pending.update(c for c in set(before) | set(after) if before.get(c) != after.get(c))
|
||||
rec.pop('error', None)
|
||||
rec['value'] = pi.pack(after)
|
||||
if regime_value is not None:
|
||||
# 抓取时刻不构成区制变化;保留最后一次语义变化的抓取时刻。
|
||||
def meaningful(x):
|
||||
return {k: v for k, v in (x or {}).items() if k != 'fetched_at'}
|
||||
if meaningful(inputs.get('regime')) != meaningful(regime_value):
|
||||
inputs['regime'] = regime_value
|
||||
pv.health(root, day, pending_codes=sorted(pending), error=None)
|
||||
if pv.digest(inputs) == old['decision']['input_hash']:
|
||||
pv.health(root, day, pending_codes=[], error=None, decision_id=old['decision']['id'])
|
||||
return {'status': 'unchanged', 'decision': old['decision']}
|
||||
bundle = pi.Bundle(records=inputs['records'], params=env['params'], cutoff_at=now.isoformat())
|
||||
shown = old.get('shown_params') or {'top': 20, 'obs_top': 10, 'theme_cap': 5}
|
||||
with bundle.use():
|
||||
data = plan.collect(day, **shown)
|
||||
full = data.pop('_full')
|
||||
data.update(main_shown=data['main'], observe_shown=data['observe'], shown_params=shown,
|
||||
main=full['main'], observe=full['observe'], market=inputs.get('market'),
|
||||
regime=inputs.get('regime'))
|
||||
published = pv.publish(root, data, inputs, env['params'], cutoff_at=now.isoformat(),
|
||||
reason='评析更新' if pending else '市场区制更新')
|
||||
plan.write_published_md(published)
|
||||
return {'status': 'published', 'decision': published['decision']}
|
||||
except Exception as e:
|
||||
pv.health(root, day, pending_codes=sorted(pending), error=f'{type(e).__name__}: {e}')
|
||||
raise
|
||||
|
|
@ -141,30 +141,22 @@ def pms_roster(day: str) -> tuple[list[str], str]:
|
|||
try:
|
||||
df = db.read_mysql(
|
||||
"pms", "SELECT roster_json, meta_json, fetched_at FROM pms_plan_snapshot "
|
||||
"WHERE plan_date = %s ORDER BY id DESC LIMIT 1", (day,))
|
||||
n_df = db.read_mysql(
|
||||
"pms", "SELECT param_value FROM pms_runtime_param WHERE param_key='PMS_PLAN_TOP_N'")
|
||||
t_df = db.read_mysql(
|
||||
"pms", "SELECT param_value FROM pms_runtime_param WHERE param_key='PMS_PLAN_TIERS'")
|
||||
"WHERE plan_date = %s AND JSON_UNQUOTE(JSON_EXTRACT(meta_json, '$.kind')) = 'consumed' "
|
||||
"ORDER BY id DESC LIMIT 1", (day,))
|
||||
except Exception as e: # noqa: BLE001
|
||||
# 异常原文会被印进周报的逐日注记。只进日志,不进报告。
|
||||
print(f" (PMS 计划快照读取失败: {e!r})")
|
||||
return [], "当天没能连上 PMS 数据库,取不到计划快照"
|
||||
if df.empty:
|
||||
return [], "PMS 无当日快照,生产名单退为桥重算"
|
||||
n = int(n_df.iloc[0, 0]) if not n_df.empty else 30
|
||||
tiers_raw = str(t_df.iloc[0, 0]) if not t_df.empty else "强传导" # 参数表无此行时退回 PMS settings 初值
|
||||
tiers = [x.strip() for x in tiers_raw.split(",") if x.strip()]
|
||||
return [], 'PMS 缺真实消费记录,排除消费效果统计'
|
||||
roster = json.loads(df.iloc[0]["roster_json"] or "[]")
|
||||
try:
|
||||
meta = json.loads(df.iloc[0]["meta_json"] or "{}")
|
||||
except (TypeError, ValueError):
|
||||
meta = {}
|
||||
mode = meta.get("rank_mode")
|
||||
codes = _roster_pick(roster, tiers, mode, n)
|
||||
seq = "上游名次升序" if mode == "upstream" else "分数降序"
|
||||
white = "、".join(tiers) if tiers else "不过滤"
|
||||
return codes, f"PMS 快照名册(档位白名单 {white},序按 rank_mode={mode or '(无)'}→{seq},N={n} 为现值)"
|
||||
if meta.get('kind') != 'consumed' or not meta.get('decision'):
|
||||
return [], '旧名册未记录实际消费参数,排除消费效果统计'
|
||||
return [common.to_prefix(c) for c in meta.get('order') or []], 'PMS 实际消费:' + str(meta.get('id'))
|
||||
|
||||
|
||||
def _uniq_codes(values) -> list[str]:
|
||||
|
|
@ -389,16 +381,16 @@ def _pricing_spread_md(groups_tbl: pd.DataFrame) -> str:
|
|||
def _read_review_snapshot(day: str):
|
||||
"""读当日计划快照,整理成与 plan.collect 返回同形的字典(_full 放全量 main/observe,顶层带
|
||||
candidates/watch)。快照不存在、坏 JSON、或 main 不是列表,回 None,由调用方回落到现算。"""
|
||||
path = os.path.join(config.PLAN_SNAPSHOT_DIR, f"plan_{day}.json")
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
snap = json.load(f)
|
||||
import plan_versions
|
||||
snap = plan_versions.load(config.PLAN_SNAPSHOT_DIR, day, morning=True)
|
||||
except (OSError, ValueError):
|
||||
return None
|
||||
main = snap.get("main")
|
||||
if not isinstance(main, list):
|
||||
return None
|
||||
return {"date": snap.get("date") or day,
|
||||
'decision': snap.get('decision'), 'regime': snap.get('regime'),
|
||||
"_full": {"main": main, "observe": snap.get("observe") or []},
|
||||
"candidates": snap.get("candidates") or [], "watch": snap.get("watch") or []}
|
||||
|
||||
|
|
@ -408,11 +400,43 @@ def _plan_for_review(day: str):
|
|||
快照读不出才回落现算;开关 PLAN_REVIEW_SOURCE=live 直接现算。现算用跑周报那一刻的评析报告、
|
||||
查询不按日期截断,质地成主轴后会把晚到的报告回头算进当天名单,四周复核要的是当天真出过的那份。
|
||||
返回 (data, 底本名)。"""
|
||||
if config.PLAN_REVIEW_SOURCE != "live":
|
||||
snap = _read_review_snapshot(day)
|
||||
if snap is not None:
|
||||
return snap, "快照"
|
||||
return plan.collect(day, top=5000, obs_top=5000, theme_cap=0), "现算"
|
||||
if snap is None:
|
||||
raise RuntimeError('缺可核验晨版,排除真实决策效果统计;不以当前研究回填')
|
||||
return snap, '首个成功发布晨版'
|
||||
|
||||
|
||||
def trace_rows(day):
|
||||
"""逐版本、逐消费记录的独立对账底本,不把盘中多版当成多个独立交易日。"""
|
||||
import plan_versions as pv
|
||||
rows, versions = [], {}
|
||||
try:
|
||||
for i, p in enumerate(pv.history(config.PLAN_SNAPSHOT_DIR, day)):
|
||||
d = p['decision']
|
||||
versions[d['id']] = p
|
||||
rows.append({'date': day, 'kind': 'morning' if i == 0 else 'revision',
|
||||
'decision_id': d['id'], 'parent_id': d.get('parent_id'),
|
||||
'at': d['cutoff_at'], 'reason': d['reason'], 'verified': True,
|
||||
'order': [r['code'] for r in p['main']], 'params': {}})
|
||||
except (OSError, ValueError):
|
||||
rows.append({'date': day, 'kind': 'missing_archive', 'verified': False})
|
||||
try:
|
||||
df = db.read_mysql('pms', 'SELECT id, fetched_at, meta_json FROM pms_plan_snapshot '
|
||||
'WHERE plan_date = %s ORDER BY id', (day,))
|
||||
for raw in df.to_dict('records'):
|
||||
meta = json.loads(raw.get('meta_json') or '{}')
|
||||
if meta.get('kind') not in ('consumed', 'rechecked'):
|
||||
continue
|
||||
did = (meta.get('decision') or {}).get('id')
|
||||
rows.append({'date': day, 'kind': meta['kind'], 'decision_id': did,
|
||||
'consumption_id': meta.get('id'), 'at': str(raw['fetched_at']),
|
||||
'verified': did in versions, 'order': meta.get('order'),
|
||||
'params': meta.get('params'), 'context': meta.get('context'),
|
||||
'filtered': meta.get('filtered')})
|
||||
except Exception as e:
|
||||
rows.append({'date': day, 'kind': 'consumption_unavailable', 'verified': False,
|
||||
'reason': type(e).__name__})
|
||||
return rows
|
||||
|
||||
|
||||
def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str,
|
||||
|
|
@ -425,14 +449,17 @@ def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str
|
|||
days = sorted(pivot.index.tolist())
|
||||
accum_by_day = {d: dict(zip(g["k"], g["accum"])) for d, g in px.groupby("trade_date")}
|
||||
|
||||
rows, notes = [], []
|
||||
rows, notes, traces = [], [], []
|
||||
covered = 0
|
||||
for day in days_plan:
|
||||
traces.extend(trace_rows(day))
|
||||
try:
|
||||
data, benben = _plan_for_review(day)
|
||||
except Exception as e: # noqa: BLE001
|
||||
notes.append(f"{day}: 计划取数失败 {e!r}")
|
||||
continue
|
||||
full = data.get("_full") or {}
|
||||
covered += 1
|
||||
main_rows, obs_rows = full.get("main", []), full.get("observe", [])
|
||||
main_codes = [r["code"] for r in main_rows]
|
||||
obs_codes = [r["code"] for r in obs_rows]
|
||||
|
|
@ -459,7 +486,7 @@ def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str
|
|||
reg = None
|
||||
try:
|
||||
import regime
|
||||
reg = regime.read_from_snapshot(day)
|
||||
reg = data.get('regime')
|
||||
except Exception: # noqa: BLE001
|
||||
reg = None
|
||||
|
||||
|
|
@ -574,10 +601,15 @@ def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str
|
|||
+ (f"({bullish_note})" if bullish_note else ""))
|
||||
|
||||
df = pd.DataFrame(rows)
|
||||
if df.empty:
|
||||
raise SystemExit("没有任何可算的期限(数据尾部不足)。")
|
||||
os.makedirs(out_dir, exist_ok=True)
|
||||
stamp = days_plan[-1]
|
||||
trace_path = os.path.join(out_dir, f'版本消费对账_{stamp}_{start}.csv')
|
||||
trace_df = pd.DataFrame([{k: json.dumps(v, ensure_ascii=False, sort_keys=True)
|
||||
if isinstance(v, (dict, list)) else v for k, v in row.items()}
|
||||
for row in traces])
|
||||
trace_df.to_csv(trace_path, index=False, encoding='utf-8-sig')
|
||||
if df.empty:
|
||||
raise SystemExit('没有可核验且期限成熟的样本;缺档明细已保存:' + trace_path)
|
||||
csv_path = os.path.join(out_dir, f"复盘明细_{stamp}_{start}.csv")
|
||||
df.to_csv(csv_path, index=False, encoding="utf-8-sig")
|
||||
|
||||
|
|
@ -641,7 +673,11 @@ def run(since: str, until: str | None, horizons: tuple, start: str, out_dir: str
|
|||
_md(pre_tbl), ""]
|
||||
else:
|
||||
md += ["## 四、按事前线上标签分组", "", "(区间内没有带环境标签的快照,本节待环境标签上线后出现。)", ""]
|
||||
md += ["## 五、逐日注记", ""] + [f"- {n}" for n in notes] + ["",
|
||||
md += [f"可核验晨版覆盖:{covered}/{len(days_plan)};缺档与旧名册不计入真实效果。", "",
|
||||
f"逐版本及消费对账:{os.path.basename(trace_path)};有效记录 "
|
||||
f"{sum(bool(r.get('verified')) for r in traces)}/{len(traces)}。"
|
||||
"日频交付名单采用当日最后一条实际消费,全部消费与修订另列,不混成独立交易日。", "",
|
||||
"## 五、逐日注记", ""] + [f"- {n}" for n in notes] + ["",
|
||||
"## 六、待决定事项", "", "(只列读数与选项,不改任何东西——由每周五人工填写。)", ""]
|
||||
md += ["## 七、三套对照(选股系统、择时决策系统、PMS 账本各自的读数摆在一张表里,只对照不合并)", "",
|
||||
_md(compare_table(lists_tbl, since, until)), ""]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,211 @@
|
|||
"""只追加的计划版本。目录内文件锁涵盖采集、校验与发布,最新文件原子替换。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import copy
|
||||
import datetime as dt
|
||||
import fcntl
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import tempfile
|
||||
|
||||
|
||||
def digest(value):
|
||||
return hashlib.sha256(json.dumps(value, ensure_ascii=False, sort_keys=True,
|
||||
separators=(',', ':'), allow_nan=False).encode()).hexdigest()
|
||||
|
||||
|
||||
def now():
|
||||
return dt.datetime.now(dt.timezone.utc).isoformat(timespec='seconds')
|
||||
|
||||
|
||||
def _date(day):
|
||||
return dt.date.fromisoformat(day).isoformat()
|
||||
|
||||
|
||||
def atomic(path, value, *, text=False):
|
||||
path = Path(path)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
fd, tmp = tempfile.mkstemp(prefix='.' + path.name, dir=path.parent)
|
||||
try:
|
||||
with os.fdopen(fd, 'w', encoding='utf-8') as f:
|
||||
if text:
|
||||
f.write(value)
|
||||
else:
|
||||
json.dump(value, f, ensure_ascii=False, sort_keys=True, allow_nan=False)
|
||||
f.flush()
|
||||
os.fsync(f.fileno())
|
||||
os.replace(tmp, path)
|
||||
directory = os.open(str(path.parent), os.O_RDONLY)
|
||||
try:
|
||||
os.fsync(directory)
|
||||
finally:
|
||||
os.close(directory)
|
||||
finally:
|
||||
if os.path.exists(tmp):
|
||||
os.unlink(tmp)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def writer(root):
|
||||
Path(root).mkdir(parents=True, exist_ok=True)
|
||||
with open(Path(root) / '.publish.lock', 'a') as lock:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX)
|
||||
yield
|
||||
|
||||
|
||||
def latest_date(root):
|
||||
days = sorted(p.stem[5:] for p in Path(root).glob('plan_????-??-??.json'))
|
||||
return days[-1] if days else None
|
||||
|
||||
|
||||
def load(root, day=None, decision_id=None, morning=False):
|
||||
if decision_id:
|
||||
if not re.fullmatch(r'\d{4}-\d{2}-\d{2}_[0-9a-f]{64}', decision_id):
|
||||
raise ValueError('计划版本标识无效')
|
||||
if day and day != decision_id[:10]:
|
||||
raise ValueError('计划日期与版本不匹配')
|
||||
latest = load(root, decision_id[:10])
|
||||
d = latest.get('decision') or {}
|
||||
if 'ancestor_ids' not in d:
|
||||
for published in history(root, decision_id[:10]):
|
||||
if published['decision']['id'] == decision_id:
|
||||
return published
|
||||
raise FileNotFoundError('该版本尚未发布')
|
||||
if decision_id != d.get('id') and decision_id not in d['ancestor_ids']:
|
||||
raise FileNotFoundError('该版本尚未发布')
|
||||
path = Path(root) / 'versions' / decision_id[:10] / (decision_id + '.json')
|
||||
result = json.loads(path.read_text())['plan']
|
||||
if result.get('decision', {}).get('id') != decision_id:
|
||||
raise ValueError('归档版本身份不一致')
|
||||
if decision_id == d.get('id') and result != latest:
|
||||
raise ValueError('发布入口与不可变版本不一致')
|
||||
return result
|
||||
day = _date(day or latest_date(root) or '')
|
||||
if morning:
|
||||
pointer = Path(root) / 'versions' / day / 'morning.json'
|
||||
if not pointer.exists():
|
||||
# 最新指针切换成功、晨版指针写入前中断:沿已发布父链恢复首版。
|
||||
versions = history(root, day)
|
||||
if not versions:
|
||||
raise FileNotFoundError('无可核验晨版')
|
||||
return versions[0]
|
||||
return load(root, day, json.loads(pointer.read_text())['id'])
|
||||
return json.loads((Path(root) / f'plan_{day}.json').read_text())
|
||||
|
||||
|
||||
def envelope(root, decision_id):
|
||||
load(root, decision_id=decision_id) # 校验标识,不接受任意文件路径
|
||||
return json.loads((Path(root) / 'versions' / decision_id[:10] / (decision_id + '.json')).read_text())
|
||||
|
||||
|
||||
def history(root, day):
|
||||
"""只沿已发布父链,目录里未切换成功的文件不是发布历史。"""
|
||||
current = load(root, day)
|
||||
result, seen = [], set()
|
||||
while current.get('decision'):
|
||||
d = current['decision']
|
||||
if not re.fullmatch(re.escape(day) + r'_[0-9a-f]{64}', d['id']):
|
||||
raise ValueError('计划版本标识无效')
|
||||
archived = json.loads((Path(root) / 'versions' / day / (d['id'] + '.json')).read_text())['plan']
|
||||
if archived != current:
|
||||
raise ValueError('发布入口与不可变版本不一致')
|
||||
if d['id'] in seen:
|
||||
raise ValueError('计划版本父链成环')
|
||||
seen.add(d['id'])
|
||||
result.append(archived)
|
||||
if not d.get('parent_id'):
|
||||
break
|
||||
parent = d['parent_id']
|
||||
if not re.fullmatch(re.escape(day) + r'_[0-9a-f]{64}', parent):
|
||||
raise ValueError('计划父版本标识无效')
|
||||
current = json.loads((Path(root) / 'versions' / day / (parent + '.json')).read_text())['plan']
|
||||
return list(reversed(result))
|
||||
|
||||
|
||||
def health(root, day, **values):
|
||||
path = Path(root) / 'versions' / _date(day) / 'health.json'
|
||||
if values:
|
||||
atomic(path, {'checked_at': now(), **values})
|
||||
return json.loads(path.read_text()) if path.exists() else {}
|
||||
|
||||
|
||||
def research(cr, previous=None):
|
||||
cr = cr or {}
|
||||
previous = previous or {}
|
||||
keys = ('overall', 'valuation', 'doubt_hard', 'expectation', 'cycle', 'period_fi',
|
||||
'period_rf', 'n_materials', 'unknown_n', 'numbers_unmatched', 'thesis', 'antithesis')
|
||||
return {k: copy.deepcopy(cr.get(k)) for k in keys} | {
|
||||
'confidence': cr.get('confidence'), 'review_ref': cr.get('review_ref'),
|
||||
'changes': [{'field': k, 'before': previous.get(k), 'after': cr.get(k)}
|
||||
for k in keys if previous and previous.get(k) != cr.get(k)],
|
||||
'checks': [{'kind': k, 'text': cr.get(k), 'status': '待核验'}
|
||||
for k in ('catalyst', 'invalidation') if cr.get(k)],
|
||||
'report_ran_at': cr.get('report_ran_at') or cr.get('ran_at'),
|
||||
'material_status': cr.get('qual'),
|
||||
'report_version_url': cr.get('report_version_url'),
|
||||
}
|
||||
|
||||
|
||||
def publish(root, plan, inputs, params, *, cutoff_at, reason):
|
||||
"""调用方必须持 writer。指纹包含代码、固定输入及配置;时间不导致空版本。"""
|
||||
day = _date(plan['date'])
|
||||
old = None
|
||||
try:
|
||||
old = load(root, day)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
ih, ph = digest(inputs), digest(params)
|
||||
prior = (old or {}).get('decision') or {}
|
||||
if (prior.get('input_hash') == ih and prior.get('params_hash') == ph
|
||||
and old.get('plan_version') == plan.get('plan_version')):
|
||||
health(root, day, pending_codes=[], error=None, decision_id=prior['id'])
|
||||
return old
|
||||
identity = digest({'input': ih, 'params': ph, 'code': plan.get('plan_version'),
|
||||
'date': day, 'parent': prior.get('id')})
|
||||
did = day + '_' + identity
|
||||
if (old or {}).get('decision', {}).get('id') == did:
|
||||
return old
|
||||
p = copy.deepcopy(plan)
|
||||
prev = {r['code']: r for bucket in ('main', 'observe') for r in (old or {}).get(bucket, [])}
|
||||
by_code = {}
|
||||
rank = 0
|
||||
for bucket in ('main', 'observe'):
|
||||
for row in p.get(bucket, []):
|
||||
rank += 1
|
||||
if row['code'] in by_code:
|
||||
raise ValueError('完整计划出现重复股票: ' + row['code'])
|
||||
row['research_rank'] = rank
|
||||
row['review_ref'] = (row.get('company_review') or {}).get('review_ref')
|
||||
row['research'] = research(row.get('company_review'), (prev.get(row['code']) or {}).get('company_review'))
|
||||
by_code[row['code']] = row
|
||||
for bucket in ('candidates', 'watch', 'main_shown', 'observe_shown'):
|
||||
p[bucket] = [{**copy.deepcopy(by_code[r['code']]), 'rank': r.get('rank', i)}
|
||||
for i, r in enumerate(p.get(bucket, []), 1)]
|
||||
p['schema_version'] = 2
|
||||
ancestors = prior.get('ancestor_ids')
|
||||
if prior and ancestors is None:
|
||||
ancestors = [v['decision']['id'] for v in history(root, day)[:-1]]
|
||||
p['decision'] = {'id': did, 'parent_id': (old or {}).get('decision', {}).get('id'),
|
||||
'ancestor_ids': ((ancestors or []) + [prior['id']]) if prior else [],
|
||||
'cutoff_at': cutoff_at, 'input_hash': ih, 'params_hash': ph, 'reason': reason}
|
||||
p['generated_at'] = now()
|
||||
p['traceability'] = 'captured_inputs'
|
||||
# 返回与落盘同一 JSON 形状(元组等不得在内存出口与历史出口有两种表示)。
|
||||
p = json.loads(json.dumps(p, ensure_ascii=False, allow_nan=False))
|
||||
version_dir = Path(root) / 'versions' / day
|
||||
path = version_dir / (did + '.json')
|
||||
if path.exists():
|
||||
# 中断重试恢复已经落盘但尚未切换的同一版本。
|
||||
p = json.loads(path.read_text())['plan']
|
||||
else:
|
||||
atomic(path, {'plan': p, 'inputs': inputs, 'params': params})
|
||||
morning = version_dir / 'morning.json'
|
||||
atomic(Path(root) / f'plan_{day}.json', p)
|
||||
if not morning.exists():
|
||||
atomic(morning, {'id': history(root, day)[0]['decision']['id']})
|
||||
health(root, day, pending_codes=[], error=None, decision_id=did)
|
||||
return p
|
||||
11
regime.py
11
regime.py
|
|
@ -105,11 +105,12 @@ def append_to_snapshot(day: str) -> dict:
|
|||
return reg
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
doc = json.load(f)
|
||||
doc["regime"] = reg
|
||||
tmp = path + ".tmp"
|
||||
with open(tmp, "w", encoding="utf-8") as f:
|
||||
json.dump(doc, f, ensure_ascii=False, indent=1)
|
||||
os.replace(tmp, path)
|
||||
# 区制也发布完整修订;旧版不就地改写。旧格式须先生成可追溯晨版。
|
||||
import plan_refresh
|
||||
result = plan_refresh.refresh(day, regime_value=reg)
|
||||
if result['status'] not in ('published', 'unchanged'):
|
||||
print(f"环境段未发布:{result['status']}")
|
||||
return reg
|
||||
print(f"环境段已写入 {path}:status={reg['status']} weak_count={reg['weak_count']} "
|
||||
f"weak_day={reg['weak_day']}")
|
||||
return reg
|
||||
|
|
|
|||
|
|
@ -7,3 +7,4 @@ PyYAML>=6.0
|
|||
fastapi>=0.110
|
||||
uvicorn>=0.29
|
||||
pymongo>=4.6
|
||||
chinesecalendar>=1.11
|
||||
|
|
|
|||
5
run.py
5
run.py
|
|
@ -184,6 +184,8 @@ def main():
|
|||
av.add_argument("--file", default="sql/astock_kg_slot_views.sql")
|
||||
av.add_argument("--dry-run", action="store_true", help="只列语句不执行")
|
||||
ra = sub.add_parser("regime-append") # 08:45 环境追加(写当日计划快照的 regime 段)
|
||||
rf = sub.add_parser('plan-refresh')
|
||||
rf.add_argument('--date', help='已发布计划的数据日,默认最新;仍检查交易日与时窗')
|
||||
ra.add_argument("--date", help="默认取 score 表最新日(与 plan 同口径)")
|
||||
jg = sub.add_parser("judgement-snapshot") # 日频行业观点快照(只写不判,攒版本史)
|
||||
jg.add_argument("--date", help="计划日,默认今天;补写过去的日期会让版本史失真,会有提示")
|
||||
|
|
@ -233,6 +235,9 @@ def main():
|
|||
if not day:
|
||||
raise SystemExit("t_factor_akg_score 还没有数据,没有当日快照可追加。")
|
||||
regime.append_to_snapshot(day)
|
||||
elif a.cmd == 'plan-refresh':
|
||||
import plan_refresh
|
||||
print(plan_refresh.refresh(a.date))
|
||||
elif a.cmd == "judgement-snapshot":
|
||||
import judgement
|
||||
judgement.snapshot(a.date)
|
||||
|
|
|
|||
25
sources.py
25
sources.py
|
|
@ -1174,7 +1174,7 @@ def catalysts_for_codes(seg_of: dict, cat_by_seg: dict, *, limit: int = 3) -> di
|
|||
# 公司深度评析(2026-09-09 接入方案,台账 053):数据基座 company_review_runs 索引表最新一行的摘要。
|
||||
# 质地由基座代码合成,这里只读不算;报告正文在对象存储,链接给人。表读不到整体缺席不断产。
|
||||
# ---------------------------------------------------------------------------
|
||||
def company_reviews(codes, ds: str, *, read_pg=None) -> dict:
|
||||
def company_reviews(codes, ds: str, *, read_pg=None, cutoff_at=None, strict=False) -> dict:
|
||||
"""{前缀码: {"period", "review_version", "overall", "groups", "valuation", "doubt_hard", "quality_reasons",
|
||||
"grade_counts", "grade_line", "thesis", "confidence", "invalidation", "antithesis", "catalyst", "brain_status",
|
||||
"ran_at", "age_days", "report_url"}};没报告的票不在字典里。"""
|
||||
|
|
@ -1184,10 +1184,14 @@ def company_reviews(codes, ds: str, *, read_pg=None) -> dict:
|
|||
return {}
|
||||
dots = [_to_dot(c) for c in want]
|
||||
try:
|
||||
cutoff = cutoff_at or (str(ds) + 'T23:59:59.999999+08:00')
|
||||
df = rp("SELECT DISTINCT ON (ts_code) ts_code, period, review_version, grades, summary, brain_used, brain_status, ran_at "
|
||||
"FROM company_review_runs WHERE status = 'ok' AND ts_code = ANY(%s) ORDER BY ts_code, ran_at DESC", (dots,))
|
||||
"FROM company_review_runs WHERE status = 'ok' AND ts_code = ANY(%s) AND ran_at <= %s "
|
||||
"ORDER BY ts_code, ran_at DESC, period DESC, review_version DESC", (dots, cutoff))
|
||||
except Exception as e: # noqa: BLE001
|
||||
logging.getLogger("bridge.company_review").warning("company_reviews 读不到: %r", e)
|
||||
if strict:
|
||||
raise
|
||||
return {}
|
||||
out: dict = {}
|
||||
for r in df.to_dict("records"):
|
||||
|
|
@ -1205,6 +1209,16 @@ def company_reviews(codes, ds: str, *, read_pg=None) -> dict:
|
|||
except ValueError:
|
||||
grades = {}
|
||||
ran = r.get("ran_at")
|
||||
# 桩与某些代理不保证过滤;本地再守一遍时间边界,绝不把未来行归零当新鲜。
|
||||
if ran is not None:
|
||||
stamp = dt.datetime.fromisoformat(str(ran).replace('Z', '+00:00'))
|
||||
if stamp.tzinfo is None:
|
||||
stamp = stamp.replace(tzinfo=dt.timezone.utc)
|
||||
edge = dt.datetime.fromisoformat(str(cutoff).replace('Z', '+00:00'))
|
||||
if edge.tzinfo is None:
|
||||
edge = edge.replace(tzinfo=dt.timezone.utc)
|
||||
if stamp > edge:
|
||||
continue
|
||||
try:
|
||||
ran_day = ran.date() if hasattr(ran, "date") else dt.date.fromisoformat(str(ran)[:10])
|
||||
age = (dt.date.fromisoformat(ds) - ran_day).days
|
||||
|
|
@ -1224,6 +1238,13 @@ def company_reviews(codes, ds: str, *, read_pg=None) -> dict:
|
|||
"brain_used": bool(r.get("brain_used")) if r.get("brain_used") not in (None, "") and r.get("brain_used") == r.get("brain_used") else False,
|
||||
"ran_at": ran_day.isoformat() if ran_day else None, "age_days": age,
|
||||
"report_url": f"{config.AKG_API_BASE}{sm.get('report_path') or '/company/' + str(r.get('ts_code')) + '/review'}"}
|
||||
for field in ('period_fi', 'period_rf', 'grade_counts_b23', 'qual', 'n_materials',
|
||||
'lynch_category', 'expectation', 'cycle', 'thesis_lens_refs',
|
||||
'unknown_n', 'numbers_unmatched', 'review_ref'):
|
||||
out[k][field] = sm.get(field)
|
||||
out[k]['report_ran_at'] = stamp.isoformat() if ran is not None else None
|
||||
out[k]['report_version_url'] = (config.AKG_API_BASE + sm['report_version_path']
|
||||
if sm.get('report_version_path') else None)
|
||||
return out
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,27 @@
|
|||
"""离线测试使用的 HTTP 装饰器替身;路由函数本体仍执行 api.py 的实现。"""
|
||||
import sys
|
||||
import types
|
||||
|
||||
try:
|
||||
import fastapi
|
||||
except ImportError:
|
||||
class Router:
|
||||
def __init__(self, *a, **kw): pass
|
||||
def get(self, *a, **kw): return lambda fn: fn
|
||||
post = api_route = get
|
||||
def include_router(self, *a, **kw): pass
|
||||
|
||||
class HTTPException(Exception):
|
||||
def __init__(self, status_code, detail):
|
||||
self.status_code, self.detail = status_code, detail
|
||||
super().__init__(detail)
|
||||
|
||||
fa = types.ModuleType('fastapi')
|
||||
fa.FastAPI = fa.APIRouter = Router
|
||||
fa.Request = type('Request', (), {})
|
||||
fa.HTTPException = HTTPException
|
||||
fa.Header = fa.Query = lambda default=None, **kw: default
|
||||
sys.modules['fastapi'] = fa
|
||||
resp = types.ModuleType('fastapi.responses')
|
||||
resp.PlainTextResponse = lambda content, **kw: content
|
||||
sys.modules['fastapi.responses'] = resp
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
"""本地离线:python ../tradingSystem/scripts/unit_test_guard.py test_daily_plan_contracts.py。
|
||||
|
||||
统一 unittest 入口,复用已有断言组;缺席驱动与 HTTP 框架用替身,禁止联网。
|
||||
"""
|
||||
import importlib
|
||||
import unittest
|
||||
import test_plan_versions
|
||||
import test_api_support # noqa: F401
|
||||
|
||||
|
||||
def main():
|
||||
suite = unittest.TestSuite()
|
||||
for name in ('test_plan_versions', 'test_plan_reviews_live', 'test_plan_review_source'):
|
||||
suite.addTests(unittest.defaultTestLoader.loadTestsFromModule(importlib.import_module(name)))
|
||||
for name in ('test_plan_cache', 'test_plan_snapshot', 'test_plan_rank_axis',
|
||||
'test_plan_quality_rank', 'test_plan_verdict', 'test_plan_logic_state',
|
||||
'test_plan_review_heads', 'test_plan_review_window', 'test_review_latest',
|
||||
'test_review_targets', 'test_card', 'test_company_quality'):
|
||||
mod = importlib.import_module(name)
|
||||
funcs = [mod.main] if hasattr(mod, 'main') else [getattr(mod, n) for n in sorted(dir(mod)) if n.startswith('test_')]
|
||||
for fn in funcs:
|
||||
def run(fn=fn):
|
||||
assert fn() in (None, 0), fn.__module__
|
||||
suite.addTest(unittest.FunctionTestCase(run, description=name + '.' + fn.__name__))
|
||||
result = unittest.TextTestRunner(verbosity=2).run(suite)
|
||||
raise SystemExit(0 if result.wasSuccessful() else 1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -21,6 +21,7 @@ for _n in ("pandas", "pymysql", "psycopg"):
|
|||
_m.DataFrame = type("DataFrame", (), {})
|
||||
sys.modules[_n] = _m
|
||||
|
||||
import test_api_support # noqa: F401
|
||||
import api # noqa: E402
|
||||
import plan # noqa: E402
|
||||
|
||||
|
|
@ -43,7 +44,7 @@ class _Spy:
|
|||
self.delay = delay
|
||||
self._lock = threading.Lock()
|
||||
|
||||
def build(self, date, top, obs_top, theme_cap):
|
||||
def build(self, date, top, obs_top, theme_cap, decision_id=None):
|
||||
with self._lock:
|
||||
self.calls.append((date, top, obs_top, theme_cap))
|
||||
time.sleep(self.delay)
|
||||
|
|
@ -154,6 +155,12 @@ def test_ttl_covers_trading_day():
|
|||
|
||||
|
||||
def main():
|
||||
from unittest.mock import patch
|
||||
import plan_versions as pv
|
||||
with patch.object(pv, 'load', side_effect=lambda *a: {
|
||||
'date': plan._latest_date('unused'),
|
||||
'decision': {'id': plan._latest_date('unused')}}), \
|
||||
patch.object(pv, 'health', return_value={}):
|
||||
test_hit_and_miss()
|
||||
test_single_flight()
|
||||
test_different_keys_not_blocked()
|
||||
|
|
|
|||
|
|
@ -160,7 +160,7 @@ def test_9_theme_cap_exemption():
|
|||
print("[case9 主题限额:质地轴下无传导票免限额,有主题仍每主题五只]")
|
||||
no_theme = [{"code": f"n{i}", "evidence": None, "rank_axis": "quality"} for i in range(6)]
|
||||
t("无传导票免五只限额", len(plan.pick_rows(no_theme, 20, 5, uncapped_no_theme=True)) == 6)
|
||||
t("uncapped 为假时无传导仍只进五只", len(plan.pick_rows(no_theme, 20, 5, uncapped_no_theme=False)) == 5)
|
||||
t("旧轴无主题同样不合桶", len(plan.pick_rows(no_theme, 20, 5, uncapped_no_theme=False)) == 6)
|
||||
themed = [{"code": f"t{i}", "evidence": {"theme": "锂电"}, "rank_axis": "quality"} for i in range(6)]
|
||||
t("有主题仍受每主题五只", len(plan.pick_rows(themed, 20, 5, uncapped_no_theme=True)) == 5)
|
||||
|
||||
|
|
@ -214,7 +214,7 @@ def test_12_no_theme_by_axis():
|
|||
items = [(f"n{i}", 200.0 - i) for i in range(6)]
|
||||
none_theme = lambda kv: None # noqa: E731 —— 六条都无主题
|
||||
t("质地轴(uncapped 真)放六条", len(plan.take_by_theme(items, 20, 5, none_theme, True)) == 6)
|
||||
t("旧轴(uncapped 假)归无传导桶、限额五条", len(plan.take_by_theme(items, 20, 5, none_theme, False)) == 5)
|
||||
t("旧轴(uncapped 假)无主题同样免限额", len(plan.take_by_theme(items, 20, 5, none_theme, False)) == 6)
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
|||
|
|
@ -1,107 +1,48 @@
|
|||
"""周报底本改读当日快照(工作包丙,2026-09-17 拍板):先读快照、无快照回落现算、开关 live 回旧。
|
||||
|
||||
离线,不连库。跑法:python3 test_plan_review_source.py,预期最后一行是 ALL OK。
|
||||
"""
|
||||
"""离线:晨版固定、缺档排除、实际消费不读取当前参数。"""
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import types
|
||||
|
||||
# 离线不连库:缺席的驱动打空壳,pandas 本机装得上用真的。
|
||||
for _n in ("psycopg", "pymysql", "dotenv"):
|
||||
if _n not in sys.modules:
|
||||
try:
|
||||
__import__(_n)
|
||||
except Exception: # noqa: BLE001
|
||||
sys.modules[_n] = types.ModuleType(_n)
|
||||
|
||||
import pandas as pd # noqa: E402,F401 —— 只为触发真 pandas 导入,plan_review 要用
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
import common # noqa: E402
|
||||
import config # noqa: E402
|
||||
import plan # noqa: E402
|
||||
import plan_review # noqa: E402
|
||||
|
||||
FAILED = []
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import pandas as pd
|
||||
import test_plan_versions as fixture
|
||||
import plan
|
||||
import config
|
||||
import plan_versions as pv
|
||||
import plan_review as review
|
||||
|
||||
|
||||
def t(name, cond, extra=""):
|
||||
print((" ok " if cond else " FAIL ") + name + ((" " + str(extra)) if not cond else ""))
|
||||
if not cond:
|
||||
FAILED.append(name)
|
||||
class ReviewHistoryTest(unittest.TestCase):
|
||||
def test_first_published_morning_and_missing_history(self):
|
||||
with tempfile.TemporaryDirectory() as root, patch.object(config, 'PLAN_SNAPSHOT_DIR', root), \
|
||||
patch.object(plan, 'collect', side_effect=AssertionError('禁止重构历史')):
|
||||
with pv.writer(root):
|
||||
a = pv.publish(root, fixture.sample(), {'x': 1}, {}, cutoff_at=fixture.CUT, reason='晨版')
|
||||
p = fixture.sample()
|
||||
p['main'].reverse()
|
||||
pv.publish(root, p, {'x': 2}, {}, cutoff_at=fixture.CUT, reason='盘中修订')
|
||||
data, source = review._plan_for_review(fixture.DAY)
|
||||
self.assertEqual(data['decision'], a['decision'])
|
||||
self.assertEqual(data['_full']['main'][0]['code'], 'SH600001')
|
||||
for old_switch in ('snapshot', 'live'):
|
||||
with patch.object(config, 'PLAN_REVIEW_SOURCE', old_switch):
|
||||
with self.assertRaises(RuntimeError):
|
||||
review._plan_for_review('2000-01-01')
|
||||
pv.atomic(root + '/plan_2000-01-01.json', {'date': '2000-01-01', 'main': []})
|
||||
with self.assertRaises(RuntimeError):
|
||||
review._plan_for_review('2000-01-01')
|
||||
|
||||
def test_consumption_uses_saved_order_not_current_tiers(self):
|
||||
meta = {'kind': 'consumed', 'id': 'consumption1', 'decision': {'id': 'v1'},
|
||||
'order': ['600002.SH', '600001.SH'], 'params': {'tiers': ['旧门槛']}}
|
||||
def read(db, sql, params):
|
||||
self.assertEqual(db, 'pms')
|
||||
self.assertNotIn('pms_param', sql)
|
||||
return pd.DataFrame([{'roster_json': '[]', 'meta_json': json.dumps(meta), 'fetched_at': '2026-10-15'}])
|
||||
with patch.object(review.db, 'read_mysql', side_effect=read):
|
||||
codes, note = review.pms_roster(fixture.DAY)
|
||||
self.assertEqual(codes, ['SH600002', 'SH600001'])
|
||||
self.assertIn('consumption1', note)
|
||||
|
||||
|
||||
def main():
|
||||
day = "2026-09-16"
|
||||
snap = {"date": day,
|
||||
"main": [{"code": "600000.SH", "rank": 1}, {"code": "000001.SZ", "rank": 2}],
|
||||
"observe": [{"code": "600519.SH", "rank": 1}],
|
||||
"candidates": [{"code": "600000.SH"}], "watch": [{"code": "000001.SZ"}]}
|
||||
old_dir, old_src, old_collect = config.PLAN_SNAPSHOT_DIR, config.PLAN_REVIEW_SOURCE, plan.collect
|
||||
calls = {"n": 0}
|
||||
|
||||
def fake_collect(d, **kw):
|
||||
calls["n"] += 1
|
||||
return {"date": d, "_full": {"main": [{"code": "X", "rank": 1}], "observe": []},
|
||||
"candidates": [], "watch": []}
|
||||
|
||||
try:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
config.PLAN_SNAPSHOT_DIR = tmp
|
||||
plan.collect = fake_collect
|
||||
with open(os.path.join(tmp, f"plan_{day}.json"), "w", encoding="utf-8") as f:
|
||||
json.dump(snap, f)
|
||||
|
||||
# 1. 有快照:读快照,不现算
|
||||
config.PLAN_REVIEW_SOURCE = "snapshot"
|
||||
data, benben = plan_review._plan_for_review(day)
|
||||
t("有快照时底本为快照", benben == "快照", benben)
|
||||
t("_full.main 来自快照", [r["code"] for r in data["_full"]["main"]] == ["600000.SH", "000001.SZ"])
|
||||
t("observe 来自快照", [r["code"] for r in data["_full"]["observe"]] == ["600519.SH"])
|
||||
t("candidates 来自快照", [r["code"] for r in data["candidates"]] == ["600000.SH"])
|
||||
t("读快照不触发现算", calls["n"] == 0, calls["n"])
|
||||
|
||||
# 2. 无快照:回落现算
|
||||
_d2, benben2 = plan_review._plan_for_review("2026-09-15")
|
||||
t("无快照时底本为现算", benben2 == "现算", benben2)
|
||||
t("回落走了 plan.collect 一次", calls["n"] == 1, calls["n"])
|
||||
|
||||
# 3. 开关 live:即使有快照也现算,逐字回旧
|
||||
config.PLAN_REVIEW_SOURCE = "live"
|
||||
_d3, benben3 = plan_review._plan_for_review(day)
|
||||
t("live 时底本为现算(有快照也不读)", benben3 == "现算", benben3)
|
||||
t("live 又现算一次", calls["n"] == 2, calls["n"])
|
||||
finally:
|
||||
config.PLAN_SNAPSHOT_DIR, config.PLAN_REVIEW_SOURCE, plan.collect = old_dir, old_src, old_collect
|
||||
|
||||
# 复审退回修补 (2026-09-17): pms_roster 的档位白名单是独立门, 两种模式都先过滤再定序; 空白名单不过滤。
|
||||
roster = [
|
||||
{"c": "600000.SH", "b": "main", "t": "强传导", "s": 90, "r": 3},
|
||||
{"c": "000001.SZ", "b": "main", "t": "弱传导", "s": 100, "r": 1},
|
||||
{"c": "600519.SH", "b": "main", "t": "强传导", "s": 80, "r": 2},
|
||||
{"c": "000002.SZ", "b": "observe", "t": "强传导", "s": 70, "r": 1},
|
||||
]
|
||||
up = plan_review._roster_pick(roster, ["强传导"], "upstream", 10)
|
||||
t("upstream: 先过强传导再按名次升序 (600519 r=2 在 600000 r=3 前; 弱传导与观察档不入)",
|
||||
up == [common.to_prefix("600519.SH"), common.to_prefix("600000.SH")], up)
|
||||
old = plan_review._roster_pick(roster, ["强传导"], "score", 10)
|
||||
t("旧模式: 先过强传导再按分数降序 (600000 s=90 在 600519 s=80 前)",
|
||||
old == [common.to_prefix("600000.SH"), common.to_prefix("600519.SH")], old)
|
||||
nofilter = plan_review._roster_pick(roster, [], "upstream", 10)
|
||||
t("白名单为空: 不按档过滤 (弱传导 000001 r=1 也入且在最前, 主榜三只全在)",
|
||||
nofilter[0] == common.to_prefix("000001.SZ") and len(nofilter) == 3, nofilter)
|
||||
|
||||
print("-" * 70)
|
||||
if FAILED:
|
||||
print("FAILED %d: %s" % (len(FAILED), "; ".join(FAILED)))
|
||||
return 1
|
||||
print("ALL OK — 周报底本:有快照读快照 / 无快照回落现算 / 开关 live 回旧 / "
|
||||
"pms_roster 档位白名单先过滤再按 rank_mode 定序")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
|
|
@ -1,172 +1,49 @@
|
|||
"""计划接口按请求实时补个股深度评析(2026-09-10)。
|
||||
|
||||
## 为什么要有这一段
|
||||
|
||||
个股深度评析是全天在跑的(数据基座每小时一批),而计划一天只装配一次、
|
||||
接口又带十二小时缓存。于是装配那一刻还没出报告的票,当天就再也补不上。
|
||||
|
||||
2026-09-10 实测:早上七点十分那份计划里 154 行只有 7 只带评析;中午十二点二十三
|
||||
到三十五分之间新跑出五份报告,而这五只(688556.SH、605598.SH、300118.SZ、
|
||||
688698.SH、300638.SZ)正是持仓管理系统当天出提议的票 —— 它当天一份都看不到,
|
||||
卡片上写的全是「数据基座还没出这家的报告」。
|
||||
|
||||
三层原因叠在一起:评析的批次时刻晚于出计划、计划一天只装配一次、接口缓存十二小时。
|
||||
实时补充一次解掉后两层:评析什么时候跑完都行,跑完下一次请求就带上。
|
||||
|
||||
## 这里钉住什么
|
||||
|
||||
一、有新报告的行会被补上(结构化字段与整句都补)。
|
||||
二、**补充绝不能污染缓存**。get_plan 里的 dict(data) 只拷了顶层,main / observe
|
||||
里的行对象与缓存里那一份是同一批;不先拷贝就改,等于把当次请求的结果写进缓存,
|
||||
全天所有人都会拿到它。这一条是这个文件存在的主要理由。
|
||||
三、上游读不到时静默沿用装配时那一份,不抛异常、不拖垮计划接口。
|
||||
四、没有任何一只票有新报告时,行对象原样返回,不做无谓的拷贝。
|
||||
|
||||
离线,不连库不起服务(上游取数用替身)。但要在容器里跑 —— api.py 的依赖链里有
|
||||
`str | None` 这种写法,需要 Python 3.10 以上,而开发机是 3.9。
|
||||
|
||||
跑法(在 155 上):
|
||||
docker exec akg_factor_bridge python3 test_plan_reviews_live.py
|
||||
预期最后一行是 ALL OK。
|
||||
"""
|
||||
import sys
|
||||
import types
|
||||
|
||||
for _n in ("pandas", "pymysql", "psycopg"):
|
||||
if _n not in sys.modules:
|
||||
try:
|
||||
__import__(_n)
|
||||
except Exception: # noqa: BLE001
|
||||
_m = types.ModuleType(_n)
|
||||
if _n == "pandas":
|
||||
_m.DataFrame = type("DataFrame", (), {})
|
||||
sys.modules[_n] = _m
|
||||
|
||||
# 开发机上多半没装 fastapi(它只在容器里跑)。造一个够用的替身,
|
||||
# 这样这份测试在开发机与容器里都能跑,测的仍然是 api.py 里的真代码。
|
||||
if "fastapi" not in sys.modules:
|
||||
try:
|
||||
__import__("fastapi")
|
||||
except Exception: # noqa: BLE001
|
||||
_fa = types.ModuleType("fastapi")
|
||||
|
||||
class _StubApp:
|
||||
def get(self, *a, **k):
|
||||
return lambda f: f
|
||||
|
||||
def post(self, *a, **k):
|
||||
return lambda f: f
|
||||
|
||||
_fa.FastAPI = lambda *a, **k: _StubApp()
|
||||
_fa.HTTPException = type("HTTPException", (Exception,), {})
|
||||
_fa.Request = type("Request", (), {})
|
||||
sys.modules["fastapi"] = _fa
|
||||
_fr = types.ModuleType("fastapi.responses")
|
||||
_fr.PlainTextResponse = type("PlainTextResponse", (), {})
|
||||
sys.modules["fastapi.responses"] = _fr
|
||||
|
||||
import api # noqa: E402
|
||||
import sources # noqa: E402
|
||||
"""离线:查询只读完整发布版本,未来评析不能局部覆盖;缓存跟随版本。"""
|
||||
import copy
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
import test_plan_versions as fixture
|
||||
import test_api_support
|
||||
import api
|
||||
import config
|
||||
import sources
|
||||
import plan
|
||||
import plan_versions as pv
|
||||
|
||||
|
||||
def t(name, cond, extra=""):
|
||||
print((" ok " if cond else " FAIL ") + name + ((" " + str(extra)) if not cond else ""))
|
||||
assert cond, name
|
||||
class PublishedQueriesTest(unittest.TestCase):
|
||||
def test_all_queries_pin_version_and_cache_advances(self):
|
||||
with tempfile.TemporaryDirectory() as root, patch.object(config, 'PLAN_SNAPSHOT_DIR', root), \
|
||||
patch.object(sources, 'company_reviews', side_effect=AssertionError('查询不得读取新评析')), \
|
||||
patch.object(plan, 'collect', side_effect=AssertionError('查询不得重装配')):
|
||||
with pv.writer(root):
|
||||
a = pv.publish(root, fixture.sample(), {'x': 1}, {}, cutoff_at=fixture.CUT, reason='晨版')
|
||||
first, _ = api._plan_cached(fixture.DAY, 20, 10, 5, False)
|
||||
p = fixture.sample()
|
||||
p['main'][0].update(quality_band=0, verdict='仅展示')
|
||||
p['main'][0]['company_review']['overall'] = '差'
|
||||
with pv.writer(root):
|
||||
b = pv.publish(root, p, {'x': 2}, {}, cutoff_at=fixture.CUT, reason='评析更新')
|
||||
current, hit = api._plan_cached(fixture.DAY, 20, 10, 5, False)
|
||||
self.assertFalse(hit)
|
||||
self.assertNotEqual(first['decision']['id'], current['decision']['id'])
|
||||
self.assertEqual(current['main'][0]['company_review']['overall'], '差')
|
||||
rows, _ = api._published_stock_rows(['600001.SH'], fixture.DAY, a['decision']['id'])
|
||||
self.assertEqual(rows['SH600001']['company_review']['overall'], '好')
|
||||
self.assertEqual(rows['SH600001']['decision_version'], a['decision'])
|
||||
self.assertEqual(pv.load(root, fixture.DAY, morning=True), a)
|
||||
self.assertEqual(api.plan_dates()['dates'], [fixture.DAY])
|
||||
# 上游失联时仍只读取已发布对象,健康状态参与缓存键。
|
||||
pv.health(root, fixture.DAY, pending_codes=['SH600001'], error='来源不可用')
|
||||
blocked, hit = api._plan_cached(fixture.DAY, 20, 10, 5, False)
|
||||
self.assertFalse(hit)
|
||||
self.assertEqual(blocked['refresh_status']['pending_codes'], ['SH600001'])
|
||||
fixed, _ = api._plan_cached(fixture.DAY, 20, 10, 5, False, a['decision']['id'])
|
||||
self.assertEqual(fixed['refresh_status'], {})
|
||||
with self.assertRaises(api.HTTPException):
|
||||
api._published_stock_rows(['600001.SH'], '2000-01-01', None)
|
||||
|
||||
|
||||
def _plan(with_review_on=()):
|
||||
"""造一份计划:main 两行、observe 一行,指定哪些代码在装配时就已经有评析。"""
|
||||
def row(code):
|
||||
r = {"code": code, "name": code, "rank": 1}
|
||||
if code in with_review_on:
|
||||
r["company_review"] = {"overall": "旧", "period": "2025Q4"}
|
||||
r["company_review_text"] = "公司深度(旧那一份)"
|
||||
return r
|
||||
return {"date": "2026-09-09",
|
||||
"main": [row("SH688556"), row("SZ300118")],
|
||||
"observe": [row("SZ300638")]}
|
||||
|
||||
|
||||
def _fake_reviews(hit: dict):
|
||||
"""替身:sources.company_reviews 的返回形状是 {前缀码: 摘要字典},没报告的票不在里面。"""
|
||||
def f(codes, ds, **kw):
|
||||
return {c: hit[c] for c in codes if c in hit}
|
||||
return f
|
||||
|
||||
|
||||
def main():
|
||||
print("=" * 62)
|
||||
print("计划接口实时补评析")
|
||||
print("=" * 62)
|
||||
orig = sources.company_reviews
|
||||
try:
|
||||
# 一、有新报告的行被补上
|
||||
print("\n[A] 补上新报告")
|
||||
data = _plan()
|
||||
sources.company_reviews = _fake_reviews({
|
||||
"SH688556": {"overall": "差", "period": "2026Q1", "thesis": "论点一句"},
|
||||
"SZ300638": {"overall": "中", "period": "2026Q1"},
|
||||
})
|
||||
n = api._attach_fresh_reviews(data, "2026-09-09")
|
||||
t("补了两只(主榜一只、观察档一只)", n == 2, n)
|
||||
t("主榜那只拿到结构化字段", (data["main"][0].get("company_review") or {}).get("overall") == "差")
|
||||
t("主榜那只拿到整句", "公司深度" in (data["main"][0].get("company_review_text") or ""))
|
||||
t("观察档那只也补上了", (data["observe"][0].get("company_review") or {}).get("overall") == "中")
|
||||
t("没报告的那只原样不动", "company_review" not in data["main"][1])
|
||||
|
||||
# 二、不污染缓存 —— 这一条是重点
|
||||
print("\n[B] 不许污染缓存(行对象与缓存共用,必须先拷贝再改)")
|
||||
cached = _plan()
|
||||
keep_main0 = cached["main"][0] # 记住缓存里那个行对象本身
|
||||
keep_obs0 = cached["observe"][0]
|
||||
served = dict(cached) # get_plan 里就是这么浅拷一层
|
||||
sources.company_reviews = _fake_reviews({
|
||||
"SH688556": {"overall": "差", "period": "2026Q1"},
|
||||
"SZ300638": {"overall": "中", "period": "2026Q1"},
|
||||
})
|
||||
api._attach_fresh_reviews(served, "2026-09-09")
|
||||
t("这次请求拿到了补充", (served["main"][0].get("company_review") or {}).get("overall") == "差")
|
||||
t("缓存里那个行对象没被写进 company_review", "company_review" not in keep_main0)
|
||||
t("缓存里观察档那行也没被写", "company_review" not in keep_obs0)
|
||||
t("补过之后返回的是新的行对象", served["main"][0] is not keep_main0)
|
||||
t("缓存里那份列表本身没被改", cached["main"][0] is keep_main0)
|
||||
|
||||
# 三、覆盖旧版本
|
||||
print("\n[C] 装配时是旧报告,现在有新的 → 覆盖")
|
||||
data = _plan(with_review_on=("SH688556",))
|
||||
sources.company_reviews = _fake_reviews({"SH688556": {"overall": "好", "period": "2026Q2"}})
|
||||
api._attach_fresh_reviews(data, "2026-09-09")
|
||||
t("覆盖成新那一份", (data["main"][0].get("company_review") or {}).get("overall") == "好")
|
||||
t("整句跟着换", "旧那一份" not in (data["main"][0].get("company_review_text") or ""))
|
||||
|
||||
# 四、上游读不到 → 静默沿用,不抛
|
||||
print("\n[D] 上游读不到时不许拖垮计划接口")
|
||||
data = _plan(with_review_on=("SH688556",))
|
||||
def _boom(codes, ds, **kw):
|
||||
raise RuntimeError("PG 连不上")
|
||||
sources.company_reviews = _boom
|
||||
n = api._attach_fresh_reviews(data, "2026-09-09")
|
||||
t("补了零只、没有抛异常", n == 0, n)
|
||||
t("装配时那一份原样保留", (data["main"][0].get("company_review") or {}).get("overall") == "旧")
|
||||
|
||||
# 五、一只都没有新报告 → 不做无谓拷贝
|
||||
print("\n[E] 一只都没有新报告时不动行对象")
|
||||
data = _plan()
|
||||
keep = data["main"][0]
|
||||
sources.company_reviews = _fake_reviews({})
|
||||
n = api._attach_fresh_reviews(data, "2026-09-09")
|
||||
t("补了零只", n == 0, n)
|
||||
t("行对象原样,没有多余拷贝", data["main"][0] is keep)
|
||||
|
||||
# 六、空计划不炸
|
||||
print("\n[F] 空计划")
|
||||
n = api._attach_fresh_reviews({"date": "2026-09-09", "main": [], "observe": []}, "2026-09-09")
|
||||
t("补了零只、没有抛异常", n == 0, n)
|
||||
finally:
|
||||
sources.company_reviews = orig
|
||||
|
||||
print("\nALL OK")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
|||
|
|
@ -47,9 +47,9 @@ def test_pick_rules():
|
|||
got = plan.pick_rows(rows, 4, 2)
|
||||
t("每个主题最多两条,第三条光伏被跳过",
|
||||
[r["code"] for r in got] == ["A", "B", "D", "E"], [r["code"] for r in got])
|
||||
t("没有主题的行归到同一个桶,不当成各自独立",
|
||||
t("没有主题的行不归到同一个桶",
|
||||
[r["code"] for r in plan.pick_rows(
|
||||
[_row("X", None, 1), _row("Y", None, 2), _row("Z", "光伏", 3)], 3, 1)] == ["X", "Z"])
|
||||
[_row("X", None, 1), _row("Y", None, 2), _row("Z", "光伏", 3)], 3, 1)] == ["X", "Y", "Z"])
|
||||
# 质地轴对照(2026-09-17):同样两只无主题票,行带 rank_axis=quality 时免主题限额,两只都进
|
||||
t("质地轴下无主题行不受主题限额(对照上一条)",
|
||||
[r["code"] for r in plan.pick_rows(
|
||||
|
|
@ -105,7 +105,8 @@ def test_from_snapshot(tmpdir=None):
|
|||
for k in ("main_shown", "observe_shown", "shown_params"):
|
||||
t("不把落盘那次的裁剪结果带出去(%s)" % k, k not in d)
|
||||
for k in ("market", "regime"):
|
||||
t("不把快照里的 %s 带出去(接口每次实时读)" % k, k not in d)
|
||||
t("环境字段来自指定快照,不在查询时覆盖(%s)" % k,
|
||||
d.get(k) == plan.plan_versions.load(config.PLAN_SNAPSHOT_DIR, ds).get(k))
|
||||
for k in ("date", "generated_at", "plan_version", "counts", "candidates", "watch",
|
||||
"segments_pointed", "card_counts", "card_params", "changes", "encoding"):
|
||||
t("顶层保留 %s" % k, k in d)
|
||||
|
|
@ -179,17 +180,20 @@ def test_freshness_guard():
|
|||
|
||||
|
||||
def test_api_falls_back():
|
||||
print("[接口回落:读不到快照不能让接口挂掉]")
|
||||
src = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "api.py"),
|
||||
encoding="utf-8").read()
|
||||
t("有总开关,能一键回到每次现算的老路", "PLAN_FROM_SNAPSHOT" in src)
|
||||
t("快照那条路包了兜底,任何异常都回落", "except Exception" in src and "回落到实时装配" in src)
|
||||
t("回落一定记一行警告说明原因(不然只会表现成偶尔变慢)",
|
||||
"_access.warning" in src and "快照读取失败" in src)
|
||||
i_try = src.index("plan.from_snapshot")
|
||||
i_live = src.index("plan.collect(date, top, obs_top, theme_cap)")
|
||||
t("先试快照、后现算", i_try < i_live)
|
||||
t("访问日志写明这次走的哪条路", "source=%s" in src)
|
||||
import ast
|
||||
from types import SimpleNamespace
|
||||
print('[缺历史快照不能用当前研究补造]')
|
||||
src = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'api.py'), encoding='utf-8').read()
|
||||
fn = next(n for n in ast.parse(src).body if isinstance(n, ast.FunctionDef) and n.name == '_plan_build')
|
||||
def missing(*a, **k): raise FileNotFoundError('无历史版本')
|
||||
def forbidden(*a, **k): raise AssertionError('查询禁止重装配')
|
||||
env = {'plan': SimpleNamespace(from_snapshot=missing, collect=forbidden)}
|
||||
exec(compile(ast.Module(body=[fn], type_ignores=[]), 'api.py', 'exec'), env)
|
||||
try:
|
||||
env['_plan_build']('2020-01-02', 20, 10, 5)
|
||||
t('缺档必须明确失败', False)
|
||||
except FileNotFoundError:
|
||||
t('缺档失败,没有回落当前数据', True)
|
||||
|
||||
|
||||
def main():
|
||||
|
|
|
|||
|
|
@ -0,0 +1,245 @@
|
|||
"""本地离线:python -m unittest test_plan_versions;全部来源用合成输入与替身。"""
|
||||
import copy
|
||||
import datetime as dt
|
||||
import json
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
import sys
|
||||
import types
|
||||
from contextlib import ExitStack
|
||||
from unittest.mock import patch
|
||||
|
||||
import pandas as pd
|
||||
for name in ('psycopg', 'pymysql'):
|
||||
try:
|
||||
__import__(name)
|
||||
except ImportError:
|
||||
sys.modules[name] = types.ModuleType(name)
|
||||
import plan
|
||||
import plan_inputs as pi
|
||||
import plan_versions as pv
|
||||
import sources
|
||||
|
||||
|
||||
DAY = '2026-10-14'
|
||||
CUT = '2026-10-15T07:10:00+08:00'
|
||||
|
||||
|
||||
def sample():
|
||||
return {'date': DAY, 'plan_version': 'test', 'main': [
|
||||
{'code': 'SH600001', 'rank': 1, 'rank_axis': 'quality', 'quality_band': 3,
|
||||
'company_review': {'overall': '好', 'review_ref': 'a', 'period_fi': '2026Q2'}},
|
||||
{'code': 'SH600002', 'rank': 2, 'rank_axis': 'quality'}],
|
||||
'observe': [], 'candidates': [], 'watch': []}
|
||||
|
||||
|
||||
class VersionsTest(unittest.TestCase):
|
||||
def test_revision_window_and_no_replacement_morning(self):
|
||||
import plan_refresh as refresh
|
||||
calendar = types.ModuleType('chinese_calendar')
|
||||
calendar.is_holiday = lambda day: day == dt.date(2026, 10, 1)
|
||||
with patch.dict(sys.modules, {'chinese_calendar': calendar}):
|
||||
for clock, expected in [('07:19:59', False), ('07:20:00', True), ('15:10:00', True), ('15:10:01', False)]:
|
||||
self.assertEqual(refresh.in_window(dt.datetime.fromisoformat('2026-10-15T' + clock + '+08:00')), expected)
|
||||
self.assertFalse(refresh.in_window(dt.datetime.fromisoformat('2026-10-01T09:00:00+08:00')))
|
||||
self.assertFalse(refresh.in_window(dt.datetime.fromisoformat('2026-10-17T09:00:00+08:00')))
|
||||
with patch.object(refresh.config, 'PLAN_SNAPSHOT_DIR', self.root):
|
||||
self.assertEqual(refresh.refresh(now=dt.datetime.fromisoformat('2026-10-15T09:00:00+08:00'))['status'], 'no_morning')
|
||||
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(self.tmp.cleanup)
|
||||
self.root = self.tmp.name
|
||||
|
||||
def publish(self, p=None, inputs=None, params=None):
|
||||
with pv.writer(self.root):
|
||||
return pv.publish(self.root, p or sample(), inputs or {'x': 1}, params or {},
|
||||
cutoff_at=CUT, reason='测试')
|
||||
|
||||
def test_append_idempotent_and_return_to_previous_inputs(self):
|
||||
a = self.publish()
|
||||
self.assertEqual(a, self.publish())
|
||||
b = self.publish(inputs={'x': 2})
|
||||
c = self.publish()
|
||||
self.assertEqual(c['decision']['parent_id'], b['decision']['id'])
|
||||
self.assertNotEqual(c['decision']['id'], a['decision']['id'])
|
||||
self.assertEqual(pv.load(self.root, DAY, morning=True), a)
|
||||
self.assertEqual(pv.load(self.root, decision_id=a['decision']['id']), a)
|
||||
|
||||
def test_failed_pointer_keeps_complete_previous_version(self):
|
||||
a = self.publish()
|
||||
real = pv.atomic
|
||||
def fail(path, value):
|
||||
if Path(path).name == 'plan_' + DAY + '.json':
|
||||
raise OSError('模拟断电')
|
||||
return real(path, value)
|
||||
with patch.object(pv, 'atomic', fail), self.assertRaises(OSError):
|
||||
self.publish(inputs={'x': 2})
|
||||
self.assertEqual(pv.load(self.root, DAY), a)
|
||||
orphans = [p.stem for p in (Path(self.root) / 'versions' / DAY).glob(DAY + '_*.json')
|
||||
if p.stem != a['decision']['id']]
|
||||
with self.assertRaises(FileNotFoundError):
|
||||
pv.load(self.root, decision_id=orphans[0])
|
||||
recovered = self.publish(inputs={'x': 2})
|
||||
self.assertEqual(recovered['decision']['parent_id'], a['decision']['id'])
|
||||
|
||||
def test_first_morning_pointer_recovers_after_interruption(self):
|
||||
real = pv.atomic
|
||||
def fail(path, value):
|
||||
if Path(path).name == 'morning.json':
|
||||
raise OSError('晨版指针中断')
|
||||
return real(path, value)
|
||||
with patch.object(pv, 'atomic', fail), self.assertRaises(OSError):
|
||||
self.publish()
|
||||
first = pv.load(self.root, DAY)
|
||||
self.assertEqual(pv.load(self.root, DAY, morning=True), first)
|
||||
self.publish(inputs={'x': 2})
|
||||
self.assertEqual(pv.load(self.root, DAY, morning=True), first)
|
||||
|
||||
def test_parallel_duplicate_publish_has_one_version(self):
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
with ThreadPoolExecutor(4) as pool:
|
||||
values = list(pool.map(lambda _: self.publish(), range(8)))
|
||||
self.assertEqual(len({p['decision']['id'] for p in values}), 1)
|
||||
self.assertEqual(len(pv.history(self.root, DAY)), 1)
|
||||
|
||||
def test_health_exposes_stopped_refresh_and_corrupt_pointer(self):
|
||||
import plan_health
|
||||
import plan_refresh
|
||||
self.publish()
|
||||
pv.atomic(Path(self.root) / 'versions' / DAY / 'health.json', {'checked_at': CUT})
|
||||
with patch.object(plan_refresh, 'in_window', return_value=True):
|
||||
status, reading = plan_health.inspect(self.root, dt.datetime.fromisoformat('2026-10-15T08:00:00+08:00'))
|
||||
self.assertEqual(status, 1)
|
||||
self.assertIn('十二分钟', reading['error'])
|
||||
bad = pv.load(self.root, DAY)
|
||||
bad['main'][0]['score'] = 999
|
||||
pv.atomic(Path(self.root) / ('plan_' + DAY + '.json'), bad)
|
||||
self.assertEqual(plan_health.inspect(self.root)[0], 2)
|
||||
|
||||
def test_full_rank_does_not_follow_query_clipping(self):
|
||||
self.publish()
|
||||
with patch.object(plan.config, 'PLAN_SNAPSHOT_DIR', self.root):
|
||||
small = plan.from_snapshot(DAY, top=1)
|
||||
big = plan.from_snapshot(DAY, top=20)
|
||||
self.assertEqual(small['main'][0]['research_rank'], big['main'][0]['research_rank'])
|
||||
self.assertEqual(big['main'][1]['research_rank'], 2)
|
||||
|
||||
def test_changed_review_all_exports_share_research(self):
|
||||
a = self.publish()
|
||||
p = sample()
|
||||
p['main'][0]['company_review'].update(overall='差', review_ref='b')
|
||||
p['candidates'] = [p['main'][0]]
|
||||
b = self.publish(p, {'x': 2})
|
||||
self.assertEqual(b['main'][0]['research'], b['candidates'][0]['research'])
|
||||
self.assertEqual(b['main'][0]['research']['changes'][0]['field'], 'overall')
|
||||
self.assertEqual(pv.load(self.root, decision_id=a['decision']['id'])['main'][0]['company_review']['overall'], '好')
|
||||
|
||||
def test_config_change_publishes_and_path_is_validated(self):
|
||||
a = self.publish()
|
||||
self.assertNotEqual(self.publish(params={'cap': 5})['decision']['id'], a['decision']['id'])
|
||||
with self.assertRaises(ValueError):
|
||||
pv.load(self.root, decision_id='../../config')
|
||||
|
||||
def test_capture_replay_typed_inputs_and_fail_closed(self):
|
||||
frame = pd.DataFrame({'code': ['A', 'B'], 'score': [1., float('nan')],
|
||||
'date': pd.to_datetime(['2026-10-14', None])})
|
||||
b = pi.Bundle(params={'CAP': 5})
|
||||
with b.use():
|
||||
first = pi.read('prices', lambda: frame)
|
||||
recorded = json.loads(json.dumps(b.records))
|
||||
with pi.Bundle(records=recorded).use():
|
||||
again = pi.read('prices', lambda: self.fail('重放不得回源'))
|
||||
pd.testing.assert_frame_equal(first, again)
|
||||
with self.assertRaises(RuntimeError):
|
||||
with pi.Bundle(records=recorded).use():
|
||||
try:
|
||||
pi.read('new-source', lambda: None)
|
||||
except RuntimeError:
|
||||
pass # 上层吞错也不能发布不完整输入。
|
||||
|
||||
def test_future_review_never_leaks_but_morning_can_use_known_review(self):
|
||||
def reader(sql, params):
|
||||
self.assertIn('ran_at <=', sql)
|
||||
return pd.DataFrame([{'ts_code': '600001.SH', 'period': '2026Q2',
|
||||
'summary': {'overall': '好'}, 'grades': {}, 'ran_at': '2026-10-15T06:50:00+08:00'}])
|
||||
self.assertEqual(sources.company_reviews(['SH600001'], DAY, read_pg=reader), {})
|
||||
self.assertEqual(sources.company_reviews(['SH600001'], DAY, read_pg=reader,
|
||||
cutoff_at=CUT)['SH600001']['overall'], '好')
|
||||
|
||||
def test_source_failure_does_not_mean_no_review(self):
|
||||
with self.assertRaises(OSError):
|
||||
sources.company_reviews(['SH600001'], DAY, strict=True,
|
||||
read_pg=lambda *a: (_ for _ in ()).throw(OSError('offline')))
|
||||
|
||||
def test_complete_revision_replays_real_assembly_without_other_sources(self):
|
||||
import config
|
||||
import factors
|
||||
import judgement
|
||||
import logic_state_daily
|
||||
import plan_refresh
|
||||
codes = ['SH600001', 'SH600002']
|
||||
reviews = {c: {'overall': '好', 'doubt_hard': False, 'age_days': 0,
|
||||
'ran_at': DAY, 'review_ref': 'first'} for c in codes}
|
||||
with ExitStack() as stack:
|
||||
def patcher(obj, name, **kw): return stack.enter_context(patch.object(obj, name, **kw))
|
||||
patcher(config, 'PLAN_SNAPSHOT_DIR', new=self.root)
|
||||
patcher(config, 'PLAN_RANK_AXIS', new='quality')
|
||||
patcher(config, 'CARD_COMPANY_QUALITY_GATE', new=True)
|
||||
patcher(config, 'LOGIC_PATH_COMPANY', new=True)
|
||||
patcher(plan, '_factor', side_effect=lambda table, day: pd.Series(
|
||||
[240., 230.] if 'score' in table else [0.3, 0.2] if 'upside' in table else [2., 2.], index=codes))
|
||||
patcher(plan, '_latest_date', return_value=DAY)
|
||||
patcher(plan, '_prev_date', return_value=None)
|
||||
patcher(plan, '_names', return_value=dict(zip(codes, ['甲', '乙'])))
|
||||
patcher(plan, '_evidence', return_value=({c: ('材料', 3, .1, 10, 1) for c in codes}, {DAY}))
|
||||
patcher(plan, '_segments_of', return_value={})
|
||||
patcher(factors, '_risk_set', return_value=set())
|
||||
patcher(judgement, 'load_previous', return_value=[])
|
||||
patcher(judgement, 'recent_rows', return_value={})
|
||||
patcher(logic_state_daily, 'history', return_value={})
|
||||
for name in ('moved_members', 'broker_reports', 'logic_claims', 'broker_actions',
|
||||
'close_prices', 'valuation_scenarios', 'analyst_events', 'event_day_fields',
|
||||
'news_flashes', 'segment_catalysts', 'catalysts_for_codes'):
|
||||
patcher(sources, name, return_value={})
|
||||
patcher(sources, 'stock_daily', return_value={c: {'pct0': 3} for c in codes})
|
||||
patcher(sources, 'night_conclusions', return_value={c: {'accum_state': '明确吸筹', 'accum_age': 0} for c in codes})
|
||||
source = patcher(sources, 'company_reviews', side_effect=lambda *a, **kw: copy.deepcopy(reviews))
|
||||
b = pi.Bundle(params=pi.effective_config(config), cutoff_at=CUT)
|
||||
with b.use():
|
||||
data = plan.collect(DAY)
|
||||
data.update(data.pop('_full'))
|
||||
inputs = {'records': b.records, 'market': {}, 'regime': None}
|
||||
with pv.writer(self.root):
|
||||
a = pv.publish(self.root, data, inputs, b.params, cutoff_at=CUT, reason='晨版')
|
||||
first = copy.deepcopy(a)
|
||||
reviews[codes[0]].update(overall='差', doubt_hard=True, review_ref='second')
|
||||
patcher(plan_refresh, 'in_window', return_value=True)
|
||||
result = plan_refresh.refresh(now=dt.datetime.fromisoformat('2026-10-15T09:00:00+08:00'))
|
||||
self.assertEqual(result['status'], 'published')
|
||||
revised = pv.load(self.root, DAY)
|
||||
bycode = {r['code']: r for r in revised['main']}
|
||||
self.assertEqual(revised['main'][0]['code'], codes[1])
|
||||
self.assertEqual(bycode[codes[0]]['company_review']['overall'], '差')
|
||||
self.assertNotEqual(bycode[codes[0]]['quality_band'], first['main'][0]['quality_band'])
|
||||
self.assertNotEqual(bycode[codes[0]]['verdict'], first['main'][0]['verdict'])
|
||||
self.assertEqual(pv.load(self.root, decision_id=first['decision']['id']), first)
|
||||
source.side_effect = OSError('上游不可达')
|
||||
with self.assertRaises(OSError):
|
||||
plan_refresh.refresh(now=dt.datetime.fromisoformat('2026-10-15T09:05:00+08:00'))
|
||||
self.assertEqual(pv.load(self.root, DAY), revised)
|
||||
self.assertEqual(pv.health(self.root, DAY)['pending_codes'], codes)
|
||||
|
||||
def test_evidence_ties_independent_of_database_row_order(self):
|
||||
rows = [{'ts_code': '600001.SH', 'target': target, 'n_sources': 2,
|
||||
'moved_ratio': .5, 'members_total': 10, 'moved': 5, 'mkt_trade_date': DAY}
|
||||
for target in ('甲', '乙')]
|
||||
with patch.object(plan.db, 'read_pg', return_value=pd.DataFrame(rows)):
|
||||
first = plan._evidence(DAY)
|
||||
with patch.object(plan.db, 'read_pg', return_value=pd.DataFrame(rows[::-1])):
|
||||
self.assertEqual(plan._evidence(DAY), first)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
4
xxl.py
4
xxl.py
|
|
@ -50,8 +50,10 @@ HERE = os.path.dirname(os.path.abspath(__file__))
|
|||
LOG_PATH = os.path.join(HERE, "data", "xxl_build.log")
|
||||
|
||||
# 步骤 → 命令(顺序即执行顺序;--date 由触发参数统一追加)
|
||||
STEP_ORDER = ("build", "plan", "push-pool", "regime-append", "judgement-snapshot", "plan-review")
|
||||
STEP_ORDER = ("build", "plan", "push-pool", "regime-append", "plan-refresh", "judgement-snapshot", "plan-review")
|
||||
STEP_CMDS = {
|
||||
# 北京时间 07:20—15:10 每五分钟;独立任务,不重复晨版构建。
|
||||
"plan-refresh": ["run.py", "plan-refresh"],
|
||||
"build": ["run.py", "build", "all", "--mode", "daily"],
|
||||
"plan": ["run.py", "plan"],
|
||||
"push-pool": ["run.py", "push-pool"],
|
||||
|
|
|
|||
Loading…
Reference in New Issue