feat: Enhance logging and parameter tracking capabilities
- Refactor `gui_core.py` to improve code readability and maintainability by adding line breaks for long statements. - Introduce `inspect_lines.py` script for inspecting specific line ranges in files. - Add `generate_param_locations.py` to scan the repository and generate a report of function definitions, argparse parameters, and tracking calls. - Implement `param_usage_tool.py` for analyzing `param_usage.json` records and scanning code for tracking calls. - Create `simulate_reservation_preempted_flow.py` and `simulate_reservation_preempted_flow_dynamic.py` to validate behavior of reservation logic under specific conditions. - Add global parameter tracking feature in `run_cli.py` to enable detailed usage tracing.
This commit is contained in:
1 parent
68c8ccea66
commit
95b4517bdd
23 files changed
+1560
-849
No files matched your search
@@ -0,0 +1,96 @@
|
||||
# 参数使用跟踪模块说明
|
||||
|
||||
简要说明如何使用新添加的 `douyin.utils.param_usage_tracker` 模块来记录参数调用位置。
|
||||
|
||||
1) 直接记录单个参数位置
|
||||
|
||||
```python
|
||||
from douyin.utils.param_usage_tracker import track_param
|
||||
|
||||
def some_fn():
|
||||
cfg_value = 42
|
||||
track_param("important_threshold", cfg_value)
|
||||
|
||||
# ... 业务逻辑 ...
|
||||
```
|
||||
|
||||
调用 `track_param` 会把一条记录追加到 `state/param_usage.json`,包含时间戳、调用文件、行号、函数名和参数的 repr。文件为逐行 JSON(NDJSON)。
|
||||
|
||||
2) 用装饰器自动记录函数调用参数
|
||||
|
||||
```python
|
||||
from douyin.utils.param_usage_tracker import track_call
|
||||
|
||||
@track_call()
|
||||
def process(item, flag=False):
|
||||
# 每次调用时,会把 func 名、定义位置、调用位置与参数记录到 state/param_usage.json
|
||||
pass
|
||||
|
||||
process(1, flag=True)
|
||||
```
|
||||
|
||||
3) 读取记录
|
||||
|
||||
可以在调试脚本或 REPL 中使用 `read_entries()` 来读取并解析记录文件:
|
||||
|
||||
```python
|
||||
from douyin.utils.param_usage_tracker import read_entries
|
||||
|
||||
entries = read_entries(limit=200)
|
||||
for e in entries:
|
||||
print(e)
|
||||
```
|
||||
|
||||
注意:为了兼容并发与减少文件读写冲突,记录以逐行 JSON 追加方式保存(NDJSON)。如需转换为 JSON 数组,可用脚本读取所有行并写入新的 JSON 文件。
|
||||
|
||||
启用全局跟踪(可选)
|
||||
|
||||
如果你想在程序运行期间自动记录仓库内所有函数的调用位置与参数,可以在启动前设置环境变量 `PARAM_TRACK_ALL=1`:
|
||||
|
||||
```bash
|
||||
PARAM_TRACK_ALL=1 python run_cli.py --help
|
||||
```
|
||||
|
||||
在 Windows PowerShell 中:
|
||||
|
||||
```powershell
|
||||
$env:PARAM_TRACK_ALL = '1'
|
||||
python run_cli.py --help
|
||||
```
|
||||
|
||||
注意:全局跟踪使用 `sys.settrace` 实现,会对性能产生显著影响,建议仅用于短时调试或在受控环境下使用。跟踪记录默认写入 `state/param_usage.json`(逐行 JSON)。
|
||||
|
||||
性能与范围控制建议:
|
||||
- 在仅需跟踪少数模块时,优先使用 `@track_call()` 装饰器或在关键位置调用 `track_param(...)`,以降低运行开销。
|
||||
- 若启用全局跟踪,请在完成诊断后关闭(退出进程)以避免长期性能下降。
|
||||
|
||||
查看与转换记录示例:
|
||||
|
||||
```python
|
||||
from douyin.utils.param_usage_tracker import read_entries
|
||||
|
||||
entries = read_entries(limit=500)
|
||||
for e in entries:
|
||||
print(e)
|
||||
|
||||
# 将 NDJSON 转为 JSON 数组
|
||||
with open('state/param_usage.json','r',encoding='utf-8') as f:
|
||||
lines = [json.loads(l) for l in f if l.strip()]
|
||||
with open('state/param_usage_array.json','w',encoding='utf-8') as f:
|
||||
json.dump(lines, f, ensure_ascii=False, indent=2)
|
||||
```
|
||||
|
||||
快速查看工具:
|
||||
|
||||
仓库中包括一个独立工具 `tools/param_usage_tool.py`,可以作为独立脚本使用来查看记录或静态扫描代码:
|
||||
|
||||
示例:
|
||||
|
||||
```bash
|
||||
python tools/param_usage_tool.py show --limit 200
|
||||
python tools/param_usage_tool.py show --param important_threshold
|
||||
python tools/param_usage_tool.py tail
|
||||
python tools/param_usage_tool.py scan track_param
|
||||
```
|
||||
|
||||
该工具不依赖运行时跟踪器,可独立拿来分析 `state/param_usage.json` 或查找代码中 `track_param`/`track_call` 的调用位置。
|
||||
+9
-22
@@ -61,8 +61,7 @@
|
||||
|
||||
2. **队列满等待状态检查**:
|
||||
- 如果已在等待状态(`queue_full_waiting_at_earliest = True`)
|
||||
- 且设置了直接参与标志(`_reservation_back_direct_attend = True`)
|
||||
- 跳过预约回退检查,直接进入阶段诊断
|
||||
- 回退后的立即参与由回退处理流程内决定(已移除 `_reservation_back_direct_attend` 标志),此时会在流程中直接处理参与逻辑或进入阶段诊断
|
||||
|
||||
3. **预约回退窗口检查**:
|
||||
- 如果队列未满,调用 `process_reservation_attend_window` 检查是否需要回退
|
||||
@@ -81,9 +80,8 @@
|
||||
- 计算位置差(`current_room_pos - target_pos`)
|
||||
- 如果不在目标位置,先下滑一次确保在直播列表中
|
||||
- 调用 `seek_reservation_room_callback` 回退到目标直播间
|
||||
5. **设置立即参与标志**:
|
||||
- 设置 `_reservation_back_direct_attend = True`
|
||||
- 设置锁定期为当前时间(`focus_until = now_ts`),表示立即参与
|
||||
5. **立即参与行为**:
|
||||
- 回退到目标直播间时会触发立即参与的行为(不再通过 `_reservation_back_direct_attend` 标志),同时设置锁定期为当前时间(`focus_until = now_ts`)以保证短期内不切换直播间
|
||||
6. **等待页面加载**:延迟1.5秒等待页面加载完成
|
||||
|
||||
**特点:**
|
||||
@@ -155,9 +153,7 @@
|
||||
### 3.1 预约回退后的参与
|
||||
|
||||
**场景1:队列满后立即参与**
|
||||
- 设置 `_reservation_back_direct_attend = True`
|
||||
- 锁定期设置为当前时间(立即参与)
|
||||
- 在阶段诊断后检查直接参与标志
|
||||
- 队列满后会在回退流程内触发立即参与行为(不再设置 `_reservation_back_direct_attend`),并设置锁定期为当前时间
|
||||
|
||||
**场景2:时间窗口内回退**
|
||||
- 回退到目标直播间后,设置锁定期和待处理标志
|
||||
@@ -167,28 +163,19 @@
|
||||
### 3.2 直接参与标志处理
|
||||
|
||||
**在福袋详情页(`stage == "FUDai_DETAIL"`):**
|
||||
1. 检查 `_reservation_back_direct_attend` 标志
|
||||
2. 如果为 `True`:
|
||||
- 清除直接参与标志
|
||||
- 设置强制参与标志(`_force_attend_after_reservation_back = True`)
|
||||
- 调用 `_handle_reservation_direct_attend()` 处理
|
||||
回退后参与由回退流程直接处理;已移除 `_reservation_back_direct_attend` 与 `_force_attend_after_reservation_back`,不再依赖这些运行时标志。详情页检测到回退待处理时会直接进入参与处理逻辑或按常规验证流程执行。
|
||||
|
||||
**在直播间(`stage == "IN_LIVE_ROOM"`):**
|
||||
1. 检查 `_reservation_back_direct_attend` 标志
|
||||
1. 检查回退待处理状态(直接参与标志已移除)
|
||||
2. 如果为 `True`:
|
||||
- 清除直接参与标志
|
||||
- 设置强制参与标志
|
||||
- 查找福袋并打开详情页
|
||||
- 然后直接参与
|
||||
|
||||
### 3.3 强制参与标志处理
|
||||
### 3.3 回退后参与决策
|
||||
|
||||
在 `basic_join_decision` 中:
|
||||
- 如果检测到 `_force_attend_after_reservation_back = True`:
|
||||
- 跳过数据验证
|
||||
- 跳过倒计时检查
|
||||
- 直接进入参与逻辑
|
||||
- 参与后清除强制参与标志
|
||||
回退后不再通过运行时强制参与标志跳过验证;现在的决策基于时间窗口与回退上下文:当回退到目标直播间且时间窗口满足参与条件时,流程会直接执行参与逻辑;否则按常规的验证与预约记录处理。
|
||||
|
||||
---
|
||||
|
||||
@@ -287,7 +274,7 @@
|
||||
↓
|
||||
4. 立即回退到最早开奖的预约直播间
|
||||
↓
|
||||
5. 设置直接参与标志(_reservation_back_direct_attend = True)
|
||||
5. 触发立即参与行为(已移除 `_reservation_back_direct_attend` 标志)
|
||||
↓
|
||||
6. 设置锁定期为当前时间(立即参与)
|
||||
↓
|
||||
|
||||
Reference in new issue
Block a user