feat: 字段级变更 diff(捕获请求体与写入前状态,详情展示与 CSV 变更列)
- ChangeCaptureManager:请求期缓存 JSON 请求体(Content-Length 预检 ≤256KB, 装饰后下游可重复读)并抓取改/删操作的写入前资源状态(fetch(gvk, name), plural→GVK 经 SchemeManager 映射);全程失败静默降级为仅操作元信息 - ChangeDiffEngine:PUT 全量 diff;PATCH 双形态(JSON Patch ops 数组按 RFC 6902 + JSON Pointer 展开、对象 body 按 Merge Patch);POST 记新增、 DELETE 记移除;根级 apiVersion/kind/status 与 metadata 服务端子键降噪; 敏感字段脱敏、单值截断 200 字符、最多 30 条(汇总条兜底) - AuditLog.spec.changes(ChangeEntry 列表)仅 2xx 成功操作落库,不建索引 - 设置项新增"记录字段变更"(默认开),随审计总开关联动 - 详情抽屉新增"字段变更"区块(类型徽章 + 字段路径 + 新旧值), CSV 导出新增"变更"列(紧凑中文摘要) - 文档同步:产品设计(定位/详情/模型/局限)、技术方案(新增 D7)、README - 测试:新增 ChangeDiffEngineTest/ChangeCaptureManagerTest 与详情结构回归, 更新 Recorder/Filter/Exporter/设置相关用例;Java 120 + UI 45 全绿
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { AuditLogSpec } from './audit'
|
||||
import { deriveAction, describeOperation, SEMANTIC_ACTIONS } from './audit'
|
||||
import {
|
||||
changeTypeLabel,
|
||||
changeValueDisplay,
|
||||
deriveAction,
|
||||
describeOperation,
|
||||
SEMANTIC_ACTIONS,
|
||||
} from './audit'
|
||||
|
||||
// 操作描述推导(这条操作真正干了什么):由落库的语义动作标注或
|
||||
// method / resourceType / path 推导中文短语。
|
||||
@@ -238,3 +244,20 @@ describe('SEMANTIC_ACTIONS 筛选选项', () => {
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
describe('字段级变更展示辅助', () => {
|
||||
it('changeTypeLabel:三种类型映射中文,未知回退原始值', () => {
|
||||
expect(changeTypeLabel('ADDED')).toBe('新增')
|
||||
expect(changeTypeLabel('UPDATED')).toBe('修改')
|
||||
expect(changeTypeLabel('REMOVED')).toBe('删除')
|
||||
expect(changeTypeLabel('unknown')).toBe('unknown')
|
||||
expect(changeTypeLabel(null)).toBe('修改')
|
||||
})
|
||||
|
||||
it('changeValueDisplay:缺失侧显示 ∅,其余原样', () => {
|
||||
expect(changeValueDisplay('新值')).toBe('新值')
|
||||
expect(changeValueDisplay(null)).toBe('∅')
|
||||
expect(changeValueDisplay(undefined)).toBe('∅')
|
||||
expect(changeValueDisplay('')).toBe('∅')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -13,6 +13,22 @@ export interface AuditLogSpec {
|
||||
statusCode: number
|
||||
remoteIp: string
|
||||
userAgent?: string | null
|
||||
/** 字段级变更列表(字段级变更 diff);历史记录或不可捕获场景缺失。 */
|
||||
changes?: AuditLogChange[] | null
|
||||
}
|
||||
|
||||
/** 字段级变更类型(与后端 ChangeType 一致)。 */
|
||||
export type AuditLogChangeType = 'ADDED' | 'UPDATED' | 'REMOVED'
|
||||
|
||||
/** 字段级变更条目(新旧值均已在后端脱敏与截断)。 */
|
||||
export interface AuditLogChange {
|
||||
/** 字段路径(点号分隔,如 spec.title)。 */
|
||||
field: string
|
||||
changeType: AuditLogChangeType
|
||||
/** 变更前的值;字段为新增时缺失。 */
|
||||
oldValue?: string | null
|
||||
/** 变更后的值;字段被移除时缺失。 */
|
||||
newValue?: string | null
|
||||
}
|
||||
|
||||
export interface AuditLog {
|
||||
@@ -265,6 +281,25 @@ export function isSuccessful(statusCode?: number | null): boolean {
|
||||
return statusCode != null && statusCode >= 200 && statusCode < 400
|
||||
}
|
||||
|
||||
/** 变更类型 → 中文标签(未知类型回退原始值)。 */
|
||||
const CHANGE_TYPE_LABELS: Record<string, string> = {
|
||||
ADDED: '新增',
|
||||
UPDATED: '修改',
|
||||
REMOVED: '删除',
|
||||
}
|
||||
|
||||
export function changeTypeLabel(changeType?: string | null): string {
|
||||
if (!changeType) {
|
||||
return '修改'
|
||||
}
|
||||
return CHANGE_TYPE_LABELS[changeType] ?? changeType
|
||||
}
|
||||
|
||||
/** 变更值展示:缺失一侧显示为 ∅(字段不存在)。 */
|
||||
export function changeValueDisplay(value?: string | null): string {
|
||||
return value == null || value === '' ? '∅' : value
|
||||
}
|
||||
|
||||
/** datetime-local 输入值(本地时区)转 ISO-8601 Instant。 */
|
||||
export function localDateTimeToInstant(value: string): string | null {
|
||||
if (!value) {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { readFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
// 结构性回归测试:不渲染组件,仅检查 SFC 源码中的字段级变更展示结构。
|
||||
// 详情抽屉需在 spec.changes 存在时渲染"字段变更"区块:
|
||||
// 每条变更含类型徽章(新增/修改/删除三色)、字段路径与新旧值(缺失侧显示 ∅);
|
||||
// 无变更(历史记录)时整块不渲染。本测试防止该区块被误删或退化为通用 JSON。
|
||||
const sfc = readFileSync(
|
||||
fileURLToPath(new URL('./AuditLogDetail.vue', import.meta.url)),
|
||||
'utf-8',
|
||||
)
|
||||
|
||||
describe('AuditLogDetail 字段变更区块', () => {
|
||||
it('存在 changes 时渲染"字段变更"区块,否则不渲染', () => {
|
||||
expect(sfc).toContain('<div v-if="changes.length" class="changes">')
|
||||
expect(sfc).toContain('字段变更')
|
||||
})
|
||||
|
||||
it('每条变更渲染类型徽章与字段路径,UPDATED 展示 旧 → 新', () => {
|
||||
expect(sfc).toContain(':class="changeBadgeClass(change.changeType)"')
|
||||
expect(sfc).toContain('changeTypeLabel(change.changeType)')
|
||||
expect(sfc).toContain('{{ change.field }}')
|
||||
expect(sfc).toContain("change.changeType === 'UPDATED'")
|
||||
expect(sfc).toContain('change-arrow')
|
||||
expect(sfc).toContain('changeValueDisplay(change.oldValue)')
|
||||
expect(sfc).toContain('changeValueDisplay(change.newValue)')
|
||||
})
|
||||
|
||||
it('三种变更类型各有独立徽章配色', () => {
|
||||
expect(sfc).toContain('.change-added')
|
||||
expect(sfc).toContain('.change-updated')
|
||||
expect(sfc).toContain('.change-removed')
|
||||
})
|
||||
|
||||
it('变更区块只读:不包含任何编辑/删除入口', () => {
|
||||
// 与设计原则 3(审计自身不可篡改)一致
|
||||
expect(sfc).not.toMatch(/VButton[^>]*type="danger"/)
|
||||
expect(sfc).not.toContain('删除变更')
|
||||
})
|
||||
})
|
||||
@@ -1,8 +1,10 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue'
|
||||
import { VButton, VSpace } from '@halo-dev/components'
|
||||
import type { AuditLog } from '@/utils/audit'
|
||||
import type { AuditLog, AuditLogChangeType } from '@/utils/audit'
|
||||
import {
|
||||
changeTypeLabel,
|
||||
changeValueDisplay,
|
||||
describeOperation,
|
||||
displayUsername,
|
||||
formatTime,
|
||||
@@ -28,6 +30,20 @@ const resultText = computed(() => {
|
||||
return isSuccessful(props.log.spec.statusCode) ? '成功' : '失败'
|
||||
})
|
||||
|
||||
/** 字段级变更(字段级变更 diff,产品设计 V2);历史记录缺失。 */
|
||||
const changes = computed(() => props.log?.spec.changes ?? [])
|
||||
|
||||
const changeBadgeClass = (changeType?: string | null): string => {
|
||||
switch (changeType as AuditLogChangeType) {
|
||||
case 'ADDED':
|
||||
return 'change-added'
|
||||
case 'REMOVED':
|
||||
return 'change-removed'
|
||||
default:
|
||||
return 'change-updated'
|
||||
}
|
||||
}
|
||||
|
||||
/** 详情展示的原始 JSON(只读)。 */
|
||||
const rawJson = computed(() =>
|
||||
props.log ? JSON.stringify(props.log, null, 2) : '',
|
||||
@@ -114,6 +130,41 @@ const rawJson = computed(() =>
|
||||
</div>
|
||||
</dl>
|
||||
|
||||
<!-- 字段级变更(只读展示,无任何修改入口) -->
|
||||
<div v-if="changes.length" class="changes">
|
||||
<h4 class="changes-title">字段变更</h4>
|
||||
<div
|
||||
v-for="(change, index) in changes"
|
||||
:key="index"
|
||||
class="change-row"
|
||||
>
|
||||
<span
|
||||
class="change-badge"
|
||||
:class="changeBadgeClass(change.changeType)"
|
||||
>
|
||||
{{ changeTypeLabel(change.changeType) }}
|
||||
</span>
|
||||
<div class="change-body">
|
||||
<div class="mono change-field">{{ change.field }}</div>
|
||||
<div v-if="change.changeType === 'UPDATED'" class="change-values">
|
||||
<span class="break-all change-old">{{
|
||||
changeValueDisplay(change.oldValue)
|
||||
}}</span>
|
||||
<span class="change-arrow">→</span>
|
||||
<span class="break-all">{{
|
||||
changeValueDisplay(change.newValue)
|
||||
}}</span>
|
||||
</div>
|
||||
<div v-else-if="change.changeType === 'ADDED'" class="change-values">
|
||||
<span class="break-all">{{ changeValueDisplay(change.newValue) }}</span>
|
||||
</div>
|
||||
<div v-else class="change-values">
|
||||
<span class="break-all">{{ changeValueDisplay(change.oldValue) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<details class="raw-json">
|
||||
<summary>原始数据</summary>
|
||||
<pre>{{ rawJson }}</pre>
|
||||
@@ -193,6 +244,67 @@ const rawJson = computed(() =>
|
||||
.break-all {
|
||||
word-break: break-all;
|
||||
}
|
||||
.changes {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
.changes-title {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
color: #0e1731;
|
||||
margin: 0 0 0.25rem;
|
||||
}
|
||||
.change-row {
|
||||
display: flex;
|
||||
gap: 0.625rem;
|
||||
padding: 0.5rem 0;
|
||||
border-bottom: 1px dashed #eee;
|
||||
}
|
||||
.change-badge {
|
||||
flex: 0 0 auto;
|
||||
align-self: flex-start;
|
||||
margin-top: 0.1rem;
|
||||
padding: 0.1rem 0.4rem;
|
||||
border-radius: 0.25rem;
|
||||
font-size: 0.6875rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
.change-added {
|
||||
color: #0f766e;
|
||||
background: rgba(76, 203, 160, 0.15);
|
||||
}
|
||||
.change-updated {
|
||||
color: #b45309;
|
||||
background: rgba(217, 119, 6, 0.12);
|
||||
}
|
||||
.change-removed {
|
||||
color: #d71d1d;
|
||||
background: rgba(215, 29, 29, 0.12);
|
||||
}
|
||||
.change-body {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
.change-field {
|
||||
font-size: 0.75rem;
|
||||
color: #0e1731;
|
||||
word-break: break-all;
|
||||
}
|
||||
.change-values {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 0.375rem;
|
||||
margin-top: 0.125rem;
|
||||
font-size: 0.75rem;
|
||||
color: #666;
|
||||
}
|
||||
.change-old {
|
||||
color: #d71d1d;
|
||||
text-decoration: line-through;
|
||||
}
|
||||
.change-arrow {
|
||||
flex: 0 0 auto;
|
||||
color: #999;
|
||||
}
|
||||
.method-badge {
|
||||
display: inline-block;
|
||||
padding: 0.1rem 0.4rem;
|
||||
|
||||
Reference in New Issue
Block a user