| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- const express = require("express");
- const router = express.Router();
- const db = require("../config/database");
- // 获取工具调用日志列表
- router.get("/", async (req, res) => {
- try {
- const { page = 1, pageSize = 10, user, mcp_tools_name, status, start_time, end_time } = req.query;
- const offset = (page - 1) * pageSize;
- // 构建查询条件
- let whereConditions = [];
- let queryParams = [];
- if (user) {
- whereConditions.push("user LIKE ?");
- queryParams.push(`%${user}%`);
- }
- if (mcp_tools_name) {
- whereConditions.push("mcp_tools_name LIKE ?");
- queryParams.push(`%${mcp_tools_name}%`);
- }
- if (status) {
- whereConditions.push("status = ?");
- queryParams.push(status);
- }
- if (start_time) {
- whereConditions.push("call_timestamp >= ?");
- queryParams.push(parseInt(start_time));
- }
- if (end_time) {
- whereConditions.push("call_timestamp <= ?");
- queryParams.push(parseInt(end_time));
- }
- const whereClause = whereConditions.length > 0 ? `WHERE ${whereConditions.join(" AND ")}` : "";
- // 获取总数
- const countQuery = `SELECT COUNT(*) as total FROM tools_call_log ${whereClause}`;
- const countResult = await db.query(countQuery, queryParams);
- const total = countResult[0].total;
- // 获取列表数据
- const listQuery = `
- SELECT id, user, token, mcp_tools_name, status, fail_msg,
- call_timestamp, finish_timestamp
- FROM tools_call_log
- ${whereClause}
- ORDER BY call_timestamp DESC
- LIMIT ? OFFSET ?
- `;
-
- const listParams = [...queryParams, parseInt(pageSize), offset];
- const list = await db.query(listQuery, listParams);
- res.json({
- success: true,
- data: {
- list,
- total,
- page: parseInt(page),
- pageSize: parseInt(pageSize),
- },
- });
- } catch (error) {
- console.error("获取工具调用日志列表失败:", error);
- res.status(500).json({
- success: false,
- error: "获取工具调用日志列表失败",
- });
- }
- });
- // 获取工具调用日志详情
- router.get("/:id", async (req, res) => {
- try {
- const { id } = req.params;
-
- const query = `
- SELECT id, user, token, mcp_tools_name, request_params, status,
- response, fail_msg, call_timestamp, finish_timestamp
- FROM tools_call_log
- WHERE id = ?
- `;
-
- const result = await db.query(query, [id]);
-
- if (result.length === 0) {
- return res.status(404).json({
- success: false,
- error: "工具调用日志不存在",
- });
- }
- res.json({
- success: true,
- data: result[0],
- });
- } catch (error) {
- console.error("获取工具调用日志详情失败:", error);
- res.status(500).json({
- success: false,
- error: "获取工具调用日志详情失败",
- });
- }
- });
- module.exports = router;
|