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);
|
const startDate = new Date(today);
|
||||||
startDate.setDate(today.getDate() - parseInt(days));
|
startDate.setDate(today.getDate() - parseInt(days));
|
||||||
|
|
||||||
// Read audit files
|
// Read from MongoDB instead of JSONL files
|
||||||
const auditDir = path.join(__dirname, '../../.memory/audit');
|
const db = require('../utils/db.util');
|
||||||
const decisions = [];
|
const collection = await db.getCollection('auditLogs');
|
||||||
|
|
||||||
// Get all audit files in date range
|
const decisions = await collection
|
||||||
const files = await fs.readdir(auditDir);
|
.find({ timestamp: { $gte: startDate } })
|
||||||
const auditFiles = files.filter(f => f.startsWith('decisions-') && f.endsWith('.jsonl'));
|
.sort({ timestamp: -1 })
|
||||||
|
.limit(parseInt(limit))
|
||||||
for (const file of auditFiles) {
|
.toArray();
|
||||||
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));
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
success: true,
|
success: true,
|
||||||
decisions: limited,
|
decisions,
|
||||||
total: decisions.length,
|
total: decisions.length,
|
||||||
limited: limited.length,
|
limited: decisions.length,
|
||||||
dateRange: {
|
dateRange: {
|
||||||
start: startDate.toISOString(),
|
start: startDate.toISOString(),
|
||||||
end: today.toISOString()
|
end: today.toISOString()
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue