"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MqttMessageModel = void 0; const database_1 = require("../config/database"); class MqttMessageModel { static async getAll(limit, offset) { let query = 'SELECT * FROM mqtt_messages ORDER BY timestamp DESC'; const params = []; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); if (offset !== undefined) { query += ' OFFSET ?'; params.push(offset); } } return await (0, database_1.executeQuery)(query, params); } static async getByClientId(clientid, limit) { let query = 'SELECT * FROM mqtt_messages WHERE clientid = ? ORDER BY timestamp DESC'; const params = [clientid]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); } return await (0, database_1.executeQuery)(query, params); } static async getByTopic(topic, limit) { let query = 'SELECT * FROM mqtt_messages WHERE topic LIKE ? ORDER BY timestamp DESC'; const params = [`%${topic}%`]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); } return await (0, database_1.executeQuery)(query, params); } static async getByType(messageType, limit) { let query = 'SELECT * FROM mqtt_messages WHERE message_type = ? ORDER BY timestamp DESC'; const params = [messageType]; if (limit !== undefined) { query += ' LIMIT ?'; params.push(limit); } return await (0, database_1.executeQuery)(query, params); } static async getByTimeRange(startTime, endTime) { const startTimestamp = startTime.getTime(); const endTimestamp = endTime.getTime(); const query = 'SELECT * FROM mqtt_messages WHERE timestamp BETWEEN ? AND ? ORDER BY timestamp DESC'; return await (0, database_1.executeQuery)(query, [startTimestamp, endTimestamp]); } static async getCount() { const query = 'SELECT COUNT(*) as count FROM mqtt_messages'; const result = await (0, database_1.executeQuery)(query); return result[0].count; } static async getTypeStats() { const query = 'SELECT message_type, COUNT(*) as count FROM mqtt_messages GROUP BY message_type'; return await (0, database_1.executeQuery)(query); } static async getQosStats() { const query = 'SELECT qos, COUNT(*) as count FROM mqtt_messages GROUP BY qos'; return await (0, database_1.executeQuery)(query); } static async getSizeStats() { const query = ` SELECT AVG(LENGTH(payload)) as avg_size, MIN(LENGTH(payload)) as min_size, MAX(LENGTH(payload)) as max_size, COUNT(*) as total_messages FROM mqtt_messages `; return await (0, database_1.executeQuery)(query); } static async getHourlyStats(hours = 24) { const query = ` SELECT DATE_FORMAT(FROM_UNIXTIME(timestamp / 1000), '%Y-%m-%d %H:00:00') as hour, COUNT(*) as message_count, AVG(LENGTH(payload)) as avg_payload_size FROM mqtt_messages WHERE timestamp >= (UNIX_TIMESTAMP() - ?) * 1000 GROUP BY hour ORDER BY hour DESC `; return await (0, database_1.executeQuery)(query, [hours]); } static async getPopularTopics(limit = 10) { const query = ` SELECT topic, COUNT(*) as message_count FROM mqtt_messages WHERE message_type = 'publish' GROUP BY topic ORDER BY message_count DESC LIMIT ? `; return await (0, database_1.executeQuery)(query, [limit]); } static async getActiveClients(limit = 10) { const query = ` SELECT clientid, COUNT(*) as message_count FROM mqtt_messages WHERE message_type = 'publish' GROUP BY clientid ORDER BY message_count DESC LIMIT ? `; return await (0, database_1.executeQuery)(query, [limit]); } static async create(messageData) { const query = ` INSERT INTO mqtt_messages (clientid, topic, payload, message_type, qos, timestamp) VALUES (?, ?, ?, ?, ?, ?) `; const values = [ messageData.clientid, messageData.topic, messageData.payload, messageData.message_type, messageData.qos, messageData.timestamp ]; const result = await (0, database_1.executeQuery)(query, values); const insertId = result.insertId; return this.getById(insertId); } static async getById(id) { const query = 'SELECT * FROM mqtt_messages WHERE id = ?'; const results = await (0, database_1.executeQuery)(query, [id]); return results.length > 0 ? results[0] : null; } static async getHeatmapData(days = 7) { const query = ` SELECT DAYOFWEEK(FROM_UNIXTIME(timestamp / 1000)) - 1 as day, HOUR(FROM_UNIXTIME(timestamp / 1000)) as hour, COUNT(*) as value FROM mqtt_messages WHERE timestamp >= (UNIX_TIMESTAMP() - ? * 24 * 3600) * 1000 GROUP BY day, hour ORDER BY day, hour `; return await (0, database_1.executeQuery)(query, [days]); } } exports.MqttMessageModel = MqttMessageModel; //# sourceMappingURL=mqttMessage.js.map