clientConnectionController.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.ClientConnectionController = void 0;
  4. const clientConnection_1 = require("../models/clientConnection");
  5. const helpers_1 = require("../utils/helpers");
  6. class ClientConnectionController {
  7. static async getAllConnections(req, res) {
  8. try {
  9. const page = Number(req.query.page) || 1;
  10. const limit = Number(req.query.limit) || 20;
  11. const offset = (page - 1) * limit;
  12. const clientid = (0, helpers_1.toString)(req.query.clientid);
  13. const event = (0, helpers_1.toString)(req.query.event);
  14. const startDate = (0, helpers_1.toString)(req.query.startDate);
  15. const endDate = (0, helpers_1.toString)(req.query.endDate);
  16. const connections = await clientConnection_1.ClientConnectionModel.getAllWithFilters(limit, offset, { clientid, event, startDate, endDate });
  17. const total = await clientConnection_1.ClientConnectionModel.getCountWithFilters({ clientid, event, startDate, endDate });
  18. res.status(200).json({
  19. success: true,
  20. data: connections,
  21. pagination: {
  22. page,
  23. limit,
  24. total,
  25. pages: Math.ceil(total / limit)
  26. }
  27. });
  28. }
  29. catch (error) {
  30. console.error('获取连接记录失败:', error);
  31. res.status(500).json({
  32. success: false,
  33. message: '获取连接记录失败',
  34. error: error instanceof Error ? error.message : '未知错误'
  35. });
  36. }
  37. }
  38. static async getConnectionsByClientId(req, res) {
  39. try {
  40. const { clientid } = req.params;
  41. const limit = Number(req.query.limit) || 50;
  42. const clientidStr = (0, helpers_1.toString)(clientid);
  43. if (!clientidStr) {
  44. res.status(400).json({
  45. success: false,
  46. message: '客户端ID不能为空'
  47. });
  48. return;
  49. }
  50. const connections = await clientConnection_1.ClientConnectionModel.getByClientId(clientidStr, limit);
  51. res.status(200).json({
  52. success: true,
  53. data: connections
  54. });
  55. }
  56. catch (error) {
  57. console.error('根据客户端ID获取连接记录失败:', error);
  58. res.status(500).json({
  59. success: false,
  60. message: '根据客户端ID获取连接记录失败',
  61. error: error instanceof Error ? error.message : '未知错误'
  62. });
  63. }
  64. }
  65. static async getConnectionsByEvent(req, res) {
  66. try {
  67. const { event } = req.params;
  68. const limit = Number(req.query.limit) || 50;
  69. const eventStr = (0, helpers_1.toString)(event);
  70. if (!eventStr || !['connected', 'disconnected'].includes(eventStr)) {
  71. res.status(400).json({
  72. success: false,
  73. message: '无效的事件类型'
  74. });
  75. return;
  76. }
  77. const connections = await clientConnection_1.ClientConnectionModel.getByEvent(eventStr, limit);
  78. res.status(200).json({
  79. success: true,
  80. data: connections
  81. });
  82. }
  83. catch (error) {
  84. console.error('根据事件类型获取连接记录失败:', error);
  85. res.status(500).json({
  86. success: false,
  87. message: '根据事件类型获取连接记录失败',
  88. error: error instanceof Error ? error.message : '未知错误'
  89. });
  90. }
  91. }
  92. static async getConnectionsByTimeRange(req, res) {
  93. try {
  94. const { startTime, endTime } = req.query;
  95. if (!startTime || !endTime) {
  96. res.status(400).json({
  97. success: false,
  98. message: '开始时间和结束时间不能为空'
  99. });
  100. return;
  101. }
  102. const start = new Date(startTime);
  103. const end = new Date(endTime);
  104. if (isNaN(start.getTime()) || isNaN(end.getTime())) {
  105. res.status(400).json({
  106. success: false,
  107. message: '无效的时间格式'
  108. });
  109. return;
  110. }
  111. const connections = await clientConnection_1.ClientConnectionModel.getByTimeRange(start, end);
  112. res.status(200).json({
  113. success: true,
  114. data: connections
  115. });
  116. }
  117. catch (error) {
  118. console.error('根据时间范围获取连接记录失败:', error);
  119. res.status(500).json({
  120. success: false,
  121. message: '根据时间范围获取连接记录失败',
  122. error: error instanceof Error ? error.message : '未知错误'
  123. });
  124. }
  125. }
  126. static async getConnectionStats(req, res) {
  127. try {
  128. const stats = await clientConnection_1.ClientConnectionModel.getEventStats();
  129. res.status(200).json({
  130. success: true,
  131. data: stats
  132. });
  133. }
  134. catch (error) {
  135. console.error('获取连接事件统计失败:', error);
  136. res.status(500).json({
  137. success: false,
  138. message: '获取连接事件统计失败',
  139. error: error instanceof Error ? error.message : '未知错误'
  140. });
  141. }
  142. }
  143. static async getDailyConnectionStats(req, res) {
  144. try {
  145. const days = req.query.days ? parseInt(req.query.days) : 7;
  146. if (days < 1 || days > 30) {
  147. res.status(400).json({
  148. success: false,
  149. message: '天数必须在1到30之间'
  150. });
  151. return;
  152. }
  153. const stats = await clientConnection_1.ClientConnectionModel.getDailyStats(days);
  154. res.status(200).json({
  155. success: true,
  156. data: stats,
  157. message: '获取每日连接统计成功'
  158. });
  159. }
  160. catch (error) {
  161. console.error('获取每日连接统计失败:', error);
  162. res.status(500).json({
  163. success: false,
  164. message: '获取每日连接统计失败',
  165. error: error instanceof Error ? error.message : String(error)
  166. });
  167. }
  168. }
  169. }
  170. exports.ClientConnectionController = ClientConnectionController;
  171. //# sourceMappingURL=clientConnectionController.js.map