fix(audit): read audit logs from MongoDB instead of JSONL files
Root cause: Audit analytics was reading from obsolete .memory/audit/*.jsonl files (last updated Oct 9), while actual audit logs are written to MongoDB auditLogs collection (current data through Oct 23). Fixed: Updated getAuditLogs() to query MongoDB auditLogs collection. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
50746199d0
commit
bf93bb08dc
1 changed files with 10 additions and 37 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue