import * as fs from 'fs'; import * as path from 'path'; import { logger } from './logger'; import { config } from '../config'; import type { LogEntry } from '../types/mock-types'; export class LogParser { private static logDataPath = config.logDataPath; static async parseLogs(): Promise { const logs: LogEntry[] = []; try { // Parse requests directory const requestsPath = path.join(this.logDataPath, 'requests'); if (fs.existsSync(requestsPath)) { const requestFiles = fs.readdirSync(requestsPath).filter(file => file.endsWith('.json')); for (const file of requestFiles) { const filePath = path.join(requestsPath, file); const content = fs.readFileSync(filePath, 'utf-8'); const logEntries = JSON.parse(content); if (Array.isArray(logEntries)) { logs.push(...logEntries); } else { logs.push(logEntries); } } } // Parse test-calls directory const testCallsPath = path.join(this.logDataPath, 'test-calls'); if (fs.existsSync(testCallsPath)) { const testCallFiles = fs.readdirSync(testCallsPath).filter(file => file.endsWith('.json')); for (const file of testCallFiles) { const filePath = path.join(testCallsPath, file); const content = fs.readFileSync(filePath, 'utf-8'); const testCall = JSON.parse(content); // Convert test call format to LogEntry format const logEntry: LogEntry = { sessionId: testCall.testId || 'test-session', timestamp: testCall.timestamp, request: { method: testCall.request.method, url: testCall.request.url, headers: testCall.request.headers, body: testCall.request.body }, response: { status: testCall.response.status, headers: testCall.response.headers, body: testCall.response.body, duration: testCall.duration }, metadata: { endpoint: this.extractEndpoint(testCall.request.url), requestType: testCall.testName || 'unknown', hasBinaryData: false } }; logs.push(logEntry); } } logger.info(`Parsed ${logs.length} log entries from ${this.logDataPath}`); return logs; } catch (_error) { logger.error('Error parsing logs', { error: error.message }); return []; } } static async getLogsByEndpoint(endpoint: string): Promise { const allLogs = await this.parseLogs(); return allLogs.filter(log => log.metadata.endpoint === endpoint); } static async getLogsByMethod(method: string): Promise { const allLogs = await this.parseLogs(); return allLogs.filter(log => log.request.method.toLowerCase() === method.toLowerCase()); } static async getLogsByStatus(status: number): Promise { const allLogs = await this.parseLogs(); return allLogs.filter(log => log.response.status === status); } static async getErrorLogs(): Promise { const allLogs = await this.parseLogs(); return allLogs.filter(log => log.response.status >= 400); } private static extractEndpoint(url: string): string { try { const urlObj = new URL(url); return urlObj.pathname; } catch { // If URL parsing fails, try to extract path from string const pathMatch = url.match(/\/[^?]*/); return pathMatch ? pathMatch[0] : '/'; } } static async getResponsePatterns(): Promise> { const logs = await this.parseLogs(); const patterns = new Map(); for (const log of logs) { const endpoint = log.metadata.endpoint; const method = log.request.method; const key = `${method} ${endpoint}`; if (!patterns.has(key)) { patterns.set(key, []); } patterns.get(key)!.push(log.response.body); } return patterns; } }