backend-api-test-doc-analytics.md
10.8 KB
接口测试文档 — analytics
1. 模块概览
| 字段 | 值 |
|---|---|
| 模块名称 | analytics |
| 模块说明 | 日志与埋点 — 会话日志查询、事件上传、统计报表、页面浏览/扫码/入群追踪 |
| 路由前缀 |
/api/v1/logs, /api/v1/analytics, /api/v1/agent-group/analytics
|
| 数据库表 |
conversation_logs, analytics_events, analytics_statistics, page_views, scan_events, group_join_events
|
| 外部依赖 | 无 |
2. 接口列表
| 序号 | 方法 | 路径 | 说明 |
|---|---|---|---|
| 1 | GET | /api/v1/logs/conversations |
查询会话日志列表 |
| 2 | POST | /api/v1/analytics/events |
上传埋点事件 |
| 3 | GET | /api/v1/analytics/statistics |
获取统计报表 |
| 4 | POST | /api/v1/agent-group/analytics/page-view |
记录页面浏览 |
| 5 | POST | /api/v1/agent-group/analytics/scan-event |
记录扫码事件 |
| 6 | POST | /api/v1/agent-group/analytics/group-join-success |
记录入群事件 |
3. 接口详细说明
3.1 查询会话日志列表
基本信息
| 字段 | 值 |
|---|---|
| 方法 | GET |
| 路径 | /api/v1/logs/conversations |
| 说明 | 分页查询会话日志,支持多维度筛选 |
| Content-Type | 无 |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| page | query | integer | 否 | 页码,默认1 |
| page_size | query | integer | 否 | 每页条数,默认20,上限100 |
| user_id | query | string | 否 | 按用户ID筛选 |
| agent_id | query | integer | 否 | 按Agent ID筛选 |
| intent_type | query | string | 否 | 按意图类型筛选 |
| time_start | query | datetime | 否 | 起始时间 |
| time_end | query | datetime | 否 | 结束时间 |
| has_error | query | boolean | 否 | 是否有错误 |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| total | integer | 总条数 |
| list | array | 日志列表 |
| list[].id | integer | 日志ID |
| list[].session_id | string | 会话ID |
| list[].user_id | string | 用户ID |
| list[].agent_id | integer | Agent ID |
| list[].agent_name | string | Agent名称 |
| list[].query | string | 用户查询 |
| list[].response | string | Agent回复 |
| list[].intent | string | 意图 |
| list[].entities | object | 实体JSON |
| list[].processing_time_ms | integer | 处理耗时(ms) |
| list[].has_error | boolean | 是否有错误 |
| list[].created_at | string | 创建时间 |
错误码
| HTTP 状态码 | 触发条件 | 错误信息示例 |
|---|---|---|
| 无 | 空结果不报错,返回 total=0, list=[] | — |
3.2 上传埋点事件
基本信息
| 字段 | 值 |
|---|---|
| 方法 | POST |
| 路径 | /api/v1/analytics/events |
| 说明 | 批量上传埋点事件 |
| Content-Type | application/json |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| events | body | array | 是 | 事件列表,至少1条 |
| events[].event_type | body | string | 是 | 事件类型,最长64字符 |
| events[].event_data | body | object | 否 | 事件数据 |
| events[].timestamp | body | datetime | 否 | 事件时间 |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| success | boolean | 是否成功 |
| received_count | integer | 接收事件数 |
3.3 获取统计报表
基本信息
| 字段 | 值 |
|---|---|
| 方法 | GET |
| 路径 | /api/v1/analytics/statistics |
| 说明 | 获取预聚合统计数据 |
| Content-Type | 无 |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| date_range | query | string | 否 | 日期范围,默认 last_7_days
|
| group_by | query | string | 否 | 分组维度 |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| total_agent_calls | integer | Agent调用总数 |
| total_sessions | integer | 会话总数 |
| avg_session_turns | float | 平均会话轮次 |
| intent_distribution | array | 意图分布 |
| intent_distribution[].intent | string | 意图名 |
| intent_distribution[].count | integer | 数量 |
| intent_distribution[].percentage | float | 百分比 |
| agent_call_ranking | array | Agent调用排行 |
| agent_call_ranking[].agent_id | integer | Agent ID |
| agent_call_ranking[].agent_name | string | Agent名称 |
| agent_call_ranking[].call_count | integer | 调用次数 |
| daily_trend | array | 每日趋势 |
| daily_trend[].date | string | 日期 |
| daily_trend[].calls | integer | 调用数 |
| daily_trend[].sessions | integer | 会话数 |
3.4 记录页面浏览
| 字段 | 值 |
|---|---|
| 方法 | POST |
| 路径 | /api/v1/agent-group/analytics/page-view |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| page_type | body | string | 是 | 页面类型,最长64 |
| user_id | body | string | 否 | 用户ID |
| session_id | body | string | 否 | 会话ID |
| device_type | body | string | 否 | 设备类型 |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| success | boolean | 是否成功 |
3.5 记录扫码事件
| 字段 | 值 |
|---|---|
| 方法 | POST |
| 路径 | /api/v1/agent-group/analytics/scan-event |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| qrcode_id | body | integer | 否 | 活码ID |
| channel_code | body | string | 否 | 渠道编码 |
| customer_user_id | body | string | 否 | 客户用户ID |
| position_code | body | string | 否 | 岗位编码 |
| employee_id | body | string | 否 | 员工ID |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| success | boolean | 是否成功 |
3.6 记录入群事件
| 字段 | 值 |
|---|---|
| 方法 | POST |
| 路径 | /api/v1/agent-group/analytics/group-join-success |
请求参数
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| group_id | body | string | 否 | 群ID |
| customer_user_id | body | string | 否 | 客户用户ID |
| employee_count | body | integer | 否 | 员工数,默认0 |
| agent_count | body | integer | 否 | Agent数,默认0 |
| channel_code | body | string | 否 | 渠道编码 |
响应参数
| 字段路径 | 类型 | 说明 |
|---|---|---|
| success | boolean | 是否成功 |
4. 测试场景
4.1 会话日志查询
| 序号 | 场景名称 | 分类 | 前置条件 | 请求参数 | 预期结果 |
|---|---|---|---|---|---|
| 1 | 无筛选查询 | 正常 | DB有2条日志 | 无筛选 | total=2 |
| 2 | 按user_id筛选 | 正常 | 2条不同user_id | user_id="emp_001" | total=1 |
| 3 | 按agent_id筛选 | 正常 | 2条不同agent_id | agent_id=102 | total=1 |
| 4 | 按intent筛选 | 正常 | 2条不同intent | intent_type="query-metric" | total=1 |
| 5 | 按has_error筛选 | 正常 | 1条有错误 | has_error=True | total=1, items[0].has_error=True |
| 6 | 按时间范围筛选 | 正常 | 1条10天前+1条近期 | time_start/time_end近1天 | total=1 |
| 7 | 组合筛选 | 正常 | 3条不同组合数据 | user_id+intent_type | total=1 |
| 8 | 空结果 | 边界 | DB无数据 | 无筛选 | total=0, list=[] |
4.2 事件上传
| 序号 | 场景名称 | 分类 | 前置条件 | 请求参数 | 预期结果 |
|---|---|---|---|---|---|
| 1 | 单条事件 | 正常 | — | 1条event | success=True, received_count=1, DB count=1 |
| 2 | 批量事件 | 正常 | — | 3条events | received_count=3, DB count=3 |
| 3 | event_data存储为JSON | 正常 | — | event_data含多个字段 | DB中可按key读取 |
4.3 统计报表
| 序号 | 场景名称 | 分类 | 前置条件 | 请求参数 | 预期结果 |
|---|---|---|---|---|---|
| 1 | 有预聚合数据 | 正常 | 插入AnalyticsStatistic | date_range="last_7_days" | total_agent_calls=100, intent_distribution长度=1 |
| 2 | 空数据 | 边界 | DB无统计数据 | — | total_agent_calls=0, intent_distribution=[] |
4.4 页面浏览追踪
| 序号 | 场景名称 | 分类 | 前置条件 | 请求参数 | 预期结果 |
|---|---|---|---|---|---|
| 1 | 记录页面浏览 | 正常 | — | page_type, user_id, device_type | success=True, DB count=1 |
| 2 | 字段持久化 | 正常 | — | page_type="数据分析页" | DB中page_type正确 |
4.5 扫码事件追踪
| 序号 | 场景名称 | 分类 | 前置条件 | 请求参数 | 预期结果 |
|---|---|---|---|---|---|
| 1 | 记录扫码 | 正常 | — | qrcode_id, channel_code | success=True, DB count=1 |
| 2 | 字段正确 | 正常 | — | qrcode_id=5 | DB中qrcode_id=5 |
4.6 入群事件追踪
| 序号 | 场景名称 | 分类 | 前置条件 | 请求参数 | 预期结果 |
|---|---|---|---|---|---|
| 1 | 记录入群 | 正常 | — | group_id, employee_count, agent_count | success=True, DB count=1 |
| 2 | 字段正确 | 正常 | — | employee_count=3 | DB中字段正确 |
4.7 扫码→入群转化集成
| 序号 | 场景名称 | 分类 | 前置条件 | 请求参数 | 预期结果 |
|---|---|---|---|---|---|
| 1 | 同客户扫码后入群 | 集成 | — | scan + join 同一customer_user_id | 2条记录channel_code一致 |
5. 对应测试代码
测试文件:backend/tests/test_analytics_api.py
| 测试类 | 测试方法 | 对应场景 |
|---|---|---|
| TestConversationLogQuery | test_list_no_filters | 4.1-1 |
| TestConversationLogQuery | test_filter_by_user_id | 4.1-2 |
| TestConversationLogQuery | test_filter_by_agent_id | 4.1-3 |
| TestConversationLogQuery | test_filter_by_intent | 4.1-4 |
| TestConversationLogQuery | test_filter_by_has_error | 4.1-5 |
| TestConversationLogQuery | test_filter_by_time_range | 4.1-6 |
| TestConversationLogQuery | test_combined_filters | 4.1-7 |
| TestConversationLogQuery | test_empty_result | 4.1-8 |
| TestAnalyticsEventUpload | test_single_event | 4.2-1 |
| TestAnalyticsEventUpload | test_batch_events | 4.2-2 |
| TestAnalyticsEventUpload | test_event_data_stored_as_json | 4.2-3 |
| TestAnalyticsStatistics | test_statistics_with_preaggregated_data | 4.3-1 |
| TestAnalyticsStatistics | test_statistics_empty | 4.3-2 |
| TestPageViewTracking | test_record_page_view | 4.4-1 |
| TestPageViewTracking | test_page_view_fields_persisted | 4.4-2 |
| TestScanEventTracking | test_record_scan_event | 4.5-1 |
| TestScanEventTracking | test_scan_event_fields | 4.5-2 |
| TestGroupJoinEventTracking | test_record_group_join | 4.6-1 |
| TestGroupJoinEventTracking | test_group_join_fields | 4.6-2 |
| TestIntegrationScanToGroupJoin | test_scan_then_join_conversion | 4.7-1 |