gao
|
a8ea132924
|
fix(stock_node+utils+cli): review findings P0/P1/P2 一轮修复
medium effort code-review 暴露 8 个 finding,按 P0/P1/P2 优先级修复:
P0 (数据正确性 / 跑得起来):
#1 replace_all_node_categories 改 scope-aware delete
之前无条件 delete(NodeCategory) + 插入 runtime filter 后的子集,
导致 --type2 0 跑把其它 6 个 category 误删。改为只 delete 本批
category_key 集合,其它 type2 不动。
#2 replace_all_stock_node_map 对称化
之前空 rows 时 early-return 留下 stale,导致「categories 新
但 mappings 旧」不一致。改为 scope-aware delete 同 #1。
#3 stock_node systemd --workers 20 → 8
20 workers × 默认 10 RPS = 200 RPS,撞穿 mairui 钻石档 100 RPS
上限,触发风控。改为 8 × 10 = 80 RPS,留 20% buffer。
P1 (静默错):
#4 is_a_share_code 接 hermes 格式
之前硬性要求 len(c)==6,iter_stock_codes 改返 hermes 时 5 个
task (moneyflow/share_snapshot/kline_5min/tick_trade/kline_daily)
会静默过滤成空 list。剥前缀再判断,兼容 'SH600519'。
#5 mairui tz 契约钉在 source 层
tz_localize 改为「已带 tz 就保留,没有再标 Asia/Shanghai」,
避免 mairui 改格式时抛 TypeError。task_tick_trade 删掉
strftime fallback + 冗长注释(契约已在 source 层 docstring)。
#6 CLI inspect.signature 过滤 kwargs
--type2 / --backfill 加在共享 p_sync parser 上,任何 task 通过
**kwargs 静默吞掉。改为 dispatcher 按 task._run 签名过滤,
不支持的参数打 warning。
P2 (维护):
#7 task_stock_node --type2 输入校验
之前空字符串 / 「concept」 / 99 都 silently collapse 成空 set,
走「无匹配叶子节点」warning 分支(被 daily_check 当正常)。
CLI 不再 silent-drop,任务层加 strict 校验,typo 返 status=error。
#8 code6_to_exchange 删 3 个死分支
4 个 code6_to_* wrapper 都先 _strip_hermes,3 个 hermes if 分支
走不到。删除后 function 简化,所有 caller 行为不变。
附带:
- tests/test_smoke.py: 11 → 13 (含 mairui_ma_daily + stock_node)
- tests/test_schema_models.py: 21 → 22 (含 kline_stock_ma_daily +
node_categories/nodes/stock_node_map)
- bin/market_sync_stock_node_run.sh 注释补充 RPS 计算
验证:
- pytest tests/ 11 passed
- is_a_share_code 8 个 hermes 边界 case 全过
- cli sync kline_daily --type2 2,3 → warning 已打印
- cli sync stock_node --type2 concept → status=error
- cli sync stock_node --type2 99 → status=error
- cli sync stock_node --type2 0 → 仅 0:0 行被刷新,其它 6 类不动
- fetch_tick_trade('600519') 返回 tz=Asia/Shanghai +08:00
|
2026-07-07 16:04:47 +08:00 |
|
gao
|
d140fee9e7
|
fix: 早盘前 09:00 stock_basic + industry_sector 没触发
work #02 (2026-07-03):daily_check 复跑仍 10 missed,细看时间窗口发现
新问题——09:00 / 09:30 该跑的 2 个早盘前 task today 没触发。
根因:worker.py 进程内 scheduler 是预期的主调度(含 09:00 / 09:30),
但 worker.py 没人拉起;systemd timers 又只覆盖 15:30 / 21:xx,缺早盘档。
昨 18h 删 monitor 同时没补 worker.py → 早盘缺口暴露。
变更:
- bin/market_sync_morning_run.sh: 轻量 morning wrapper,只跑 stock_basic
+ industry_sector 串行(~25min),不复用 runall_once.py 的 8 task
- bin/systemd/market-sync-morning.{service,timer}: Mon..Fri 09:00 daily
触发,TimeoutStartSec=3600 (1h)
- docs/works/2026-07-03-02-morning-no-trigger.md: 完整 work 记录
不在本 work 范围(更大重构):把 worker.py 拉起作为 long-running,
与 systemd timer 收敛到单一调度器——见 dual-scheduler-overlap memory。
Co-Authored-By: Claude <noreply@anthropic.com>
|
2026-07-03 10:50:25 +08:00 |
|
gao
|
295177027f
|
fix: market-sync 2h timeout 截断 + 加 daily-check 巡检
work #01 (2026-07-03):昨天 15:30 runall 中 kline_5min 全量 69min 把 2h
窗口吃满,share_snapshot / market_regime 被 systemd SIGTERM 截断;
今天又叠加 worker.py 未长驻,导致 10 个 task missed。
变更:
- bin/systemd/market-sync.service: TimeoutStartSec 7200 → 14400 (4h)
- bin/daily_sync_check.py: 新增每日巡检,读 PG dataset_registry,
对比 SCHEDULE 判定 ok/missed/failed/stuck/never_run,写 JSON 报告,
失败时 POST HMAC webhook
- bin/systemd/market-sync-daily-check.{service,timer}: 23:00 daily 触发
daily_check(替代 session-only 的 /loop 方案)
- config/daily_check_secrets.env.template: webhook 配置模板
- docs/works/2026-07-03-01-market-sync-timeout.md: 完整 work 记录
(根因 + 中期/长期改进建议)
Co-Authored-By: Claude <noreply@anthropic.com>
|
2026-07-03 10:48:20 +08:00 |
|
gao
|
7a985dddd5
|
refactor: PG-only 迁移 + 龙虎榜/tick/moneyflow 同步 + stock_code 统一 + mairui 编码修复
## 1. PG-only 重构
- 删 app/core/db/connection.py + schema.py (MySQL 路径)
- 新 app/core/db/{orm,models,pg_bootstrap}.py — SQLAlchemy 2.x ORM 一键建表
- 16 张业务表全在 market_data schema,原生 TIMESTAMPTZ / JSONB / Float / TEXT
- requirements.txt 删 PyMySQL 路径,加 psycopg2
## 2. 同步任务扩展(3 个新 task)
- **tick_trade** (mairui hsrl/zbjy):当天逐笔交易,21:00 发布
- **moneyflow** (mairui hsstock/history/transaction):个股资金流,21:30 发布
- **longhubang** (akshare):龙虎榜聚合层 + 席位层(2 张新表)
- 长虎榜放宽 akshare 政策:仅"无替代源 + 烟测通过"场景允许
- data_eastmoney 私有 API 不需要(akshare 烟测通过)
- 3-timer 设计:
- 15:30 market-sync.service (8 base tasks via runall_once)
- 21:05 market-sync-tick.service (tick_trade)
- 21:35 market-sync-moneyflow.service (moneyflow)
- 22:00 market-sync-lhb.service (longhubang,新加)
- bin/systemd/ 新增 tick / moneyflow / lhb 各 1 对 service+timer
- bin/market_sync_*_run.sh wrapper 脚本(不做法定节假日过滤,fail-open)
## 3. stock_code 统一为带 SH/SZ/BJ 前缀
- 历史 bug:stocks.code 用 SH600519,但 kline/moneyflow/tick_trade/kline_5min
/stock_sector_map/industry 6 张表用纯 6 位 600519,跨表 JOIN 全部 0 行
- 新增 to_hermes() 工具:6位 / 9位(mairui `000001.SZ` 格式)→ 统一 SH000001
- 5 个 task 改写:用 to_hermes(code6) 写入 stock_code
- 一次性迁移 6 张表存量 154M 行(CASE WHEN 探测 + 去重 + 加前缀)
- ORM: stock_sector_map.stock_code / industry.code String(6)→String(10)
## 4. Bug 修复
- **share table stock_code 格式**:之前写 6 位不带前缀,与 stocks 不一致
→ 修 task_share_snapshot + 一次性 UPDATE 63,417 行加前缀
- **share_snapshot warning 状态错填 last_error**:
→ 加 mark_sync_warning() 走专用路径,不写 last_failure_at / last_error
- **schedule config lastRun 不同步**:
→ 加 update_job_status_for_dataset(),SyncTask.run() 完成后自动镜像
→ cli/runall 触发的 task 也能更新 schedule config
## 5. mairui UTF-8 编码修复
- 历史 bug:mairui.py:_fetch 用 latin-1 兜底解码,把所有 UTF-8 中文名
double-encoded 写入 stocks.name(如 `歌华有线` 变成 `æ\xad\x8cå\x8d\x8e...`)
- 加 _decode_response():UTF-8 → GBK → latin-1 兜底
- 一次性修复 stocks.name 5,213 行:
- 4,370 行 (encode('latin-1').decode('utf-8') 反向解码)
- 616 行 (含 fullwidth A,宽松 printable 检查)
- 820 行 (mid-character 截断,重新从 mairui 拉)
## 6. 测试
- tests/test_smoke.py: TASKS 10→11, SYNC_DEFINITIONS 10→11
- tests/test_schema_models.py: 16→18 张表,新增 longhubang_daily/seat
- pytest 11/11 passed
## 验证
- 6 张表 0 残留无前缀行
- stocks JOIN kline_stock / kline_5min / moneyflow / tick_trade / stock_sector_map:88-100% 命中
- 5,213 stocks.name 全部正确 UTF-8 中文
- pytest 11/11 passed
|
2026-07-01 22:00:58 +08:00 |
|