"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeviceLogModel = void 0; const database_1 = require("../config/database"); class DeviceLogModel { static async getByClientId(clientid, filters, limit, offset) { const params = [clientid]; let whereClause = 'WHERE clientid = ?'; if (filters?.event_type) { whereClause += ' AND event_type = ?'; params.push(filters.event_type); } if (filters?.start_time) { whereClause += ' AND event_time >= ?'; params.push(filters.start_time); } if (filters?.end_time) { whereClause += ' AND event_time <= ?'; params.push(filters.end_time); } const countQuery = `SELECT COUNT(*) as total FROM vw_device_logs ${whereClause}`; const countResult = await (0, database_1.executeQuery)(countQuery, params); const total = countResult[0].total; let query = `SELECT * FROM vw_device_logs ${whereClause} ORDER BY event_time DESC`; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } const logs = await (0, database_1.executeQuery)(query, params); return { logs, total }; } static async getEventTypesStats(clientid) { const query = ` SELECT event_type, COUNT(*) as count FROM vw_device_logs WHERE clientid = ? GROUP BY event_type `; return await (0, database_1.executeQuery)(query, [clientid]); } static async getRecentLogs(limit = 50) { const query = `SELECT * FROM vw_device_logs ORDER BY event_time DESC LIMIT ?`; return await (0, database_1.executeQuery)(query, [limit]); } static async getConnectDisconnectLogs(clientid, limit) { let query = ` SELECT id, clientid, event_type, event_time, username, peername, proto_ver, node, details FROM vw_device_logs WHERE clientid = ? AND event_type IN ('connect', 'disconnect') ORDER BY event_time DESC `; const params = [clientid]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); } return await (0, database_1.executeQuery)(query, params); } static async getPublishLogs(clientid, limit) { let query = ` SELECT id, clientid, event_type, event_time, topic, payload, qos, node FROM vw_device_logs WHERE clientid = ? AND event_type = 'publish' ORDER BY event_time DESC `; const params = [clientid]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); } return await (0, database_1.executeQuery)(query, params); } static async getSubscribeLogs(clientid, limit) { let query = ` SELECT id, clientid, event_type, event_time, topic, qos, node FROM vw_device_logs WHERE clientid = ? AND event_type IN ('subscribe', 'unsubscribe') ORDER BY event_time DESC `; const params = [clientid]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); } return await (0, database_1.executeQuery)(query, params); } static async getDailyStats(clientid, days = 7) { const query = ` SELECT DATE(event_time) as date, COUNT(*) as total_events, SUM(CASE WHEN event_type = 'connect' THEN 1 ELSE 0 END) as connects, SUM(CASE WHEN event_type = 'disconnect' THEN 1 ELSE 0 END) as disconnects, SUM(CASE WHEN event_type = 'publish' THEN 1 ELSE 0 END) as publishes, SUM(CASE WHEN event_type = 'subscribe' THEN 1 ELSE 0 END) as subscribes, SUM(CASE WHEN event_type = 'unsubscribe' THEN 1 ELSE 0 END) as unsubscribes FROM vw_device_logs WHERE clientid = ? AND event_time >= DATE_SUB(NOW(), INTERVAL ? DAY) GROUP BY DATE(event_time) ORDER BY date DESC `; return await (0, database_1.executeQuery)(query, [clientid, days]); } } exports.DeviceLogModel = DeviceLogModel; //# sourceMappingURL=deviceLog.js.map