Files
planet/frontend/src/pages/DataSources/DataSources.tsx
T

1197 lines
46 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import type { Key } from 'react'
import {
Button,
Card,
Checkbox,
Col,
Drawer,
Alert,
Form,
Input,
Modal,
Progress,
Row,
Select,
Space,
Table,
Tabs,
Tag,
Tooltip,
Typography,
message,
} from 'antd'
import {
CopyOutlined,
InfoCircleOutlined,
PauseCircleOutlined,
PlayCircleOutlined,
SyncOutlined,
} from '@ant-design/icons'
import axios, { type AxiosResponse } from 'axios'
import { useNavigate } from 'react-router-dom'
import AppLayout from '../../components/AppLayout/AppLayout'
import ScrollbarOverlay from '../../components/Scrollbar/ScrollbarOverlay'
import { formatDateTimeZhCN } from '../../utils/datetime'
import { formatPhaseMetric, getPhaseDisplay, getPhaseSummary } from '../../utils/phaseProgress'
const { Text } = Typography
const COLLECTION_REFRESH_DELAY_MS = 800
interface BuiltInDataSource {
id: number
source: string
name: string
display_name?: string
module: string
priority: string
frequency: string
endpoint?: string
is_active: boolean
collector_class: string
last_run: string | null
last_run_at?: string | null
last_status?: string | null
product?: string
is_running: boolean
task_id: number | null
progress: number | null
phase?: string | null
phase_progress?: number | null
phase_message?: string | null
phase_current?: number | null
phase_total?: number | null
phase_unit?: string | null
records_processed: number | null
total_records: number | null
collected_records?: number
has_collected_data?: boolean
is_free?: boolean
requires_credentials?: boolean
credential_provider?: string | null
credential_status?: string
}
interface CustomDataSourceOverride {
id: number
name: string
description: string | null
source_type: string
endpoint: string
auth_type: string
is_active: boolean
created_at: string
updated_at: string | null
}
interface RealtimeSourceHealth {
connection_state: string
last_seen_at: string | null
last_success_at: string | null
last_error: string | null
message_rate: number | null
lag_seconds: number | null
updated_at: string | null
}
interface RealtimeSourceStats {
total_observations: number
observations_24h: number
observations_1h: number
unique_mmsi_total: number
unique_mmsi_24h: number
latest_observed_at: string | null
latest_collected_at: string | null
}
interface RealtimeSourceRuntime {
running: boolean
done: boolean
runtime: string
}
interface RealtimeSource {
source: string
name: string
display_name: string
kind: 'builtin' | 'custom'
config_id?: number
source_type: string
endpoint?: string
is_active: boolean
credential_configured: boolean
message_types: string[]
bounding_boxes: unknown[]
config: Record<string, any>
runtime: RealtimeSourceRuntime
health: RealtimeSourceHealth | null
stats: RealtimeSourceStats
}
interface EditableDataSourceConfig {
id: number
name: string
description: string | null
source_type: string
endpoint: string
auth_type: string
auth_config: Record<string, any>
headers: Record<string, string>
config: Record<string, any>
is_active?: boolean
}
interface UnifiedDataSource {
key: string
id: number
name: string
display_name: string
source: string
module?: string
priority?: string
frequency?: string
endpoint?: string
is_active: boolean
collector_class?: string
source_type?: string
auth_type?: string
last_run_at?: string | null
last_status?: string | null
product?: string
is_running?: boolean
progress?: number | null
phase?: string | null
phase_progress?: number | null
phase_message?: string | null
phase_current?: number | null
phase_total?: number | null
phase_unit?: string | null
task_id?: number | null
created_at?: string
updated_at?: string | null
description?: string | null
headers?: Record<string, string>
config?: Record<string, any>
is_free?: boolean
requires_credentials?: boolean
credential_provider?: string | null
credential_status?: string
collected_records?: number
has_collected_data?: boolean
}
interface ViewDataSource extends UnifiedDataSource {
headers: Record<string, string>
config: Record<string, any>
}
type TriggerDatasourceConflict = {
reason?: string
message?: string
progress?: number | null
phase?: string | null
phase_progress?: number | null
phase_message?: string | null
phase_current?: number | null
phase_total?: number | null
phase_unit?: string | null
records_processed?: number | null
total_records?: number | null
}
type TriggerDatasourceResult =
| { ok: true; response: AxiosResponse<any> }
| { ok: false; status: number; detail?: string | TriggerDatasourceConflict }
type DatasourceTaskStatus = {
is_running: boolean
task_id?: number | null
progress?: number | null
phase?: string | null
phase_progress?: number | null
phase_message?: string | null
phase_current?: number | null
phase_total?: number | null
phase_unit?: string | null
records_processed?: number | null
total_records?: number | null
status?: string | null
}
type ActiveFilter = 'enabled' | 'disabled' | 'all'
type StatusFilter = 'all' | 'success' | 'failed' | 'running' | 'not_run'
type CollectedFilter = 'all' | 'collected' | 'uncollected'
const PRODUCT_LABELS: Record<string, string> = {
vessels: '船只',
cables: '海底光缆',
satellites: '卫星',
bgp: 'BGP',
compute: '算力',
ai: 'AI',
media: '媒体',
other: '其他',
}
const PRODUCT_TAG_COLORS: Record<string, string> = {
vessels: 'cyan',
cables: 'geekblue',
satellites: 'purple',
bgp: 'volcano',
compute: 'blue',
ai: 'magenta',
media: 'green',
other: 'default',
}
function isRealtimeBuiltinSource(source: BuiltInDataSource | UnifiedDataSource) {
return source.source === 'aisstream_vessels'
}
function normalizeBuiltin(source: BuiltInDataSource): UnifiedDataSource {
return {
key: `builtin:${source.id}`,
id: source.id,
name: source.name,
display_name: source.display_name || source.name,
source: source.source,
module: source.module,
priority: source.priority,
product: source.product,
frequency: source.frequency,
endpoint: source.endpoint,
is_active: source.is_active,
collector_class: source.collector_class,
last_run_at: source.last_run_at || source.last_run,
last_status: source.last_status,
is_running: source.is_running,
progress: source.progress,
phase: source.phase,
phase_progress: source.phase_progress,
phase_message: source.phase_message,
phase_current: source.phase_current,
phase_total: source.phase_total,
phase_unit: source.phase_unit,
task_id: source.task_id,
is_free: source.is_free,
requires_credentials: source.requires_credentials,
credential_provider: source.credential_provider,
credential_status: source.credential_status,
collected_records: source.collected_records,
has_collected_data: source.has_collected_data,
headers: {},
config: {},
}
}
function DataSources() {
const [messageApi, contextHolder] = message.useMessage()
const navigate = useNavigate()
const [modal, modalContextHolder] = Modal.useModal()
const [builtInSources, setBuiltInSources] = useState<BuiltInDataSource[]>([])
const [customOverrides, setCustomOverrides] = useState<CustomDataSourceOverride[]>([])
const [realtimeSources, setRealtimeSources] = useState<RealtimeSource[]>([])
const [loading, setLoading] = useState(false)
const [realtimeLoading, setRealtimeLoading] = useState(false)
const [realtimeActionSource, setRealtimeActionSource] = useState<string | null>(null)
const [activeTab, setActiveTab] = useState('tasks')
const [triggerAllLoading, setTriggerAllLoading] = useState(false)
const [forceTriggerAll, setForceTriggerAll] = useState(false)
const [selectedRowKeys, setSelectedRowKeys] = useState<Key[]>([])
const [productFilter, setProductFilter] = useState<string>('all')
const [moduleFilter, setModuleFilter] = useState<string>('all')
const [activeFilter, setActiveFilter] = useState<ActiveFilter>('enabled')
const [statusFilter, setStatusFilter] = useState<StatusFilter>('all')
const [collectedFilter, setCollectedFilter] = useState<CollectedFilter>('all')
const [searchQuery, setSearchQuery] = useState('')
const [viewDrawerVisible, setViewDrawerVisible] = useState(false)
const [runningTasksVisible, setRunningTasksVisible] = useState(false)
const [viewingSource, setViewingSource] = useState<ViewDataSource | null>(null)
const [recordCount, setRecordCount] = useState<number | null>(null)
const [tableHeight, setTableHeight] = useState(360)
const tableRegionRef = useRef<HTMLDivElement | null>(null)
const taskBuiltInSources = useMemo(
() => builtInSources.filter((source) => !isRealtimeBuiltinSource(source)),
[builtInSources],
)
const allSources = useMemo(() => taskBuiltInSources.map(normalizeBuiltin), [taskBuiltInSources])
const selectedSourceIds = useMemo(
() => selectedRowKeys
.map((key) => allSources.find((source) => source.key === key)?.id)
.filter((id): id is number => typeof id === 'number'),
[allSources, selectedRowKeys],
)
const activeBuiltInCount = taskBuiltInSources.filter((source) => source.is_active).length
const collectedBuiltInCount = taskBuiltInSources.filter((source) => source.has_collected_data).length
const runningBuiltInSources = taskBuiltInSources.filter((source) => source.is_running)
const runningBuiltInCount = runningBuiltInSources.length
const aggregateProgress = runningBuiltInCount > 0
? Math.round(
taskBuiltInSources
.filter((source) => source.is_running)
.reduce((sum, source) => sum + (source.progress || 0), 0) / runningBuiltInCount,
)
: 0
const fetchData = useCallback(async () => {
setLoading(true)
try {
const params = {
product: productFilter === 'all' ? undefined : productFilter,
module: moduleFilter === 'all' ? undefined : moduleFilter,
is_active: activeFilter === 'all' ? undefined : activeFilter === 'enabled',
run_status: statusFilter === 'all' ? undefined : statusFilter,
collected: collectedFilter === 'all' ? undefined : collectedFilter === 'collected',
q: searchQuery.trim() || undefined,
}
const [builtinRes, customRes] = await Promise.all([
axios.get('/api/v1/datasources', { params }),
axios.get('/api/v1/datasources/configs'),
])
setBuiltInSources(builtinRes.data.data || [])
setCustomOverrides(customRes.data.data || [])
} catch (error) {
console.error('Failed to fetch data:', error)
messageApi.error('获取数据源列表失败')
} finally {
setLoading(false)
}
}, [activeFilter, collectedFilter, messageApi, moduleFilter, productFilter, searchQuery, statusFilter])
const fetchRealtimeSources = useCallback(async () => {
setRealtimeLoading(true)
try {
const response = await axios.get('/api/v1/realtime-sources')
setRealtimeSources(response.data.data || [])
} catch (error) {
console.error('Failed to fetch realtime sources:', error)
messageApi.error('获取实时流状态失败')
} finally {
setRealtimeLoading(false)
}
}, [messageApi])
useEffect(() => {
void fetchData()
}, [fetchData])
useEffect(() => {
void fetchRealtimeSources()
}, [fetchRealtimeSources])
useEffect(() => {
const visibleKeys = new Set(allSources.map((source) => source.key))
setSelectedRowKeys((keys) => keys.filter((key) => visibleKeys.has(String(key))))
}, [allSources])
useEffect(() => {
const updateHeight = () => {
setTableHeight(Math.max(260, (tableRegionRef.current?.offsetHeight || 0) - 56))
}
updateHeight()
if (typeof ResizeObserver === 'undefined') return undefined
const observer = new ResizeObserver(updateHeight)
if (tableRegionRef.current) observer.observe(tableRegionRef.current)
return () => observer.disconnect()
}, [allSources.length])
const runRealtimeAction = async (source: string, action: 'start' | 'stop' | 'restart') => {
try {
setRealtimeActionSource(`${source}:${action}`)
await axios.post(`/api/v1/realtime-sources/${encodeURIComponent(source)}/${action}`)
messageApi.success(action === 'start' ? '实时流已启动' : action === 'stop' ? '实时流已停止' : '实时流已重连')
await Promise.all([fetchRealtimeSources(), fetchData()])
} catch (error: unknown) {
const err = error as { response?: { data?: { detail?: string } } }
messageApi.error(err.response?.data?.detail || '实时流操作失败')
} finally {
setRealtimeActionSource(null)
}
}
const fetchDatasourceTaskStatus = async (id: number) => {
const res = await axios.get<DatasourceTaskStatus>(`/api/v1/datasources/${id}/task-status`)
return res.data
}
const triggerDatasource = async (id: number, options?: { force?: boolean }) => {
const res = await axios.post(`/api/v1/datasources/${id}/trigger`, null, {
params: { force: options?.force ?? false },
validateStatus: (status) => status < 500,
})
if (res.status >= 400) {
return { ok: false, status: res.status, detail: res.data?.detail } satisfies TriggerDatasourceResult
}
if (res.data.task_id) {
void fetchData()
} else {
window.setTimeout(fetchData, COLLECTION_REFRESH_DELAY_MS)
}
return { ok: true, response: res } satisfies TriggerDatasourceResult
}
const confirmForceTrigger = (id: number, taskInfo?: TriggerDatasourceConflict) => {
modal.confirm({
title: '当前任务未完成',
content: (
<div>
<p>{taskInfo?.message || '当前采集任务仍在运行,重新触发会丢失本次未完成进度。'}</p>
<p>当前阶段: {getPhaseDisplay(taskInfo || {})}</p>
<p>当前进度: {typeof taskInfo?.progress === 'number' ? `${Math.round(taskInfo.progress)}%` : '未知'}</p>
<p>确认后会强制取消当前采集,并重新开始采集。</p>
</div>
),
okText: '强制重新采集',
cancelText: '取消',
okButtonProps: { danger: true },
onOk: async () => {
const result = await triggerDatasource(id, { force: true })
if (result.ok) {
messageApi.success('已强制重新触发')
return
}
const detail = result.detail
messageApi.error(typeof detail === 'string' ? detail : detail?.message || '强制重新采集失败')
},
})
}
const triggerDatasourceWithPrecheck = async (id: number) => {
const taskStatus = await fetchDatasourceTaskStatus(id)
if (taskStatus.is_running) {
confirmForceTrigger(id, taskStatus)
return
}
const result = await triggerDatasource(id)
if (result.ok) {
messageApi.success('任务已触发')
return
}
const detail = result.detail
if (result.status === 409 && typeof detail === 'object' && detail?.reason === 'running_task_in_progress') {
confirmForceTrigger(id, detail)
return
}
messageApi.error(typeof detail === 'string' ? detail : detail?.message || '触发失败')
}
const handleTriggerAll = async () => {
try {
setTriggerAllLoading(true)
const res = await axios.post('/api/v1/datasources/trigger-batch', {
source_ids: selectedSourceIds,
force: forceTriggerAll,
product: selectedSourceIds.length ? undefined : productFilter === 'all' ? undefined : productFilter,
module: selectedSourceIds.length ? undefined : moduleFilter === 'all' ? undefined : moduleFilter,
is_active: selectedSourceIds.length ? undefined : activeFilter === 'all' ? undefined : activeFilter === 'enabled',
run_status: selectedSourceIds.length ? undefined : statusFilter === 'all' ? undefined : statusFilter,
collected: selectedSourceIds.length ? undefined : collectedFilter === 'all' ? undefined : collectedFilter === 'collected',
q: selectedSourceIds.length ? undefined : searchQuery.trim() || undefined,
})
const triggered = res.data.triggered || []
const skipped = res.data.skipped || []
const failed = res.data.failed || []
messageApi.success([
`已触发 ${triggered.length} 个`,
skipped.length ? `跳过 ${skipped.length} 个` : null,
failed.length ? `失败 ${failed.length} 个` : null,
].filter(Boolean).join(','))
setSelectedRowKeys([])
void fetchData()
} catch (error: unknown) {
const err = error as { response?: { data?: { detail?: string } } }
messageApi.error(err.response?.data?.detail || '批量触发失败')
} finally {
setTriggerAllLoading(false)
}
}
const handleToggle = async (id: number, current: boolean) => {
const endpoint = current ? 'disable' : 'enable'
try {
await axios.post(`/api/v1/datasources/${id}/${endpoint}`)
messageApi.success(`${current ? '已禁用' : '已启用'}`)
void fetchData()
} catch (error: unknown) {
const err = error as { response?: { data?: { detail?: string } } }
messageApi.error(err.response?.data?.detail || '操作失败')
}
}
const handleViewSource = async (source: UnifiedDataSource) => {
try {
{
const override = customOverrides.find((item) => item.name === source.source)
const [detailRes, statsRes, overrideDetail] = await Promise.all([
axios.get(`/api/v1/datasources/${source.id}`),
axios.get(`/api/v1/datasources/${source.id}/stats`),
override ? axios.get<EditableDataSourceConfig>(`/api/v1/datasources/configs/${override.id}`).then((res) => res.data) : Promise.resolve(null),
])
const data = detailRes.data
setViewingSource({
...source,
name: data.name,
display_name: data.display_name || source.display_name,
endpoint: overrideDetail?.endpoint || data.endpoint || source.endpoint || '',
auth_type: overrideDetail?.auth_type || 'none',
headers: overrideDetail?.headers || {},
config: overrideDetail?.config || {},
collector_class: data.collector_class,
module: data.module,
priority: data.priority,
frequency: data.frequency,
is_free: data.is_free,
requires_credentials: data.requires_credentials,
credential_provider: data.credential_provider,
credential_status: data.credential_status,
})
setRecordCount(statsRes.data.total_records || 0)
}
setViewDrawerVisible(true)
} catch (error) {
console.error(error)
messageApi.error('获取数据源信息失败')
}
}
const handleCopyLink = async (value: string, successText: string) => {
try {
await navigator.clipboard.writeText(value)
messageApi.success(successText)
} catch {
messageApi.error('复制失败,请手动复制')
}
}
const formatCount = (value?: number | null) => Number(value || 0).toLocaleString()
const formatLagSeconds = (value?: number | null) => {
if (value === null || value === undefined || !Number.isFinite(Number(value))) return '-'
const seconds = Number(value)
if (seconds < 60) return `${Math.round(seconds)} 秒`
if (seconds < 3600) return `${Math.round(seconds / 60)} 分钟`
return `${Math.round(seconds / 3600)} 小时`
}
const getConnectionColor = (state?: string | null) => {
if (state === 'connected') return 'success'
if (state === 'connecting' || state === 'reconnecting') return 'processing'
if (state === 'disconnected') return 'default'
return 'warning'
}
const renderConfigPreview = (value: unknown) => {
if (!Array.isArray(value) || value.length === 0) return '-'
return JSON.stringify(value)
}
const realtimeTotals = realtimeSources.reduce(
(acc, source) => ({
running: acc.running + (source.runtime?.running ? 1 : 0),
total: acc.total + (source.stats?.total_observations || 0),
recent24h: acc.recent24h + (source.stats?.observations_24h || 0),
}),
{ running: 0, total: 0, recent24h: 0 },
)
const renderRealtimeSource = (source: RealtimeSource) => {
const state = source.health?.connection_state || (source.runtime?.running ? 'running' : 'disconnected')
const actionBusyPrefix = `${source.source}:`
return (
<Card
key={source.source}
size="small"
title={(
<Space wrap>
<Text strong>{source.display_name || source.name}</Text>
<Tag color={source.kind === 'builtin' ? 'blue' : 'purple'}>{source.kind === 'builtin' ? '内置' : '自定义'}</Tag>
<Tag color={source.is_active ? 'success' : 'default'}>{source.is_active ? '启用' : '禁用'}</Tag>
<Tag color={source.credential_configured ? 'success' : 'warning'}>
{source.credential_configured ? '凭证已配置' : '缺少凭证'}
</Tag>
<Tag color={source.runtime?.running ? 'processing' : 'default'}>
{source.runtime?.running ? '运行中' : '未运行'}
</Tag>
</Space>
)}
extra={(
<Space size={6}>
<Button
size="small"
icon={<PlayCircleOutlined />}
disabled={!source.is_active || source.runtime?.running}
loading={realtimeActionSource === `${source.source}:start`}
onClick={() => { void runRealtimeAction(source.source, 'start') }}
>
启动
</Button>
<Button
size="small"
icon={<PauseCircleOutlined />}
disabled={!source.runtime?.running}
loading={realtimeActionSource === `${source.source}:stop`}
onClick={() => { void runRealtimeAction(source.source, 'stop') }}
>
停止
</Button>
<Button
size="small"
icon={<SyncOutlined />}
loading={realtimeActionSource === `${source.source}:restart` || Boolean(realtimeActionSource?.startsWith(actionBusyPrefix))}
onClick={() => { void runRealtimeAction(source.source, 'restart') }}
>
重连
</Button>
</Space>
)}
>
<Space direction="vertical" size={12} style={{ width: '100%' }}>
<Row gutter={[12, 12]}>
<Col xs={12} md={6}>
<Text type="secondary">连接状态</Text>
<div><Tag color={getConnectionColor(source.health?.connection_state)}>{state}</Tag></div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">累计入库</Text>
<div>{formatCount(source.stats?.total_observations)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">近 24h</Text>
<div>{formatCount(source.stats?.observations_24h)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">近 1h</Text>
<div>{formatCount(source.stats?.observations_1h)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">唯一 MMSI</Text>
<div>{formatCount(source.stats?.unique_mmsi_total)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">近 24h MMSI</Text>
<div>{formatCount(source.stats?.unique_mmsi_24h)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">最近收到</Text>
<div>{formatDateTimeZhCN(source.health?.last_seen_at)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">数据延迟</Text>
<div>{formatLagSeconds(source.health?.lag_seconds)}</div>
</Col>
</Row>
<Row gutter={[12, 12]}>
<Col xs={24} md={12}>
<Text type="secondary">Endpoint</Text>
<Input value={source.endpoint || '-'} readOnly />
</Col>
<Col xs={24} md={12}>
<Text type="secondary">Message Types</Text>
<Input value={source.message_types?.length ? source.message_types.join(', ') : '-'} readOnly />
</Col>
<Col xs={24}>
<Text type="secondary">Bounding Boxes</Text>
<Input value={renderConfigPreview(source.bounding_boxes)} readOnly />
</Col>
</Row>
<Row gutter={[12, 12]}>
<Col xs={12} md={6}>
<Text type="secondary">最近成功</Text>
<div>{formatDateTimeZhCN(source.health?.last_success_at)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">状态更新时间</Text>
<div>{formatDateTimeZhCN(source.health?.updated_at)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">最近观测</Text>
<div>{formatDateTimeZhCN(source.stats?.latest_observed_at)}</div>
</Col>
<Col xs={12} md={6}>
<Text type="secondary">最近入库</Text>
<div>{formatDateTimeZhCN(source.stats?.latest_collected_at)}</div>
</Col>
</Row>
{source.health?.last_error ? (
<Alert showIcon type="error" message="最近错误" description={source.health.last_error} />
) : null}
</Space>
</Card>
)
}
const columns = [
{
title: '名称',
dataIndex: 'display_name',
key: 'name',
width: 260,
ellipsis: true,
render: (_: string, record: UnifiedDataSource) => (
<Space direction="vertical" size={0}>
<Button type="link" style={{ padding: 0, height: 22 }} onClick={() => { void handleViewSource(record) }}>
{record.display_name}
</Button>
<Text type="secondary" style={{ fontSize: 12 }}>{record.source}</Text>
</Space>
),
},
{
title: '类型',
key: 'kind',
width: 100,
render: () => <Tag color="blue">内置</Tag>,
},
{
title: '产品域',
key: 'product',
width: 110,
render: (_: unknown, record: UnifiedDataSource) => {
const product = record.product || 'other'
return <Tag color={PRODUCT_TAG_COLORS[product] || 'default'}>{PRODUCT_LABELS[product] || product}</Tag>
},
},
{
title: '层级/类型',
key: 'module',
width: 120,
render: (_: unknown, record: UnifiedDataSource) => <Tag>{record.module}</Tag>,
},
{
title: '频率',
dataIndex: 'frequency',
key: 'frequency',
width: 90,
render: (value: string | undefined) => value || '-',
},
{
title: '最近采集',
dataIndex: 'last_run_at',
key: 'last_run_at',
width: 180,
render: (value: string | null | undefined) => formatDateTimeZhCN(value) || '-',
},
{
title: '已采集',
key: 'collected',
width: 110,
render: (_: unknown, record: UnifiedDataSource) => (
<Tag color={record.has_collected_data ? 'success' : 'default'}>
{record.has_collected_data ? `${record.collected_records || 0} 条` : '未采集'}
</Tag>
),
},
{
title: '状态',
key: 'status',
width: 180,
render: (_: unknown, record: UnifiedDataSource) => {
if (record.is_running) {
return (
<Tooltip title={getPhaseDisplay(record)}>
<Tag color="processing">{getPhaseSummary(record)}</Tag>
</Tooltip>
)
}
if (!record.last_status) return <Tag>未执行</Tag>
return <Tag color={record.last_status === 'success' ? 'success' : record.last_status === 'failed' ? 'error' : 'default'}>{record.last_status}</Tag>
},
},
{
title: '操作',
key: 'action',
fixed: 'right' as const,
width: 190,
render: (_: unknown, record: UnifiedDataSource) => (
<Space size={4}>
<Button type="link" size="small" icon={<SyncOutlined />} disabled={!record.is_active} onClick={() => { void triggerDatasourceWithPrecheck(record.id) }}>
触发
</Button>
<Button
type="link"
size="small"
icon={record.is_active ? <PauseCircleOutlined /> : <PlayCircleOutlined />}
danger={record.is_active}
style={record.is_active ? undefined : { color: '#52c41a' }}
onClick={() => { void handleToggle(record.id, record.is_active) }}
>
{record.is_active ? '禁用' : '启用'}
</Button>
</Space>
),
},
]
return (
<AppLayout>
{contextHolder}
{modalContextHolder}
<div className="page-shell">
<div className="page-shell__header">
<h2 style={{ margin: 0 }}>数据源</h2>
</div>
<Tabs
activeKey={activeTab}
onChange={setActiveTab}
items={[
{
key: 'tasks',
label: '采集任务',
children: (
<div className="page-shell__body data-source-builtin-tab">
<div className="data-source-bulk-toolbar">
<div className="data-source-bulk-toolbar__meta">
<div className="data-source-bulk-toolbar__title">采集实时进度</div>
<div className="data-source-bulk-toolbar__progress">
<div className="data-source-bulk-toolbar__progress-copy">
<span>总体进度</span>
<strong>{aggregateProgress}%</strong>
</div>
<Progress percent={aggregateProgress} size="small" status={runningBuiltInCount > 0 ? 'active' : 'normal'} showInfo={false} strokeColor="#1677ff" />
</div>
<div className="data-source-bulk-toolbar__stats">
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">当前筛选</span>
<strong>{allSources.length}</strong>
</div>
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">内置</span>
<strong>{taskBuiltInSources.length}</strong>
</div>
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">已启用</span>
<strong>{activeBuiltInCount}</strong>
</div>
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">已有数据</span>
<strong>{collectedBuiltInCount}</strong>
</div>
{selectedSourceIds.length > 0 ? (
<div className="data-source-bulk-toolbar__stat-pill data-source-bulk-toolbar__stat-pill--success">
<span className="data-source-bulk-toolbar__stat-label">已选择</span>
<strong>{selectedSourceIds.length}</strong>
</div>
) : null}
{runningBuiltInCount > 0 ? (
<Tooltip title="查看采集中任务">
<button
type="button"
className="data-source-bulk-toolbar__running-pill"
onClick={() => setRunningTasksVisible(true)}
>
<span className="data-source-bulk-toolbar__running-dot" />
<span className="data-source-bulk-toolbar__stat-label">采集中</span>
<strong>{runningBuiltInCount}</strong>
<span className="data-source-bulk-toolbar__running-arrow">›</span>
</button>
</Tooltip>
) : null}
</div>
</div>
<Space size={12} align="center">
<Tooltip title="这里用于查看数据源状态和触发采集;接口、凭证和自定义源配置请到设置中心维护。">
<InfoCircleOutlined style={{ fontSize: 16, color: '#8c8c8c', cursor: 'default' }} />
</Tooltip>
<Checkbox checked={forceTriggerAll} onChange={(event) => setForceTriggerAll(event.target.checked)}>
强制重新采集
</Checkbox>
<Button type="primary" size="middle" icon={<SyncOutlined />} loading={triggerAllLoading} onClick={handleTriggerAll}>
{selectedSourceIds.length ? '采集选中项' : '一键采集'}
</Button>
</Space>
</div>
<div className="data-source-filter-toolbar">
<Select
className="data-source-filter-toolbar__control"
value={productFilter}
onChange={setProductFilter}
options={[
{ value: 'all', label: '全部产品域' },
{ value: 'vessels', label: '船只' },
{ value: 'cables', label: '海底光缆' },
{ value: 'satellites', label: '卫星' },
{ value: 'bgp', label: 'BGP' },
{ value: 'compute', label: '算力' },
{ value: 'ai', label: 'AI' },
{ value: 'media', label: '媒体' },
{ value: 'other', label: '其他' },
]}
/>
<Select
className="data-source-filter-toolbar__control"
value={moduleFilter}
onChange={setModuleFilter}
options={[
{ value: 'all', label: '全部层级' },
{ value: 'L1', label: 'L1' },
{ value: 'L2', label: 'L2' },
{ value: 'L3', label: 'L3' },
{ value: 'L4', label: 'L4' },
]}
/>
<Select
className="data-source-filter-toolbar__control"
value={activeFilter}
onChange={setActiveFilter}
options={[
{ value: 'enabled', label: '已启用' },
{ value: 'disabled', label: '已禁用' },
{ value: 'all', label: '全部启用状态' },
]}
/>
<Select
className="data-source-filter-toolbar__control"
value={statusFilter}
onChange={setStatusFilter}
options={[
{ value: 'all', label: '全部执行状态' },
{ value: 'success', label: '最近成功' },
{ value: 'failed', label: '最近失败' },
{ value: 'running', label: '采集中' },
{ value: 'not_run', label: '未执行' },
]}
/>
<Select
className="data-source-filter-toolbar__control"
value={collectedFilter}
onChange={setCollectedFilter}
options={[
{ value: 'all', label: '全部数据状态' },
{ value: 'collected', label: '已采集' },
{ value: 'uncollected', label: '未采集' },
]}
/>
<Input.Search
className="data-source-filter-toolbar__search"
allowClear
placeholder="搜索名称、标识或采集器"
value={searchQuery}
onChange={(event) => setSearchQuery(event.target.value)}
/>
</div>
<div ref={tableRegionRef} className="table-scroll-region data-source-table-region">
<Table
columns={columns}
dataSource={allSources}
rowKey="key"
rowSelection={{
selectedRowKeys,
onChange: setSelectedRowKeys,
preserveSelectedRowKeys: false,
columnWidth: 40,
}}
loading={loading}
pagination={false}
scroll={{ x: 1350, y: tableHeight }}
tableLayout="fixed"
size="small"
/>
<ScrollbarOverlay containerRef={tableRegionRef} targetSelector=".ant-table-body" />
</div>
</div>
),
},
{
key: 'realtime',
label: '实时流',
children: (
<div className="page-shell__body data-source-builtin-tab">
<div className="data-source-bulk-toolbar">
<div className="data-source-bulk-toolbar__meta">
<div className="data-source-bulk-toolbar__title">实时流健康</div>
<div className="data-source-bulk-toolbar__progress">
<div className="data-source-bulk-toolbar__progress-copy">
<span>运行中</span>
<strong>{realtimeTotals.running}</strong>
</div>
<Progress
percent={realtimeSources.length ? Math.round((realtimeTotals.running / realtimeSources.length) * 100) : 0}
size="small"
status={realtimeTotals.running > 0 ? 'active' : 'normal'}
showInfo={false}
strokeColor="#1677ff"
/>
</div>
<div className="data-source-bulk-toolbar__stats">
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">实时源</span>
<strong>{realtimeSources.length}</strong>
</div>
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">运行中</span>
<strong>{realtimeTotals.running}</strong>
</div>
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">累计入库</span>
<strong>{formatCount(realtimeTotals.total)}</strong>
</div>
<div className="data-source-bulk-toolbar__stat-pill">
<span className="data-source-bulk-toolbar__stat-label">近 24h</span>
<strong>{formatCount(realtimeTotals.recent24h)}</strong>
</div>
</div>
</div>
<Button icon={<SyncOutlined />} loading={realtimeLoading} onClick={() => { void fetchRealtimeSources() }}>
刷新
</Button>
</div>
<Space direction="vertical" size={12} style={{ width: '100%' }}>
{realtimeSources.length ? realtimeSources.map(renderRealtimeSource) : (
<Card size="small">
<Text type="secondary">当前没有实时流数据源。</Text>
</Card>
)}
</Space>
</div>
),
},
]}
/>
</div>
<Modal
title="采集中任务"
open={runningTasksVisible}
onCancel={() => setRunningTasksVisible(false)}
footer={<Button onClick={() => setRunningTasksVisible(false)}>关闭</Button>}
width={680}
>
<Space direction="vertical" size={12} style={{ width: '100%' }}>
{runningBuiltInSources.length ? runningBuiltInSources.map((source) => (
<Card key={source.id} size="small">
<Space direction="vertical" size={8} style={{ width: '100%' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', gap: 12 }}>
<Space direction="vertical" size={0}>
<Text strong>{source.display_name || source.name}</Text>
<Text type="secondary" style={{ fontSize: 12 }}>
{source.source}{source.task_id ? ` · #${source.task_id}` : ''}
</Text>
</Space>
<Tooltip title={getPhaseDisplay(source)}>
<Tag color="processing">{getPhaseSummary(source)}</Tag>
</Tooltip>
</div>
{source.phase_message ? (
<Text type="secondary" style={{ fontSize: 12 }}>{source.phase_message}</Text>
) : null}
<Progress
percent={Math.round(source.progress || 0)}
size="small"
status="active"
strokeColor="#1677ff"
/>
<Text type="secondary" style={{ fontSize: 12 }}>
{source.phase_unit === 'bytes'
? `下载 ${formatPhaseMetric(source) || '准备中'}`
: `已处理 ${source.records_processed ?? 0}${source.total_records ? ` / ${source.total_records}` : ''}`}
</Text>
</Space>
</Card>
)) : (
<Text type="secondary">当前没有采集中任务。</Text>
)}
</Space>
</Modal>
<Drawer
title="查看数据源"
width={600}
open={viewDrawerVisible}
onClose={() => {
setViewDrawerVisible(false)
setViewingSource(null)
setRecordCount(null)
}}
footer={<div style={{ textAlign: 'right' }}><Button onClick={() => setViewDrawerVisible(false)}>关闭</Button></div>}
>
{viewingSource && (
<Space direction="vertical" size={16} style={{ width: '100%' }}>
<Card size="small" bordered={false} style={{ background: '#fafafa' }}>
<Row gutter={[12, 12]}>
<Col span={24}>
<Space>
<Tag color="blue">内置数据源</Tag>
<Tag color={viewingSource.is_active ? 'green' : 'default'}>{viewingSource.is_active ? '启用' : '禁用'}</Tag>
</Space>
</Col>
<Col span={24}>
<div style={{ marginBottom: 4, color: '#8c8c8c', fontSize: 12 }}>名称</div>
<Input value={viewingSource.display_name} disabled />
</Col>
<Col span={24}>
<div style={{ marginBottom: 4, color: '#8c8c8c', fontSize: 12 }}>标识</div>
<Input value={viewingSource.source} disabled />
</Col>
<Col span={12}>
<div style={{ marginBottom: 4, color: '#8c8c8c', fontSize: 12 }}>模块</div>
<Input value={viewingSource.module || '-'} disabled />
</Col>
<Col span={12}>
<div style={{ marginBottom: 4, color: '#8c8c8c', fontSize: 12 }}>优先级</div>
<Input value={viewingSource.priority || '-'} disabled />
</Col>
<Col span={12}>
<div style={{ marginBottom: 4, color: '#8c8c8c', fontSize: 12 }}>频率</div>
<Input value={viewingSource.frequency || '-'} disabled />
</Col>
<Col span={12}>
<div style={{ marginBottom: 4, color: '#8c8c8c', fontSize: 12 }}>数据量</div>
<Input value={recordCount === null ? '-' : `${recordCount} 条`} disabled />
</Col>
<Col span={24}>
<div style={{ marginBottom: 4, color: '#8c8c8c', fontSize: 12 }}>采集器</div>
<Input value={viewingSource.collector_class || '-'} disabled />
</Col>
</Row>
</Card>
{viewingSource.requires_credentials ? (
<Alert
type={viewingSource.credential_status === 'supported' ? 'info' : 'warning'}
showIcon
message="需要采集器凭证"
description={viewingSource.credential_status === 'supported' ? '请在设置中心的采集器设置中维护该采集器凭证。' : '该采集器需要凭证,配置入口待接入。'}
action={viewingSource.credential_status === 'supported' ? (
<Button
size="small"
type="primary"
onClick={() => navigate(`/settings?tab=collector_credentials&collector=${encodeURIComponent(viewingSource.source)}`)}
>
去配置
</Button>
) : undefined}
/>
) : null}
<Form layout="vertical">
<Form.Item label="采集源 API 链接">
<Space.Compact style={{ width: '100%' }}>
<Input value={viewingSource.endpoint || '-'} readOnly />
<Tooltip title={viewingSource.endpoint ? '复制采集源 API 链接' : '当前没有可复制的采集源 API 链接'}>
<Button disabled={!viewingSource.endpoint} icon={<CopyOutlined />} onClick={() => viewingSource.endpoint && handleCopyLink(viewingSource.endpoint, '采集源 API 链接已复制')} />
</Tooltip>
</Space.Compact>
</Form.Item>
<Form.Item label="请求头">
<pre style={{ background: '#f5f5f5', padding: 12, borderRadius: 4, overflow: 'auto', margin: 0 }}>
{JSON.stringify(viewingSource.headers || {}, null, 2)}
</pre>
</Form.Item>
<Form.Item label="运行参数">
<pre style={{ background: '#f5f5f5', padding: 12, borderRadius: 4, overflow: 'auto', margin: 0 }}>
{JSON.stringify(viewingSource.config || {}, null, 2)}
</pre>
</Form.Item>
</Form>
</Space>
)}
</Drawer>
</AppLayout>
)
}
export default DataSources