@@ -61,6 +61,35 @@
|
||||
padding: 8px 12px;
|
||||
min-width: 180px;
|
||||
}
|
||||
.source-audit-scroll {
|
||||
overflow-x: auto;
|
||||
}
|
||||
.source-audit-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: 13px;
|
||||
line-height: 1.6;
|
||||
text-align: left;
|
||||
}
|
||||
.source-audit-table th,
|
||||
.source-audit-table td {
|
||||
padding: 10px 12px;
|
||||
border-bottom: 1px solid var(--border-base);
|
||||
vertical-align: top;
|
||||
}
|
||||
.source-audit-table th {
|
||||
color: var(--ink-mute);
|
||||
font-weight: 500;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.source-audit-table td:first-child {
|
||||
white-space: nowrap;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.source-audit-table td:last-child {
|
||||
min-width: 220px;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.source-schedule-form > small {
|
||||
color: var(--ink-mute);
|
||||
font-size: 12px;
|
||||
|
||||
@@ -27,5 +27,8 @@ describe('source management separation', () => {
|
||||
const html = renderToStaticMarkup(<SourceSettingsAudit connections={[row]} />)
|
||||
expect(html).toContain('数据源设置审计')
|
||||
expect(html).not.toContain('保存')
|
||||
expect(html).not.toContain('<select')
|
||||
expect(html).toContain('全部数据源')
|
||||
expect(html).toContain('操作记录')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -2,7 +2,9 @@ import './SourceManagement.css'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { archiveSourceConnection, fetchSourceConnectionList, fetchSourceSettingsRevisions } from './sourceConnections'
|
||||
import { useOptionalAuth } from './auth'
|
||||
import type { SourceConnectionListItem, SourceSettingsRevision } from './sourceConnections'
|
||||
import type { SourceConnectionListItem } from './sourceConnections'
|
||||
import { fetchSourceAudit, sourceAuditActions } from './sourceSettingsAudit'
|
||||
import type { SourceAuditItem } from './sourceSettingsAudit'
|
||||
import { configuredInterval, saveSourceRefresh } from './sourceRefreshPolicy'
|
||||
|
||||
const types: Record<string, string> = {
|
||||
@@ -341,19 +343,28 @@ export function SourceRefreshPolicy({
|
||||
}
|
||||
|
||||
export function SourceSettingsAudit({ connections }: { connections: SourceConnectionListItem[] }) {
|
||||
const [id, setID] = useState(connections[0]?.id ?? '')
|
||||
const [rows, setRows] = useState<SourceSettingsRevision[]>([])
|
||||
const [rows, setRows] = useState<SourceAuditItem[]>([])
|
||||
const [total, setTotal] = useState(0)
|
||||
const [offset, setOffset] = useState(0)
|
||||
const [retry, setRetry] = useState(0)
|
||||
const [error, setError] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [loading, setLoading] = useState(true)
|
||||
useEffect(() => {
|
||||
setOffset(0)
|
||||
setRows([])
|
||||
setTotal(0)
|
||||
}, [connections])
|
||||
useEffect(() => {
|
||||
const controller = new AbortController()
|
||||
setRows([])
|
||||
setError('')
|
||||
if (!id) return
|
||||
setLoading(true)
|
||||
void fetchSourceSettingsRevisions(id, controller.signal)
|
||||
.then((items) => {
|
||||
if (!controller.signal.aborted) setRows(items)
|
||||
setError('')
|
||||
void fetchSourceAudit(offset, controller.signal)
|
||||
.then((page) => {
|
||||
if (controller.signal.aborted) return
|
||||
setRows((previous) =>
|
||||
offset === 0 ? page.items : [...new Map([...previous, ...page.items].map((row) => [row.id, row])).values()]
|
||||
)
|
||||
setTotal(page.total)
|
||||
})
|
||||
.catch((cause) => {
|
||||
if (!controller.signal.aborted) setError(errorText(cause))
|
||||
@@ -362,51 +373,61 @@ export function SourceSettingsAudit({ connections }: { connections: SourceConnec
|
||||
if (!controller.signal.aborted) setLoading(false)
|
||||
})
|
||||
return () => controller.abort()
|
||||
}, [id])
|
||||
}, [offset, retry, connections])
|
||||
return (
|
||||
<section className="settings-panel wide">
|
||||
<section className="settings-panel wide source-audit-panel" aria-label="数据源操作记录">
|
||||
<h2>数据源设置审计</h2>
|
||||
<label>
|
||||
数据源
|
||||
<select value={id} onChange={(event) => setID(event.target.value)}>
|
||||
{connections.map((row) => (
|
||||
<option key={row.id} value={row.id}>
|
||||
{row.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<p>全部数据源的配置操作记录,按时间倒序展示,包含已移除的连接。</p>
|
||||
{rows.length ? (
|
||||
<div className="source-audit-scroll">
|
||||
<table className="source-audit-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>时间</th>
|
||||
<th>操作人</th>
|
||||
<th>数据源</th>
|
||||
<th>操作</th>
|
||||
<th>变更内容</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{rows.map((row) => (
|
||||
<tr key={row.id}>
|
||||
<td>
|
||||
<time dateTime={row.created_at}>
|
||||
{new Date(row.created_at).toLocaleString('zh-CN', { hour12: false })}
|
||||
</time>
|
||||
</td>
|
||||
<td>{row.actor || '未记录'}</td>
|
||||
<td>{row.source_name}</td>
|
||||
<td>{sourceAuditActions[row.action] ?? '配置操作'}</td>
|
||||
<td>{row.detail}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
) : null}
|
||||
{loading ? <p role="status">加载操作记录中…</p> : null}
|
||||
{error ? (
|
||||
<p role="alert" className="error-message">
|
||||
{error}
|
||||
{error}{' '}
|
||||
<button className="secondary" onClick={() => setRetry((value) => value + 1)}>
|
||||
重试
|
||||
</button>
|
||||
</p>
|
||||
) : loading ? (
|
||||
<p>加载中…</p>
|
||||
) : !rows.length ? (
|
||||
<p>暂无设置变更记录。</p>
|
||||
) : (
|
||||
<ul className="revision-list">
|
||||
{rows.map((row) => (
|
||||
<li key={row.id}>
|
||||
<b>修订 {row.revision_number}</b>
|
||||
<span>
|
||||
{row.state === 'active'
|
||||
? '当前生效'
|
||||
: row.state === 'retired'
|
||||
? '历史版本'
|
||||
: row.state === 'validated'
|
||||
? '待启用'
|
||||
: '草稿或失败'}
|
||||
</span>
|
||||
<small>
|
||||
{row.created_by} · {new Date(row.created_at).toLocaleString('zh-CN')} ·{' '}
|
||||
{row.enabled ? '允许自动刷新' : '已暂停'} ·{' '}
|
||||
{row.refresh_interval_seconds ? `周期 ${row.refresh_interval_seconds} 秒` : '仅手动'}
|
||||
</small>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
) : null}
|
||||
{!loading && !error && !rows.length ? <p>暂无数据源配置操作记录。</p> : null}
|
||||
{rows.length ? (
|
||||
<p>
|
||||
已展示 {rows.length} / {total} 条记录
|
||||
</p>
|
||||
) : null}
|
||||
{!loading && !error && rows.length < total ? (
|
||||
<button className="secondary" onClick={() => setOffset(offset + 100)}>
|
||||
加载更多
|
||||
</button>
|
||||
) : null}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
import { fetchSourceAudit } from './sourceSettingsAudit'
|
||||
afterEach(() => vi.unstubAllGlobals())
|
||||
describe('all-source operation audit', () => {
|
||||
it('requests paginated global history without a source selector', async () => {
|
||||
const page = {
|
||||
items: [
|
||||
{
|
||||
id: '1',
|
||||
connection_id: 'removed',
|
||||
source_name: '已移除来源',
|
||||
action: 'archived',
|
||||
actor: 'tester',
|
||||
created_at: '2026-10-27T02:00:00Z',
|
||||
detail: '移除连接',
|
||||
},
|
||||
],
|
||||
total: 101,
|
||||
limit: 100,
|
||||
offset: 100,
|
||||
}
|
||||
const fetcher = vi.fn().mockResolvedValue({ ok: true, json: async () => ({ data: page }) })
|
||||
vi.stubGlobal('fetch', fetcher)
|
||||
expect(await fetchSourceAudit(100)).toEqual(page)
|
||||
expect(fetcher.mock.calls[0][0]).toBe('/api/v1/source-connections/audit?limit=100&offset=100')
|
||||
})
|
||||
it('does not disguise errors as empty history', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ ok: false }))
|
||||
await expect(fetchSourceAudit()).rejects.toThrow('无法读取')
|
||||
})
|
||||
it('rejects malformed history', async () => {
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi
|
||||
.fn()
|
||||
.mockResolvedValue({ ok: true, json: async () => ({ data: { items: [], total: '0', offset: 0, limit: 100 } }) })
|
||||
)
|
||||
await expect(fetchSourceAudit()).rejects.toThrow('格式无效')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
export type SourceAuditItem = {
|
||||
id: string
|
||||
connection_id: string
|
||||
source_name: string
|
||||
action: string
|
||||
actor: string
|
||||
created_at: string
|
||||
detail: string
|
||||
}
|
||||
export type SourceAuditPage = { items: SourceAuditItem[]; total: number; limit: number; offset: number }
|
||||
export async function fetchSourceAudit(offset = 0, signal?: AbortSignal): Promise<SourceAuditPage> {
|
||||
const response = await fetch(`/api/v1/source-connections/audit?limit=100&offset=${offset}`, {
|
||||
signal,
|
||||
headers: { Accept: 'application/json' },
|
||||
})
|
||||
if (!response.ok) throw new Error('无法读取数据源操作记录,请重试')
|
||||
const { data } = await response.json()
|
||||
if (
|
||||
!data ||
|
||||
!Array.isArray(data.items) ||
|
||||
!Number.isInteger(data.total) ||
|
||||
data.total < 0 ||
|
||||
data.offset !== offset ||
|
||||
!Number.isInteger(data.limit) ||
|
||||
data.limit < 1
|
||||
)
|
||||
throw new Error('数据源操作记录格式无效')
|
||||
for (const item of data.items) {
|
||||
if (
|
||||
!item ||
|
||||
['id', 'connection_id', 'source_name', 'action', 'actor', 'created_at', 'detail'].some(
|
||||
(key) => typeof item[key] !== 'string'
|
||||
) ||
|
||||
!Number.isFinite(Date.parse(item.created_at))
|
||||
)
|
||||
throw new Error('数据源操作记录格式无效')
|
||||
}
|
||||
return data
|
||||
}
|
||||
export const sourceAuditActions: Record<string, string> = {
|
||||
created: '添加连接',
|
||||
settings_updated: '修改配置',
|
||||
paused: '暂停自动刷新',
|
||||
resumed: '恢复自动刷新',
|
||||
archived: '移除连接',
|
||||
restored: '恢复连接',
|
||||
historical_settings: '历史设置记录',
|
||||
}
|
||||
Reference in New Issue
Block a user