Compare commits
6 Commits
main
...
codex/plan
| Author | SHA1 | Date |
|---|---|---|
|
|
c11ae6591e | |
|
|
0a112783c4 | |
|
|
04339be2b0 | |
|
|
a9fc789c86 | |
|
|
69a18f287e | |
|
|
504f03661a |
82
api.py
82
api.py
|
|
@ -20,7 +20,7 @@ import time
|
|||
import threading
|
||||
|
||||
import pandas as pd
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi import FastAPI, HTTPException, Request, Body, Header, BackgroundTasks
|
||||
from fastapi.responses import PlainTextResponse, JSONResponse, HTMLResponse
|
||||
|
||||
import card
|
||||
|
|
@ -96,6 +96,58 @@ def dividend_candidate_feed(code: str = None):
|
|||
raise HTTPException(status_code=503, detail="高股息资料暂不可用,请稍后再试") from exc
|
||||
|
||||
|
||||
@app.get('/plan/user-candidates/search')
|
||||
def search_user_candidates(q: str = ''):
|
||||
import user_candidates as uc
|
||||
query = q.strip().upper()[:40]
|
||||
if not query:
|
||||
return {'ok': True, 'items': []}
|
||||
names = uc.catalog()
|
||||
items = []
|
||||
for code, name in names.items():
|
||||
try:
|
||||
uc.normalize(code)
|
||||
except ValueError:
|
||||
continue
|
||||
if query in code or query in name.upper():
|
||||
items.append({'code': code, 'name': name})
|
||||
return {'ok': True, 'items': sorted(items, key=lambda x: x['code'])[:30]}
|
||||
|
||||
|
||||
@app.get('/plan/user-candidates')
|
||||
def list_user_candidates():
|
||||
import user_candidates as uc
|
||||
return uc.listing(config.PLAN_SNAPSHOT_DIR)
|
||||
|
||||
|
||||
@app.post('/plan/user-candidates')
|
||||
def add_user_candidates(background_tasks: BackgroundTasks, payload: dict = Body(...),
|
||||
x_job_key: str = Header(None)):
|
||||
from xxl import _require_key
|
||||
import user_candidates as uc
|
||||
_require_key(x_job_key, None)
|
||||
try:
|
||||
result = uc.save(config.PLAN_SNAPSHOT_DIR, payload, payload.get('actor') or 'PMS', names=uc.catalog())
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
background_tasks.add_task(uc.refresh_saved)
|
||||
return result
|
||||
|
||||
|
||||
@app.post('/plan/user-candidates/{identity}/remove')
|
||||
def remove_user_candidate(identity: str, background_tasks: BackgroundTasks,
|
||||
payload: dict = Body(...), x_job_key: str = Header(None)):
|
||||
from xxl import _require_key
|
||||
import user_candidates as uc
|
||||
_require_key(x_job_key, None)
|
||||
try:
|
||||
result = uc.remove(config.PLAN_SNAPSHOT_DIR, identity, payload.get('actor') or 'PMS')
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
background_tasks.add_task(uc.refresh_saved)
|
||||
return result
|
||||
|
||||
|
||||
@app.get("/plan/dates")
|
||||
def plan_dates(limit: int = 30):
|
||||
from pathlib import Path
|
||||
|
|
@ -128,12 +180,13 @@ _PLAN_CACHE_MAX = 8 # 不同参数组合最多留几份,防内存慢
|
|||
_PLAN_BUILD_LOCKS: dict = {} # 每个缓存键一把「正在算」的锁,见 _plan_cached 里的说明
|
||||
|
||||
|
||||
def _plan_build(date, top, obs_top, theme_cap, decision_id=None) -> dict:
|
||||
def _plan_build(date, top, obs_top, theme_cap, decision_id=None, include_user_candidates=False) -> dict:
|
||||
"""只读已发布版本。缺档明确报错,禁止用当前研究重构历史。"""
|
||||
return plan.from_snapshot(date, top, obs_top, theme_cap, decision_id=decision_id)
|
||||
return plan.from_snapshot(date, top, obs_top, theme_cap, decision_id=decision_id,
|
||||
include_user_candidates=include_user_candidates)
|
||||
|
||||
|
||||
def _plan_cached(date, top, obs_top, theme_cap, nocache: bool, decision_id=None):
|
||||
def _plan_cached(date, top, obs_top, theme_cap, nocache: bool, decision_id=None, include_user_candidates=False):
|
||||
"""返回 (data, 命中与否)。data 是 plan.collect 的结果(已去掉 _full)。
|
||||
|
||||
键里放**真实数据日**而不是请求里的 date:不传 date 时数据日随构建更新,明早换日后
|
||||
|
|
@ -143,7 +196,10 @@ def _plan_cached(date, top, obs_top, theme_cap, nocache: bool, decision_id=None)
|
|||
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))
|
||||
if not decision_id and not date:
|
||||
import user_candidates as uc
|
||||
state = dict(state, pending_codes=sorted(set(state.get('pending_codes') or []) | set(uc.live_pending(config.PLAN_SNAPSHOT_DIR, published))))
|
||||
key = (pinned or pv.digest(published), pv.digest(state), int(top), int(obs_top), int(theme_cap), bool(include_user_candidates))
|
||||
now = time.time()
|
||||
if not nocache and PLAN_CACHE_SEC > 0:
|
||||
with _PLAN_CACHE_LOCK:
|
||||
|
|
@ -167,7 +223,8 @@ def _plan_cached(date, top, obs_top, theme_cap, nocache: bool, decision_id=None)
|
|||
hit = _PLAN_CACHE.get(key)
|
||||
if hit and time.time() - hit[0] < PLAN_CACHE_SEC:
|
||||
return hit[1], True
|
||||
data = _plan_build(ds_key, top, obs_top, theme_cap, pinned)
|
||||
data = _plan_build(ds_key, top, obs_top, theme_cap, pinned,
|
||||
**({'include_user_candidates': True} if include_user_candidates else {}))
|
||||
data['refresh_status'] = state
|
||||
if PLAN_CACHE_SEC > 0:
|
||||
with _PLAN_CACHE_LOCK:
|
||||
|
|
@ -184,14 +241,14 @@ def _plan_cached(date, top, obs_top, theme_cap, nocache: bool, decision_id=None)
|
|||
@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,
|
||||
decision_id: str | None = None):
|
||||
decision_id: str | None = None, include_user_candidates: bool = False):
|
||||
"""向下兼容承诺(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), decision_id)
|
||||
data, cached = _plan_cached(date, top, obs_top, theme_cap, bool(nocache), decision_id, include_user_candidates)
|
||||
except (RuntimeError, ValueError, OSError) as e:
|
||||
raise HTTPException(status_code=404, detail=str(e))
|
||||
data = dict(data) # 下面几个键按请求写,不污染缓存里那一份
|
||||
|
|
@ -216,11 +273,12 @@ def get_plan(request: Request, date: str | None = None, format: str = "json",
|
|||
|
||||
|
||||
@app.get('/plan/check')
|
||||
def check_plan_stock(code: str):
|
||||
def check_plan_stock(code: str, include_user_candidates: bool = False):
|
||||
"""采纳专用逐票读取。只读已发布计划,不装配、不查研究库。"""
|
||||
from common import to_prefix
|
||||
try:
|
||||
return JSONResponse(content=plan_versions.stock_check(config.PLAN_SNAPSHOT_DIR, to_prefix(code)))
|
||||
return JSONResponse(content=plan_versions.stock_check(config.PLAN_SNAPSHOT_DIR, to_prefix(code),
|
||||
include_user_candidates=include_user_candidates))
|
||||
except (OSError, ValueError, RuntimeError) as e:
|
||||
raise HTTPException(status_code=503, detail=str(e))
|
||||
|
||||
|
|
@ -232,13 +290,15 @@ def refresh(date: str | None = None, key: str | None = None):
|
|||
key=key, x_job_key=None)
|
||||
|
||||
|
||||
def _published_stock_rows(want, date, decision_id):
|
||||
def _published_stock_rows(want, date, decision_id, include_user_candidates=False):
|
||||
try:
|
||||
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 {}, {}
|
||||
import user_candidates as uc
|
||||
published = uc.consumer_view(published, include_user_candidates)
|
||||
rows = {r['code']: dict(r, decision=bucket, source='published',
|
||||
traceability=published.get('traceability') or 'legacy_unverified',
|
||||
decision_version=published.get('decision'), date=published['date'],
|
||||
|
|
|
|||
10
card.py
10
card.py
|
|
@ -191,8 +191,14 @@ def judge(ev: dict[str, Any], *, start_pct: float = 3.0,
|
|||
if gates["covered"]:
|
||||
reasons.append(f"券商覆盖,预期空间 {upside:+.0%}")
|
||||
|
||||
all_gates = all(gates.values())
|
||||
failed = [k for k, ok in gates.items() if not ok]
|
||||
# 人工来源替代发现条件;pointed 仍保留真实传导状态,不改变排序和其他门槛。
|
||||
user_selected = bool(ev.get('user_selected'))
|
||||
if user_selected and not gates['pointed']:
|
||||
missing = [x for x in missing if x != '所在环节当日没有被传导指向']
|
||||
reasons.insert(0, '用户添加,按现有条件评估')
|
||||
effective = dict(gates, pointed=gates['pointed'] or user_selected)
|
||||
all_gates = all(effective.values())
|
||||
failed = [k for k, ok in effective.items() if not ok]
|
||||
# 判决依据是这张卡上唯一一句"为什么",PMS 原样显示在页面上给人读。所以它必须是
|
||||
# 一句完整的中文,带上具体数字,不许出现门槛的变量名(started 这类)或信号的
|
||||
# 枚举值(SELL 这类)——那些是系统之间传递用的,不是给人看的。
|
||||
|
|
|
|||
|
|
@ -0,0 +1,103 @@
|
|||
# 个股评析全池覆盖开发规格
|
||||
|
||||
日期为2026年10月28日。本文时间均为北京时间。
|
||||
|
||||
## 目标与授权
|
||||
|
||||
用户要求本轮覆盖整个产业池和高股息池。
|
||||
当前候选优先,未完成对象不因每日名单变化而丢失。
|
||||
本轮补普通个股评析,不自动扩大分红专项研究。
|
||||
股票资格、排序、模型判据和交易参数保持。
|
||||
|
||||
## 改前实测
|
||||
|
||||
178基座提交7ef107e,评析队列为零。
|
||||
当前目标148家,全部已有报告、定性及评议。
|
||||
产业池3833家,仅747家有报告。
|
||||
桥高股息池169家,仅9家有报告。
|
||||
两个池有交集,缺口不能直接相加。
|
||||
本轮只读预览并集3885家,已有747家,首次报告缺口3138家。
|
||||
高股息缺报告的160家均未进入留存的评析目标名单。
|
||||
|
||||
近72小时43次成功运行平均185.5秒。
|
||||
第95百分位246.4秒,最大286.5秒。
|
||||
近七天评议平均输出13465个token。
|
||||
原每日上限为评议150家、定性200家、评议输出300万token。
|
||||
|
||||
## 具体工作
|
||||
|
||||
1. 复用原每小时批任务和review队列,不新增任务或表。
|
||||
2. 自动读取全部产业池成员。
|
||||
3. 从桥已发布的高股息研究接口读取研究对象。
|
||||
不按投资资格eligible过滤,基座不重新计算股息筛选。
|
||||
4. 在现有job_runs保存累计研究名单,名单变化才追加快照。
|
||||
已纳入对象继续保留。来源读取失败保留旧名单并报告。
|
||||
旧快照读失败时停止扩展投递,不能用空名单覆盖。
|
||||
5. 新报告优先于名单外旧报告升级。
|
||||
当前候选保留优先名额,剩余名额持续补名单外对象。
|
||||
旧报告的复核不能无限挤占首次报告。
|
||||
6. 每批完整运行最多30家。每日评议、定性各600家。
|
||||
评议输出上限900万token,单次模型预算保持。
|
||||
每日额度不足时不继续投递完整运行。
|
||||
7. 用Redis短期凭据防止重复投递。
|
||||
失败对象延后重试,队列积压时暂停增加整批任务。
|
||||
重试不删除旧报告,不清空历史记录。
|
||||
8. 名单外补齐避开06:00至08:00、20:00至23:00。
|
||||
原候选任务仍按原调度执行。
|
||||
晚间行业催化与分红专项使用原队列和时刻。
|
||||
9. 日检分别显示有报告、定性、评议及首次报告缺口。
|
||||
无财务数据、材料薄和失败不能宣称完整研究已完成。
|
||||
|
||||
## 偏差发现与撤销
|
||||
|
||||
新增配置文件控制覆盖开关和每日额度。
|
||||
关闭覆盖开关即回到原目标集合与每日额度。
|
||||
旧报告、固定引用和累计名单保留。
|
||||
日检报告来源失败、剩余缺口和最近调度状态。
|
||||
同一对象反复失败时先暂停其重试,不阻塞后面的对象。
|
||||
|
||||
按每日有效完成约450至550家估算,首次覆盖约需一周。
|
||||
这只是按现有耗时计算的容量预估。
|
||||
上游缺财务数据及缺年报的对象单列,不能承诺完整结论。
|
||||
|
||||
## 分批验收预期
|
||||
|
||||
### 本地
|
||||
|
||||
- 合并去重和排序确定,重复读取不改变名单。
|
||||
- 高股息资格尚未通过的对象仍能进入研究。
|
||||
- 来源暂不可用时累计待办不丢,旧快照损坏时不覆盖。
|
||||
- 当前候选和首次报告均能获得名额。
|
||||
- 预算不足、已有在途及失败冷却时不重复投递。
|
||||
- 保护时段只运行原候选。
|
||||
- 旧指纹、有效标志、固定报告引用和财务期口径不变。
|
||||
- 全部离线unittest通过。
|
||||
|
||||
### 部署前只读预览
|
||||
|
||||
在178容器内核对两个池并集、首次报告缺口和首批代码。
|
||||
预览不保存名单,不登记运行,不投递任务,不调用模型。
|
||||
核对桥消费的company_review_runs及报告路径保持。
|
||||
|
||||
### 服务生效与生产判收
|
||||
|
||||
容器重建须取得用户同意。
|
||||
按仓库规则统一重建四类worker。
|
||||
beat调度不改,不需要重建。
|
||||
首批显式投递review队列,核对报告增量与预算读数。
|
||||
首批成功不等于全池覆盖完成。
|
||||
后续按实际缺口下降和失败清单判收。
|
||||
|
||||
## 开发者参考
|
||||
|
||||
原targets.collect仍供材料需求单和旧入口使用。
|
||||
扩大覆盖只在评析批任务启用,避免一次扩大数千张采集单。
|
||||
累计名单使用job_runs的既有tail JSON字段。
|
||||
临时投递凭据使用Redis,带过期和所有者校验。
|
||||
同一凭据只能从待执行原子转为执行中一次。
|
||||
失败冷却六小时;无数据沿用七天复查,三次评议失败沿用原放弃规则。
|
||||
预算沿用索引表的当日登记口径,含失败状态。
|
||||
每次模型调用仍受原单次输出上限约束。
|
||||
这不是服务商账单的逐次计费审计,不能把登记量称为实际费用。
|
||||
排序只决定研究先后,不形成证券评分。
|
||||
报告内容及下游排序由现有逻辑继续处理。
|
||||
|
|
@ -0,0 +1,129 @@
|
|||
# 个股评析全池覆盖交接
|
||||
|
||||
本轮日期为2026年10月28日,时间按北京时间。
|
||||
规格见《个股评析全池覆盖开发规格_2026-10-28.md》。
|
||||
|
||||
## 用户决定
|
||||
|
||||
用户要求本轮直接覆盖整个产业池和高股息池。
|
||||
本轮扩大普通个股评析,不扩大分红专项研究,也不改交易参数。
|
||||
|
||||
## 已实现
|
||||
|
||||
复用原每小时批任务、review队列及两个工作并发。
|
||||
每批最多30家,当前候选保留10个优先名额。
|
||||
没有候选待办时,30个名额均可补首次报告。
|
||||
高股息研究对象取桥已发布的现有接口。
|
||||
基座不重新计算股息筛选,也不按投资资格过滤研究。
|
||||
|
||||
累计范围沿用job_runs的tail字段,仅名单变化时追加快照。
|
||||
来源失败保留旧名单;旧名单损坏时停止扩展。
|
||||
在途凭据有所有者校验和六小时过期。
|
||||
失败冷却六小时,任务不反复抢同一批名额。
|
||||
每日评议和定性上限各600家,评议输出上限900万token。
|
||||
开关和额度由config/company_review_coverage.json热读。
|
||||
06:00至08:00、20:00至23:00仅处理原候选。
|
||||
其余时段允许补齐全池,原beat时刻保持。
|
||||
|
||||
材料需求单仍使用原targets.collect。
|
||||
本轮没有一次扩开数千张采集需求单。
|
||||
报告判据、财务期、幂等指纹和固定引用保持。
|
||||
批任务返回保留原有字段,新增覆盖和来源读数。
|
||||
|
||||
## 部署前实测证据
|
||||
|
||||
178原代码为7ef107e,旧服务正在运行。
|
||||
当前候选148家,全部已有评析、定性与评议。
|
||||
产业池3833家,高股息研究池169家。
|
||||
新范围去重后3885家,已有747家,缺3138家。
|
||||
首批只读预览为30家首次报告与60家原候选轻量复核。
|
||||
两路范围来源读取成功,review队列为零。
|
||||
预览未保存累计名单,未投任务,未调用模型。
|
||||
|
||||
本地全部离线unittest为264项,通过。
|
||||
新增26项覆盖轮转、预算、来源失败、在途及任务收尾测试。
|
||||
已有指纹、报告与财务历史边界测试保持通过。
|
||||
桥sources.company_reviews仍直接读原索引字段。
|
||||
报告路径与输出格式未改变。
|
||||
|
||||
## 部署前边界(已由下文上线记录更新)
|
||||
|
||||
代码尚未在178拉取,工作容器尚未重建。
|
||||
新范围尚未投递,不能把只读预览称为覆盖完成。
|
||||
服务器现有未提交文件保留。
|
||||
其他仓库只同步本文和规格副本,并追加桥决定台账。
|
||||
桥现有未提交台账与其他工作保持。
|
||||
|
||||
## 上线及撤销
|
||||
|
||||
用户同意后,在review队列与在途任务为空时统一重建四类worker。
|
||||
保留原worker副本数,不调整并发,不重建beat或前端。
|
||||
核验热读配置与只读预览后,显式向review队列投递首批。
|
||||
首批成功后,按自然小时批次继续推进。
|
||||
首次覆盖容量预估约一周,不保证缺材料对象得到完整结论。
|
||||
|
||||
撤销时在本地将配置enabled改为false,再提交并由178拉取。
|
||||
下一批恢复原名单和每日额度。
|
||||
已在途任务可完成,旧报告与累计名单保留。
|
||||
|
||||
## 可运行验证
|
||||
|
||||
```bash
|
||||
# 本地 · /Users/baobao/Documents/work/project/astock-kg
|
||||
PYTHONDONTWRITEBYTECODE=1 PYTHONPATH=backend python3 -m unittest discover -s backend/tests -t backend
|
||||
git diff --check
|
||||
```
|
||||
|
||||
预期264项通过,差异检查无输出。
|
||||
|
||||
```bash
|
||||
# 基座机 · ~/project/astock-kg,部署后
|
||||
docker compose exec -T -e PYTHONPATH=/app backend python /app/scripts/company_review_coverage.py
|
||||
```
|
||||
|
||||
预期dry_run为true,dispatched为空。
|
||||
初始targets为3885、missing_reports为3138,后续随真实产出改变。
|
||||
source_errors应为空;无数据与材料薄须分开看。
|
||||
新增的日检行同时显示最近批次时刻及状态。
|
||||
|
||||
## 2026-10-28生产上线记录
|
||||
|
||||
本节时间均为北京时间。
|
||||
用户明确同意部署并启动补齐。
|
||||
15:35再次核查四条队列均为零,四类worker在途任务均为空。
|
||||
178从7ef107e快进到42191df,服务器已有未提交文件保持一致。
|
||||
15:35:52统一重建四类worker,镜像、各服务副本数及并发保持。
|
||||
15:36四个新节点均返回pong。
|
||||
beat、前端及数据库容器未重建。
|
||||
|
||||
部署后只读预览与部署前一致。
|
||||
范围3885家,有报告747家,首次缺口3138家。
|
||||
每日额度为评议600家、定性600家、评议输出900万token。
|
||||
名单来源无异常,预计首批为30家首次报告及60家轻量复核。
|
||||
|
||||
15:36:32显式向review队列投递批次。
|
||||
任务号为e20798d0-6186-4a96-9093-f35a133cf70f。
|
||||
15:36:33批任务成功,实际投递90项。
|
||||
其中30项带定性与评议,60项仅复核原候选。
|
||||
累计名单已保存,初始90项均持有在途凭据。
|
||||
首批投递成功不等于90项执行完成。
|
||||
|
||||
15:37部署后完整日检通过,19项正常、零警告、零错误。
|
||||
新增覆盖行和最近批次行均成功输出。
|
||||
15:38接口健康检查返回HTTP 200。
|
||||
|
||||
15:40:46首份新增报告000543.SZ成功落库。
|
||||
报告、评议、定性状态均为ok,在途凭据自动删除。
|
||||
最新报告和固定引用接口均返回HTTP 200,各51418字节。
|
||||
固定引用为3aae8fc1e87736811af30027715c12f64b9a656075608dbfff981338a97b07d1。
|
||||
15:41:43复查累计名单3885家,有报告748家,剩余3137家。
|
||||
在途89项、重试冷却零项,两路名单来源仍无异常。
|
||||
当日登记评议8家、定性8家、评议输出104020个token。
|
||||
|
||||
本轮已判收部署、批次投递、首份报告和凭据收尾。
|
||||
首批全部完成、下一次自然小时批次及全池覆盖尚未判收。
|
||||
后续由原定时任务继续推进,无需人工逐批投递。
|
||||
缺财务数据、材料薄及失败须分列,不能充作完整结论。
|
||||
|
||||
部署日志在178的~/akg_logs/review-coverage-deploy-20261028-1535/。
|
||||
目录保留前后服务状态、原镜像与命令、拉取记录和首批预览。
|
||||
61
plan.py
61
plan.py
|
|
@ -21,6 +21,7 @@ from __future__ import annotations # 注解不在定义时求值:开发机
|
|||
import datetime as dt
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
|
||||
import pandas as pd
|
||||
|
||||
|
|
@ -159,12 +160,15 @@ def _rerank_by_axis(ranked, cards: dict, axis: str, stale_days: int, has_tier: b
|
|||
def _key(item):
|
||||
k = item[0]
|
||||
s = float(item[1])
|
||||
available = pd.notna(s)
|
||||
c = cards.get(k) or {}
|
||||
band = c.get("quality_band")
|
||||
if band is None:
|
||||
band = card.quality_band(c.get("company_review"), stale_days)
|
||||
pointed = bool((c.get("gates") or {}).get("pointed"))
|
||||
if has_tier:
|
||||
if not available:
|
||||
group, s = -float('inf'), -float('inf')
|
||||
elif has_tier:
|
||||
tier = int((s - 190.0) // 20)
|
||||
group = s - 200.0 - tier * 20.0 # 剥掉档位:传导档位不进次序
|
||||
else:
|
||||
|
|
@ -373,7 +377,7 @@ def valuations_for(codes: list, ds: str) -> dict:
|
|||
|
||||
|
||||
def _assemble_cards(ds: str, codes: list, ev: dict, upside: pd.Series,
|
||||
mkt_days: set, risk: set | None = None) -> tuple[dict, list]:
|
||||
mkt_days: set, risk: set | None = None, user_candidates=None) -> tuple[dict, list]:
|
||||
"""候选卡装配(2026-09-02 方案第 2.2 节第三项):对档位表里的全部票(主榜与观察档,
|
||||
裁剪之前)读三路证据、逐票判决。规则在 card.py,取数在 sources.py,这里只做对齐。
|
||||
|
||||
|
|
@ -429,7 +433,7 @@ def _assemble_cards(ds: str, codes: list, ev: dict, upside: pd.Series,
|
|||
evd = {"pointed": bool(mv or e), "theme": theme, "n_sources": n_sources,
|
||||
"chain_fit": (mv or {}).get("chain_fit"),
|
||||
"pct0": d.get("pct0"), "covered": up is not None, "upside": up,
|
||||
"risk_name": k in risk,
|
||||
"risk_name": k in risk, "user_selected": k in (user_candidates or {}),
|
||||
"accum_state": n.get("accum_state"), "accum_score": n.get("accum_score"),
|
||||
"accum_age": n.get("accum_age"), "y_signal": n.get("signal"),
|
||||
"stale_snapshot": stale, "logic": logic.get(k) or []}
|
||||
|
|
@ -501,7 +505,7 @@ def _assemble_cards(ds: str, codes: list, ev: dict, upside: pd.Series,
|
|||
|
||||
|
||||
def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
||||
theme_cap: int = 5) -> dict:
|
||||
theme_cap: int = 5, user_candidates=None) -> dict:
|
||||
"""装配一天的计划为结构化字典。数据缺失抛 RuntimeError(api 侧转 404)。
|
||||
|
||||
2026-09-02 起附带候选卡:main / observe 的装配、排序、裁剪一字不动(下游 PMS 只读
|
||||
|
|
@ -529,16 +533,35 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
names = _names()
|
||||
ev, mkt_days = _evidence(ds)
|
||||
|
||||
import user_candidates as uc
|
||||
score = score[[bool(re.fullmatch(r'(SH(?:60|68)\d{4}|SZ(?:00|30)\d{4})', str(k))) for k in score.index]]
|
||||
selected = {uc.normalize(r['code']): r for r in (user_candidates or [])}
|
||||
if selected:
|
||||
# 完整证券名称随真实修订录制,重放时不回源,也不补造历史名称。
|
||||
manual_names = plan_inputs.read('user_candidates.catalog', uc.catalog)
|
||||
names = dict(names)
|
||||
for code in selected:
|
||||
if manual_names.get(code):
|
||||
names[code] = manual_names[code]
|
||||
ordinary = set(score.index)
|
||||
extras = [k for k in selected if k not in ordinary]
|
||||
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')
|
||||
if extras:
|
||||
main = pd.concat([main, pd.Series({k: float('nan') for k in extras})])
|
||||
score = pd.concat([score, pd.Series({k: float('nan') for k in extras})])
|
||||
generated_at = dt.datetime.now().isoformat(timespec="seconds")
|
||||
try: # 风险名单只读一次:候选卡与升降档原因共用
|
||||
import factors
|
||||
risk = plan_inputs.factor_read('_risk_set') or set()
|
||||
except Exception: # noqa: BLE001
|
||||
risk = set()
|
||||
if selected and config.EXCLUDE_RISK_NAMES:
|
||||
risk = set(risk) | {k for k in selected
|
||||
if str(names.get(k) or '').replace(' ', '').startswith(factors._RISK_PREFIXES)
|
||||
or str(names.get(k) or '').endswith('退')}
|
||||
cards, segments_pointed = _assemble_cards(
|
||||
ds, list(main.index) + list(obs.index), ev, upside, mkt_days, risk=risk)
|
||||
ds, list(main.index) + list(obs.index), ev, upside, mkt_days, risk=risk, user_candidates=selected)
|
||||
|
||||
# 排序轴(2026-09-17):拿到评析后在主榜与观察档这两个分数序列上重排一次。
|
||||
# 下面的 _pick 出接口、_full_rows 出复盘快照、快照又喂逐票逻辑状态入库与评析目标名单——
|
||||
|
|
@ -582,10 +605,10 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
evidence = {"theme": c["theme"], "n_sources": c.get("n_sources"),
|
||||
"moved_ratio": None, "source": "moved_view"}
|
||||
r = {"rank": rank, "code": k, "name": names.get(k),
|
||||
"score": round(float(s), 2),
|
||||
"score": round(float(s), 2) if pd.notna(s) else None,
|
||||
# 调整分只随行展示、供排序追溯;档位标签仍按原始 score 解码(工作包四)
|
||||
"score_adj": round(
|
||||
float(s) + _quality_bonus(c.get("company_review"), config.PLAN_QUALITY_BONUS), 2),
|
||||
float(s) + _quality_bonus(c.get("company_review"), config.PLAN_QUALITY_BONUS), 2) if pd.notna(s) else None,
|
||||
"evidence": evidence,
|
||||
"heat": _val(heat, k), "upside": _val(upside, k)}
|
||||
# 排序轴四键(2026-09-17):质地带、主轴名、是否被指向(候选卡门槛一)、旧序全量名次。
|
||||
|
|
@ -598,7 +621,7 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
r["pointed"] = bool((c.get("gates") or {}).get("pointed"))
|
||||
if config.PLAN_RANK_AXIS == "quality":
|
||||
r["rank_old"] = rank_old_map.get(k)
|
||||
if with_tier:
|
||||
if with_tier and pd.notna(s):
|
||||
r["tier"] = _tier_label(s)
|
||||
if c:
|
||||
# 2026-09-03 新增只联入:basis(判决依据一句话)、logic(因果论断带出处的文字行)、
|
||||
|
|
@ -631,6 +654,11 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
"confirm": c.get("confirm"), "failed_gates": c.get("failed_gates"),
|
||||
"chain_fit": c.get("chain_fit"),
|
||||
"logic_claims": c.get("logic_claims") or []})
|
||||
if k in selected:
|
||||
r['user_candidate'] = selected[k]
|
||||
r['user_required'] = k not in ordinary or not r['pointed']
|
||||
if not names.get(k):
|
||||
r.update(verdict=card.VERDICT_SHOW, basis='证券资料尚未齐全,暂不参与交易')
|
||||
return r
|
||||
|
||||
def _full_rows(ranked: pd.Series, with_tier: bool) -> list:
|
||||
|
|
@ -727,6 +755,7 @@ def collect(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
for v in (card.VERDICT_CANDIDATE, card.VERDICT_WATCH, card.VERDICT_SHOW)}
|
||||
result = {
|
||||
"date": ds,
|
||||
"user_candidates": list(selected.values()),
|
||||
"generated_at": generated_at,
|
||||
"plan_version": version.git_short_rev(),
|
||||
"counts": {"main": int(len(main)), "observe": int(len(obs)),
|
||||
|
|
@ -948,8 +977,8 @@ def render_md(d: dict) -> str:
|
|||
L.append("| # | 代码 | 名称 | 总分 | 档位 | 传导证据 | 热度 | 预期空间 |")
|
||||
L.append("|---|------|------|------|------|----------|------|----------|")
|
||||
for r in d["main"]:
|
||||
L.append(f"| {r['rank']} | {r['code']} | {r['name'] or '—'} | {r['score']:.1f} "
|
||||
f"| {r['tier']} | {_fmt_ev(r['evidence'])} "
|
||||
L.append(f"| {r['rank']} | {r['code']} | {r['name'] or '—'} | {_fmt_num(r['score'])} "
|
||||
f"| {r.get('tier') or '—'} | {_fmt_ev(r['evidence'])} "
|
||||
f"| {_fmt_num(r['heat'])} | {_fmt_pct(r['upside'])} |")
|
||||
L.append("")
|
||||
|
||||
|
|
@ -960,7 +989,7 @@ def render_md(d: dict) -> str:
|
|||
L.append("| # | 代码 | 名称 | 分 | 传导证据 | 热度 |")
|
||||
L.append("|---|------|------|----|----------|------|")
|
||||
for r in d["observe"]:
|
||||
L.append(f"| {r['rank']} | {r['code']} | {r['name'] or '—'} | {r['score']:.1f} "
|
||||
L.append(f"| {r['rank']} | {r['code']} | {r['name'] or '—'} | {_fmt_num(r['score'])} "
|
||||
f"| {_fmt_ev(r['evidence'])} | {_fmt_num(r['heat'])} |")
|
||||
L.append("")
|
||||
|
||||
|
|
@ -1046,7 +1075,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, decision_id=None) -> dict:
|
||||
theme_cap: int = 5, decision_id=None, include_user_candidates=False) -> dict:
|
||||
"""从当日 JSON 快照读计划,按请求参数截取。**不做任何装配、不查任何库。**
|
||||
|
||||
为什么要这条路(2026-09-10,台账 057): collect 每次请求都现场装配整池,实测三十三秒,
|
||||
|
|
@ -1063,6 +1092,8 @@ def from_snapshot(date: str | None = None, top: int = 20, obs_top: int = 10,
|
|||
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")
|
||||
snap = plan_versions.load(config.PLAN_SNAPSHOT_DIR, ds, decision_id)
|
||||
import user_candidates as uc
|
||||
snap = uc.consumer_view(snap, include_user_candidates)
|
||||
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")
|
||||
|
|
@ -1107,10 +1138,12 @@ def _generate(date, top, obs_top, theme_cap):
|
|||
write_published_md(published)
|
||||
print(f"复用已发布计划 {ds},未重新采集输入")
|
||||
return os.path.join(config.PLAN_SNAPSHOT_DIR, f"plan_{ds}.md")
|
||||
import user_candidates as uc
|
||||
selected = uc.active(config.PLAN_SNAPSHOT_DIR)
|
||||
bundle = plan_inputs.Bundle(params=plan_inputs.effective_config(config.module))
|
||||
try:
|
||||
with bundle.use():
|
||||
data = collect(ds, top, obs_top, theme_cap)
|
||||
data = collect(ds, top, obs_top, theme_cap, **({"user_candidates": selected} if selected else {}))
|
||||
data['market'] = sources.market_context(data['date'])
|
||||
except RuntimeError as e:
|
||||
raise SystemExit(str(e))
|
||||
|
|
@ -1126,7 +1159,7 @@ def _generate(date, top, obs_top, theme_cap):
|
|||
"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")
|
||||
snap = plan_versions.publish(config.PLAN_SNAPSHOT_DIR, snap,
|
||||
{'records': bundle.records, 'market': data['market'], 'regime': data.get('regime')},
|
||||
{'records': bundle.records, 'market': data['market'], 'regime': data.get('regime'), 'user_candidates': selected},
|
||||
bundle.params, cutoff_at=bundle.cutoff_at, reason='晨版生成')
|
||||
text = write_published_md(snap)
|
||||
# ---- 逐票日频逻辑状态表(2026-09-07 第三件桥侧前置):只在这一次生成里写,/plan 的实时重算
|
||||
|
|
|
|||
|
|
@ -44,6 +44,21 @@ def refresh(day=None, *, now=None, regime_value=None):
|
|||
try:
|
||||
env = pv.envelope(root, old['decision']['id'])
|
||||
inputs = copy.deepcopy(env['inputs'])
|
||||
import user_candidates as uc
|
||||
selected = uc.active(root, now)
|
||||
candidates_changed = selected != inputs.get('user_candidates', [])
|
||||
if candidates_changed:
|
||||
pending.update(r['code'] for r in selected + inputs.get('user_candidates', []))
|
||||
inputs['user_candidates'] = selected
|
||||
pv.health(root, day, pending_codes=sorted(pending), error=None)
|
||||
if selected and not any(r['name'] == 'user_candidates.catalog' for r in inputs['records'].values()):
|
||||
# 旧发布版尚未录制完整证券名单时,作为本次真实修订的新输入补齐。
|
||||
pending.update(r['code'] for r in selected)
|
||||
pv.health(root, day, pending_codes=sorted(pending), error=None)
|
||||
catalog_bundle = pi.Bundle(cutoff_at=now.isoformat())
|
||||
with catalog_bundle.use():
|
||||
pi.read('user_candidates.catalog', uc.catalog)
|
||||
inputs['records'].update(catalog_bundle.records)
|
||||
for rec in inputs['records'].values():
|
||||
if rec['name'] != 'sources.company_reviews':
|
||||
continue
|
||||
|
|
@ -67,16 +82,21 @@ def refresh(day=None, *, now=None, regime_value=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())
|
||||
bundle = pi.Bundle(records=None if candidates_changed else inputs['records'],
|
||||
params=env['params'], cutoff_at=now.isoformat())
|
||||
if candidates_changed:
|
||||
# 原有调用继续读冻结输入;新增股票带来的新取数在本次真实修订中录制。
|
||||
bundle.records = copy.deepcopy(inputs['records'])
|
||||
shown = old.get('shown_params') or {'top': 20, 'obs_top': 10, 'theme_cap': 5}
|
||||
with bundle.use():
|
||||
data = plan.collect(day, **shown)
|
||||
data = plan.collect(day, **shown, **({'user_candidates': selected} if selected else {}))
|
||||
inputs['records'] = bundle.records
|
||||
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 '市场区制更新')
|
||||
reason='用户候选更新' if candidates_changed else '评析更新' if pending else '市场区制更新')
|
||||
plan.write_published_md(published)
|
||||
return {'status': 'published', 'decision': published['decision']}
|
||||
except Exception as e:
|
||||
|
|
|
|||
|
|
@ -233,7 +233,7 @@ def publish(root, plan, inputs, params, *, cutoff_at, reason):
|
|||
return p
|
||||
|
||||
|
||||
def stock_check(root, code):
|
||||
def stock_check(root, code, include_user_candidates=False):
|
||||
"""只读完整发布文件的一只股票。不存在与取数失败严格区分;不按展示名次裁剪。"""
|
||||
if not re.fullmatch(r'(SH|SZ|BJ)\d{6}', code):
|
||||
raise ValueError('股票代码无效')
|
||||
|
|
@ -243,20 +243,24 @@ def stock_check(root, code):
|
|||
raise FileNotFoundError('无已发布计划')
|
||||
path = Path(root) / f'plan_{day}.json'
|
||||
health_path = Path(root) / 'versions' / day / 'health.json'
|
||||
candidates_path = Path(root) / 'user_candidates.json'
|
||||
def identity(p):
|
||||
try:
|
||||
s = p.stat()
|
||||
return (s.st_dev, s.st_ino, s.st_size, s.st_mtime_ns, s.st_ctime_ns)
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
before = (identity(path), identity(health_path))
|
||||
before = (identity(path), identity(health_path), identity(candidates_path))
|
||||
published = load(root, day)
|
||||
state = health(root, day)
|
||||
if (day != latest_date(root) or before != (identity(path), identity(health_path))):
|
||||
import user_candidates as uc
|
||||
state = dict(state, pending_codes=sorted(set(state.get('pending_codes') or []) | set(uc.live_pending(root, published))))
|
||||
if (day != latest_date(root) or before != (identity(path), identity(health_path), identity(candidates_path))):
|
||||
continue
|
||||
if (published.get('date') != day or not (published.get('decision') or {}).get('id')
|
||||
or not all(isinstance(published.get(b), list) for b in ('main', 'observe'))):
|
||||
raise ValueError('完整发布计划不可核验')
|
||||
published = uc.consumer_view(published, include_user_candidates)
|
||||
rows = {b: [r for r in published[b] if r.get('code') == code] for b in ('main', 'observe')}
|
||||
count = sum(map(len, rows.values()))
|
||||
if count > 1:
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@ except ImportError:
|
|||
fa.FastAPI = fa.APIRouter = Router
|
||||
fa.Request = type('Request', (), {})
|
||||
fa.HTTPException = HTTPException
|
||||
fa.Header = fa.Query = lambda default=None, **kw: default
|
||||
fa.Body = fa.Header = fa.Query = lambda default=None, **kw: default
|
||||
fa.BackgroundTasks = type('BackgroundTasks', (), {'add_task': lambda self, *a, **kw: None})
|
||||
sys.modules['fastapi'] = fa
|
||||
resp = types.ModuleType('fastapi.responses')
|
||||
resp.PlainTextResponse = lambda content, **kw: content
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ 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', 'test_plan_stock_check',
|
||||
'test_plan_rank_switch'):
|
||||
'test_plan_rank_switch', 'test_user_candidates'):
|
||||
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',
|
||||
|
|
|
|||
|
|
@ -0,0 +1,195 @@
|
|||
"""用户候选登记、原有门槛与版本闭环的断网回归。"""
|
||||
import copy
|
||||
import datetime as dt
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch, Mock
|
||||
from contextlib import ExitStack
|
||||
import pandas as pd
|
||||
import test_api_support
|
||||
import user_candidates as uc
|
||||
import plan, config, card, sources, factors, judgement, logic_state_daily
|
||||
import plan_inputs as pi
|
||||
import plan_versions as pv
|
||||
import plan_refresh
|
||||
|
||||
NOW = dt.datetime.fromisoformat('2026-11-03T10:00:00+08:00')
|
||||
DAY = '2026-11-02'
|
||||
PAYLOAD = {'codes': ['600001.SH'], 'reason': '行业需求变化', 'expires_on': '2026-11-10'}
|
||||
USER_NAMES = dict(zip(
|
||||
['SZ300133', 'SZ300364', 'SZ300182', 'SH603533', 'SZ300291', 'SZ301052'],
|
||||
['华策影视', '中文在线', '捷成股份', '掌阅科技', '百纳千成', '果麦文化']))
|
||||
|
||||
class CandidateTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temp = tempfile.TemporaryDirectory()
|
||||
self.root = self.temp.name
|
||||
self.addCleanup(self.temp.cleanup)
|
||||
|
||||
def test_exchange_validation_batch_atomic_and_dedup(self):
|
||||
for code in ('830001.BJ', '920001', 'SH000001', 'SZ600001', '123', '510300.SH'):
|
||||
with self.assertRaises(ValueError):
|
||||
uc.save(self.root, dict(PAYLOAD, codes=['600001', code]), '甲', NOW)
|
||||
self.assertEqual(uc.read(self.root)['rows'], [])
|
||||
r = uc.save(self.root, dict(PAYLOAD, codes=['600001', 'SH600001', '600001.SH']), '甲', NOW)
|
||||
self.assertEqual(r['added'], 1)
|
||||
self.assertEqual(uc.save(self.root, PAYLOAD, '乙', NOW)['existing'], 1)
|
||||
self.assertEqual(uc.read(self.root)['rows'][0]['created_by'], '甲')
|
||||
|
||||
def test_pasted_names_mixed_codes_and_separators_save_canonical_codes(self):
|
||||
text = '华策影视 中文在线,捷成股份\n掌阅科技;百纳千成、果麦文化'
|
||||
result = uc.save(self.root, dict(PAYLOAD, codes=[text, '300133.SZ']), '甲', NOW, names=USER_NAMES)
|
||||
self.assertEqual(result['added'], 6)
|
||||
self.assertEqual([r['code'] for r in uc.read(self.root)['rows']], list(USER_NAMES))
|
||||
self.assertEqual([r['name'] for r in uc.listing(self.root, NOW)['rows']], list(USER_NAMES.values())[::-1])
|
||||
self.assertEqual(uc.save(self.root, dict(PAYLOAD, codes=text), '甲', NOW, names=USER_NAMES)['existing'], 6)
|
||||
|
||||
def test_unmatched_or_ambiguous_name_does_not_partially_save(self):
|
||||
names = dict(USER_NAMES, SH600001='同名', SZ000001='同名')
|
||||
for value, message in [('华策影视 不存在的股票', '未找到沪深 A 股'),
|
||||
('华策影视 同名', '对应多只股票'),
|
||||
('华策影视 中文', '未找到沪深 A 股'),
|
||||
('华策影视 920001', '仅支持沪深 A 股'),
|
||||
(' '.join(['华策影视'] * 31), '1 至 30')]:
|
||||
with self.assertRaisesRegex(ValueError, message):
|
||||
uc.save(self.root, dict(PAYLOAD, codes=[value]), '甲', NOW, names=names)
|
||||
self.assertFalse(Path(self.root, 'user_candidates.json').exists())
|
||||
|
||||
def test_catalog_reads_full_listed_shanghai_shenzhen_universe(self):
|
||||
import db
|
||||
rows = [{'gp_code_two': c[2:]+'.'+c[:2], 'gp_name': n} for c, n in USER_NAMES.items()]
|
||||
rows += [{'gp_code_two': '920001.BJ', 'gp_name': '排除示例'}, {'gp_code_two': '510300.SH', 'gp_name': '基金示例'}]
|
||||
with patch.object(db, 'read_mysql', return_value=pd.DataFrame(rows)) as read:
|
||||
self.assertEqual(uc.catalog(), USER_NAMES)
|
||||
self.assertEqual(read.call_args.args[0], 'heat')
|
||||
self.assertIn("list_status='L'", read.call_args.args[1])
|
||||
self.assertIn("gp_code_two LIKE '%.SH' OR gp_code_two LIKE '%.SZ'", read.call_args.args[1])
|
||||
|
||||
def test_search_and_add_use_full_catalog_without_industry_membership(self):
|
||||
import api
|
||||
with patch.object(config, 'XXL_TRIGGER_KEY', 'test-secret'), patch.object(config, 'PLAN_SNAPSHOT_DIR', self.root), \
|
||||
patch.object(uc, 'catalog', return_value=USER_NAMES), patch.object(plan, '_names', side_effect=AssertionError('不能依赖行业池')):
|
||||
self.assertEqual(api.search_user_candidates('中文在线')['items'], [{'code': 'SZ300364', 'name': '中文在线'}])
|
||||
background = Mock()
|
||||
expiry = (dt.datetime.now(uc.BJ).date() + dt.timedelta(days=1)).isoformat()
|
||||
result = api.add_user_candidates(background, dict(PAYLOAD, codes=list(USER_NAMES.values()), expires_on=expiry), x_job_key='test-secret')
|
||||
self.assertEqual(result['added'], 6)
|
||||
background.add_task.assert_called_once_with(uc.refresh_saved)
|
||||
|
||||
def test_expiry_remove_and_readd_do_not_erase_history(self):
|
||||
uc.save(self.root, PAYLOAD, '甲', NOW)
|
||||
old = uc.active(self.root, NOW)[0]
|
||||
pub = {'user_candidates': [old]}
|
||||
uc.remove(self.root, old['id'], '甲', NOW)
|
||||
self.assertEqual(uc.live_pending(self.root, pub, NOW), ['SH600001'])
|
||||
self.assertEqual(uc.remove(self.root, old['id'], '甲', NOW)['revision'], 2)
|
||||
uc.save(self.root, PAYLOAD, '甲', NOW)
|
||||
self.assertNotEqual(uc.active(self.root, NOW)[0]['id'], old['id'])
|
||||
self.assertEqual(len(uc.read(self.root)['rows']), 2)
|
||||
later = NOW + dt.timedelta(days=8)
|
||||
self.assertEqual(uc.active(self.root, later), [])
|
||||
|
||||
def test_manual_discovery_never_fakes_transmission_or_other_gates(self):
|
||||
evidence = {'pointed': False, 'user_selected': True, 'pct0': 4,
|
||||
'covered': True, 'upside': .2, 'accum_state': '明确吸筹', 'accum_age': 0}
|
||||
result = card.judge(evidence)
|
||||
self.assertEqual(result['verdict'], card.VERDICT_CANDIDATE)
|
||||
self.assertFalse(result['gates']['pointed'])
|
||||
for change in ({'covered': False}, {'upside': -.2}, {'risk_name': True},
|
||||
{'pct0': 0}, {'y_signal': 'SELL'}, {'accum_state': '潜在吸筹'}):
|
||||
self.assertEqual(card.judge(dict(evidence, **change))['verdict'], card.VERDICT_SHOW)
|
||||
self.assertEqual(card.judge(dict(evidence, user_selected=False))['verdict'], card.VERDICT_SHOW)
|
||||
|
||||
def test_mutations_require_key_and_do_not_publish_inline(self):
|
||||
import api
|
||||
with patch.object(config, 'XXL_TRIGGER_KEY', 'test-secret'), patch.object(config, 'PLAN_SNAPSHOT_DIR', self.root):
|
||||
with self.assertRaises(api.HTTPException):
|
||||
api.add_user_candidates(None, PAYLOAD, x_job_key='bad')
|
||||
self.assertFalse(Path(self.root, 'user_candidates.json').exists())
|
||||
|
||||
def test_old_consumers_exclude_user_source_before_top_and_cache_is_separate(self):
|
||||
import api
|
||||
source={'id':'a'*32,'code':'SH600001'}
|
||||
row={'code':'SH600001','score':250,'rank':1,'research_rank':1,'evidence':{},
|
||||
'user_candidate':source,'user_required':True,'verdict':'候选'}
|
||||
ordinary={'code':'SH600002','score':240,'rank':2,'research_rank':2,'evidence':{},'verdict':'候选'}
|
||||
published={'date':DAY,'decision':{'id':'v-opt-in'},'main':[row,ordinary],'observe':[],
|
||||
'user_candidates':[source],'candidates':[row,ordinary],'watch':[],
|
||||
'counts':{'main':2,'observe':0}}
|
||||
original=copy.deepcopy(published)
|
||||
with patch.object(config,'PLAN_SNAPSHOT_DIR',self.root),patch.object(pv,'load',return_value=published):
|
||||
default,_=api._plan_cached(DAY,1,0,0,True)
|
||||
opted,_=api._plan_cached(DAY,1,0,0,True,include_user_candidates=True)
|
||||
again,hit=api._plan_cached(DAY,1,0,0,False)
|
||||
self.assertEqual(default['main'][0]['code'],'SH600002')
|
||||
self.assertEqual(opted['main'][0]['code'],'SH600001')
|
||||
self.assertEqual(default,again)
|
||||
self.assertTrue(hit)
|
||||
self.assertNotIn('user_candidates',default)
|
||||
self.assertEqual(default['counts']['main'],1)
|
||||
self.assertEqual(len(default['candidates']),1)
|
||||
self.assertEqual(original,published)
|
||||
|
||||
def test_revision_add_remove_replay_and_frozen_morning(self):
|
||||
codes = ['SH600001', 'SH600002']
|
||||
with ExitStack() as stack:
|
||||
def stub(obj, name, **kw): return stack.enter_context(patch.object(obj, name, **kw))
|
||||
stub(config, 'PLAN_SNAPSHOT_DIR', new=self.root)
|
||||
stub(config, 'PLAN_RANK_AXIS', new='quality')
|
||||
stub(plan, '_factor', side_effect=lambda table, day: pd.Series(
|
||||
[240.,230.] if 'score' in table else [.3,.2] if 'upside' in table else [2.,2.], index=codes))
|
||||
stub(plan, '_latest_date', return_value=DAY)
|
||||
stub(plan, '_prev_date', return_value=None)
|
||||
stub(plan, '_names', return_value={'SH600001':'甲','SH600002':'乙'})
|
||||
stub(uc, 'catalog', return_value={'SH600001':'甲','SH600002':'乙','SH600003':'丙'})
|
||||
stub(plan, '_evidence', return_value=({}, {DAY}))
|
||||
stub(plan, '_segments_of', return_value={})
|
||||
stub(factors, '_risk_set', return_value=set())
|
||||
stub(judgement, 'load_previous', return_value=[])
|
||||
stub(judgement, 'recent_rows', return_value={})
|
||||
stub(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'):
|
||||
stub(sources, name, return_value={})
|
||||
stub(sources, 'stock_daily', return_value={c:{'pct0':4} for c in codes})
|
||||
stub(sources, 'night_conclusions', return_value={c:{'accum_state':'明确吸筹','accum_age':0} for c in codes})
|
||||
stub(sources, 'company_reviews', side_effect=lambda cs,*a,**kw: {c:{'overall':'好','age_days':0,'review_ref':'r'} for c in cs})
|
||||
bundle = pi.Bundle(params=pi.effective_config(config), cutoff_at='2026-11-03T07:10:00+08:00')
|
||||
with bundle.use():
|
||||
data = plan.collect(DAY)
|
||||
data.update(data.pop('_full'))
|
||||
with pv.writer(self.root):
|
||||
morning = pv.publish(self.root,data,{'records':bundle.records,'market':{},'regime':None},
|
||||
bundle.params,cutoff_at=bundle.cutoff_at,reason='晨版')
|
||||
frozen = copy.deepcopy(morning)
|
||||
uc.save(self.root,dict(PAYLOAD,codes=['600001','600003']), '甲', NOW)
|
||||
self.assertEqual(pv.stock_check(self.root,'SH600001')['refresh_status']['pending_codes'],['SH600001','SH600003'])
|
||||
result = plan_refresh.refresh(now=NOW)
|
||||
self.assertEqual(result['status'],'published')
|
||||
published = pv.load(self.root)
|
||||
rows = {r['code']:r for r in published['main']}
|
||||
self.assertEqual(rows['SH600001']['verdict'],'候选')
|
||||
self.assertFalse(rows['SH600001']['pointed'])
|
||||
self.assertEqual(rows['SH600001']['score'],240.)
|
||||
self.assertIsNone(rows['SH600003']['score'])
|
||||
self.assertEqual(rows['SH600003']['name'], '丙')
|
||||
self.assertNotIn('tier',rows['SH600003'])
|
||||
self.assertFalse(pv.stock_check(self.root,'SH600003')['found'])
|
||||
self.assertTrue(pv.stock_check(self.root,'SH600003',include_user_candidates=True)['found'])
|
||||
json.dumps(published,allow_nan=False)
|
||||
self.assertIn('SH600003', Path(self.root, f'plan_{DAY}.md').read_text())
|
||||
self.assertEqual(pv.load(self.root,DAY,morning=True),frozen)
|
||||
self.assertEqual(plan_refresh.refresh(now=NOW)['status'],'unchanged')
|
||||
for source in uc.active(self.root,NOW):
|
||||
uc.remove(self.root,source['id'],'甲',NOW)
|
||||
plan_refresh.refresh(now=NOW)
|
||||
after = pv.load(self.root)
|
||||
self.assertEqual(after['main'][0]['verdict'],'仅展示')
|
||||
self.assertNotIn('SH600003',[r['code'] for r in after['main']])
|
||||
self.assertEqual(pv.load(self.root,DAY,morning=True),frozen)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
@ -0,0 +1,212 @@
|
|||
"""用户补充的候选来源。与计划共用文件锁,先保存,再由修订任务发布。"""
|
||||
from __future__ import annotations
|
||||
|
||||
import datetime as dt
|
||||
import json
|
||||
import re
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import plan_versions as pv
|
||||
|
||||
BJ = ZoneInfo('Asia/Shanghai')
|
||||
|
||||
|
||||
def consumer_view(published, include_user_candidates=False):
|
||||
"""旧调用方只取系统来源;显式声明支持新来源后才返回人工候选。"""
|
||||
if include_user_candidates or not published.get('user_candidates'):
|
||||
return published
|
||||
out = dict(published)
|
||||
out.pop('user_candidates', None)
|
||||
for bucket in ('main', 'observe', 'main_shown', 'observe_shown', 'candidates', 'watch'):
|
||||
if bucket in out:
|
||||
out[bucket] = [{k: v for k, v in r.items() if k not in ('user_candidate', 'user_required')}
|
||||
for r in out[bucket] if not r.get('user_required')]
|
||||
if 'counts' in out:
|
||||
out['counts'] = dict(out['counts'], main=len(out.get('main', [])), observe=len(out.get('observe', [])))
|
||||
if 'card_counts' in out:
|
||||
rows = out.get('main', []) + out.get('observe', [])
|
||||
out['card_counts'] = {v: sum(r.get('verdict') == v for r in rows) for v in out['card_counts']}
|
||||
return out
|
||||
|
||||
|
||||
def normalize(value):
|
||||
value = str(value or '').strip().upper()
|
||||
if re.fullmatch(r'\d{6}\.(SH|SZ)', value):
|
||||
value = value[-2:] + value[:6]
|
||||
elif re.fullmatch(r'\d{6}', value):
|
||||
value = ('SH' if value.startswith(('60', '68')) else 'SZ') + value
|
||||
if not re.fullmatch(r'(SH(?:60|68)\d{4}|SZ(?:00|30)\d{4})', value):
|
||||
raise ValueError('仅支持沪深 A 股,请检查股票代码')
|
||||
return value
|
||||
|
||||
|
||||
def catalog():
|
||||
"""使用上市证券基础表,不能把部分行业池当作完整可搜索名单。"""
|
||||
import db
|
||||
frame = db.read_mysql('heat',
|
||||
"SELECT gp_code_two,gp_name FROM gp_code_all WHERE list_status='L' "
|
||||
"AND (gp_code_two LIKE '%.SH' OR gp_code_two LIKE '%.SZ')")
|
||||
out = {}
|
||||
for row in frame.to_dict('records'):
|
||||
try:
|
||||
code = normalize(row.get('gp_code_two'))
|
||||
except ValueError:
|
||||
continue
|
||||
name = str(row.get('gp_name') or '').strip()
|
||||
if name:
|
||||
out[code] = name
|
||||
if not out:
|
||||
raise ValueError('证券名称资料暂不可用,请稍后重试')
|
||||
return out
|
||||
|
||||
|
||||
def resolve(raw, names=None):
|
||||
"""批量名称只按完整简称精确匹配;全部解析成功后才允许保存。"""
|
||||
if isinstance(raw, str):
|
||||
raw = [raw]
|
||||
if not isinstance(raw, list) or any(not isinstance(v, str) for v in raw):
|
||||
raise ValueError('请输入股票名称或代码')
|
||||
tokens = [v for item in raw for v in re.split(r'[\s,,;;、]+', item.strip()) if v]
|
||||
if not 1 <= len(tokens) <= 30:
|
||||
raise ValueError('每次可添加 1 至 30 只股票')
|
||||
index = None
|
||||
codes = []
|
||||
for value in tokens:
|
||||
try:
|
||||
code = normalize(value)
|
||||
except ValueError:
|
||||
# 明确的证券代码继续走原交易所限制,不尝试按名称解释。
|
||||
if re.fullmatch(r'(?:(?:SH|SZ|BJ)?\d+(?:\.(?:SH|SZ|BJ))?)', value.upper()):
|
||||
raise
|
||||
if index is None:
|
||||
source = names() if callable(names) else names or {}
|
||||
index = {}
|
||||
for candidate, name in source.items():
|
||||
try:
|
||||
canonical = normalize(candidate)
|
||||
except ValueError:
|
||||
continue
|
||||
index.setdefault(str(name).strip().upper(), set()).add(canonical)
|
||||
matches = index.get(value.upper(), set())
|
||||
if not matches:
|
||||
raise ValueError(f'未找到沪深 A 股“{value[:40]}”,请检查完整名称或使用代码') from None
|
||||
if len(matches) != 1:
|
||||
raise ValueError(f'“{value[:40]}”对应多只股票,请使用代码') from None
|
||||
code = next(iter(matches))
|
||||
if code not in codes:
|
||||
codes.append(code)
|
||||
return codes
|
||||
|
||||
|
||||
def read(root):
|
||||
path = Path(root) / 'user_candidates.json'
|
||||
if not path.exists():
|
||||
return {'revision': 0, 'rows': []}
|
||||
state = json.loads(path.read_text())
|
||||
if not isinstance(state.get('rows'), list) or not isinstance(state.get('revision'), int):
|
||||
raise ValueError('添加记录暂不可用')
|
||||
return state
|
||||
|
||||
|
||||
def active(root, now=None):
|
||||
today = (now or dt.datetime.now(BJ)).astimezone(BJ).date().isoformat()
|
||||
return [dict(r) for r in read(root)['rows']
|
||||
if not r.get('removed_at') and r['expires_on'] >= today]
|
||||
|
||||
|
||||
def save(root, payload, actor, now=None, *, names=None):
|
||||
now = now or dt.datetime.now(BJ)
|
||||
codes = resolve(payload.get('codes'), names)
|
||||
reason = str(payload.get('reason') or '').strip()
|
||||
if not reason or len(reason) > 300:
|
||||
raise ValueError('请填写添加理由,最多 300 字')
|
||||
try:
|
||||
expires = dt.date.fromisoformat(str(payload.get('expires_on') or ''))
|
||||
except ValueError as exc:
|
||||
raise ValueError('请选择关注截止日期') from exc
|
||||
if not now.astimezone(BJ).date() <= expires <= now.astimezone(BJ).date() + dt.timedelta(days=90):
|
||||
raise ValueError('关注期限应在今天至未来 90 天内')
|
||||
with pv.writer(root):
|
||||
state = read(root)
|
||||
current = {r['code'] for r in active(root, now)}
|
||||
added = []
|
||||
for code in codes:
|
||||
if code in current:
|
||||
continue
|
||||
row = {'id': uuid.uuid4().hex, 'code': code, 'reason': reason,
|
||||
'expires_on': expires.isoformat(), 'created_at': now.isoformat(),
|
||||
'created_by': str(actor)[:100]}
|
||||
if isinstance(names, dict) and names.get(code):
|
||||
row['name'] = names[code]
|
||||
state['rows'].append(row)
|
||||
added.append(row)
|
||||
if added:
|
||||
state['revision'] += 1
|
||||
pv.atomic(Path(root) / 'user_candidates.json', state)
|
||||
mark_pending(root, [r['code'] for r in added])
|
||||
return {'ok': True, 'added': len(added), 'existing': len(codes) - len(added),
|
||||
'revision': state['revision']}
|
||||
|
||||
|
||||
def remove(root, identity, actor, now=None):
|
||||
now = now or dt.datetime.now(BJ)
|
||||
with pv.writer(root):
|
||||
state = read(root)
|
||||
row = next((r for r in state['rows'] if r['id'] == identity), None)
|
||||
if row is None:
|
||||
raise ValueError('添加记录不存在,请刷新后重试')
|
||||
if not row.get('removed_at'):
|
||||
row.update(removed_at=now.isoformat(), removed_by=str(actor)[:100])
|
||||
state['revision'] += 1
|
||||
pv.atomic(Path(root) / 'user_candidates.json', state)
|
||||
mark_pending(root, [row['code']])
|
||||
return {'ok': True, 'revision': state['revision']}
|
||||
|
||||
|
||||
def mark_pending(root, codes):
|
||||
day = pv.latest_date(root)
|
||||
if day:
|
||||
state = pv.health(root, day)
|
||||
pv.health(root, day, pending_codes=sorted(set(state.get('pending_codes') or []) | set(codes)))
|
||||
|
||||
|
||||
def live_pending(root, published, now=None):
|
||||
"""写后未发布、删除或到期时,即使后台失败也不能继续消费旧人工来源。"""
|
||||
current = {r['code']: r['id'] for r in active(root, now)}
|
||||
frozen = {r['code']: r['id'] for r in published.get('user_candidates', [])}
|
||||
return sorted(c for c in current.keys() | frozen.keys() if current.get(c) != frozen.get(c))
|
||||
|
||||
|
||||
def listing(root, now=None):
|
||||
now = now or dt.datetime.now(BJ)
|
||||
state = read(root)
|
||||
try:
|
||||
published = pv.load(root) if pv.latest_date(root) else {}
|
||||
except FileNotFoundError:
|
||||
published = {}
|
||||
material = {r['code']: r for b in ('main', 'observe') for r in published.get(b, [])}
|
||||
expected = {r['id'] for r in published.get('user_candidates', [])}
|
||||
today = now.astimezone(BJ).date().isoformat()
|
||||
rows = []
|
||||
for source in reversed(state['rows']):
|
||||
row = material.get(source['code']) or {}
|
||||
status = ('removed' if source.get('removed_at') else
|
||||
'expired' if source['expires_on'] < today else
|
||||
'pending' if source['id'] not in expected else 'evaluated')
|
||||
rows.append({**source, 'status': status, 'name': row.get('name') or source.get('name'),
|
||||
'verdict': row.get('verdict'), 'basis': row.get('basis'),
|
||||
'decision_id': (published.get('decision') or {}).get('id')})
|
||||
return {'ok': True, 'rows': rows[:300], 'revision': state['revision']}
|
||||
|
||||
|
||||
def refresh_saved():
|
||||
"""请求结束后尝试发布;失败保留登记和待核验状态,定时修订会重试。"""
|
||||
import logging
|
||||
import plan_refresh
|
||||
try:
|
||||
return plan_refresh.refresh()
|
||||
except Exception:
|
||||
logging.getLogger(__name__).exception('用户候选已保存,计划修订待重试')
|
||||
return {'status': 'pending'}
|
||||
Loading…
Reference in New Issue