|
| 1 | +import React, { useState, useMemo } from 'react'; |
| 2 | + |
| 3 | +type TimeRange = '24h' | '7d' | '30d' | '90d'; |
| 4 | + |
| 5 | +interface ActivityLog { |
| 6 | + id: string; |
| 7 | + type: 'success' | 'warning' | 'error'; |
| 8 | + message: string; |
| 9 | + timestamp: string; |
| 10 | + model?: string; |
| 11 | +} |
| 12 | + |
| 13 | +interface ModelUsage { |
| 14 | + name: string; |
| 15 | + usage: number; |
| 16 | + percentage: number; |
| 17 | +} |
| 18 | + |
| 19 | +const AnalyticsPage: React.FC = () => { |
| 20 | + const [timeRange, setTimeRange] = useState<TimeRange>('7d'); |
| 21 | + |
| 22 | + // Mock data - would come from API in production |
| 23 | + const metrics = useMemo(() => { |
| 24 | + const baseMetrics = { |
| 25 | + totalPrompts: 1247, |
| 26 | + avgResponseTime: 2.3, |
| 27 | + successRate: 96.8, |
| 28 | + apiCost: 142.50 |
| 29 | + }; |
| 30 | + |
| 31 | + // Adjust metrics based on time range |
| 32 | + const multipliers = { |
| 33 | + '24h': 0.1, |
| 34 | + '7d': 1, |
| 35 | + '30d': 4, |
| 36 | + '90d': 12 |
| 37 | + }; |
| 38 | + |
| 39 | + const multiplier = multipliers[timeRange]; |
| 40 | + |
| 41 | + return { |
| 42 | + totalPrompts: Math.round(baseMetrics.totalPrompts * multiplier), |
| 43 | + avgResponseTime: baseMetrics.avgResponseTime, |
| 44 | + successRate: baseMetrics.successRate, |
| 45 | + apiCost: baseMetrics.apiCost * multiplier |
| 46 | + }; |
| 47 | + }, [timeRange]); |
| 48 | + |
| 49 | + const modelUsage: ModelUsage[] = [ |
| 50 | + { name: 'GPT-4', usage: 485, percentage: 39 }, |
| 51 | + { name: 'Claude 3 Opus', usage: 374, percentage: 30 }, |
| 52 | + { name: 'GPT-4 Turbo', usage: 249, percentage: 20 }, |
| 53 | + { name: 'Gemini Pro', usage: 87, percentage: 7 }, |
| 54 | + { name: 'Others', usage: 52, percentage: 4 } |
| 55 | + ]; |
| 56 | + |
| 57 | + const recentActivities: ActivityLog[] = [ |
| 58 | + { id: '1', type: 'success', message: 'Prompt optimization completed', timestamp: '2 minutes ago', model: 'GPT-4' }, |
| 59 | + { id: '2', type: 'success', message: 'Code review workflow executed', timestamp: '15 minutes ago', model: 'Claude 3' }, |
| 60 | + { id: '3', type: 'warning', message: 'High latency detected (3.8s)', timestamp: '1 hour ago', model: 'GPT-4 Turbo' }, |
| 61 | + { id: '4', type: 'success', message: 'Batch processing completed (50 items)', timestamp: '2 hours ago' }, |
| 62 | + { id: '5', type: 'error', message: 'API rate limit reached', timestamp: '3 hours ago', model: 'Gemini Pro' }, |
| 63 | + { id: '6', type: 'success', message: 'Marketing copy generated', timestamp: '4 hours ago', model: 'GPT-4' }, |
| 64 | + { id: '7', type: 'success', message: 'Data analysis completed', timestamp: '5 hours ago', model: 'Claude 3' }, |
| 65 | + { id: '8', type: 'warning', message: 'Token limit exceeded, truncated', timestamp: '6 hours ago' } |
| 66 | + ]; |
| 67 | + |
| 68 | + const getStatusIcon = (type: string) => { |
| 69 | + switch (type) { |
| 70 | + case 'success': return '✅'; |
| 71 | + case 'warning': return '⚠️'; |
| 72 | + case 'error': return '❌'; |
| 73 | + default: return '📝'; |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + const getStatusColor = (type: string) => { |
| 78 | + switch (type) { |
| 79 | + case 'success': return '#28a745'; |
| 80 | + case 'warning': return '#ffc107'; |
| 81 | + case 'error': return '#dc3545'; |
| 82 | + default: return '#6c757d'; |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + return ( |
| 87 | + <div> |
| 88 | + <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '20px' }}> |
| 89 | + <h2>📊 Analytics & Metrics</h2> |
| 90 | + |
| 91 | + {/* Time Range Filter */} |
| 92 | + <div style={{ display: 'flex', gap: '5px' }}> |
| 93 | + {(['24h', '7d', '30d', '90d'] as TimeRange[]).map(range => ( |
| 94 | + <button |
| 95 | + key={range} |
| 96 | + onClick={() => setTimeRange(range)} |
| 97 | + style={{ |
| 98 | + padding: '8px 16px', |
| 99 | + backgroundColor: timeRange === range ? '#007bff' : 'white', |
| 100 | + color: timeRange === range ? 'white' : '#212529', |
| 101 | + border: '1px solid #dee2e6', |
| 102 | + borderRadius: '4px', |
| 103 | + cursor: 'pointer', |
| 104 | + fontSize: '14px', |
| 105 | + fontWeight: timeRange === range ? '600' : '400' |
| 106 | + }} |
| 107 | + > |
| 108 | + {range} |
| 109 | + </button> |
| 110 | + ))} |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + |
| 114 | + {/* Key Metrics */} |
| 115 | + <div style={{ |
| 116 | + display: 'grid', |
| 117 | + gridTemplateColumns: 'repeat(auto-fit, minmax(250px, 1fr))', |
| 118 | + gap: '20px', |
| 119 | + marginBottom: '30px' |
| 120 | + }}> |
| 121 | + <div style={{ |
| 122 | + backgroundColor: 'white', |
| 123 | + padding: '20px', |
| 124 | + borderRadius: '8px', |
| 125 | + border: '1px solid #e9ecef', |
| 126 | + boxShadow: '0 1px 3px rgba(0,0,0,0.1)' |
| 127 | + }}> |
| 128 | + <div style={{ fontSize: '14px', color: '#6c757d', marginBottom: '8px' }}>Total Prompts Executed</div> |
| 129 | + <div style={{ fontSize: '32px', fontWeight: 'bold', color: '#212529' }}>{metrics.totalPrompts.toLocaleString()}</div> |
| 130 | + <div style={{ fontSize: '12px', color: '#28a745', marginTop: '8px' }}>↑ 12% from previous period</div> |
| 131 | + </div> |
| 132 | + |
| 133 | + <div style={{ |
| 134 | + backgroundColor: 'white', |
| 135 | + padding: '20px', |
| 136 | + borderRadius: '8px', |
| 137 | + border: '1px solid #e9ecef', |
| 138 | + boxShadow: '0 1px 3px rgba(0,0,0,0.1)' |
| 139 | + }}> |
| 140 | + <div style={{ fontSize: '14px', color: '#6c757d', marginBottom: '8px' }}>Avg Response Time</div> |
| 141 | + <div style={{ fontSize: '32px', fontWeight: 'bold', color: '#212529' }}>{metrics.avgResponseTime}s</div> |
| 142 | + <div style={{ fontSize: '12px', color: '#28a745', marginTop: '8px' }}>↓ 8% faster</div> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div style={{ |
| 146 | + backgroundColor: 'white', |
| 147 | + padding: '20px', |
| 148 | + borderRadius: '8px', |
| 149 | + border: '1px solid #e9ecef', |
| 150 | + boxShadow: '0 1px 3px rgba(0,0,0,0.1)' |
| 151 | + }}> |
| 152 | + <div style={{ fontSize: '14px', color: '#6c757d', marginBottom: '8px' }}>Success Rate</div> |
| 153 | + <div style={{ fontSize: '32px', fontWeight: 'bold', color: '#212529' }}>{metrics.successRate}%</div> |
| 154 | + <div style={{ fontSize: '12px', color: '#28a745', marginTop: '8px' }}>↑ 2.3% improvement</div> |
| 155 | + </div> |
| 156 | + |
| 157 | + <div style={{ |
| 158 | + backgroundColor: 'white', |
| 159 | + padding: '20px', |
| 160 | + borderRadius: '8px', |
| 161 | + border: '1px solid #e9ecef', |
| 162 | + boxShadow: '0 1px 3px rgba(0,0,0,0.1)' |
| 163 | + }}> |
| 164 | + <div style={{ fontSize: '14px', color: '#6c757d', marginBottom: '8px' }}>API Costs</div> |
| 165 | + <div style={{ fontSize: '32px', fontWeight: 'bold', color: '#212529' }}>${metrics.apiCost.toFixed(2)}</div> |
| 166 | + <div style={{ fontSize: '12px', color: '#dc3545', marginTop: '8px' }}>↑ 5% from previous period</div> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + |
| 170 | + {/* Top Models by Usage */} |
| 171 | + <div style={{ |
| 172 | + backgroundColor: 'white', |
| 173 | + padding: '20px', |
| 174 | + borderRadius: '8px', |
| 175 | + border: '1px solid #e9ecef', |
| 176 | + marginBottom: '30px', |
| 177 | + boxShadow: '0 1px 3px rgba(0,0,0,0.1)' |
| 178 | + }}> |
| 179 | + <h3 style={{ marginTop: 0, marginBottom: '20px' }}>Top Models by Usage</h3> |
| 180 | + {modelUsage.map((model, index) => ( |
| 181 | + <div key={index} style={{ marginBottom: '15px' }}> |
| 182 | + <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '5px' }}> |
| 183 | + <span style={{ fontSize: '14px', fontWeight: '500' }}>{model.name}</span> |
| 184 | + <span style={{ fontSize: '14px', color: '#6c757d' }}> |
| 185 | + {model.usage} runs ({model.percentage}%) |
| 186 | + </span> |
| 187 | + </div> |
| 188 | + <div style={{ |
| 189 | + width: '100%', |
| 190 | + height: '8px', |
| 191 | + backgroundColor: '#e9ecef', |
| 192 | + borderRadius: '4px', |
| 193 | + overflow: 'hidden' |
| 194 | + }}> |
| 195 | + <div style={{ |
| 196 | + width: `${model.percentage}%`, |
| 197 | + height: '100%', |
| 198 | + backgroundColor: '#007bff', |
| 199 | + borderRadius: '4px', |
| 200 | + transition: 'width 0.3s ease' |
| 201 | + }} /> |
| 202 | + </div> |
| 203 | + </div> |
| 204 | + ))} |
| 205 | + </div> |
| 206 | + |
| 207 | + {/* Recent Activity Feed */} |
| 208 | + <div style={{ |
| 209 | + backgroundColor: 'white', |
| 210 | + padding: '20px', |
| 211 | + borderRadius: '8px', |
| 212 | + border: '1px solid #e9ecef', |
| 213 | + boxShadow: '0 1px 3px rgba(0,0,0,0.1)' |
| 214 | + }}> |
| 215 | + <h3 style={{ marginTop: 0, marginBottom: '20px' }}>Recent Activity</h3> |
| 216 | + <div style={{ display: 'flex', flexDirection: 'column', gap: '12px' }}> |
| 217 | + {recentActivities.map(activity => ( |
| 218 | + <div |
| 219 | + key={activity.id} |
| 220 | + style={{ |
| 221 | + display: 'flex', |
| 222 | + alignItems: 'start', |
| 223 | + padding: '12px', |
| 224 | + backgroundColor: '#f8f9fa', |
| 225 | + borderRadius: '6px', |
| 226 | + borderLeft: `4px solid ${getStatusColor(activity.type)}` |
| 227 | + }} |
| 228 | + > |
| 229 | + <span style={{ fontSize: '20px', marginRight: '12px' }}> |
| 230 | + {getStatusIcon(activity.type)} |
| 231 | + </span> |
| 232 | + <div style={{ flex: 1 }}> |
| 233 | + <div style={{ fontSize: '14px', color: '#212529', marginBottom: '4px' }}> |
| 234 | + {activity.message} |
| 235 | + </div> |
| 236 | + <div style={{ fontSize: '12px', color: '#6c757d' }}> |
| 237 | + {activity.timestamp} |
| 238 | + {activity.model && ` • ${activity.model}`} |
| 239 | + </div> |
| 240 | + </div> |
| 241 | + </div> |
| 242 | + ))} |
| 243 | + </div> |
| 244 | + </div> |
| 245 | + </div> |
| 246 | + ); |
| 247 | +}; |
| 248 | + |
| 249 | +export default AnalyticsPage; |
0 commit comments