forked from linkong/planet
592 lines
21 KiB
TypeScript
592 lines
21 KiB
TypeScript
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
import {
|
|
Table, Tag, Space, Card, Select, Input, Button,
|
|
Modal, Descriptions, Spin, Empty, Tooltip, Typography, Grid
|
|
} from 'antd'
|
|
import type { ColumnsType } from 'antd/es/table'
|
|
import {
|
|
DatabaseOutlined, GlobalOutlined, CloudServerOutlined,
|
|
AppstoreOutlined, EyeOutlined, SearchOutlined, FilterOutlined, ReloadOutlined
|
|
} from '@ant-design/icons'
|
|
import axios from 'axios'
|
|
import AppLayout from '../../components/AppLayout/AppLayout'
|
|
|
|
const { Title, Text } = Typography
|
|
const { useBreakpoint } = Grid
|
|
|
|
interface CollectedData {
|
|
id: number
|
|
source: string
|
|
source_id: string
|
|
data_type: string
|
|
name: string
|
|
title: string | null
|
|
description: string | null
|
|
country: string | null
|
|
city: string | null
|
|
latitude: string | null
|
|
longitude: string | null
|
|
value: string | null
|
|
unit: string | null
|
|
metadata: Record<string, any> | null
|
|
collected_at: string
|
|
reference_date: string | null
|
|
is_valid: number
|
|
}
|
|
|
|
interface Summary {
|
|
total_records: number
|
|
by_source: Record<string, Record<string, number>>
|
|
source_totals: Array<{ source: string; count: number }>
|
|
}
|
|
|
|
function DataList() {
|
|
const screens = useBreakpoint()
|
|
const isCompact = !screens.lg
|
|
const topbarRef = useRef<HTMLDivElement | null>(null)
|
|
const workspaceRef = useRef<HTMLDivElement | null>(null)
|
|
const mainAreaRef = useRef<HTMLDivElement | null>(null)
|
|
const rightColumnRef = useRef<HTMLDivElement | null>(null)
|
|
const tableHeaderRef = useRef<HTMLDivElement | null>(null)
|
|
const hasCustomLeftWidthRef = useRef(false)
|
|
|
|
const [mainAreaWidth, setMainAreaWidth] = useState(0)
|
|
const [mainAreaHeight, setMainAreaHeight] = useState(0)
|
|
const [rightColumnHeight, setRightColumnHeight] = useState(0)
|
|
const [tableHeaderHeight, setTableHeaderHeight] = useState(0)
|
|
const [leftPanelWidth, setLeftPanelWidth] = useState(360)
|
|
|
|
const [data, setData] = useState<CollectedData[]>([])
|
|
const [loading, setLoading] = useState(false)
|
|
const [summary, setSummary] = useState<Summary | null>(null)
|
|
const [total, setTotal] = useState(0)
|
|
const [page, setPage] = useState(1)
|
|
const [pageSize, setPageSize] = useState(20)
|
|
const [sourceFilter, setSourceFilter] = useState<string | undefined>()
|
|
const [typeFilter, setTypeFilter] = useState<string | undefined>()
|
|
const [countryFilter, setCountryFilter] = useState<string | undefined>()
|
|
const [searchText, setSearchText] = useState('')
|
|
const [sources, setSources] = useState<string[]>([])
|
|
const [types, setTypes] = useState<string[]>([])
|
|
const [countries, setCountries] = useState<string[]>([])
|
|
const [detailVisible, setDetailVisible] = useState(false)
|
|
const [detailData, setDetailData] = useState<CollectedData | null>(null)
|
|
const [detailLoading, setDetailLoading] = useState(false)
|
|
|
|
useEffect(() => {
|
|
const updateLayout = () => {
|
|
setMainAreaWidth(mainAreaRef.current?.offsetWidth || 0)
|
|
setMainAreaHeight(mainAreaRef.current?.offsetHeight || 0)
|
|
setRightColumnHeight(rightColumnRef.current?.offsetHeight || 0)
|
|
setTableHeaderHeight(tableHeaderRef.current?.offsetHeight || 0)
|
|
}
|
|
|
|
updateLayout()
|
|
|
|
if (typeof ResizeObserver === 'undefined') {
|
|
return undefined
|
|
}
|
|
|
|
const observer = new ResizeObserver(updateLayout)
|
|
if (workspaceRef.current) observer.observe(workspaceRef.current)
|
|
if (topbarRef.current) observer.observe(topbarRef.current)
|
|
if (mainAreaRef.current) observer.observe(mainAreaRef.current)
|
|
if (rightColumnRef.current) observer.observe(rightColumnRef.current)
|
|
if (tableHeaderRef.current) observer.observe(tableHeaderRef.current)
|
|
|
|
return () => observer.disconnect()
|
|
}, [isCompact])
|
|
|
|
useEffect(() => {
|
|
if (isCompact || mainAreaWidth === 0) {
|
|
return
|
|
}
|
|
|
|
const minLeft = 260
|
|
const minRight = 360
|
|
const maxLeft = Math.max(minLeft, mainAreaWidth - minRight - 12)
|
|
const preferredLeft = Math.max(minLeft, Math.min(Math.round((mainAreaWidth - 12) / 4), maxLeft))
|
|
|
|
setLeftPanelWidth((current) => {
|
|
if (!hasCustomLeftWidthRef.current) {
|
|
return preferredLeft
|
|
}
|
|
return Math.max(minLeft, Math.min(current, maxLeft))
|
|
})
|
|
}, [isCompact, mainAreaWidth])
|
|
|
|
const beginHorizontalResize = (event: React.MouseEvent<HTMLDivElement>) => {
|
|
if (isCompact) return
|
|
event.preventDefault()
|
|
hasCustomLeftWidthRef.current = true
|
|
const startX = event.clientX
|
|
const startWidth = leftPanelWidth
|
|
const containerWidth = mainAreaRef.current?.offsetWidth || 0
|
|
|
|
const onMove = (moveEvent: MouseEvent) => {
|
|
const minLeft = 260
|
|
const minRight = 360
|
|
const maxLeft = Math.max(minLeft, containerWidth - minRight - 12)
|
|
const nextWidth = startWidth + moveEvent.clientX - startX
|
|
setLeftPanelWidth(Math.max(minLeft, Math.min(nextWidth, maxLeft)))
|
|
}
|
|
|
|
const onUp = () => {
|
|
window.removeEventListener('mousemove', onMove)
|
|
window.removeEventListener('mouseup', onUp)
|
|
}
|
|
|
|
window.addEventListener('mousemove', onMove)
|
|
window.addEventListener('mouseup', onUp)
|
|
}
|
|
|
|
const fetchData = async () => {
|
|
setLoading(true)
|
|
try {
|
|
const params = new URLSearchParams({
|
|
page: page.toString(),
|
|
page_size: pageSize.toString(),
|
|
})
|
|
if (sourceFilter) params.append('source', sourceFilter)
|
|
if (typeFilter) params.append('data_type', typeFilter)
|
|
if (countryFilter) params.append('country', countryFilter)
|
|
if (searchText) params.append('search', searchText)
|
|
|
|
const res = await axios.get(`/api/v1/collected?${params}`)
|
|
setData(res.data.data)
|
|
setTotal(res.data.total)
|
|
} catch (error) {
|
|
console.error('Failed to fetch data:', error)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
const fetchSummary = async () => {
|
|
try {
|
|
const res = await axios.get('/api/v1/collected/summary')
|
|
setSummary(res.data)
|
|
} catch (error) {
|
|
console.error('Failed to fetch summary:', error)
|
|
}
|
|
}
|
|
|
|
const fetchFilters = async () => {
|
|
try {
|
|
const [sourcesRes, typesRes, countriesRes] = await Promise.all([
|
|
axios.get('/api/v1/collected/sources'),
|
|
axios.get('/api/v1/collected/types'),
|
|
axios.get('/api/v1/collected/countries'),
|
|
])
|
|
setSources(sourcesRes.data.sources || [])
|
|
setTypes(typesRes.data.data_types || [])
|
|
setCountries(countriesRes.data.countries || [])
|
|
} catch (error) {
|
|
console.error('Failed to fetch filters:', error)
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
fetchSummary()
|
|
fetchFilters()
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
fetchData()
|
|
}, [page, pageSize, sourceFilter, typeFilter, countryFilter])
|
|
|
|
const handleSearch = () => {
|
|
setPage(1)
|
|
fetchData()
|
|
}
|
|
|
|
const handleReset = () => {
|
|
setSourceFilter(undefined)
|
|
setTypeFilter(undefined)
|
|
setCountryFilter(undefined)
|
|
setSearchText('')
|
|
setPage(1)
|
|
setTimeout(fetchData, 0)
|
|
}
|
|
|
|
const handleViewDetail = async (id: number) => {
|
|
setDetailVisible(true)
|
|
setDetailLoading(true)
|
|
try {
|
|
const res = await axios.get(`/api/v1/collected/${id}`)
|
|
setDetailData(res.data)
|
|
} catch (error) {
|
|
console.error('Failed to fetch detail:', error)
|
|
} finally {
|
|
setDetailLoading(false)
|
|
}
|
|
}
|
|
|
|
const getSourceIcon = (source: string) => {
|
|
const iconMap: Record<string, React.ReactNode> = {
|
|
top500: <CloudServerOutlined />,
|
|
huggingface_models: <AppstoreOutlined />,
|
|
huggingface_datasets: <DatabaseOutlined />,
|
|
huggingface_spaces: <AppstoreOutlined />,
|
|
telegeography_cables: <GlobalOutlined />,
|
|
epoch_ai_gpu: <CloudServerOutlined />,
|
|
}
|
|
return iconMap[source] || <DatabaseOutlined />
|
|
}
|
|
|
|
const getTypeColor = (type: string) => {
|
|
const colors: Record<string, string> = {
|
|
supercomputer: 'red',
|
|
model: 'blue',
|
|
dataset: 'green',
|
|
space: 'purple',
|
|
submarine_cable: 'cyan',
|
|
gpu_cluster: 'orange',
|
|
ixp: 'magenta',
|
|
network: 'gold',
|
|
facility: 'lime',
|
|
}
|
|
return colors[type] || 'default'
|
|
}
|
|
|
|
const activeFilterCount = useMemo(
|
|
() => [sourceFilter, typeFilter, countryFilter, searchText.trim()].filter(Boolean).length,
|
|
[sourceFilter, typeFilter, countryFilter, searchText]
|
|
)
|
|
|
|
const summaryItems = useMemo(() => {
|
|
const items = [
|
|
{ key: 'total', label: '总记录', value: summary?.total_records || 0, icon: <DatabaseOutlined /> },
|
|
{ key: 'result', label: '筛选结果', value: total, icon: <SearchOutlined /> },
|
|
{ key: 'filters', label: '启用筛选', value: activeFilterCount, icon: <FilterOutlined /> },
|
|
{ key: 'sources', label: '数据源数', value: sources.length, icon: <DatabaseOutlined /> },
|
|
]
|
|
|
|
for (const item of (summary?.source_totals || []).slice(0, isCompact ? 3 : 5)) {
|
|
items.push({
|
|
key: item.source,
|
|
label: item.source,
|
|
value: item.count,
|
|
icon: getSourceIcon(item.source),
|
|
})
|
|
}
|
|
|
|
return items
|
|
}, [summary, total, activeFilterCount, isCompact, sources.length])
|
|
|
|
const treemapColumns = useMemo(() => {
|
|
if (isCompact) return 1
|
|
if (leftPanelWidth < 360) return 2
|
|
if (leftPanelWidth < 520) return 3
|
|
return 4
|
|
}, [isCompact, leftPanelWidth])
|
|
|
|
const treemapRowHeight = useMemo(() => {
|
|
if (isCompact) return 88
|
|
if (leftPanelWidth < 360) return 44
|
|
if (leftPanelWidth < 520) return 48
|
|
return 56
|
|
}, [isCompact, leftPanelWidth])
|
|
|
|
const treemapItems = useMemo(() => {
|
|
const palette = ['ocean', 'sky', 'mint', 'amber', 'rose', 'violet', 'slate']
|
|
const maxValue = Math.max(...summaryItems.map((item) => item.value), 1)
|
|
const allowTallTiles = !isCompact && leftPanelWidth >= 520
|
|
|
|
return summaryItems.map((item, index) => {
|
|
const ratio = item.value / maxValue
|
|
let colSpan = 1
|
|
let rowSpan = 1
|
|
|
|
if (allowTallTiles && index === 0) {
|
|
colSpan = Math.min(2, treemapColumns)
|
|
rowSpan = 2
|
|
} else if (allowTallTiles && ratio >= 0.7) {
|
|
colSpan = Math.min(2, treemapColumns)
|
|
rowSpan = 2
|
|
} else if (allowTallTiles && ratio >= 0.35) {
|
|
rowSpan = 2
|
|
}
|
|
|
|
return {
|
|
...item,
|
|
colSpan,
|
|
rowSpan,
|
|
tone: palette[index % palette.length],
|
|
}
|
|
})
|
|
}, [summaryItems, isCompact, leftPanelWidth, treemapColumns])
|
|
|
|
const pageHeight = '100%'
|
|
const desktopTableHeight = rightColumnHeight - tableHeaderHeight - 132
|
|
const compactTableHeight = mainAreaHeight - tableHeaderHeight - 156
|
|
const tableHeight = Math.max(180, isCompact ? compactTableHeight : desktopTableHeight)
|
|
|
|
const splitLayoutStyle = isCompact
|
|
? undefined
|
|
: { gridTemplateColumns: `${leftPanelWidth}px 12px minmax(0, 1fr)` }
|
|
|
|
const columns: ColumnsType<CollectedData> = [
|
|
{ title: 'ID', dataIndex: 'id', key: 'id', width: 80 },
|
|
{
|
|
title: '名称',
|
|
dataIndex: 'name',
|
|
key: 'name',
|
|
width: 280,
|
|
ellipsis: true,
|
|
render: (name: string, record: CollectedData) => (
|
|
<Tooltip title={name}>
|
|
<Button type="link" onClick={() => handleViewDetail(record.id)}>
|
|
{name}
|
|
</Button>
|
|
</Tooltip>
|
|
),
|
|
},
|
|
{
|
|
title: '数据源',
|
|
dataIndex: 'source',
|
|
key: 'source',
|
|
width: 170,
|
|
render: (source: string) => <Tag icon={getSourceIcon(source)}>{source}</Tag>,
|
|
},
|
|
{
|
|
title: '类型',
|
|
dataIndex: 'data_type',
|
|
key: 'data_type',
|
|
width: 120,
|
|
render: (type: string) => <Tag color={getTypeColor(type)}>{type}</Tag>,
|
|
},
|
|
{ title: '国家/地区', dataIndex: 'country', key: 'country', width: 130, ellipsis: true },
|
|
{
|
|
title: '数值',
|
|
dataIndex: 'value',
|
|
key: 'value',
|
|
width: 140,
|
|
render: (value: string | null, record: CollectedData) => (value ? `${value} ${record.unit || ''}` : '-'),
|
|
},
|
|
{
|
|
title: '采集时间',
|
|
dataIndex: 'collected_at',
|
|
key: 'collected_at',
|
|
width: 180,
|
|
render: (time: string) => new Date(time).toLocaleString('zh-CN'),
|
|
},
|
|
{
|
|
title: '操作',
|
|
key: 'action',
|
|
width: 96,
|
|
render: (_: unknown, record: CollectedData) => (
|
|
<Button type="link" icon={<EyeOutlined />} onClick={() => handleViewDetail(record.id)}>
|
|
详情
|
|
</Button>
|
|
),
|
|
},
|
|
]
|
|
|
|
return (
|
|
<AppLayout>
|
|
<div ref={workspaceRef} className="data-list-workspace" style={{ height: pageHeight }}>
|
|
<div ref={topbarRef} className="data-list-topbar">
|
|
<div>
|
|
<Title level={4} style={{ margin: 0 }}>采集数据</Title>
|
|
</div>
|
|
<Space size={8} wrap>
|
|
<Tag color="blue" style={{ marginInlineEnd: 0 }}>
|
|
结果 {total.toLocaleString()} 条
|
|
</Tag>
|
|
<Tag color="default" style={{ marginInlineEnd: 0 }}>
|
|
筛选 {activeFilterCount} 项
|
|
</Tag>
|
|
</Space>
|
|
</div>
|
|
|
|
<div ref={mainAreaRef} className="data-list-controls-shell">
|
|
<div className="data-list-split-layout" style={splitLayoutStyle}>
|
|
<Card
|
|
className="data-list-summary-card data-list-summary-card--panel"
|
|
title="数据概览"
|
|
size="small"
|
|
bodyStyle={{ padding: isCompact ? 12 : 16 }}
|
|
>
|
|
<div
|
|
className="data-list-summary-treemap"
|
|
style={{
|
|
gridTemplateColumns: `repeat(${treemapColumns}, minmax(0, 1fr))`,
|
|
gridAutoRows: `minmax(${treemapRowHeight}px, 1fr)`,
|
|
}}
|
|
>
|
|
{treemapItems.map((item) => (
|
|
<div
|
|
key={item.key}
|
|
className={`data-list-treemap-tile data-list-treemap-tile--${item.tone}`}
|
|
style={{
|
|
gridColumn: `span ${item.colSpan}`,
|
|
gridRow: `span ${item.rowSpan}`,
|
|
}}
|
|
>
|
|
<div className="data-list-treemap-head">
|
|
<span className="data-list-summary-tile-icon">{item.icon}</span>
|
|
<Text className="data-list-treemap-label">{item.label}</Text>
|
|
</div>
|
|
<div className="data-list-treemap-body">
|
|
<Text strong className="data-list-summary-tile-value">
|
|
{item.value.toLocaleString()}
|
|
</Text>
|
|
</div>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Card>
|
|
|
|
{!isCompact && (
|
|
<div
|
|
className="data-list-resize-handle data-list-resize-handle--vertical"
|
|
onMouseDown={beginHorizontalResize}
|
|
role="separator"
|
|
aria-orientation="vertical"
|
|
aria-label="调整左右分栏宽度"
|
|
/>
|
|
)}
|
|
|
|
<div ref={rightColumnRef} className="data-list-right-column">
|
|
<Card className="data-list-table-shell" bodyStyle={{ padding: 0 }}>
|
|
<div ref={tableHeaderRef} className="data-list-table-header data-list-table-header--with-filters">
|
|
<div className="data-list-table-header-main">
|
|
<Space size={8} wrap>
|
|
<Text strong>数据列表</Text>
|
|
<Text type="secondary">共 {total.toLocaleString()} 条结果</Text>
|
|
</Space>
|
|
<Space size={8} wrap>
|
|
<Button size="small" onClick={handleReset}>清空</Button>
|
|
<Button size="small" icon={<ReloadOutlined />} onClick={fetchData}>刷新</Button>
|
|
<Button size="small" type="primary" icon={<SearchOutlined />} onClick={handleSearch}>
|
|
搜索
|
|
</Button>
|
|
</Space>
|
|
</div>
|
|
<div className="data-list-filter-grid data-list-filter-grid--balanced data-list-filter-grid--header">
|
|
<Select
|
|
size="middle"
|
|
placeholder="数据源"
|
|
allowClear
|
|
value={sourceFilter}
|
|
onChange={(value) => {
|
|
setSourceFilter(value)
|
|
setPage(1)
|
|
}}
|
|
options={sources.map((source) => ({ label: source, value: source }))}
|
|
style={{ width: '100%' }}
|
|
/>
|
|
<Select
|
|
size="middle"
|
|
placeholder="数据类型"
|
|
allowClear
|
|
value={typeFilter}
|
|
onChange={(value) => {
|
|
setTypeFilter(value)
|
|
setPage(1)
|
|
}}
|
|
options={types.map((type) => ({ label: type, value: type }))}
|
|
style={{ width: '100%' }}
|
|
/>
|
|
<Select
|
|
size="middle"
|
|
placeholder="国家"
|
|
allowClear
|
|
value={countryFilter}
|
|
onChange={(value) => {
|
|
setCountryFilter(value)
|
|
setPage(1)
|
|
}}
|
|
options={countries.map((country) => ({ label: country, value: country }))}
|
|
style={{ width: '100%' }}
|
|
/>
|
|
<Input
|
|
size="middle"
|
|
placeholder="搜索名称"
|
|
value={searchText}
|
|
onChange={(event) => setSearchText(event.target.value)}
|
|
onPressEnter={handleSearch}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="table-scroll-region data-list-table-region" style={{ padding: isCompact ? 10 : 12 }}>
|
|
<Table
|
|
columns={columns}
|
|
dataSource={data}
|
|
rowKey="id"
|
|
loading={loading}
|
|
virtual
|
|
scroll={{ x: 'max-content', y: tableHeight }}
|
|
tableLayout="fixed"
|
|
size={isCompact ? 'small' : 'middle'}
|
|
pagination={{
|
|
current: page,
|
|
pageSize,
|
|
total,
|
|
onChange: (nextPage, nextPageSize) => {
|
|
setPage(nextPage)
|
|
setPageSize(nextPageSize)
|
|
},
|
|
showSizeChanger: true,
|
|
showTotal: (count) => `共 ${count} 条`,
|
|
}}
|
|
/>
|
|
</div>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<Modal
|
|
title="数据详情"
|
|
open={detailVisible}
|
|
onCancel={() => setDetailVisible(false)}
|
|
footer={[
|
|
<Button key="close" onClick={() => setDetailVisible(false)}>
|
|
关闭
|
|
</Button>,
|
|
]}
|
|
width={700}
|
|
>
|
|
{detailLoading ? (
|
|
<div style={{ textAlign: 'center', padding: 40 }}>
|
|
<Spin size="large" />
|
|
</div>
|
|
) : detailData ? (
|
|
<Descriptions column={2} bordered>
|
|
<Descriptions.Item label="ID">{detailData.id}</Descriptions.Item>
|
|
<Descriptions.Item label="数据源">{detailData.source}</Descriptions.Item>
|
|
<Descriptions.Item label="数据类型">{detailData.data_type}</Descriptions.Item>
|
|
<Descriptions.Item label="原始ID">{detailData.source_id || '-'}</Descriptions.Item>
|
|
<Descriptions.Item label="名称" span={2}>{detailData.name}</Descriptions.Item>
|
|
<Descriptions.Item label="标题" span={2}>{detailData.title || '-'}</Descriptions.Item>
|
|
<Descriptions.Item label="描述" span={2}>{detailData.description || '-'}</Descriptions.Item>
|
|
<Descriptions.Item label="国家">{detailData.country || '-'}</Descriptions.Item>
|
|
<Descriptions.Item label="城市">{detailData.city || '-'}</Descriptions.Item>
|
|
<Descriptions.Item label="经度">{detailData.longitude || '-'}</Descriptions.Item>
|
|
<Descriptions.Item label="纬度">{detailData.latitude || '-'}</Descriptions.Item>
|
|
<Descriptions.Item label="数值">{detailData.value} {detailData.unit || ''}</Descriptions.Item>
|
|
<Descriptions.Item label="采集时间">
|
|
{new Date(detailData.collected_at).toLocaleString('zh-CN')}
|
|
</Descriptions.Item>
|
|
<Descriptions.Item label="参考日期">
|
|
{detailData.reference_date ? new Date(detailData.reference_date).toLocaleDateString('zh-CN') : '-'}
|
|
</Descriptions.Item>
|
|
<Descriptions.Item label="元数据" span={2}>
|
|
<pre style={{ margin: 0, maxHeight: 200, overflow: 'auto' }}>
|
|
{JSON.stringify(detailData.metadata || {}, null, 2)}
|
|
</pre>
|
|
</Descriptions.Item>
|
|
</Descriptions>
|
|
) : (
|
|
<Empty description="暂无数据" />
|
|
)}
|
|
</Modal>
|
|
</AppLayout>
|
|
)
|
|
}
|
|
|
|
export default DataList
|