toolsCallLog.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const express = require("express");
  2. const router = express.Router();
  3. const db = require("../config/database");
  4. // 获取工具调用日志列表
  5. router.get("/", async (req, res) => {
  6. try {
  7. const { page = 1, pageSize = 10, user, mcp_tools_name, status, start_time, end_time } = req.query;
  8. const offset = (page - 1) * pageSize;
  9. // 构建查询条件
  10. let whereConditions = [];
  11. let queryParams = [];
  12. if (user) {
  13. whereConditions.push("user LIKE ?");
  14. queryParams.push(`%${user}%`);
  15. }
  16. if (mcp_tools_name) {
  17. whereConditions.push("mcp_tools_name LIKE ?");
  18. queryParams.push(`%${mcp_tools_name}%`);
  19. }
  20. if (status) {
  21. whereConditions.push("status = ?");
  22. queryParams.push(status);
  23. }
  24. if (start_time) {
  25. whereConditions.push("call_timestamp >= ?");
  26. queryParams.push(parseInt(start_time));
  27. }
  28. if (end_time) {
  29. whereConditions.push("call_timestamp <= ?");
  30. queryParams.push(parseInt(end_time));
  31. }
  32. const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(" AND ")}` : "";
  33. // 获取总数
  34. const countQuery = `SELECT COUNT(*) as total FROM tools_call_log ${whereClause}`;
  35. const countResult = await db.query(countQuery, queryParams);
  36. const total = countResult[0].total;
  37. // 获取列表数据
  38. const listQuery = `
  39. SELECT id, user, token, mcp_tools_name, status, fail_msg,
  40. call_timestamp, finish_timestamp
  41. FROM tools_call_log
  42. ${whereClause}
  43. ORDER BY call_timestamp DESC
  44. LIMIT ? OFFSET ?
  45. `;
  46. const listParams = [...queryParams, parseInt(pageSize), offset];
  47. const list = await db.query(listQuery, listParams);
  48. res.json({
  49. success: true,
  50. data: {
  51. list,
  52. total,
  53. page: parseInt(page),
  54. pageSize: parseInt(pageSize),
  55. },
  56. });
  57. } catch (error) {
  58. console.error("获取工具调用日志列表失败:", error);
  59. res.status(500).json({
  60. success: false,
  61. error: "获取工具调用日志列表失败",
  62. });
  63. }
  64. });
  65. // 获取工具调用日志详情
  66. router.get("/:id", async (req, res) => {
  67. try {
  68. const { id } = req.params;
  69. const query = `
  70. SELECT id, user, token, mcp_tools_name, request_params, status,
  71. response, fail_msg, call_timestamp, finish_timestamp
  72. FROM tools_call_log
  73. WHERE id = ?
  74. `;
  75. const result = await db.query(query, [id]);
  76. if (result.length === 0) {
  77. return res.status(404).json({
  78. success: false,
  79. error: "工具调用日志不存在",
  80. });
  81. }
  82. res.json({
  83. success: true,
  84. data: result[0],
  85. });
  86. } catch (error) {
  87. console.error("获取工具调用日志详情失败:", error);
  88. res.status(500).json({
  89. success: false,
  90. error: "获取工具调用日志详情失败",
  91. });
  92. }
  93. });
  94. module.exports = router;