authLog.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AuthLogModel = void 0;
  4. const database_1 = require("../config/database");
  5. class AuthLogModel {
  6. static async getAll(limit, offset) {
  7. let query = 'SELECT * FROM auth_log ORDER BY created_at 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 getById(id) {
  20. const query = 'SELECT * FROM auth_log WHERE id = ?';
  21. const logs = await (0, database_1.executeQuery)(query, [id]);
  22. return logs.length > 0 ? logs[0] : null;
  23. }
  24. static async getByClientid(clientid, limit, offset) {
  25. let query = 'SELECT * FROM auth_log WHERE clientid = ? ORDER BY created_at DESC';
  26. const params = [clientid];
  27. if (limit !== undefined) {
  28. query += ' LIMIT ?';
  29. params.push(limit);
  30. if (offset !== undefined) {
  31. query += ' OFFSET ?';
  32. params.push(offset);
  33. }
  34. }
  35. return await (0, database_1.executeQuery)(query, params);
  36. }
  37. static async getByUsername(username, limit, offset) {
  38. let query = 'SELECT * FROM auth_log WHERE username = ? ORDER BY created_at DESC';
  39. const params = [username];
  40. if (limit !== undefined) {
  41. query += ' LIMIT ?';
  42. params.push(limit);
  43. if (offset !== undefined) {
  44. query += ' OFFSET ?';
  45. params.push(offset);
  46. }
  47. }
  48. return await (0, database_1.executeQuery)(query, params);
  49. }
  50. static async getByOperationType(operationType, limit, offset) {
  51. let query = 'SELECT * FROM auth_log WHERE operation_type = ? ORDER BY created_at DESC';
  52. const params = [operationType];
  53. if (limit !== undefined) {
  54. query += ' LIMIT ?';
  55. params.push(limit);
  56. if (offset !== undefined) {
  57. query += ' OFFSET ?';
  58. params.push(offset);
  59. }
  60. }
  61. return await (0, database_1.executeQuery)(query, params);
  62. }
  63. static async getByResult(result, limit, offset) {
  64. let query = 'SELECT * FROM auth_log WHERE result = ? ORDER BY created_at DESC';
  65. const params = [result];
  66. if (limit !== undefined) {
  67. query += ' LIMIT ?';
  68. params.push(limit);
  69. if (offset !== undefined) {
  70. query += ' OFFSET ?';
  71. params.push(offset);
  72. }
  73. }
  74. return await (0, database_1.executeQuery)(query, params);
  75. }
  76. static async getByTimeRange(startTime, endTime, limit, offset) {
  77. let query = 'SELECT * FROM auth_log WHERE created_at BETWEEN ? AND ? ORDER BY created_at DESC';
  78. const params = [startTime, endTime];
  79. if (limit !== undefined) {
  80. query += ' LIMIT ?';
  81. params.push(limit);
  82. if (offset !== undefined) {
  83. query += ' OFFSET ?';
  84. params.push(offset);
  85. }
  86. }
  87. return await (0, database_1.executeQuery)(query, params);
  88. }
  89. static async getCount() {
  90. const query = 'SELECT COUNT(*) as count FROM auth_log';
  91. const result = await (0, database_1.executeQuery)(query);
  92. return result[0].count;
  93. }
  94. static async getOperationTypeStats(startTime, endTime) {
  95. let query = `
  96. SELECT
  97. operation_type,
  98. COUNT(*) as count
  99. FROM auth_log
  100. `;
  101. const params = [];
  102. if (startTime && endTime) {
  103. query += ' WHERE created_at BETWEEN ? AND ?';
  104. params.push(startTime, endTime);
  105. }
  106. query += ' GROUP BY operation_type';
  107. return await (0, database_1.executeQuery)(query, params);
  108. }
  109. static async getResultStats(startTime, endTime) {
  110. let query = `
  111. SELECT
  112. result,
  113. COUNT(*) as count
  114. FROM auth_log
  115. `;
  116. const params = [];
  117. if (startTime && endTime) {
  118. query += ' WHERE created_at BETWEEN ? AND ?';
  119. params.push(startTime, endTime);
  120. }
  121. query += ' GROUP BY result';
  122. return await (0, database_1.executeQuery)(query, params);
  123. }
  124. static async getDailyStats(days = 7) {
  125. const query = `
  126. SELECT
  127. DATE(created_at) as date,
  128. COUNT(*) as total,
  129. SUM(CASE WHEN result = 'success' THEN 1 ELSE 0 END) as success,
  130. SUM(CASE WHEN result = 'failure' THEN 1 ELSE 0 END) as failure
  131. FROM auth_log
  132. WHERE created_at >= DATE_SUB(CURRENT_DATE(), INTERVAL ? DAY)
  133. GROUP BY DATE(created_at)
  134. ORDER BY date DESC
  135. `;
  136. return await (0, database_1.executeQuery)(query, [days]);
  137. }
  138. static async getHourlyStats(hours = 24) {
  139. const query = `
  140. SELECT
  141. HOUR(created_at) as hour,
  142. COUNT(*) as total,
  143. SUM(CASE WHEN result = 'success' THEN 1 ELSE 0 END) as success,
  144. SUM(CASE WHEN result = 'failure' THEN 1 ELSE 0 END) as failure
  145. FROM auth_log
  146. WHERE created_at >= DATE_SUB(NOW(), INTERVAL ? HOUR)
  147. GROUP BY HOUR(created_at)
  148. ORDER BY hour DESC
  149. `;
  150. return await (0, database_1.executeQuery)(query, [hours]);
  151. }
  152. static async getTopClients(limit = 10) {
  153. const query = `
  154. SELECT
  155. clientid,
  156. COUNT(*) as count
  157. FROM auth_log
  158. WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
  159. GROUP BY clientid
  160. ORDER BY count DESC
  161. LIMIT ?
  162. `;
  163. return await (0, database_1.executeQuery)(query, [limit]);
  164. }
  165. static async getTopIps(limit = 10) {
  166. const query = `
  167. SELECT
  168. ip_address,
  169. COUNT(*) as count
  170. FROM auth_log
  171. WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
  172. GROUP BY ip_address
  173. ORDER BY count DESC
  174. LIMIT ?
  175. `;
  176. return await (0, database_1.executeQuery)(query, [limit]);
  177. }
  178. static async create(authLog) {
  179. const query = `
  180. INSERT INTO auth_log (clientid, username, ip_address, operation_type, result, reason, topic)
  181. VALUES (?, ?, ?, ?, ?, ?, ?)
  182. `;
  183. const params = [
  184. authLog.clientid,
  185. authLog.username,
  186. authLog.ip_address,
  187. authLog.operation_type,
  188. authLog.result,
  189. authLog.reason || null,
  190. authLog.topic || null
  191. ];
  192. const result = await (0, database_1.executeQuery)(query, params);
  193. return { ...authLog, id: result.insertId, created_at: new Date() };
  194. }
  195. static async search(searchTerm, limit, offset) {
  196. let query = `
  197. SELECT * FROM auth_log
  198. WHERE clientid LIKE ? OR username LIKE ? OR ip_address LIKE ? OR operation_type LIKE ? OR result LIKE ? OR reason LIKE ? OR topic LIKE ?
  199. ORDER BY created_at DESC
  200. `;
  201. const params = [
  202. `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`,
  203. `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`,
  204. `%${searchTerm}%`
  205. ];
  206. if (limit !== undefined) {
  207. query += ' LIMIT ?';
  208. params.push(limit);
  209. if (offset !== undefined) {
  210. query += ' OFFSET ?';
  211. params.push(offset);
  212. }
  213. }
  214. return await (0, database_1.executeQuery)(query, params);
  215. }
  216. static async getSearchCount(searchTerm) {
  217. const query = `
  218. SELECT COUNT(*) as count
  219. FROM auth_log
  220. WHERE clientid LIKE ? OR username LIKE ? OR ip_address LIKE ? OR operation_type LIKE ? OR result LIKE ? OR reason LIKE ? OR topic LIKE ?
  221. `;
  222. const params = [
  223. `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`,
  224. `%${searchTerm}%`, `%${searchTerm}%`, `%${searchTerm}%`,
  225. `%${searchTerm}%`
  226. ];
  227. const result = await (0, database_1.executeQuery)(query, params);
  228. return result[0].count;
  229. }
  230. static async cleanup(daysToKeep = 30) {
  231. const query = 'DELETE FROM auth_log WHERE created_at < DATE_SUB(NOW(), INTERVAL ? DAY)';
  232. const result = await (0, database_1.executeQuery)(query, [daysToKeep]);
  233. return result.affectedRows;
  234. }
  235. static async getRecent(limit = 10) {
  236. const query = 'SELECT * FROM auth_log ORDER BY created_at DESC LIMIT ?';
  237. return await (0, database_1.executeQuery)(query, [limit]);
  238. }
  239. static async cleanupOldLogs(daysToKeep = 30) {
  240. return await this.cleanup(daysToKeep);
  241. }
  242. static async getFullStats() {
  243. try {
  244. const totalQuery = 'SELECT COUNT(*) as count FROM auth_log';
  245. const totalResult = await (0, database_1.executeQuery)(totalQuery);
  246. const total = totalResult[0].count;
  247. const resultQuery = `
  248. SELECT
  249. result,
  250. COUNT(*) as count
  251. FROM auth_log
  252. GROUP BY result
  253. `;
  254. const resultStats = await (0, database_1.executeQuery)(resultQuery);
  255. let success = 0;
  256. let failure = 0;
  257. resultStats.forEach((stat) => {
  258. if (stat.result === 'success') {
  259. success = stat.count;
  260. }
  261. else if (stat.result === 'failure') {
  262. failure = stat.count;
  263. }
  264. });
  265. const operationQuery = `
  266. SELECT
  267. operation_type,
  268. COUNT(*) as count
  269. FROM auth_log
  270. GROUP BY operation_type
  271. `;
  272. const operationStats = await (0, database_1.executeQuery)(operationQuery);
  273. const byOperationType = {};
  274. operationStats.forEach((stat) => {
  275. byOperationType[stat.operation_type] = stat.count;
  276. });
  277. const todayQuery = `
  278. SELECT COUNT(*) as count
  279. FROM auth_log
  280. WHERE DATE(created_at) = CURDATE()
  281. `;
  282. const todayResult = await (0, database_1.executeQuery)(todayQuery);
  283. const today = todayResult[0].count;
  284. const weekQuery = `
  285. SELECT COUNT(*) as count
  286. FROM auth_log
  287. WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
  288. `;
  289. const weekResult = await (0, database_1.executeQuery)(weekQuery);
  290. const week = weekResult[0].count;
  291. const monthQuery = `
  292. SELECT COUNT(*) as count
  293. FROM auth_log
  294. WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
  295. `;
  296. const monthResult = await (0, database_1.executeQuery)(monthQuery);
  297. const month = monthResult[0].count;
  298. return {
  299. total,
  300. success,
  301. failure,
  302. byOperationType,
  303. byTimeRange: {
  304. today,
  305. week,
  306. month
  307. }
  308. };
  309. }
  310. catch (error) {
  311. console.error('获取认证日志统计信息失败:', error);
  312. return {
  313. total: 0,
  314. success: 0,
  315. failure: 0,
  316. byOperationType: {},
  317. byTimeRange: {
  318. today: 0,
  319. week: 0,
  320. month: 0
  321. }
  322. };
  323. }
  324. }
  325. static async getOperationStats(startTime, endTime) {
  326. return await this.getOperationTypeStats(startTime, endTime);
  327. }
  328. static async getCountByMultipleConditions(conditions, startTime, endTime, fuzzyFields) {
  329. let query = 'SELECT COUNT(*) as count FROM auth_log WHERE 1=1';
  330. const params = [];
  331. for (const [key, value] of Object.entries(conditions)) {
  332. if (value !== undefined && value !== null) {
  333. if (fuzzyFields && fuzzyFields.includes(key)) {
  334. query += ` AND ${key} LIKE ?`;
  335. params.push(`%${value}%`);
  336. }
  337. else if (Array.isArray(value)) {
  338. const placeholders = value.map(() => '?').join(', ');
  339. query += ` AND ${key} IN (${placeholders})`;
  340. params.push(...value);
  341. }
  342. else {
  343. query += ` AND ${key} = ?`;
  344. params.push(value);
  345. }
  346. }
  347. }
  348. if (startTime && endTime) {
  349. query += ' AND created_at BETWEEN ? AND ?';
  350. params.push(startTime, endTime);
  351. }
  352. const result = await (0, database_1.executeQuery)(query, params);
  353. return result[0].count;
  354. }
  355. static async getByMultipleConditions(conditions, startTime, endTime, limit, offset, fuzzyFields) {
  356. let query = 'SELECT * FROM auth_log WHERE 1=1';
  357. const params = [];
  358. for (const [key, value] of Object.entries(conditions)) {
  359. if (value !== undefined && value !== null) {
  360. if (fuzzyFields && fuzzyFields.includes(key)) {
  361. query += ` AND ${key} LIKE ?`;
  362. params.push(`%${value}%`);
  363. }
  364. else if (Array.isArray(value)) {
  365. const placeholders = value.map(() => '?').join(', ');
  366. query += ` AND ${key} IN (${placeholders})`;
  367. params.push(...value);
  368. }
  369. else {
  370. query += ` AND ${key} = ?`;
  371. params.push(value);
  372. }
  373. }
  374. }
  375. if (startTime && endTime) {
  376. query += ' AND created_at BETWEEN ? AND ?';
  377. params.push(startTime, endTime);
  378. }
  379. query += ' ORDER BY created_at DESC';
  380. if (limit !== undefined && offset !== undefined) {
  381. query += ' LIMIT ? OFFSET ?';
  382. params.push(limit, offset);
  383. }
  384. return await (0, database_1.executeQuery)(query, params);
  385. }
  386. static async getCountByTimeRange(startTime, endTime) {
  387. const query = 'SELECT COUNT(*) as count FROM auth_log WHERE created_at BETWEEN ? AND ?';
  388. const result = await (0, database_1.executeQuery)(query, [startTime, endTime]);
  389. return result[0].count;
  390. }
  391. static async getCountByResult(result) {
  392. const query = 'SELECT COUNT(*) as count FROM auth_log WHERE result = ?';
  393. const queryResult = await (0, database_1.executeQuery)(query, [result]);
  394. return queryResult[0].count;
  395. }
  396. static async getCountByIpAddress(ipAddress) {
  397. const query = 'SELECT COUNT(*) as count FROM auth_log WHERE ip_address = ?';
  398. const result = await (0, database_1.executeQuery)(query, [ipAddress]);
  399. return result[0].count;
  400. }
  401. static async getCountByOperationType(operationType) {
  402. const query = 'SELECT COUNT(*) as count FROM auth_log WHERE operation_type = ?';
  403. const result = await (0, database_1.executeQuery)(query, [operationType]);
  404. return result[0].count;
  405. }
  406. static async getCountByUsername(username) {
  407. const query = 'SELECT COUNT(*) as count FROM auth_log WHERE username = ?';
  408. const result = await (0, database_1.executeQuery)(query, [username]);
  409. return result[0].count;
  410. }
  411. static async getByIpAddress(ipAddress, limit, offset) {
  412. let query = 'SELECT * FROM auth_log WHERE ip_address = ? ORDER BY created_at DESC';
  413. const params = [ipAddress];
  414. if (limit !== undefined && offset !== undefined) {
  415. query += ' LIMIT ? OFFSET ?';
  416. params.push(limit, offset);
  417. }
  418. return await (0, database_1.executeQuery)(query, params);
  419. }
  420. static async getCountByClientid(clientid) {
  421. const query = 'SELECT COUNT(*) as count FROM auth_log WHERE clientid = ?';
  422. const result = await (0, database_1.executeQuery)(query, [clientid]);
  423. return result[0].count;
  424. }
  425. }
  426. exports.AuthLogModel = AuthLogModel;
  427. //# sourceMappingURL=authLog.js.map