import { useState, useEffect, useCallback } from 'react'; import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, PieChart, Pie, Cell, } from 'recharts'; import type { UserStatsResponse } from '../../types/statistics'; import { getUserStats } from '../../services/statisticsService'; import DateRangeFilter from '../../components/DateRangeFilter'; const PIE_COLORS = ['#3b82f6', '#10b981', '#f59e0b', '#ef4444', '#8b5cf6', '#06b6d4']; const ROLE_BADGE: Record = { ADMIN: 'bg-red-100 text-red-800', MANAGER: 'bg-blue-100 text-blue-800', USER: 'bg-green-100 text-green-800', }; const getToday = () => new Date().toISOString().slice(0, 10); const UserStatsPage = () => { const [startDate, setStartDate] = useState(getToday()); const [endDate, setEndDate] = useState(getToday()); const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(true); const fetchData = useCallback(async () => { setIsLoading(true); try { const res = await getUserStats(startDate, endDate); if (res.success && res.data) { setData(res.data); } } finally { setIsLoading(false); } }, [startDate, endDate]); useEffect(() => { fetchData(); }, [fetchData]); const handlePreset = (days: number) => { const today = getToday(); if (days === 0) { setStartDate(today); } else { const d = new Date(); d.setDate(d.getDate() - days); setStartDate(d.toISOString().slice(0, 10)); } setEndDate(today); }; if (isLoading) { return (
로딩 중...
); } return (

사용자 통계

{!data ? (

데이터가 없습니다

) : ( <> {/* Summary Cards */}

전체 사용자

{data.totalUsers}

API Key 보유 사용자

{data.usersWithActiveKey}

API 요청 사용자

{data.totalActiveUsers}

{/* Charts */}
{/* Chart 1: Daily Active Users */}

일별 API 요청 사용자 추이

{data.dailyActiveUsers.length > 0 ? ( ) : (

데이터가 없습니다

)}
{/* Chart 2: Role Distribution Donut */}

역할별 요청 분포

{data.roleDistribution.length > 0 ? ( {data.roleDistribution.map((_, idx) => ( ))} ) : (

데이터가 없습니다

)}
{/* Table: Top Users */}

상위 사용자 Top 10

{data.topUsers.length > 0 ? (
{data.topUsers.slice(0, 10).map((user, idx) => ( ))}
순위 사용자 역할 요청 수 성공률
{idx + 1} {user.userName} {user.role} {user.requestCount.toLocaleString()} {user.successRate.toFixed(1)}%
) : (

데이터가 없습니다

)}
)}
); }; export default UserStatsPage;