diff --git a/src/controllers/audit.controller.js b/src/controllers/audit.controller.js index 167cc400..fdad5cfd 100644 --- a/src/controllers/audit.controller.js +++ b/src/controllers/audit.controller.js @@ -36,48 +36,21 @@ async function getAuditLogs(req, res) { const startDate = new Date(today); startDate.setDate(today.getDate() - parseInt(days)); - // Read audit files - const auditDir = path.join(__dirname, '../../.memory/audit'); - const decisions = []; + // Read from MongoDB instead of JSONL files + const db = require('../utils/db.util'); + const collection = await db.getCollection('auditLogs'); - // Get all audit files in date range - const files = await fs.readdir(auditDir); - const auditFiles = files.filter(f => f.startsWith('decisions-') && f.endsWith('.jsonl')); - - for (const file of auditFiles) { - const filePath = path.join(auditDir, file); - const content = await fs.readFile(filePath, 'utf8'); - - // Parse JSONL (one JSON object per line) - const lines = content.trim().split('\n'); - - for (const line of lines) { - if (!line.trim()) continue; - - try { - const decision = JSON.parse(line); - const decisionDate = new Date(decision.timestamp); - - if (decisionDate >= startDate) { - decisions.push(decision); - } - } catch (parseError) { - logger.error('Error parsing audit line:', parseError); - } - } - } - - // Sort by timestamp (most recent first) - decisions.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp)); - - // Apply limit - const limited = decisions.slice(0, parseInt(limit)); + const decisions = await collection + .find({ timestamp: { $gte: startDate } }) + .sort({ timestamp: -1 }) + .limit(parseInt(limit)) + .toArray(); res.json({ success: true, - decisions: limited, + decisions, total: decisions.length, - limited: limited.length, + limited: decisions.length, dateRange: { start: startDate.toISOString(), end: today.toISOString()