deviceLog.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DeviceLogModel = void 0;
  4. const database_1 = require("../config/database");
  5. class DeviceLogModel {
  6. static async getByClientId(clientid, filters, limit, offset) {
  7. const params = [clientid];
  8. let whereClause = 'WHERE clientid = ?';
  9. if (filters?.event_type) {
  10. whereClause += ' AND event_type = ?';
  11. params.push(filters.event_type);
  12. }
  13. if (filters?.start_time) {
  14. whereClause += ' AND event_time >= ?';
  15. params.push(filters.start_time);
  16. }
  17. if (filters?.end_time) {
  18. whereClause += ' AND event_time <= ?';
  19. params.push(filters.end_time);
  20. }
  21. const countQuery = `SELECT COUNT(*) as total FROM vw_device_logs ${whereClause}`;
  22. const countResult = await (0, database_1.executeQuery)(countQuery, params);
  23. const total = countResult[0].total;
  24. let query = `SELECT * FROM vw_device_logs ${whereClause} ORDER BY event_time DESC`;
  25. if (limit !== undefined) {
  26. query += ' LIMIT ?';
  27. params.push(limit);
  28. if (offset !== undefined) {
  29. query += ' OFFSET ?';
  30. params.push(offset);
  31. }
  32. }
  33. const logs = await (0, database_1.executeQuery)(query, params);
  34. return { logs, total };
  35. }
  36. static async getEventTypesStats(clientid) {
  37. const query = `
  38. SELECT
  39. event_type,
  40. COUNT(*) as count
  41. FROM vw_device_logs
  42. WHERE clientid = ?
  43. GROUP BY event_type
  44. `;
  45. return await (0, database_1.executeQuery)(query, [clientid]);
  46. }
  47. static async getRecentLogs(limit = 50) {
  48. const query = `SELECT * FROM vw_device_logs ORDER BY event_time DESC LIMIT ?`;
  49. return await (0, database_1.executeQuery)(query, [limit]);
  50. }
  51. static async getConnectDisconnectLogs(clientid, limit) {
  52. let query = `
  53. SELECT
  54. id,
  55. clientid,
  56. event_type,
  57. event_time,
  58. username,
  59. peername,
  60. proto_ver,
  61. node,
  62. details
  63. FROM vw_device_logs
  64. WHERE clientid = ?
  65. AND event_type IN ('connect', 'disconnect')
  66. ORDER BY event_time DESC
  67. `;
  68. const params = [clientid];
  69. if (limit !== undefined) {
  70. query += ' LIMIT ?';
  71. params.push(limit);
  72. }
  73. return await (0, database_1.executeQuery)(query, params);
  74. }
  75. static async getPublishLogs(clientid, limit) {
  76. let query = `
  77. SELECT
  78. id,
  79. clientid,
  80. event_type,
  81. event_time,
  82. topic,
  83. payload,
  84. qos,
  85. node
  86. FROM vw_device_logs
  87. WHERE clientid = ?
  88. AND event_type = 'publish'
  89. ORDER BY event_time DESC
  90. `;
  91. const params = [clientid];
  92. if (limit !== undefined) {
  93. query += ' LIMIT ?';
  94. params.push(limit);
  95. }
  96. return await (0, database_1.executeQuery)(query, params);
  97. }
  98. static async getSubscribeLogs(clientid, limit) {
  99. let query = `
  100. SELECT
  101. id,
  102. clientid,
  103. event_type,
  104. event_time,
  105. topic,
  106. qos,
  107. node
  108. FROM vw_device_logs
  109. WHERE clientid = ?
  110. AND event_type IN ('subscribe', 'unsubscribe')
  111. ORDER BY event_time DESC
  112. `;
  113. const params = [clientid];
  114. if (limit !== undefined) {
  115. query += ' LIMIT ?';
  116. params.push(limit);
  117. }
  118. return await (0, database_1.executeQuery)(query, params);
  119. }
  120. static async getDailyStats(clientid, days = 7) {
  121. const query = `
  122. SELECT
  123. DATE(event_time) as date,
  124. COUNT(*) as total_events,
  125. SUM(CASE WHEN event_type = 'connect' THEN 1 ELSE 0 END) as connects,
  126. SUM(CASE WHEN event_type = 'disconnect' THEN 1 ELSE 0 END) as disconnects,
  127. SUM(CASE WHEN event_type = 'publish' THEN 1 ELSE 0 END) as publishes,
  128. SUM(CASE WHEN event_type = 'subscribe' THEN 1 ELSE 0 END) as subscribes,
  129. SUM(CASE WHEN event_type = 'unsubscribe' THEN 1 ELSE 0 END) as unsubscribes
  130. FROM vw_device_logs
  131. WHERE clientid = ?
  132. AND event_time >= DATE_SUB(NOW(), INTERVAL ? DAY)
  133. GROUP BY DATE(event_time)
  134. ORDER BY date DESC
  135. `;
  136. return await (0, database_1.executeQuery)(query, [clientid, days]);
  137. }
  138. }
  139. exports.DeviceLogModel = DeviceLogModel;
  140. //# sourceMappingURL=deviceLog.js.map