feat: 增加日志

This commit is contained in:
李如威 2025-09-29 11:43:24 +08:00
parent 125722c87d
commit 45fab652a0
3 changed files with 49 additions and 12 deletions

View File

@ -460,8 +460,17 @@ def create_app():
@app.get("/api/logs", tags=["Log"], description="日志列表", response_model=TableResponse) @app.get("/api/logs", tags=["Log"], description="日志列表", response_model=TableResponse)
async def log_list(p:dict = Depends(pagination_query), user: DB.User = Depends(get_current_user)): async def log_list(p:dict = Depends(pagination_query), user: DB.User = Depends(get_current_user)):
page, page_size = p["page"], p["page_size"] page, page_size = p["page"], p["page_size"]
users = await DB.User.all()
res = await DB.WorkLogs.all().order_by('-id').limit(page_size).offset((page - 1) * page_size) res = await DB.WorkLogs.all().order_by('-id').limit(page_size).offset((page - 1) * page_size)
return TableResponse(data=[x.json() for x in res], total=len(res)) count = await DB.WorkLogs.all().count()
data = []
for l in res:
u = next((x for x in users if x.id == l.user_id), None)
obj = l.json()
if u:
obj["user"] = u.username
data.append(obj)
return TableResponse(data=data, total=count)
return app return app

View File

@ -0,0 +1,24 @@
import { Collapse, Modal } from "antd"
import { useState } from "react"
const LogDetail = ({ record }) => {
const [open, setOpen] = useState(false)
const items = record?.result?.logs?.map((e, i) => ({
key: i + '',
label: e?.title,
children: <p style={e?.result?.err ? { color: 'red' } : {}}> {e?.result?.err || e?.result?.out }</p>
}))
console.log(items)
return <>
<a onClick={() => setOpen(true)}>查看</a>
<Modal width={800} open={open} onCancel={() => setOpen(false)} title="日志详情">
<Collapse items={items}/>
</Modal>
</>
}
export default LogDetail;

View File

@ -1,8 +1,9 @@
import { ProTable } from "@ant-design/pro-components"; import { ProTable } from "@ant-design/pro-components";
import { Card } from "antd"; import { Card, Space, Typography } from "antd";
import { useRef } from "react"; import { useRef } from "react";
import { formatSeconds } from "../Flow"; import { formatSeconds } from "../Flow";
import { tableList } from "../../api/log"; import { tableList } from "../../api/log";
import LogDetail from "./Detail";
const LogPage = () => { const LogPage = () => {
const actionRef = useRef(null); const actionRef = useRef(null);
@ -22,21 +23,21 @@ const LogPage = () => {
} }
}, },
{ {
dataIndex: 'last_user', dataIndex: 'user',
title: '执行用户', title: '执行用户',
}, },
{ {
dataIndex: 'last_run_time', dataIndex: 'create_time',
title: '执行时间', title: '执行时间',
render: (_, record) => record?.last_run_time ? new Date(record?.last_run_time * 1000).toLocaleString() : '-' render: (_, record) => record?.create_time ? new Date(record?.create_time * 1000).toLocaleString() : '-'
}, },
{ {
dataIndex: 'time', dataIndex: 'time',
title: '执行耗时', title: '执行耗时',
render: (_, record) => record?.time ? formatSeconds(record?.time) : '-', render: (_, record) => record?.result?.time ? formatSeconds(record?.result?.time) : '-',
}, },
{ {
dataIndex: 'last_status', dataIndex: 'status',
title: '执行结果', title: '执行结果',
valueType: 'select', valueType: 'select',
valueEnum: { valueEnum: {
@ -53,14 +54,17 @@ const LogPage = () => {
width: 60, width: 60,
fixed: 'right', fixed: 'right',
render: (_, record) => [ render: (_, record) => [
<a>查看</a> <LogDetail record={record} key='detail' />
] ]
} }
]; ];
const onRequest = async (params) => { const onRequest = async ({current: page, pageSize: page_size,...params}) => {
const res = await tableList(params) const res = await tableList({ page, page_size, ...params })
console.log(res); let index = (page - 1) * page_size + 1;
return {}; res?.data?.forEach((e, i) => {
e.index = index + i
});
return res;
} }
return <Card title="操作日志"> return <Card title="操作日志">
<ProTable <ProTable