Files
xiaoqu_mcp_service/xiaoqu-agent-service/doc/socket-log-list-mcp-design.md
T
my_local_kimi_dev2 ba3581fc9d feat(mcp): 新增出纸机通信日志查询工具 hardware.socketlog.list.query
- 对应老系统 SocketLogController /hardwareLog/listByPage,读 ES 月索引 socket_log_yyyyMM
- 双集群按 Redis Dictionary.SocketLogPath 切换,返回 cluster 便于排障
- 拍板:不加行级 ACL、允许服务 Token、logType 0-18/99、srcLogContent 不截断、
  不做名称富化、rows 默认 20 上限 100、索引不存在返回空 + notice
2026-11-30 16:48:35 +08:00

406 lines
20 KiB
Markdown
Raw 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.
# 设备通信日志 MCP 工具设计(hardware.socketlog.list.query)
对应老接口:`POST /hardwareLog/listByPage`(xiaoqu-franchisee `controller/hard/SocketLogController.java:107-194`)。
> 上一版误按 `HardwareLogController.queryByPage`(设备操作日志)设计,本版已按 `SocketLogController`
> 重做;误建的 `hardware-log-list-mcp-design.md` 已删除。本文件是唯一设计稿。
## 1. 结论摘要
- 工具名:`hardware.socketlog.list.query`,category=`hardware`,kind=`read`。
- 数据源:出纸机通信日志月索引 `socket_log_yyyyMM`,**双 ES 集群**,由 Redis 字典
`Dictionary.SocketLogPath` 决定读云集群还是本地集群(老接口原语义)。
- 查询条件:`snCode` 必填;`logType` 多选;时间范围必须是同一个月,缺省查当天。
- 安全:按王恒 2026-11-30 拍板**不施加行级 ACL**(对齐老接口,只靠 Token scope 约束能力面);
分页默认 20、上限 100,深分页按 ES `max_result_window=10000` 保护。
- 只读:不实现 `/sourceExport`、`/customerExport` 两个导出接口,不做任何写操作。
## 2. 老接口行为(行号级)
### 2.1 入参
`SocketLogPageInput extends PageInput`(`controller/input/SocketLogPageInput.java`):
| 字段 | 说明 |
|---|---|
| `snCode` | 设备 SN,必填;为空直接返回失败「请传递设备号」(`SocketLogController.java:112-114`) |
| `quertTime` | 查询日期范围,格式 `yyyy-MM-dd ~ yyyy-MM-dd`,Swagger 注明必须同一个月(`:14-15`) |
| `logType` | 日志类型多选,逗号分隔(`:16`,注释列 0-14) |
| `page` / `rows` | 页码 / 每页条数;`PageInput` 默认 10 条、上限 999(`PageInput.java:26-29`) |
### 2.2 查询构造
`listByPage`(`:107-194`)全部走 `BaseSearchSupportImpl.findListByPage`:
| 条件 | 构造 | 实际 ES 查询 |
|---|---|---|
| `snCode` | `Search.type=0` + String(`:121-125`) | `match snCode`(standard analyzer,大小写不敏感、可能分词) |
| 时间范围 | `Search.type=1`(`:135-140` / `:147-152`) | `range createTime` 闭区间,值为 `yyyy-MM-dd HH:mm:ss` 解析出的 `Date` |
| `logType` | `Search.type=2`(`:154-162`) | `terms logType`(数值) |
| 排序 | `sortMap.put("createTime", DESC)`(`:119`) | `sort createTime desc`,无第二排序键 |
| 分页 | `SearchPagination(rows, null, page)`(`:118`) | `from=(page-1)*rows`、`size=rows`,`track_total_hits=true` |
### 2.3 索引与集群切换(本接口最容易搞错的地方)
**索引名**(`:126-153`):
- 传了 `quertTime`:取**开始日期所在月**,`socket_log_` + `yyyyMM`;
- 没传:取**服务器当天所在月**,`socket_log_` + `yyyyMM`;
- 跨月范围不会报错,但只查开始月索引,结束月数据被静默丢弃(老接口的坑)。
生产/开发实测:`socket_log_yyyyMM` 是**别名**,物理索引是 `socket_log_yyyyMMNN`(NN 为月内序号)。
月别名会指向该月全部序号索引;云集群上的清理任务只保留最新一个物理索引
(`SocketLogIndexCleanTask.java`),本地集群保留历史。
**集群切换**(`:177-192`):
```java
Object SocketLogPath = redisTemplate.opsForHash().get(Constants.Dictionary, "SocketLogPath");
if (SocketLogPath != null && "offline".equals(SocketLogPath + "")) {
socketLogESService2.findListByPage(...); // es_hosts2 / restClient2,本地集群
} else {
socketLogESService.findListByPage(...); // es_hosts / restClient,云集群
}
```
- 字典 key:Redis db5 hash `Dictionary` 的 `SocketLogPath` 字段;
- 只有值为 `offline` 才走本地集群,其余(含 key 不存在)都走云集群;
- 2026-11-30 实测:**dev=`online`(云)**,**prod=`offline`(本地)**。
### 2.4 出参
`PageOutput<SocketLogEntity>`(`controller/output/PageOutput.java`):
- `page`:当前页;
- `records`:**总条数**(`result.getPagination().getTotalCount()`);
- `total`:**总页数**(`getTotalPage()`,`Pagination.java:37-42`);
- `rows`:`SocketLogEntity` 列表,全字段:
`id/createId/createTime/modifyId/modifyTime/softDelete/snCode/buildingId/districtId/propertyId/agentId/logType/logContent/useType/messageType/srcLogContent/tableName`。
注意:老接口 `records`/`total` 的命名与直觉相反,MCP 侧会改成 `total`=总条数、`totalPages`=总页数。
### 2.5 老接口的坑
1. **完全无行级权限**:任何登录用户可查任意 SN 的原始通信日志。
2. **跨月静默丢数据**:只按开始月选索引,结束月部分不查也不报错。
3. **`snCode` 用 `match`**:standard analyzer 会把 `AB-123` 拆成 `ab`/`123`,可能误配;
本次改用 `match_phrase`(见 §4)。
4. **深层分页无保护**:`from+size` 超过 ES `max_result_window`(默认 10000)会被 ES 拒绝,
老接口直接把 500 抛给前端。
5. **索引不存在直接 404**:`socketLog_yyyyMM` 别名不存在时 ES 返回 `index_not_found_exception`,
老接口未捕获。
6. **不校验 `quertTime` 格式**:`DateToConverter.parseDate` 失败返回 null,range 查询行为不可控。
7. **无 `softDelete` 过滤**:当前数据全是 0,但老接口不看该字段。
8. **`tableName` 字段误导**:文档里的 `tableName` 是写入端打的日期标签(如 `socket_log_20261130`),
不是实际查询索引,不要拿它拼索引。
## 3. 实测 mapping 与数据(2026-11-30,只读)
### 3.1 集群与字典
| 环境 | Redis `Dictionary.SocketLogPath` | 老接口实际集群 | 地址 |
|---|---|---|---|
| dev | `online` | 云集群 | `192.168.1.152:9200` |
| prod | `offline` | **本地集群** | `114.253.9.110:19800` |
> 本服务 `EsClientConfig` 已按 `app.es.cloud.*` / `app.es.local.*` 提供两个低阶
> `RestClient` bean,生产环境变量已配好,可直接复用。
### 3.2 索引与数据量
| 环境/集群 | 当前月别名 | 物理索引 | 文档数 | 大小 |
|---|---|---|---|---|
| dev 云 | `socket_log_202611` | `socket_log_20261104` | 276 | 278.7 kb |
| prod 云 | `socket_log_202611` | `socket_log_20261104` | 273,680,095 | 156.5 gb |
| prod 本地 | `socket_log_202611` | `socket_log_20261101`~`20261104` | **1,426,950,061** | 约 380 gb(4 个索引合计) |
- prod 本地按 `snCode + 约一日范围`(11-25 08:00~23:59)实测命中 1058 条,**耗时约 4.8s**
(4 分片 × 4 索引);本服务 ES 客户端 socketTimeout=60s,可覆盖。
- 云集群只保留最新物理索引;切回 `online` 后历史月份不可查。本地集群保留历史月别名。
### 3.3 Mapping(dev 云 / prod 本地一致)
| 字段 | 类型 | 备注 |
|---|---|---|
| `id` | long | |
| `createTime` / `modifyTime` | date | `_source` 为 ISO 文本,`range` 查询必须发 **epoch 毫秒** |
| `snCode` / `logContent` / `srcLogContent` / `tableName` | text + `.keyword` | keyword `ignore_above=256` |
| `logType` / `messageType` / `useType` / `softDelete` | long | |
| `agentId` / `propertyId` / `districtId` / `buildingId` / `createId` / `modifyId` | long | 可用于行级 ACL 过滤 |
dev 实测:
- `softDelete` 全部 0;`agentId`/`propertyId`/`districtId` 缺失 0 条(ACL 可用)。
- `logType` 分布:1 出纸 120、0 下上线 68、2 rfid 34、3 报警 28、16 自由指令 16、
10 设备心跳 4、6 背景值检测 2、7 锁 2、99 未知指令 2。
- `logContent` 最大 83 字符、`srcLogContent` 最大 145 字符(本页样本,未发现超长内容)。
### 3.4 时间格式
`createTime` 为 `date` 类型,直接发 `"2026-11-01 00:00:00"` 会被 400 拒绝;
必须转 **epoch 毫秒(Asia/Shanghai)**,或 ISO 带时区。老系统能跑是因为 transport 客户端
把 `Date` 序列化成 epoch 毫秒。MCP 侧统一用 epoch 毫秒。
### 3.5 索引不存在
`socket_log_202610`(不存在的别名)实测返回 HTTP 404 +
`index_not_found_exception`;低阶客户端会抛 `ResponseException`,gateway 需单独识别。
## 4. 设计决策(2026-11-30 王恒已拍板)
| # | 决策 | 结论 |
|---|---|---|
| 1 | 集群选择 | 完整复刻老接口:读 Redis `Dictionary.SocketLogPath`,`offline`→本地,其余→云;返回体带 `cluster` 便于排障 |
| 2 | 索引选择 | `socket_log_` + 开始时间 `yyyyMM`;**时间范围必须同一个月**,跨月直接 `INVALID_ARGUMENT`,不静默丢数据 |
| 3 | 行级权限 | **不施加**(拍板:不加)。与老接口一致,仅依赖 Token scope 约束工具能力面;返回体不按 `agentId`/`propertyId` 过滤 |
| 4 | snCode 匹配 | `match_phrase snCode`,修掉老 `match` 的分词误配;大小写不敏感 |
| 5 | logType | `List<Integer>` 多选,`terms` 查询;校验 0-18、99,映射中文文案 |
| 6 | 时间入参 | `startTime`/`endTime`,支持 `yyyy-MM-dd` 与 `yyyy-MM-dd HH:mm:ss`;日期型 end 补 23:59:59;两者都传或都不传,缺省查当天 |
| 7 | 分页 | 默认 20、上限 100;`from+size` 超 10000 按窗口截断并 notice;`total` 精确条数 + `totalPages` |
| 8 | 排序 | `createTime desc, id desc`(老接口只有 `createTime desc`,加 id 保证稳定) |
| 9 | 返回字段 | 返回 `SocketLogEntity` 全字段 + `logTypeText/messageTypeText/useTypeText`;`srcLogContent` 不截断 |
| 10 | 索引不存在 | 不报 500:返回空结果 + `notice`(该月索引在当前集群不存在),`total=0` |
| 11 | softDelete | 与老接口一致不过滤(当前数据全 0);在文档注明 |
| 12 | 名称富化 | v1 不做:不查 user/agent/property/district 名称,只返回 id,避免给 ES 查询引入额外 DB 依赖 |
| 13 | 导出 | 不做 `/sourceExport`、`/customerExport`,本期只做列表 |
| 14 | Token | **允许**用户 Token 与服务 Token(拍板:允许);因不施加行级 ACL,两者查询口径一致 |
拍板结论(2026-11-30):1=不加 ACL;2=允许服务 Token;3=logType 白名单 0-18、99;
4=`srcLogContent` 不截断;5=不做名称富化;6=分页默认 20、上限 100;7=索引不存在返回空 + notice。
## 5. 工具契约
### 5.1 元数据
```
name: hardware.socketlog.list.query
category: hardware
kind: read
description: 出纸机通信日志分页查询,对应老系统 /hardwareLog/listByPage(SocketLogController)。
snCode 必填;logType 多选;时间范围必须同一个月,缺省当天。
返回原始通信内容 srcLogContent 与解析内容 logContent。
不施加行级 ACL(拍板对齐老接口),仅由 Token scope 控制工具能力面。
page 默认 1,rows 默认 20、上限 100。
```
### 5.2 入参
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| `snCode` | String | 是 | 设备 SN,去空格后 1~64 字符;`match_phrase snCode` |
| `logType` | List\<Integer\> | 否 | 日志类型多选;允许 0-18、99;不传=全部 |
| `startTime` | String | 否 | 开始时间,`yyyy-MM-dd` 或 `yyyy-MM-dd HH:mm:ss`,含 |
| `endTime` | String | 否 | 结束时间,同上;日期型补 23:59:59,含;必须与 startTime 同月 |
| `page` | Integer | 否 | 页码,从 1 开始,默认 1 |
| `rows` | Integer | 否 | 每页条数,默认 20,上限 100,超出截断并 notice |
校验规则:
- `snCode` 空白 → `INVALID_ARGUMENT`。
- `startTime`/`endTime` 必须同时传或同时不传;只传一个 → `INVALID_ARGUMENT`。
- 两者都为空 → 当天 `00:00:00` ~ `23:59:59`。
- 格式非法、`start > end`、跨自然月 → `INVALID_ARGUMENT`。
- `logType` 含非法值 → `INVALID_ARGUMENT`。
- `page < 1` 按 1;`rows < 1` 按默认值。
`logType` 文案(取自 `TypeToNameUtils.getLogTypeName`;老 Swagger 只列到 14,实际还有 15-18、99):
| 值 | 文案 | 值 | 文案 |
|---|---|---|---|
| 0 | 下上线 | 10 | 设备心跳 |
| 1 | 出纸 | 11 | 出纸开关 |
| 2 | rfid | 12 | 出纸口清理 |
| 3 | 报警 | 13 | 出纸模式设定 |
| 4 | 错误 | 14 | 设置亮灯响铃模式 |
| 5 | 有人无人 | 15 | 工程模式设定 |
| 6 | 背景值检测 | 16 | 自由指令 |
| 7 | 锁 | 17 | 三挡纸量检测 |
| 8 | 上纸动作 | 18 | 更改设备类型 |
| 9 | 设备升级 | 99 | 未知指令 |
`messageType`:0=上行、1=下行;`useType`:0=运维、1=普通人员。
### 5.3 出参
```json
{
"snCode": "X1A1200800000055",
"cluster": "cloud",
"index": "socket_log_202611",
"total": 48,
"totalPages": 3,
"page": 1,
"rows": 20,
"records": [
{
"id": 7528007542032649457,
"createTime": "2026-11-25 15:01:55",
"modifyTime": "2026-11-25 15:01:55",
"snCode": "X1A1200800000055",
"logType": 2,
"logTypeText": "rfid",
"logContent": "rfid识别,rfid=fjklewj1501;长度=18000厘米",
"srcLogContent": "X1A1200800000055:up===RFID:fjklewj1501;===SRC:RFID,0,18000,1.2;===2026-11-25 15:01:55",
"messageType": 0,
"messageTypeText": "上行",
"useType": 1,
"useTypeText": "普通人员",
"agentId": 17,
"propertyId": 177,
"districtId": 318,
"buildingId": 392,
"createId": 0,
"modifyId": 8991,
"softDelete": 0,
"tableName": "socket_log_20261125"
}
],
"notice": "rows 超过上限 100,已截断为 100"
}
```
- `total`:命中总条数(对应老接口 `records`);
- `totalPages`:总页数(对应老接口 `total`);
- `notice`:仅在 rows 截断、深分页截断、索引不存在时出现;
- `cluster`:`cloud` 或 `local`。
### 5.4 错误码
| 场景 | 错误码 |
|---|---|
| snCode 为空、时间格式非法、跨月、logType 非法、只传一个时间 | `INVALID_ARGUMENT` |
| Redis / ES 连接失败、ES 5xx | `UPSTREAM_UNAVAILABLE` |
| 索引不存在(404) | 不报错,返回空 + notice |
### 5.5 ES 查询 DSL(示意)
```json
{
"from": 0,
"size": 20,
"track_total_hits": true,
"query": {
"bool": {
"filter": [
{ "match_phrase": { "snCode": "X1A1200800000055" } },
{ "range": { "createTime": { "gte": 1795536000000, "lte": 1795622399000 } } },
{ "terms": { "logType": [0, 1, 2] } }
]
}
},
"sort": [
{ "createTime": "desc" },
{ "id": "desc" }
]
}
```
- `range` 值必须是 epoch 毫秒;
- 无 ACL:`bool` 里只有 `filter`,不生成 `should`。
## 6. 行级 ACL
**不施加**(2026-11-30 王恒拍板)。与老接口一致:只要 Token 的作用域包含该工具,
即可查询任意 SN 的通信日志。数据可见性完全由 `agent_scope` / `scope_tool` 授权面控制。
设计稿早期建议的 `agentId`/`propertyId` 行级过滤不实现,`PermissionService` 无需改动。
## 7. 与老系统差异
| 项 | 老接口 | 新工具 |
|---|---|---|
| 权限 | 无 ACL | 无 ACL(拍板对齐老接口) |
| 跨月 | 静默只查开始月 | 显式拒绝 |
| snCode 匹配 | `match`(可能分词误配) | `match_phrase` |
| 时间格式 | `Date` 隐式序列化 | 显式 epoch 毫秒 |
| 深分页 | 超 10000 直接 500 | 窗口截断 + notice |
| 索引不存在 | 404 异常 | 空结果 + notice |
| 分页默认 | 10 条、上限 999 | 20 条、上限 100 |
| 排序 | `createTime desc` | `createTime desc, id desc` |
| 导出 | 两个导出接口 | 不实现 |
## 8. 改动清单
| 文件 | 改动 |
|---|---|
| `src/main/java/me/iiv/agent/es/SocketLogEsGateway.java` | 新增接口:`search(cluster, index, snCode, logTypes, gteMillis, lteMillis, from, size)` |
| `src/main/java/me/iiv/agent/es/SocketLogEsGatewayImpl.java` | 新增实现:注入 `esCloudRestClient` + `esLocalRestClient`,构造 DSL,解析 `_source`,404 识别 |
| `src/main/java/me/iiv/agent/mcp/tool/SocketLogTools.java` | 新增工具:参数校验、Redis 集群选择、索引名、结果映射 |
| `src/test/java/me/iiv/agent/mcp/tool/SocketLogToolsTest.java` | 新增单测 |
| `src/test/java/me/iiv/agent/es/SocketLogEsGatewayImplTest.java` | 新增 gateway 单测 |
| `doc/agent-mcp-tools-reference.md` | 上线后追加工具章节(§59) |
| `AGENTS.md` | 上线后更新「近期需求示例」 |
无 DDL、无新表、无配置项变更(复用现有 ES/Redis 配置)。
## 9. 测试计划
### 9.1 单元测试
- 参数:snCode 空、时间格式非法、只传一个时间、start>end、跨月、logType 非法、rows 截断。
- 集群:Redis 返回 `offline` → local;`online` / null → cloud;Redis 异常 → `UPSTREAM_UNAVAILABLE`。
- 索引:开始月拼 `socket_log_yyyyMM`;缺省当天;跨月拒绝。
- 分页:默认 20/1;rows>100 截断;from+size>10000 窗口截断 + notice。
- 映射:logType 0-18/99 文案;messageType 0/1;useType 0/1;createTime ISO → 文本。
- 异常:ES 404 → 空 + notice;ES 500 → `UPSTREAM_UNAVAILABLE`。
- 响应:`total`/`totalPages`/`page`/`rows`/`records` 字段与老接口语义对照。
### 9.2 Gateway 测试
- DSL:`match_phrase`、`terms logType`、`range` epoch 毫秒、`sort createTime+id`、`from/size`、`track_total_hits`。
- 404 `index_not_found_exception` 识别。
- `_source` 解析:日期两种形态、缺失字段、数值字段。
### 9.3 测试环境冒烟
1. `./deploy/test-up.sh` 起本机测试环境(dev profile,Redis `SocketLogPath=online` → 云集群)。
2. 用 `scopeCode=test` 签用户 Token,MCP initialize + tools/call。
3. 用 dev 真实 SN `X1A1200800000055` 查本月:预期 `total>0`,`cluster=cloud`,`index=socket_log_202611`。
4. 传 `logType=[2]`、时间范围、分页边界各跑一次。
5. Redis 字典异常场景由单测覆盖(无 ACL 场景)。
6. 非法参数验证 `INVALID_ARGUMENT`。
7. 全程只读,不产生任何写操作。
### 9.4 生产只读核验
- 部署后 `tools/list` 可见;用 prod 真实 SN(如 `X3A41Y0000053453`)查当天:
预期 `cluster=local`、`index=socket_log_202611`、`total>0`。
- 观察耗时(本地集群单 SN 单日约 5s),确认不超 60s。
- 不做任何写操作。
## 10. 上线步骤
按 `AGENTS.md` 标准流程:
1. 设计评审(本文档)→ 拍板。
2. 实现 + 单测 + `./mvnw clean package` 全量通过。
3. 测试环境冒烟(§9.3)。
4. `git push origin main` → `./deploy/deploy.sh remote-deploy`。
5. 幂等绑 prod 研发专用 scope 6(新口径);dev 绑测试专用 scope。
6. 生产只读核验(§9.4)。
7. 建 ai-proj 需求(项目 224)+ 任务关联,回写实施记录。
8. 更新 `doc/agent-mcp-tools-reference.md` 与 `AGENTS.md`。
## 11. 拍板结论(2026-11-30)
| # | 决策点 | 结论 |
|---|---|---|
| 1 | 行级 ACL | **不加**(对齐老接口,仅靠 Token scope) |
| 2 | 服务 Token | **允许** |
| 3 | `logType` 白名单 | **0-18、99**;未来固件新增类型需更新白名单 |
| 4 | `srcLogContent` 截断 | **不截断** |
| 5 | 名称富化 | **不做**,只返回 id |
| 6 | 分页默认值 | **20/100** |
| 7 | 索引不存在 | **返回空 + notice** |
## 12. 风险与注意事项
- **双集群一致性**:prod 读本地集群,dev 读云集群;`SocketLogPath` 被改动会直接切换数据源。
返回体带 `cluster`,排障时先看这个字段。
- **本地集群数据量巨大**:当前月 14.3 亿条 / 380 GB,单 SN 单日查询约 5s;禁止放开 snCode 必填,
禁止跨月大范围扫描。
- **云集群只有最新物理索引**:若字典切回 `online`,历史月份会返回空 + notice,不是数据丢失。
- **无行级 ACL**:能拿到该工具 scope 的调用方可查询任意 SN 的原始通信日志,
开通 scope 时需按此风险控制(与老接口一致,是明确拍板结果)。
- **`tableName` 不是索引名**:它只是写入端的日期标签,禁止用于拼查询索引。
- **`track_total_hits=true`**:精确总数在超大结果集上可能较慢;SN 过滤后可控,保持与老接口一致。