import React, { useState, useEffect } from 'react'; import { Box, Container, Grid, Card, CardContent, Typography, Alert, CircularProgress, useTheme, useMediaQuery, } from '@mui/material'; import { motion, AnimatePresence } from 'framer-motion'; import { DollarSign, TrendingUp, AlertTriangle, Activity, Zap, BarChart3, PieChart, Clock } from 'lucide-react'; // Services import { billingService } from '../../services/billingService'; import { monitoringService } from '../../services/monitoringService'; // Types import { DashboardData, UsageStats } from '../../types/billing'; import { SystemHealth } from '../../types/monitoring'; // Components (we'll create these next) import BillingOverview from './BillingOverview'; import CostBreakdown from './CostBreakdown'; import UsageTrends from './UsageTrends'; import SystemHealthIndicator from '../monitoring/SystemHealthIndicator'; import UsageAlerts from './UsageAlerts'; // Animation variants const containerVariants = { hidden: { opacity: 0, y: 20 }, visible: { opacity: 1, y: 0, transition: { duration: 0.6, staggerChildren: 0.1 } } }; const cardVariants = { hidden: { opacity: 0, y: 20 }, visible: { opacity: 1, y: 0, transition: { duration: 0.4 } } }; const BillingDashboard: React.FC = () => { const theme = useTheme(); const isMobile = useMediaQuery(theme.breakpoints.down('md')); // State management const [dashboardData, setDashboardData] = useState(null); const [systemHealth, setSystemHealth] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [lastUpdated, setLastUpdated] = useState(new Date()); // Fetch dashboard data const fetchDashboardData = async () => { try { setLoading(true); setError(null); console.log('🔍 [DASHBOARD DEBUG] Starting data fetch...'); // Fetch billing and monitoring data in parallel const [billingData, healthData] = await Promise.all([ billingService.getDashboardData(), monitoringService.getSystemHealth() ]); console.log('🔍 [DASHBOARD DEBUG] Received billing data:', billingData); console.log('🔍 [DASHBOARD DEBUG] Received health data:', healthData); console.log('🔍 [DASHBOARD DEBUG] Billing data current_usage:', billingData?.current_usage); console.log('🔍 [DASHBOARD DEBUG] Billing data summary:', billingData?.summary); console.log('🔍 [DASHBOARD DEBUG] Billing data trends:', billingData?.trends); setDashboardData(billingData); setSystemHealth(healthData); setLastUpdated(new Date()); console.log('✅ [DASHBOARD DEBUG] Data set successfully'); } catch (err) { console.error('❌ [DASHBOARD DEBUG] Error fetching dashboard data:', err); setError(err instanceof Error ? err.message : 'Failed to load dashboard data'); } finally { setLoading(false); } }; // Initial data fetch useEffect(() => { fetchDashboardData(); }, []); // Auto-refresh every 30 seconds useEffect(() => { const interval = setInterval(() => { fetchDashboardData(); }, 30000); return () => clearInterval(interval); }, []); // Loading state if (loading && !dashboardData) { return ( Loading billing dashboard... ); } // Error state if (error && !dashboardData) { return ( Retry } > {error} ); } if (!dashboardData) { return null; } return ( {/* Section Header */} 💰 Billing & Usage Dashboard Monitor your API usage, costs, and system performance in real-time Last updated: {lastUpdated.toLocaleTimeString()} {/* Main Dashboard Grid */} {/* Top Row - Overview Cards */} {/* Middle Row - Cost Breakdown */} {/* Middle Row - Usage Trends */} {/* Bottom Row - Detailed Metrics */} Detailed Usage Metrics {/* Usage Summary */} {dashboardData.current_usage.total_calls.toLocaleString()} Total API Calls This month {/* Token Usage */} {(dashboardData.current_usage.total_tokens / 1000).toFixed(1)}k Tokens Used This month {/* Average Response Time */} {dashboardData.current_usage.avg_response_time.toFixed(0)}ms Avg Response Time Last 24 hours {/* Error Rate */} 5 ? 'error.main' : 'success.main', fontWeight: 'bold' }} > {dashboardData.current_usage.error_rate.toFixed(2)}% Error Rate Last 24 hours ); }; export default BillingDashboard;