tasks.md
7.26 KB
1. 数据库与基础设施准备
-
1.1 创建对话日志表 migration
- 字段: log_id, session_id, user_id, agent_id, agent_name, query, response, intent, entities (JSON), timestamp, processing_time_ms, has_error
- 建立索引: user_id, agent_id, intent, timestamp, has_error
-
1.2 创建埋点事件表 migration
- 字段: event_id, event_type, event_data (JSON), timestamp
- 建立索引: event_type, timestamp
-
1.3 创建统计表 migration
- 字段: stat_date, dimension, dimension_value, total_agent_calls, total_sessions, avg_session_turns, intent_distribution (JSON), agent_call_ranking (JSON), daily_trend (JSON)
-
1.4 创建页面访问埋点表 migration
- 字段: page_view_id, page_type, user_id, session_id, timestamp, device_type
- 建立索引: page_type, user_id, timestamp
-
1.5 创建活码扫码事件表 migration
- 字段: scan_event_id, qrcode_id, channel_code, customer_user_id, scan_time, position_code, employee_id
- 建立索引: qrcode_id, channel_code, customer_user_id, scan_time
-
1.6 创建入群成功事件表 migration
- 字段: group_join_event_id, group_id, customer_user_id, employee_count, agent_count, join_time, channel_code
- 建立索引: group_id, customer_user_id, join_time, channel_code
- 1.7 配置消息队列 (如 Redis Queue / Kafka / RabbitMQ)
- 1.8 配置定时任务用于预聚合统计数据
2. 模型与数据层 (Model & Schema)
-
2.1 定义 SQLAlchemy ORM 模型
- ConversationLog 模型
- AnalyticsEvent 模型
- AnalyticsStatistic 模型
- PageView 模型
- ScanEvent 模型
- GroupJoinEvent 模型
-
2.2 定义 Pydantic 请求与响应模型
- ConversationLogListRequest (Query 参数: page, page_size, user_id, agent_id, intent_type, time_start, time_end, has_error)
- ConversationLogListResponse (包含 total 与 list)
- AnalyticsEventRequest (包含 events 数组)
- AnalyticsEventResponse (包含 success 与 received_count)
- AnalyticsStatisticsRequest (Query 参数: date_range, group_by)
- AnalyticsStatisticsResponse (包含所有统计字段)
- PageViewRequest (字段: page_type, user_id, session_id, timestamp, device_type)
- PageViewResponse (字段: success)
- ScanEventRequest (字段: qrcode_id, channel_code, customer_user_id, scan_time, position_code, employee_id)
- ScanEventResponse (字段: success)
- GroupJoinEventRequest (字段: group_id, customer_user_id, employee_count, agent_count, join_time, channel_code)
- GroupJoinEventResponse (字段: success)
3. 业务逻辑层 (Service Layer)
-
3.1 实现对话日志查询逻辑
- 3.1.1 接收并验证查询参数: page, page_size, user_id, agent_id, intent_type, time_start, time_end, has_error
- 3.1.2 构建动态查询条件 (多参数 AND 组合)
- 3.1.3 执行分页查询
- 3.1.4 返回总数与日志列表
-
3.2 实现埋点事件上报逻辑
- 3.2.1 接收 events 数组并进行基本格式验证
- 3.2.2 异步将事件写入消息队列 (不阻塞主流程)
- 3.2.3 立即返回响应,包含 received_count
-
3.3 实现埋点事件消费与存储逻辑
- 3.3.1 消费消息队列中的事件
- 3.3.2 持久化到埋点事件表
-
3.4 实现统计数据查询逻辑
- 3.4.1 接收并验证参数: date_range (last_7_days, last_30_days, 自定义), group_by (agent, intent, date)
- 3.4.2 查询预聚合统计表
- 3.4.3 返回完整统计数据结构
-
3.5 实现统计数据预聚合定时任务逻辑
- 3.5.1 按日/按小时聚合原始事件数据
- 3.5.2 更新统计表
-
3.6 实现页面访问埋点逻辑
- 3.6.1 接收并验证参数: page_type, user_id, session_id, timestamp, device_type
- 3.6.2 持久化到页面访问埋点表
- 3.6.3 返回 success: true
-
3.7 实现活码扫码事件埋点逻辑
- 3.7.1 接收并验证参数: qrcode_id, channel_code, customer_user_id, scan_time, position_code, employee_id
- 3.7.2 持久化到活码扫码事件表
- 3.7.3 返回 success: true
-
3.8 实现入群成功事件埋点逻辑
- 3.8.1 接收并验证参数: group_id, customer_user_id, employee_count, agent_count, join_time, channel_code
- 3.8.2 持久化到入群成功事件表
- 3.8.3 返回 success: true
4. 接口层 (API Layer)
-
4.1 挂载
GET /api/v1/logs/conversations查询对话日志接口- 4.1.1 实现 Query 参数绑定与验证
- 4.1.2 调用 Service 层查询逻辑
- 4.1.3 返回标准化 JSON 响应
-
4.2 挂载
POST /api/v1/analytics/events埋点数据上报接口- 4.2.1 实现 Request Body 绑定与验证
- 4.2.2 调用 Service 层异步上报逻辑
- 4.2.3 立即返回响应 (success 与 received_count)
-
4.3 挂载
GET /api/v1/analytics/statistics查询埋点统计数据接口- 4.3.1 实现 Query 参数绑定与验证
- 4.3.2 调用 Service 层统计查询逻辑
- 4.3.3 返回完整统计数据
-
4.4 挂载
POST /api/v1/agent-group/analytics/page-view页面访问埋点接口- 4.4.1 实现 Request Body 绑定与验证
- 4.4.2 调用 Service 层页面访问埋点逻辑
- 4.4.3 返回 success 响应
-
4.5 挂载
POST /api/v1/agent-group/analytics/scan-event活码扫码事件埋点接口- 4.5.1 实现 Request Body 绑定与验证
- 4.5.2 调用 Service 层扫码事件埋点逻辑
- 4.5.3 返回 success 响应
-
4.6 挂载
POST /api/v1/agent-group/analytics/group-join-success入群成功事件埋点接口- 4.6.1 实现 Request Body 绑定与验证
- 4.6.2 调用 Service 层入群事件埋点逻辑
- 4.6.3 返回 success 响应
5. 测试与验证
-
5.1 编写对话日志查询单元测试
- 5.1.1 测试无筛选条件的分页查询
- 5.1.2 测试按 user_id 筛选
- 5.1.3 测试按 agent_id 筛选
- 5.1.4 测试按 intent_type 筛选
- 5.1.5 测试按时间范围 (time_start, time_end) 筛选
- 5.1.6 测试按 has_error 筛选
- 5.1.7 测试多条件组合筛选
-
5.2 编写埋点事件上报单元测试
- 5.2.1 测试单个事件上报
- 5.2.2 测试批量事件上报
- 5.2.3 测试响应 received_count 正确性
-
5.3 编写统计数据查询单元测试
- 5.3.1 测试 date_range = last_7_days
- 5.3.2 测试 date_range = last_30_days
- 5.3.3 测试 group_by 参数生效
- 5.3.4 验证所有统计字段完整返回
- 5.4 集成测试: 埋点上报 → 消费存储 → 预聚合 → 查询统计的完整链路
-
5.5 编写页面访问埋点单元测试
- 5.5.1 测试正常上报流程
- 5.5.2 测试参数缺失验证
- 5.5.3 验证数据持久化正确
-
5.6 编写活码扫码事件埋点单元测试
- 5.6.1 测试正常上报流程
- 5.6.2 测试参数缺失验证
- 5.6.3 验证数据持久化正确
-
5.7 编写入群成功事件埋点单元测试
- 5.7.1 测试正常上报流程
- 5.7.2 测试参数缺失验证
- 5.7.3 验证数据持久化正确
- 5.8 集成测试: 扫码 → 记录扫码事件 → 入群 → 记录入群事件 → 计算转化率的完整链路