Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
Copyright (C) 2025 QuantumNous
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
For commercial licensing, please contact support@quantumnous.com
|
||||
*/
|
||||
import React, {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
} from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Activity } from 'lucide-react';
|
||||
import { Button, Select, Spin, Switch, Table, Tag } from '@douyinfe/semi-ui';
|
||||
import { API } from '../../../helpers/api';
|
||||
import { showError } from '../../../helpers';
|
||||
import OrgPanel from '../components/OrgPanel';
|
||||
|
||||
const PAGE_SIZE = 20;
|
||||
const WINDOW_OPTIONS = [
|
||||
{ value: 24, label: '近 24 小时' },
|
||||
{ value: 24 * 7, label: '近 7 天' },
|
||||
{ value: 24 * 30, label: '近 30 天' },
|
||||
];
|
||||
|
||||
const fmtTime = (v) => (v ? new Date(v * 1000).toLocaleString() : '-');
|
||||
|
||||
// OrgDsHitLogs 命中审计子 tab:本企业口径的聚合统计 + 命中明细(仅元信息,绝无原文)。
|
||||
const OrgDsHitLogs = ({ orgId }) => {
|
||||
const { t } = useTranslation();
|
||||
const headers = useMemo(() => ({ 'X-Org-Id': String(orgId) }), [orgId]);
|
||||
const [stats, setStats] = useState(null);
|
||||
// 统计失败必须显式呈现:合规巡检语境下"静默显示 0"会被当成真实结论
|
||||
const [statsError, setStatsError] = useState(false);
|
||||
const [windowHours, setWindowHours] = useState(24);
|
||||
const [logs, setLogs] = useState([]);
|
||||
const [page, setPage] = useState(1);
|
||||
const [total, setTotal] = useState(0);
|
||||
const [onlySensitive, setOnlySensitive] = useState(false);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [loadError, setLoadError] = useState(false);
|
||||
// 请求代次守卫:快速切换时间窗/筛选/翻页时,旧请求的迟到响应不得覆盖新状态——
|
||||
// 否则统计牌标着"近 30 天"显示的却是 24 小时的迟到数据(R39 修复)。
|
||||
const statsSeqRef = useRef(0);
|
||||
const logsSeqRef = useRef(0);
|
||||
// 记录最后一次尝试的页码:翻页失败后"重试"应重放失败的那次动作,而不是回到上次成功页
|
||||
const lastAttemptedPageRef = useRef(1);
|
||||
|
||||
const fetchStats = useCallback(
|
||||
async (hours) => {
|
||||
const seq = ++statsSeqRef.current;
|
||||
try {
|
||||
const res = await API.get(
|
||||
`/api/org/data-security/metrics?since_hours=${hours}`,
|
||||
{ headers },
|
||||
);
|
||||
if (seq !== statsSeqRef.current) return;
|
||||
if (res.data.success) {
|
||||
setStats(res.data.data);
|
||||
setStatsError(false);
|
||||
} else {
|
||||
setStatsError(true);
|
||||
showError(res.data.message || t('读取统计失败'));
|
||||
}
|
||||
} catch (err) {
|
||||
if (seq !== statsSeqRef.current) return;
|
||||
setStatsError(true);
|
||||
// axios 错误已由全局拦截器弹出中文提示,这里只兜非网络类异常
|
||||
if (err?.name !== 'AxiosError') showError(err);
|
||||
}
|
||||
},
|
||||
[headers, t],
|
||||
);
|
||||
|
||||
const fetchLogs = useCallback(
|
||||
async (p, sensitiveOnly) => {
|
||||
const seq = ++logsSeqRef.current;
|
||||
lastAttemptedPageRef.current = p;
|
||||
setLoading(true);
|
||||
setLoadError(false);
|
||||
try {
|
||||
const res = await API.get(
|
||||
`/api/org/data-security/logs?page=${p}&page_size=${PAGE_SIZE}&only_sensitive=${sensitiveOnly}`,
|
||||
{ headers },
|
||||
);
|
||||
if (seq !== logsSeqRef.current) return;
|
||||
if (res.data.success) {
|
||||
setLogs(res.data.data?.items || []);
|
||||
setTotal(res.data.data?.total || 0);
|
||||
setPage(p);
|
||||
} else {
|
||||
setLoadError(true);
|
||||
showError(res.data.message || t('加载命中审计失败'));
|
||||
}
|
||||
} catch (err) {
|
||||
if (seq !== logsSeqRef.current) return;
|
||||
setLoadError(true);
|
||||
if (err?.name !== 'AxiosError') showError(err);
|
||||
} finally {
|
||||
if (seq === logsSeqRef.current) setLoading(false);
|
||||
}
|
||||
},
|
||||
[headers, t],
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
fetchStats(windowHours);
|
||||
}, [fetchStats, windowHours]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchLogs(1, onlySensitive);
|
||||
}, [fetchLogs, onlySensitive]);
|
||||
|
||||
const w = stats?.window_stats || {};
|
||||
// 统计未就绪/失败时显示 '-' 而非 0:0 是一个有含义的合规结论,不能由失败态冒充
|
||||
const statValue = (v) => (statsError || stats === null ? '-' : (v ?? 0));
|
||||
const statTiles = [
|
||||
{ label: t('审计记录数'), value: statValue(w.total) },
|
||||
{
|
||||
label: t('命中敏感 PII 请求'),
|
||||
value: statValue(w.sensitive_hits),
|
||||
danger: true,
|
||||
},
|
||||
{ label: t('命中总次数'), value: statValue(w.total_hits) },
|
||||
{ label: t('旁路请求'), value: statValue(w.bypassed_count) },
|
||||
{
|
||||
label: t('fail_open 请求'),
|
||||
value: statValue(w.fail_open_count),
|
||||
danger: true,
|
||||
},
|
||||
];
|
||||
|
||||
const columns = [
|
||||
{ title: t('时间'), dataIndex: 'created_at', render: fmtTime, width: 170 },
|
||||
{ title: t('用户'), dataIndex: 'user_id', width: 80 },
|
||||
{
|
||||
title: t('模型'),
|
||||
dataIndex: 'model',
|
||||
render: (v) => <span className='break-all'>{v || '-'}</span>,
|
||||
},
|
||||
{ title: t('命中数'), dataIndex: 'hit_count', width: 80 },
|
||||
{
|
||||
title: t('敏感 PII'),
|
||||
dataIndex: 'has_sensitive_pii',
|
||||
width: 90,
|
||||
render: (v) =>
|
||||
v ? <Tag color='red'>{t('是')}</Tag> : <Tag>{t('否')}</Tag>,
|
||||
},
|
||||
{
|
||||
title: t('豁免'),
|
||||
dataIndex: 'bypassed',
|
||||
width: 80,
|
||||
render: (v) => (v ? <Tag color='orange'>{t('是')}</Tag> : '-'),
|
||||
},
|
||||
{
|
||||
title: 'fail_open',
|
||||
dataIndex: 'fail_open',
|
||||
width: 100,
|
||||
render: (v) => (v ? <Tag color='red'>fail_open</Tag> : '-'),
|
||||
},
|
||||
{ title: t('原文长度'), dataIndex: 'content_length', width: 90 },
|
||||
{ title: t('耗时(ms)'), dataIndex: 'desensitize_ms', width: 90 },
|
||||
];
|
||||
|
||||
return (
|
||||
<OrgPanel
|
||||
icon={Activity}
|
||||
title={t('命中审计')}
|
||||
subtitle={t('仅记录规则命中统计与元信息,绝不存储请求原文')}
|
||||
padding='p-4'
|
||||
className='mt-3 mb-2'
|
||||
actions={
|
||||
<Select
|
||||
value={windowHours}
|
||||
onChange={setWindowHours}
|
||||
optionList={WINDOW_OPTIONS.map((o) => ({ ...o, label: t(o.label) }))}
|
||||
size='small'
|
||||
style={{ width: 130 }}
|
||||
/>
|
||||
}
|
||||
>
|
||||
<div className='grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-5 gap-3 mb-4'>
|
||||
{statTiles.map((s) => (
|
||||
<div
|
||||
key={s.label}
|
||||
className='rounded-lg border border-[var(--border-subtle)] bg-[var(--surface-card)] px-3 py-2.5'
|
||||
>
|
||||
<div className='text-xs text-gray-500 dark:text-gray-400 truncate'>
|
||||
{s.label}
|
||||
</div>
|
||||
<div
|
||||
className={`mt-1 text-xl font-semibold ${
|
||||
s.danger && Number(s.value) > 0
|
||||
? 'text-red-500'
|
||||
: 'text-[var(--text-primary)]'
|
||||
}`}
|
||||
style={{ fontVariantNumeric: 'tabular-nums' }}
|
||||
>
|
||||
{s.value}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<div className='flex items-center justify-between gap-3 mb-3 flex-wrap'>
|
||||
<div className='flex items-center gap-2'>
|
||||
<Switch
|
||||
checked={onlySensitive}
|
||||
onChange={setOnlySensitive}
|
||||
size='small'
|
||||
/>
|
||||
<span className='text-sm text-gray-600 dark:text-gray-300'>
|
||||
{t('仅看命中敏感 PII 的请求')}
|
||||
</span>
|
||||
</div>
|
||||
{(loadError || statsError) && (
|
||||
<Button
|
||||
size='small'
|
||||
type='danger'
|
||||
theme='light'
|
||||
onClick={() => {
|
||||
if (statsError) fetchStats(windowHours);
|
||||
if (loadError)
|
||||
fetchLogs(lastAttemptedPageRef.current, onlySensitive);
|
||||
}}
|
||||
>
|
||||
{t('加载失败,点击重试')}
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Spin spinning={loading}>
|
||||
<Table
|
||||
columns={columns}
|
||||
dataSource={logs}
|
||||
rowKey='id'
|
||||
size='small'
|
||||
scroll={{ x: 'max(100%, 980px)' }}
|
||||
empty={
|
||||
<span className='text-sm text-gray-400'>
|
||||
{loadError
|
||||
? t('加载失败,点击上方按钮重试')
|
||||
: t(
|
||||
'暂无命中记录。平台开启脱敏且本企业流量命中规则后,这里会出现审计明细。',
|
||||
)}
|
||||
</span>
|
||||
}
|
||||
pagination={{
|
||||
currentPage: page,
|
||||
pageSize: PAGE_SIZE,
|
||||
total,
|
||||
onPageChange: (p) => fetchLogs(p, onlySensitive),
|
||||
}}
|
||||
/>
|
||||
</Spin>
|
||||
</OrgPanel>
|
||||
);
|
||||
};
|
||||
|
||||
export default OrgDsHitLogs;
|
||||
Reference in New Issue
Block a user