clientConnection.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ClientConnectionModel = void 0;
  4. const database_1 = require("../config/database");
  5. class ClientConnectionModel {
  6. static async getAll(limit, offset) {
  7. let query = 'SELECT * FROM vw_client_connections ORDER BY timestamp DESC';
  8. const params = [];
  9. if (limit !== undefined) {
  10. query += ' LIMIT ?';
  11. params.push(limit);
  12. if (offset !== undefined) {
  13. query += ' OFFSET ?';
  14. params.push(offset);
  15. }
  16. }
  17. return await (0, database_1.executeQuery)(query, params);
  18. }
  19. static async getAllWithFilters(limit, offset, filters) {
  20. let query = 'SELECT * FROM vw_client_connections WHERE 1=1';
  21. const params = [];
  22. if (filters?.clientid) {
  23. query += ' AND clientid LIKE ?';
  24. params.push(`%${filters.clientid}%`);
  25. }
  26. if (filters?.event) {
  27. query += ' AND event = ?';
  28. params.push(filters.event);
  29. }
  30. if (filters?.startDate) {
  31. query += ' AND timestamp >= ?';
  32. params.push(new Date(filters.startDate));
  33. }
  34. if (filters?.endDate) {
  35. query += ' AND timestamp <= ?';
  36. params.push(new Date(filters.endDate));
  37. }
  38. query += ' ORDER BY timestamp DESC';
  39. if (limit !== undefined) {
  40. query += ' LIMIT ?';
  41. params.push(limit);
  42. if (offset !== undefined) {
  43. query += ' OFFSET ?';
  44. params.push(offset);
  45. }
  46. }
  47. return await (0, database_1.executeQuery)(query, params);
  48. }
  49. static async getCountWithFilters(filters) {
  50. let query = 'SELECT COUNT(*) as count FROM vw_client_connections WHERE 1=1';
  51. const params = [];
  52. if (filters?.clientid) {
  53. query += ' AND clientid LIKE ?';
  54. params.push(`%${filters.clientid}%`);
  55. }
  56. if (filters?.event) {
  57. query += ' AND event = ?';
  58. params.push(filters.event);
  59. }
  60. if (filters?.startDate) {
  61. query += ' AND timestamp >= ?';
  62. params.push(new Date(filters.startDate));
  63. }
  64. if (filters?.endDate) {
  65. query += ' AND timestamp <= ?';
  66. params.push(new Date(filters.endDate));
  67. }
  68. const result = await (0, database_1.executeQuery)(query, params);
  69. return result[0].count;
  70. }
  71. static async getCount() {
  72. const query = 'SELECT COUNT(*) as count FROM vw_client_connections';
  73. const result = await (0, database_1.executeQuery)(query);
  74. return result[0].count;
  75. }
  76. static async getByClientId(clientid, limit) {
  77. let query = 'SELECT * FROM vw_client_connections WHERE clientid = ? ORDER BY timestamp DESC';
  78. const params = [clientid];
  79. if (limit !== undefined) {
  80. query += ' LIMIT ?';
  81. params.push(limit);
  82. }
  83. return await (0, database_1.executeQuery)(query, params);
  84. }
  85. static async getByEvent(event, limit) {
  86. let query = 'SELECT * FROM vw_client_connections WHERE event = ? ORDER BY timestamp DESC';
  87. const params = [event];
  88. if (limit !== undefined) {
  89. query += ' LIMIT ?';
  90. params.push(limit);
  91. }
  92. return await (0, database_1.executeQuery)(query, params);
  93. }
  94. static async getByTimeRange(startTime, endTime) {
  95. const query = 'SELECT * FROM vw_client_connections WHERE timestamp BETWEEN ? AND ? ORDER BY timestamp DESC';
  96. return await (0, database_1.executeQuery)(query, [startTime, endTime]);
  97. }
  98. static async getEventStats() {
  99. const query = 'SELECT event, COUNT(*) as count FROM vw_client_connections GROUP BY event';
  100. return await (0, database_1.executeQuery)(query);
  101. }
  102. static async getConnectionStats() {
  103. const query = `
  104. SELECT
  105. CASE
  106. WHEN event = 'client.connected' THEN 'connected'
  107. WHEN event = 'client.disconnected' THEN 'disconnected'
  108. ELSE 'other'
  109. END as connection_type,
  110. COUNT(*) as count
  111. FROM vw_client_connections
  112. WHERE event IN ('client.connected', 'client.disconnected')
  113. GROUP BY connection_type
  114. `;
  115. return await (0, database_1.executeQuery)(query);
  116. }
  117. static async create(connectionData) {
  118. const query = `
  119. INSERT INTO client_connections (username, clientid, event, timestamp, connected_at, node, peername, sockname, proto_name, proto_ver, keepalive, clean_start, reason, connection_duration)
  120. VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  121. `;
  122. const values = [
  123. connectionData.username || null,
  124. connectionData.clientid,
  125. connectionData.event,
  126. connectionData.timestamp || new Date(),
  127. connectionData.connected_at || null,
  128. connectionData.node,
  129. connectionData.peername || '',
  130. connectionData.sockname || '',
  131. connectionData.proto_name || 'MQTT',
  132. connectionData.proto_ver || 4,
  133. connectionData.keepalive || 60,
  134. connectionData.clean_start ?? 1,
  135. connectionData.reason || null,
  136. connectionData.connection_duration || null
  137. ];
  138. const result = await (0, database_1.executeQuery)(query, values);
  139. return { ...connectionData, id: result.insertId, created_at: new Date() };
  140. }
  141. static async getById(id) {
  142. const query = 'SELECT * FROM vw_client_connections WHERE id = ?';
  143. const results = await (0, database_1.executeQuery)(query, [id]);
  144. return results.length > 0 ? results[0] : null;
  145. }
  146. static async getDailyStats(days = 7) {
  147. const query = `
  148. SELECT
  149. DATE(timestamp) as date,
  150. COUNT(*) as total_connections,
  151. SUM(CASE WHEN event = 'connect' THEN 1 ELSE 0 END) as connections,
  152. SUM(CASE WHEN event = 'disconnect' THEN 1 ELSE 0 END) as disconnections
  153. FROM vw_client_connections
  154. WHERE timestamp >= DATE_SUB(NOW(), INTERVAL ${days} DAY)
  155. GROUP BY DATE(timestamp)
  156. ORDER BY date DESC
  157. `;
  158. return await (0, database_1.executeQuery)(query);
  159. }
  160. }
  161. exports.ClientConnectionModel = ClientConnectionModel;
  162. //# sourceMappingURL=clientConnection.js.map