/** 资产概览:统计卡、协议分布、扫描覆盖、最近资产。 */ import { useMemo, useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { Banner, Button, Empty, Progress, RadioGroup, Space, Typography } from '@douyinfe/semi-ui'; import { IconArrowRight, IconRefresh, IconSearch } from '@douyinfe/semi-icons'; import { api } from '../../lib/http'; import { useAuth } from '../../lib/auth'; import { formatNumber, relativeTime } from '../../lib/format'; import { useT } from '../../lib/i18n'; import { useResource } from '../../components/hooks'; import { DataTable, ErrorBanner, PageHeader, SectionCard, StatGrid } from '../../components/ui'; import { ProtocolTag, RiskTag, TooltipText } from '../asset-library/components/AssetBits'; import { assetTargetLabel } from '../asset-library/components/assetTarget'; import './page.css'; const { Text } = Typography; const PERIODS = [ { value: 7, label: '7 天' }, { value: 30, label: '30 天' }, { value: 90, label: '90 天' } ]; function percentOf(count, total) { const value = Number(count || 0); const base = Number(total || 0); if (!base) return 0; return Math.round((value / base) * 1000) / 10; } export default function AssetOverviewPage() { const t = useT(); const navigate = useNavigate(); const { hasPermission, hasAnyPermission } = useAuth(); const canRead = hasPermission('asset:read'); const canCollect = hasAnyPermission(['asset:read', 'fofa:execute']); const [days, setDays] = useState(30); const statsRes = useResource(() => api.get('/assets/stats', { days }), [days], { skip: !canRead }); const recentRes = useResource( () => api.get('/assets', { page: 1, page_size: 10, sort_by: 'last_seen_at', sort_order: 'desc' }), [], { skip: !canRead } ); const stats = statsRes.data || {}; const coverage = stats.coverage || {}; const protocols = Array.isArray(stats.protocols) ? stats.protocols : []; const protocolTotal = useMemo( () => protocols.reduce((sum, item) => sum + Number(item.count || 0), 0), [protocols] ); const recentAssets = Array.isArray(recentRes.data?.assets) ? recentRes.data.assets : []; const metrics = [ { id: 'total', label: t('assets.totalAssets', null, '资产总数'), value: formatNumber(stats.total || 0), detail: t('assets.totalAssetsHint', null, '当前纳入持续监测的资产'), loading: statsRes.loading }, { id: 'ips', label: t('assets.ipCount', null, 'IP 数量'), value: formatNumber(stats.ips || 0), loading: statsRes.loading }, { id: 'domains', label: t('assets.domainCount', null, '域名数量'), value: formatNumber(stats.domains || 0), loading: statsRes.loading }, { id: 'ports', label: t('assets.portCount', null, '端口数量'), value: formatNumber(stats.ports || 0), loading: statsRes.loading }, { id: 'protocols', label: t('assets.serviceTypes', null, '服务类型'), value: formatNumber(protocols.length), detail: protocols[0] ? `${t('assets.topProtocol', null, '主要协议')}:${protocols[0].name}` : undefined, loading: statsRes.loading }, { id: 'recent', label: t('assets.recentCount', null, '近 7 天发现'), value: formatNumber(stats.recent || 0), detail: t('assets.recentShare', { percent: percentOf(stats.recent, stats.total) }, `占当前资产总量的 ${percentOf(stats.recent, stats.total)}%`), loading: statsRes.loading } ]; const goLibrary = asset => { const suffix = asset?.id ? `?id=${encodeURIComponent(asset.id)}` : ''; navigate(`/asset-library${suffix}`, asset ? { state: { asset } } : undefined); }; const columns = [ { title: t('assets.target', null, '目标'), dataIndex: 'host', render: (_, record) => (
{[record.ip, record.domain].filter(Boolean).join(' · ') || '—'}
) }, { title: t('assets.service', null, '服务'), dataIndex: 'protocol', width: 150, render: (_, record) => }, { title: t('assets.source', null, '来源'), dataIndex: 'source', width: 120, render: value => }, { title: t('assets.riskLevel', null, '风险等级'), dataIndex: 'risk_level', width: 120, render: (_, record) => }, { title: t('assets.lastSeen', null, '最近发现'), dataIndex: 'last_seen_at', width: 130, render: value => {relativeTime(value, t)} }, { title: t('common.actions', null, '操作'), dataIndex: 'id', width: 100, render: (_, record) => ( ) } ]; const coverageItems = [ { label: t('assets.scannedAssets', null, '已扫描资产'), value: formatNumber(coverage.scanned || 0) }, { label: t('assets.recentlyScanned', null, '近 30 天覆盖'), value: formatNumber(coverage.scanned_30d || 0) }, { label: t('assets.neverScanned', null, '从未扫描'), value: formatNumber(coverage.never_scanned || 0) }, { label: t('assets.staleScans', null, '超过 30 天未扫描'), value: formatNumber(coverage.stale || 0) } ]; return (
setDays(Number(event.target.value) || 30)} /> {canCollect && ( )} )} /> {!canRead && ( )} {canRead && statsRes.reload()} />} {canRead && } {canRead && (
{t('assets.protocolKinds', { count: protocols.length }, `${protocols.length} 种协议`)}} > {protocols.length === 0 ? ( ) : (
{protocols.map(item => { const percent = percentOf(item.count, stats.total || protocolTotal); return (
{item.name || 'unknown'} {formatNumber(item.count || 0)} · {percent}%
); })}
)}
{t('assets.coverageMeta', { scanned: coverage.scanned || 0, total: stats.total || 0 }, `${coverage.scanned || 0} / ${stats.total || 0} 已覆盖`)}} >
{t('assets.overallCoverage', null, '总体覆盖率')} = 80 ? 'var(--semi-color-success)' : undefined} format={percent => `${percent}%`} />
{coverageItems.map(item => (
{item.label} {item.value}
))}
)} {canRead && ( navigate('/asset-library')}> {t('assets.viewLibrary', null, '查看资产库')} )} > recentRes.reload()} /> recentRes.reload()} emptyText={t('common.noData', null, '暂无数据')} /> )}
); }