// Request metrics and analytics
export class RequestMetrics {
  private metrics = {
    totalRequests: 0,
    cacheHits: 0,
    errors: 0,
    totalTime: 0,
    endpointStats: new Map<string, { count: number; totalTime: number; errors: number }>()
  }
  
  recordRequest(endpoint: string, duration: number, cached: boolean, error?: any): void {
    this.metrics.totalRequests++
    this.metrics.totalTime += duration
    
    if (cached) {
      this.metrics.cacheHits++
    }
    
    if (error) {
      this.metrics.errors++
    }
    
    // Update per-endpoint stats
    const stats = this.metrics.endpointStats.get(endpoint) || { count: 0, totalTime: 0, errors: 0 }
    stats.count++
    stats.totalTime += duration
    if (error) stats.errors++
    this.metrics.endpointStats.set(endpoint, stats)
  }
  
  getStats() {
    const endpointStats: any = {}
    this.metrics.endpointStats.forEach((stats, endpoint) => {
      endpointStats[endpoint] = {
        count: stats.count,
        avgTime: stats.totalTime / stats.count,
        errorRate: (stats.errors / stats.count) * 100
      }
    })
    
    return {
      totalRequests: this.metrics.totalRequests,
      cacheHitRate: (this.metrics.cacheHits / this.metrics.totalRequests) * 100,
      errorRate: (this.metrics.errors / this.metrics.totalRequests) * 100,
      avgResponseTime: this.metrics.totalTime / this.metrics.totalRequests,
      endpointStats
    }
  }
}