"use strict"; /** * AnomalyRepository — CRUD for anomalies table with filters. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.AnomalyRepository = void 0; function rowToAnomaly(row) { return { id: row.id, sessionId: row.session_id, type: row.type, severity: row.severity, description: row.description, actionTrace: JSON.parse(row.action_trace_json), evidence: { ...JSON.parse(row.evidence_json), screenshotPath: row.screenshot_path ?? undefined, domSnapshotPath: row.dom_snapshot_path ?? undefined, }, observationId: '', timestamp: row.detected_at, }; } class AnomalyRepository { constructor(db) { this.db = db; } create(anomaly, sessionId) { this.db .prepare(`INSERT INTO anomalies (id, session_id, type, severity, description, action_trace_json, evidence_json, screenshot_path, dom_snapshot_path, detected_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`) .run(anomaly.id, sessionId, anomaly.type, anomaly.severity, anomaly.description, JSON.stringify(anomaly.actionTrace), JSON.stringify({ httpLog: anomaly.evidence.httpLog, rawErrors: anomaly.evidence.rawErrors }), anomaly.evidence.screenshotPath ?? null, anomaly.evidence.domSnapshotPath ?? null, anomaly.timestamp); } findById(id) { const row = this.db .prepare('SELECT * FROM anomalies WHERE id = ?') .get(id); return row ? rowToAnomaly(row) : undefined; } findAll(filters) { const conditions = []; const values = []; if (filters?.sessionId) { conditions.push('session_id = ?'); values.push(filters.sessionId); } if (filters?.severity) { conditions.push('severity = ?'); values.push(filters.severity); } if (filters?.type) { conditions.push('type = ?'); values.push(filters.type); } const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''; const rows = this.db .prepare(`SELECT * FROM anomalies ${where} ORDER BY detected_at DESC`) .all(...values); return rows.map(rowToAnomaly); } countBySeverity(severities) { if (severities.length === 0) return 0; const placeholders = severities.map(() => '?').join(', '); const result = this.db .prepare(`SELECT COUNT(*) as cnt FROM anomalies WHERE severity IN (${placeholders})`) .get(...severities); return result.cnt; } count() { const result = this.db.prepare('SELECT COUNT(*) as cnt FROM anomalies').get(); return result.cnt; } } exports.AnomalyRepository = AnomalyRepository;