feat(日志管理): 列表改按上报用户精确筛选,去掉设备/文件名模糊搜索与设备列
- /api/admin/device-logs/files 的 keyword LIKE 换成 uid 等值过滤:device_log_file 只有 uid 没有 username,模糊匹配设备名会跨用户串结果,而下拉里选中的用户必须是精确命中。 - 采集配置弹窗的 keyword(终端覆盖搜索)与查看器标题里的设备名保留:那是设备维度唯一还有用的地方。 - 新增护栏 tests/device-logs-user-filter.test.ts(5 例,变异自证 5/5 转红):表头回出现「设备」、 占位行 colspan 与列数不符、接口带回 keyword、下拉没绑上、用户选项没在 onMounted 加载,都会红。 - 上一笔提交里 DeviceLogsPage 已按 uid 传参,本笔补齐 api 层与后端,故单独 checkout 上一笔时 vue-tsc 不过。
This commit is contained in:
@@ -58,7 +58,8 @@ export interface DeviceLogConfigData {
|
||||
|
||||
export interface DeviceLogQuery {
|
||||
source?: string
|
||||
keyword?: string
|
||||
/** 指定用户(users.id);不传=全部上报者,含 uid 为空的采集机与未登录客户端。 */
|
||||
uid?: number
|
||||
startDate?: string
|
||||
endDate?: string
|
||||
page: number
|
||||
@@ -69,7 +70,7 @@ export interface DeviceLogQuery {
|
||||
export async function fetchDeviceLogFiles(params: DeviceLogQuery): Promise<DeviceLogPage> {
|
||||
const query: Record<string, string | number> = { page: params.page, pageSize: params.pageSize }
|
||||
if (params.source) query.source = params.source
|
||||
if (params.keyword) query.keyword = params.keyword
|
||||
if (params.uid) query.uid = params.uid
|
||||
if (params.startDate) query.startDate = params.startDate
|
||||
if (params.endDate) query.endDate = params.endDate
|
||||
const { data } = await http.get('/api/admin/device-logs/files', { params: query })
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { readSource } from './helpers.ts'
|
||||
|
||||
// 日志管理列表改「用户维度」的护栏(2026-10-04 用户要求:去掉设备/文件名模糊搜索与设备列,加用户筛选)。
|
||||
// 后端查询侧已有 DeviceLogServiceTest 锁住 uid 精确过滤;这里补前端这一段——
|
||||
// 页面筛选控件、请求参数、表头列三处任意一处退回设备维度,本用例会红而不是悄悄生效。
|
||||
|
||||
const PAGE = 'src/pages/records/DeviceLogsPage.vue'
|
||||
const API = 'src/api/device-logs.ts'
|
||||
|
||||
/** 取第一张表 = 日志文件列表;采集配置弹窗里另有一张含「设备」的表,混着判会误报。 */
|
||||
function firstTable(src: string): string {
|
||||
const matched = /<table[\s\S]*?<\/table>/.exec(src)
|
||||
assert.ok(matched, '页面里没找到 <table>,取块规则已失效')
|
||||
return matched[0]
|
||||
}
|
||||
|
||||
function functionBody(src: string, from: string, to: string): string {
|
||||
const start = src.indexOf(from)
|
||||
const end = src.indexOf(to)
|
||||
assert.ok(start >= 0, `api 里找不到 ${from}`)
|
||||
assert.ok(end > start, `api 里 ${to} 缺失或在 ${from} 之前`)
|
||||
return src.slice(start, end)
|
||||
}
|
||||
|
||||
test('test_device_log_list_header_is_user_scoped_without_device_column', () => {
|
||||
const table = firstTable(readSource(PAGE))
|
||||
assert.match(table, /<th[^>]*>\s*用户\s*<\/th>/, '列表表头没有「用户」列')
|
||||
assert.doesNotMatch(table, /设备/, '列表表头又出现「设备」列')
|
||||
})
|
||||
|
||||
test('test_device_log_list_colspan_matches_header_column_count', () => {
|
||||
// 「删了列忘了改占位行 colspan」是这类改动最常留下的残行
|
||||
const table = firstTable(readSource(PAGE))
|
||||
const columns = (table.match(/<th[\s>]/g) ?? []).length
|
||||
assert.ok(columns >= 2, `表头只数到 ${columns} 列,取块规则已失效`)
|
||||
const spans = [...table.matchAll(/colspan="(\d+)"/g)].map((m) => Number(m[1]))
|
||||
assert.ok(spans.length >= 1, '列表 tbody 没有 colspan 占位行')
|
||||
for (const span of spans) {
|
||||
assert.equal(span, columns, `占位行 colspan=${span} 与表头列数 ${columns} 不符`)
|
||||
}
|
||||
})
|
||||
|
||||
test('test_device_log_page_wires_user_dropdown_to_uid_param', () => {
|
||||
const page = readSource(PAGE)
|
||||
assert.match(page, /指定用户[\s\S]{0,220}?v-model="uidFilter"/, '筛选条没有绑定 uidFilter 的下拉')
|
||||
assert.match(page, /uid:\s*uidFilter\.value\s*\?\s*Number\(uidFilter\.value\)/, '请求未把所选用户映射成 uid')
|
||||
})
|
||||
|
||||
test('test_device_log_list_query_carries_no_fuzzy_keyword', () => {
|
||||
const body = functionBody(readSource(API), 'export async function fetchDeviceLogFiles', 'export async function fetchDeviceLogContent')
|
||||
assert.match(body, /query\.uid = params\.uid/, '列表接口不再下发 uid')
|
||||
assert.doesNotMatch(body, /keyword/, '列表接口又带回 keyword 模糊匹配')
|
||||
// 页面整份都不该出现 keyword:设备/文件名模糊搜索的入口应彻底消失
|
||||
assert.doesNotMatch(readSource(PAGE), /\bkeyword\b/, '日志管理页仍存在模糊搜索入参')
|
||||
})
|
||||
|
||||
test('test_device_log_user_dropdown_is_filled_from_admin_users', () => {
|
||||
// 复用生成记录页那份用户下拉,避免两处各写一套解析
|
||||
const page = readSource(PAGE)
|
||||
assert.match(page, /import \{[^}]*fetchHistoryUserOptions[^}]*\} from/, '用户下拉没有走既有的 admin users 取数')
|
||||
// 断言锁定挂载时的**调用点**:只写 /loadUserOptions\(\)/ 会匹配到函数声明本身,恒绿
|
||||
assert.match(
|
||||
page,
|
||||
/onMounted\(\(\)\s*=>\s*\{[^}]*loadUserOptions\(\)/,
|
||||
'用户选项未在 onMounted 里加载(首屏下拉会是空的)',
|
||||
)
|
||||
})
|
||||
+4
-3
@@ -51,11 +51,12 @@ public class AdminDeviceLogController {
|
||||
|
||||
@GetMapping("/files")
|
||||
@Operation(summary = "日志文件分页列表",
|
||||
description = "source=client/maixiang;keyword 模糊匹配设备名/设备ID/文件名;日期为闭区间(早于保留窗口自动收紧)。")
|
||||
description = "source=client/maixiang;uid 按上报用户精确过滤(未登录/采集机的行 uid 为空,不匹配任何 uid);"
|
||||
+ "日期为闭区间(早于保留窗口自动收紧)。")
|
||||
public ApiResponse<DeviceLogPageVo> files(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "来源") @RequestParam(required = false) String source,
|
||||
@Parameter(description = "关键字(设备/文件名)") @RequestParam(required = false) String keyword,
|
||||
@Parameter(description = "指定用户 uid") @RequestParam(required = false) Long uid,
|
||||
@Parameter(description = "起始日期(含)") @RequestParam(required = false)
|
||||
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startDate,
|
||||
@Parameter(description = "结束日期(含)") @RequestParam(required = false)
|
||||
@@ -63,7 +64,7 @@ public class AdminDeviceLogController {
|
||||
@RequestParam(defaultValue = "1") Long page,
|
||||
@RequestParam(defaultValue = "20") Long pageSize) {
|
||||
requireSuperAdmin(request);
|
||||
return ApiResponse.success(deviceLogService.page(source, keyword, startDate, endDate, page, pageSize));
|
||||
return ApiResponse.success(deviceLogService.page(source, uid, startDate, endDate, page, pageSize));
|
||||
}
|
||||
|
||||
@GetMapping("/content")
|
||||
|
||||
+5
-8
@@ -159,7 +159,7 @@ public class DeviceLogService {
|
||||
|
||||
// ---------------------------------------------------------------- 查询
|
||||
|
||||
public DeviceLogPageVo page(String source, String keyword, LocalDate startDate, LocalDate endDate,
|
||||
public DeviceLogPageVo page(String source, Long uid, LocalDate startDate, LocalDate endDate,
|
||||
Long pageParam, Long pageSizeParam) {
|
||||
int retentionDays = properties.retentionDaysOrDefault();
|
||||
LocalDate minDate = LocalDate.now().minusDays(retentionDays - 1L);
|
||||
@@ -173,11 +173,8 @@ public class DeviceLogService {
|
||||
if (source != null && !source.isBlank()) {
|
||||
qw.eq(DeviceLogFileEntity::getSource, source.trim());
|
||||
}
|
||||
if (keyword != null && !keyword.isBlank()) {
|
||||
String kw = keyword.trim();
|
||||
qw.and(w -> w.like(DeviceLogFileEntity::getDeviceName, kw)
|
||||
.or().like(DeviceLogFileEntity::getDeviceId, kw)
|
||||
.or().like(DeviceLogFileEntity::getFileName, kw));
|
||||
if (uid != null) {
|
||||
qw.eq(DeviceLogFileEntity::getUid, uid);
|
||||
}
|
||||
qw.ge(DeviceLogFileEntity::getLogDate, from);
|
||||
if (endDate != null) {
|
||||
@@ -207,8 +204,8 @@ public class DeviceLogService {
|
||||
vo.setPage(safePage);
|
||||
vo.setPageSize(safeSize);
|
||||
vo.setRetentionDays(retentionDays);
|
||||
log.info("[device-log] 列表查询 source={} keyword={} 起={} 止={} page={} size={} 命中={}",
|
||||
source, keyword, from, endDate, safePage, safeSize, total);
|
||||
log.info("[device-log] 列表查询 source={} uid={} 起={} 止={} page={} size={} 命中={}",
|
||||
source, uid, from, endDate, safePage, safeSize, total);
|
||||
return vo;
|
||||
}
|
||||
|
||||
|
||||
+35
@@ -1,6 +1,8 @@
|
||||
package com.nanri.aiimage.modules.devicelog.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.Wrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
@@ -184,4 +186,37 @@ class DeviceLogServiceTest {
|
||||
assertThat(vo.getContent()).isEqualTo("AAAA\nBBBB\n");
|
||||
assertThat(vo.isTruncated()).isFalse();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 列表筛选
|
||||
|
||||
/** 捕获 page() 传给 selectCount 的查询条件(计数与列表走同一条 wrapper 构造)。 */
|
||||
@SuppressWarnings("rawtypes")
|
||||
private AbstractWrapper capturePageWrapper(String source, Long uid) {
|
||||
when(mapper.selectCount(any())).thenReturn(0L);
|
||||
service().page(source, uid, null, null, 1L, 15L);
|
||||
ArgumentCaptor<Wrapper> captor = ArgumentCaptor.forClass(Wrapper.class);
|
||||
verify(mapper).selectCount(captor.capture());
|
||||
return (AbstractWrapper) captor.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageFiltersByUidExactly() {
|
||||
AbstractWrapper wrapper = capturePageWrapper(null, 7L);
|
||||
assertThat(wrapper.getSqlSegment()).contains("uid =");
|
||||
assertThat(wrapper.getParamNameValuePairs().values()).contains(7L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageWithoutUidKeepsAnonymousReportersVisible() {
|
||||
// 未登录的桌面端与麦象采集机 uid 为空:不指定用户时它们必须仍在结果里
|
||||
assertThat(capturePageWrapper(null, null).getSqlSegment()).doesNotContain("uid");
|
||||
}
|
||||
|
||||
/** 契约:列表筛选已改为用户维度,设备名/设备ID/文件名的模糊匹配不再存在于查询路径。 */
|
||||
@Test
|
||||
void pageNoLongerMatchesDeviceOrFileNameFuzzily() {
|
||||
String sql = capturePageWrapper("client", 7L).getSqlSegment();
|
||||
assertThat(sql).contains("source =");
|
||||
assertThat(sql).doesNotContain("device_name", "device_id", "file_name");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user