import { useQuery } from '@tanstack/react-query'; import { AlertTriangle, Gauge, Layers3, Wrench } from 'lucide-react-native'; import { Text, View } from 'react-native'; import { LineTrendChart } from '@/src/components/line-trend-chart'; import { ListCard } from '@/src/components/list-card'; import { ScreenShell } from '@/src/components/screen-shell'; import { StatCard } from '@/src/components/stat-card'; import { getDashboardModels, getDashboardStats, getDashboardTrend, listAccounts } from '@/src/services/admin'; function getDateRange() { const end = new Date(); const start = new Date(); start.setDate(end.getDate() - 6); const toDate = (value: Date) => value.toISOString().slice(0, 10); return { start_date: toDate(start), end_date: toDate(end), }; } export default function MonitorScreen() { const range = getDateRange(); const statsQuery = useQuery({ queryKey: ['monitor-stats'], queryFn: getDashboardStats, }); const trendQuery = useQuery({ queryKey: ['monitor-trend', range.start_date, range.end_date], queryFn: () => getDashboardTrend({ ...range, granularity: 'day' }), }); const modelsQuery = useQuery({ queryKey: ['monitor-models', range.start_date, range.end_date], queryFn: () => getDashboardModels(range), }); const accountsQuery = useQuery({ queryKey: ['monitor-accounts'], queryFn: () => listAccounts(''), }); const stats = statsQuery.data; const trendPoints = (trendQuery.data?.trend ?? []).map((item) => ({ label: item.date.slice(5), value: item.total_tokens, })); const topModels = (modelsQuery.data?.models ?? []).slice(0, 3); const incidentAccounts = (accountsQuery.data?.items ?? []).filter((item) => item.status === 'error' || item.error_message).slice(0, 5); return ( {trendPoints.length > 1 ? ( `${Math.round(value / 1000)}k`} /> ) : null} {topModels.map((item) => ( {item.model} 请求 {item.requests} · Token {Math.round(item.total_tokens / 1000)}k ${Number(item.cost).toFixed(2)} ))} {topModels.length === 0 ? 暂无模型统计 : null} {incidentAccounts.map((item) => ( {item.name} {item.platform} · {item.status || 'unknown'} · {item.schedulable ? '可调度' : '暂停调度'} {item.error_message || '状态异常,建议从运维视角继续排查这个上游账号'} ))} {incidentAccounts.length === 0 ? 当前没有检测到异常账号。 : null} ); }