Files
telegram-customer-bot/WORKFLOW_VISUALIZATION.txt
2025-11-01 21:58:31 +08:00

206 lines
12 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

╔══════════════════════════════════════════════════════════════════════════╗
║ 自动翻页与缓存系统工作流程图 ║
╚══════════════════════════════════════════════════════════════════════════╝
场景1: 首次搜索(缓存未命中)
═══════════════════════════════════════════════════════════════════
┌─────────────┐
│ 用户发送命令 │ /text 德州扑克
└──────┬──────┘
┌──────────────────┐
│ Bot检查数据库缓存 │
└──────┬───────────┘
┌─────────┐
│ 未命中? │
└────┬────┘
│ Yes
┌────────────────────────┐ ┌─────────────────────────┐
│ 转发到搜索Bot @openaiw │──────→│ 搜索Bot返回第1页结果 │
└────────────────────────┘ └───────────┬─────────────┘
┌────────────────────────────────────┐
│ 【前台】立即显示给用户(秒开) │
│ - 显示第1页内容 │
│ - 显示翻页按钮 │
│ - 用户可以立即操作 │
└────────────────────────────────────┘
┌────────────────────────────────────┐
│ 【后台】启动自动翻页任务(异步) │
│ asyncio.create_task() │
└───────────┬────────────────────────┘
┌───────────────────────────────────────┐
│ 后台循环翻页(用户无感知) │
│ │
│ for page in range(2, 11): │
│ ├─ 等待2秒 │
│ ├─ 点击"下一页"按钮 │
│ ├─ 获取新页面内容 │
│ ├─ 保存到数据库 │
│ │ • 文本内容 │
│ │ • 按钮数据JSON
│ │ • 创建时间 │
│ └─ 检查是否还有下一页 │
│ │
│ 【日志】第2页已保存 │
│ 【日志】第3页已保存 │
│ ... │
│ 【日志】完成共10页 │
└───────────────────────────────────────┘
┌─────────────────────────┐
│ 全部保存到SQLite数据库 │
│ /home/atai/bot_data/ │
│ cache.db │
└─────────────────────────┘
场景2: 重复搜索(缓存命中)
═══════════════════════════════════════════════════════════════════
┌─────────────┐
│ 用户发送命令 │ /text 德州扑克(再次搜索)
└──────┬──────┘
┌──────────────────┐
│ Bot检查数据库缓存 │
└──────┬───────────┘
┌─────────┐
│ 命中? │
└────┬────┘
│ Yes!
┌────────────────────────────────────┐
│ 从数据库读取缓存数据(秒开!) │
│ • 读取文本内容 │
│ • 读取按钮JSON │
│ • 重建InlineKeyboardMarkup │
│ • 更新access_count +1 │
└────────┬───────────────────────────┘
┌─────────────────────────┐
│ 立即显示给用户(秒开!) │
│ - 不需要请求搜索Bot │
│ - 不需要网络请求 │
│ - 按钮完整恢复 │
│ - 响应时间 < 100ms │
└─────────────────────────┘
场景3: 用户翻页(缓存命中)
═══════════════════════════════════════════════════════════════════
┌──────────────┐
│ 用户点击按钮 │ [下一页]
└──────┬───────┘
┌───────────────────────┐
│ Bot解析callback_data │ 例如page_5
└──────┬────────────────┘
┌─────────────────────────────┐
│ 从session获取搜索上下文 │
│ • command: /text │
│ • keyword: 德州扑克 │
│ • page: 5 │
└──────┬──────────────────────┘
┌──────────────────────┐
│ 检查数据库缓存 │ get_cache(/text, 德州扑克, 5)
└──────┬───────────────┘
┌─────────┐
│ 命中? │
└────┬────┘
│ Yes!
┌─────────────────────────────┐
│ 从缓存加载第5页秒开
│ - 完整内容 │
│ - 所有按钮 │
│ - edit_message直接替换 │
└─────────────────────────────┘
═══════════════════════════════════════════════════════════════════
关键性能指标
═══════════════════════════════════════════════════════════════════
首次搜索:
├─ 用户看到第1页~1-2秒取决于搜索Bot
├─ 后台翻页启动:立即(异步)
└─ 完成10页缓存~20秒用户无感知
重复搜索:
├─ 缓存命中:<100ms秒开
├─ 按钮完整性100%
└─ 无需网络请求
缓存翻页:
├─ 页面切换:<100ms秒开
├─ 从缓存加载:是
└─ 请求搜索Bot
═══════════════════════════════════════════════════════════════════
数据库结构
═══════════════════════════════════════════════════════════════════
search_cache 表:
┌─────────────────┬──────────────────────────────────────┐
│ 字段 │ 说明 │
├─────────────────┼──────────────────────────────────────┤
│ id │ 主键,自增 │
│ command │ 命令(如 /text, /search
│ keyword │ 关键词 │
│ page │ 页码 │
│ result_text │ 结果文本 │
│ result_html │ HTML格式备用
│ buttons_json │ 按钮数据JSON格式
│ result_hash │ 内容哈希(用于去重) │
│ created_at │ 创建时间 │
│ expires_at │ 过期时间30天
│ access_count │ 访问次数 │
│ last_accessed │ 最后访问时间 │
└─────────────────┴──────────────────────────────────────┘
索引:
• idx_unique_cache (command, keyword, page, result_hash) - 防重复
• idx_search (command, keyword, page) - 快速查询
• idx_expires (expires_at) - 过期清理
═══════════════════════════════════════════════════════════════════
当前数据统计
═══════════════════════════════════════════════════════════════════
总缓存记录245条
按钮完整性100% (245/245)
数据库大小652KB
有效缓存245条
过期缓存0条
热门缓存:
1. /text "德州扑克" - 73页
2. /text "科技" - 52页
3. /text "德州" - 50页
═══════════════════════════════════════════════════════════════════