/** * Carrier Monitoring Dashboard */ import React, { useState, useEffect } from 'react'; import { CarrierStats, CarrierHealthCheck } from '@/types/carrier'; export const CarrierMonitoring: React.FC = () => { const [stats, setStats] = useState([]); const [health, setHealth] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [timeRange, setTimeRange] = useState('24h'); useEffect(() => { fetchMonitoringData(); // Refresh every 30 seconds const interval = setInterval(fetchMonitoringData, 30000); return () => clearInterval(interval); }, [timeRange]); const fetchMonitoringData = async () => { setLoading(true); setError(null); try { const [statsRes, healthRes] = await Promise.all([ fetch(`/api/v1/carriers/stats?timeRange=${timeRange}`, { headers: { Authorization: `Bearer ${localStorage.getItem('accessToken')}`, }, }), fetch('/api/v1/carriers/health', { headers: { Authorization: `Bearer ${localStorage.getItem('accessToken')}`, }, }), ]); if (!statsRes.ok || !healthRes.ok) { throw new Error('Failed to fetch monitoring data'); } const [statsData, healthData] = await Promise.all([statsRes.json(), healthRes.json()]); setStats(statsData); setHealth(healthData); } catch (err: any) { setError(err.message); } finally { setLoading(false); } }; const getHealthStatus = (carrierId: string): CarrierHealthCheck | undefined => { return health.find(h => h.carrierId === carrierId); }; const getHealthColor = (status: string) => { switch (status) { case 'healthy': return 'bg-green-100 text-green-800'; case 'degraded': return 'bg-yellow-100 text-yellow-800'; case 'down': return 'bg-red-100 text-red-800'; default: return 'bg-gray-100 text-gray-800'; } }; // Calculate overall stats const totalRequests = stats.reduce((sum, s) => sum + s.totalRequests, 0); const totalSuccessful = stats.reduce((sum, s) => sum + s.successfulRequests, 0); const totalFailed = stats.reduce((sum, s) => sum + s.failedRequests, 0); const overallSuccessRate = totalRequests > 0 ? (totalSuccessful / totalRequests) * 100 : 0; const avgResponseTime = stats.length > 0 ? stats.reduce((sum, s) => sum + s.averageResponseTime, 0) / stats.length : 0; return (
{/* Header */}

Carrier Monitoring

Real-time monitoring of carrier API performance and health

{/* Error */} {error && (

{error}

)} {/* Overall Stats */}
Total Requests
{totalRequests.toLocaleString()}
Success Rate
{overallSuccessRate.toFixed(1)}%
Failed Requests
{totalFailed.toLocaleString()}
Avg Response Time
{avgResponseTime.toFixed(0)}ms
{/* Carrier Stats Table */}

Carrier Performance

{stats.length === 0 ? (

No monitoring data available

) : (
{stats.map(stat => { const healthStatus = getHealthStatus(stat.carrierId); const successRate = (stat.successfulRequests / stat.totalRequests) * 100; return ( ); })}
Carrier Health Requests Success Rate Error Rate Avg Response Availability Last Request
{stat.carrierName}
{healthStatus && ( {healthStatus.status.toUpperCase()} )} {stat.totalRequests.toLocaleString()} = 95 ? 'text-green-600' : successRate >= 80 ? 'text-yellow-600' : 'text-red-600' }`} > {successRate.toFixed(1)}% {stat.errorRate.toFixed(1)}% {stat.averageResponseTime.toFixed(0)}ms = 99 ? 'text-green-600' : stat.availability >= 95 ? 'text-yellow-600' : 'text-red-600' }`} > {stat.availability.toFixed(2)}% {stat.lastRequestAt ? new Date(stat.lastRequestAt).toLocaleString() : 'Never'}
)}
{/* Health Alerts */} {health.some(h => h.errors.length > 0) && (

Active Alerts

{health .filter(h => h.errors.length > 0) .map(healthCheck => (
{stats.find(s => s.carrierId === healthCheck.carrierId)?.carrierName || 'Unknown Carrier'}
{healthCheck.status.toUpperCase()}
    {healthCheck.errors.map((error: string, index: number) => (
  • • {error}
  • ))}
))}
)}
); };