import type { LogEntry } from '../types/mock-types'; import { LogParser } from './log-parser'; import { logger } from './logger'; export interface LogCategory { endpoint: string; method: string; successCount: number; errorCount: number; averageResponseTime: number; examples: LogEntry[]; } export class LogCategorizer { private static categories: Map = new Map(); private static initialized = false; static async initialize(): Promise { if (this.initialized) return; try { const logs = await LogParser.parseLogs(); this.categorizeLogs(logs); this.initialized = true; logger.info(`Categorized ${logs.length} logs into ${this.categories.size} categories`); } catch (_error) { logger.error('Failed to initialize log categorizer', { error: error.message }); } } private static categorizeLogs(logs: LogEntry[]): void { for (const log of logs) { const key = `${log.request.method} ${log.metadata.endpoint}`; if (!this.categories.has(key)) { this.categories.set(key, { endpoint: log.metadata.endpoint, method: log.request.method, successCount: 0, errorCount: 0, averageResponseTime: 0, examples: [] }); } const category = this.categories.get(key)!; if (log.response.status >= 400) { category.errorCount++; } else { category.successCount++; } category.examples.push(log); // Update average response time const totalTime = category.examples.reduce((sum, example) => sum + example.response.duration, 0); category.averageResponseTime = totalTime / category.examples.length; } } static async getCategories(): Promise { await this.initialize(); return Array.from(this.categories.values()); } static async getCategory(method: string, endpoint: string): Promise { await this.initialize(); const key = `${method} ${endpoint}`; return this.categories.get(key) || null; } static async getEndpoints(): Promise { await this.initialize(); const endpoints = new Set(); for (const category of this.categories.values()) { endpoints.add(category.endpoint); } return Array.from(endpoints); } static async getMethods(): Promise { await this.initialize(); const methods = new Set(); for (const category of this.categories.values()) { methods.add(category.method); } return Array.from(methods); } static async getErrorCategories(): Promise { await this.initialize(); return Array.from(this.categories.values()).filter(category => category.errorCount > 0); } static async getSuccessCategories(): Promise { await this.initialize(); return Array.from(this.categories.values()).filter(category => category.successCount > 0); } static async getCategoryStats(): Promise<{ totalCategories: number; totalLogs: number; successRate: number; averageResponseTime: number; }> { await this.initialize(); const categories = Array.from(this.categories.values()); const totalLogs = categories.reduce((sum, cat) => sum + cat.successCount + cat.errorCount, 0); const totalSuccess = categories.reduce((sum, cat) => sum + cat.successCount, 0); const totalResponseTime = categories.reduce((sum, cat) => sum + (cat.averageResponseTime * (cat.successCount + cat.errorCount)), 0); return { totalCategories: categories.length, totalLogs, successRate: totalLogs > 0 ? (totalSuccess / totalLogs) * 100 : 0, averageResponseTime: totalLogs > 0 ? totalResponseTime / totalLogs : 0 }; } }