mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-08 05:29:51 +00:00
Save and display execution records
This commit is contained in:
parent
03b2a9da66
commit
2d10fa0218
@ -2,6 +2,8 @@ package domain
|
||||
|
||||
import "time"
|
||||
|
||||
var ValidityDuration = time.Hour * 24 * 10
|
||||
|
||||
type Certificate struct {
|
||||
Meta
|
||||
SAN string `json:"san"`
|
||||
|
22
internal/domain/workflow_run_log.go
Normal file
22
internal/domain/workflow_run_log.go
Normal file
@ -0,0 +1,22 @@
|
||||
package domain
|
||||
|
||||
type RunLogOutput struct {
|
||||
Time string `json:"time"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type RunLog struct {
|
||||
NodeName string `json:"nodeName"`
|
||||
Error string `json:"error"`
|
||||
Outputs []RunLogOutput `json:"outputs"`
|
||||
}
|
||||
|
||||
type WorkflowRunLog struct {
|
||||
Meta
|
||||
Workflow string `json:"workflow"`
|
||||
Log []RunLog `json:"log"`
|
||||
Succeed bool `json:"succeed"`
|
||||
Error string `json:"error"`
|
||||
}
|
@ -5,6 +5,7 @@ import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/pocketbase/pocketbase/models"
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/utils/app"
|
||||
)
|
||||
@ -15,6 +16,21 @@ func NewWorkflowRepository() *WorkflowRepository {
|
||||
return &WorkflowRepository{}
|
||||
}
|
||||
|
||||
func (w *WorkflowRepository) SaveRunLog(ctx context.Context, log *domain.WorkflowRunLog) error {
|
||||
collection, err := app.GetApp().Dao().FindCollectionByNameOrId("workflow_run_log")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
record := models.NewRecord(collection)
|
||||
|
||||
record.Set("workflow", log.Workflow)
|
||||
record.Set("log", log.Log)
|
||||
record.Set("succeed", log.Succeed)
|
||||
record.Set("error", log.Error)
|
||||
|
||||
return app.GetApp().Dao().SaveRecord(record)
|
||||
}
|
||||
|
||||
func (w *WorkflowRepository) Get(ctx context.Context, id string) (*domain.Workflow, error) {
|
||||
record, err := app.GetApp().Dao().FindRecordById("workflow", id)
|
||||
if err != nil {
|
||||
|
@ -43,8 +43,8 @@ func (w *WorkflowOutputRepository) Get(ctx context.Context, nodeId string) (*dom
|
||||
rs := &domain.WorkflowOutput{
|
||||
Meta: domain.Meta{
|
||||
Id: record.GetId(),
|
||||
Created: record.GetTime("created"),
|
||||
Updated: record.GetTime("updated"),
|
||||
Created: record.GetCreated().Time(),
|
||||
Updated: record.GetUpdated().Time(),
|
||||
},
|
||||
Workflow: record.GetString("workflow"),
|
||||
NodeId: record.GetString("nodeId"),
|
||||
@ -73,15 +73,15 @@ func (w *WorkflowOutputRepository) GetCertificate(ctx context.Context, nodeId st
|
||||
rs := &domain.Certificate{
|
||||
Meta: domain.Meta{
|
||||
Id: record.GetId(),
|
||||
Created: record.GetTime("created"),
|
||||
Updated: record.GetTime("updated"),
|
||||
Created: record.GetDateTime("created").Time(),
|
||||
Updated: record.GetDateTime("updated").Time(),
|
||||
},
|
||||
Certificate: record.GetString("certificate"),
|
||||
PrivateKey: record.GetString("privateKey"),
|
||||
IssuerCertificate: record.GetString("issuerCertificate"),
|
||||
SAN: record.GetString("san"),
|
||||
Output: record.GetString("output"),
|
||||
ExpireAt: record.GetTime("expireAt"),
|
||||
ExpireAt: record.GetDateTime("expireAt").Time(),
|
||||
CertUrl: record.GetString("certUrl"),
|
||||
CertStableUrl: record.GetString("certStableUrl"),
|
||||
Workflow: record.GetString("workflow"),
|
||||
|
@ -2,6 +2,7 @@ package nodeprocessor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/usual2970/certimate/internal/applicant"
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
@ -45,9 +46,17 @@ func (a *applyNode) Run(ctx context.Context) error {
|
||||
}
|
||||
|
||||
if output != nil && output.Succeed {
|
||||
a.AddOutput(ctx, a.node.Name, "已申请过")
|
||||
cert, err := a.outputRepo.GetCertificate(ctx, a.node.Id)
|
||||
if err != nil {
|
||||
a.AddOutput(ctx, a.node.Name, "获取证书失败", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if time.Until(cert.ExpireAt) > domain.ValidityDuration {
|
||||
a.AddOutput(ctx, a.node.Name, "已申请过证书,且证书在有效期内")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// 获取Applicant
|
||||
apply, err := applicant.GetWithApplyNode(a.node)
|
||||
@ -65,12 +74,18 @@ func (a *applyNode) Run(ctx context.Context) error {
|
||||
a.AddOutput(ctx, a.node.Name, "申请成功")
|
||||
|
||||
// 记录申请结果
|
||||
// 保持一个节点只有一个输出
|
||||
outputId := ""
|
||||
if output != nil {
|
||||
outputId = output.Id
|
||||
}
|
||||
output = &domain.WorkflowOutput{
|
||||
Workflow: GetWorkflowId(ctx),
|
||||
NodeId: a.node.Id,
|
||||
Node: a.node,
|
||||
Succeed: true,
|
||||
Output: a.node.Output,
|
||||
Meta: domain.Meta{Id: outputId},
|
||||
}
|
||||
|
||||
cert, err := x509.ParseCertificateFromPEM(certificate.Certificate)
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/utils/xtime"
|
||||
)
|
||||
|
||||
type conditionNode struct {
|
||||
@ -21,7 +20,7 @@ func NewConditionNode(node *domain.WorkflowNode) *conditionNode {
|
||||
|
||||
// 条件节点没有任何操作
|
||||
func (c *conditionNode) Run(ctx context.Context) error {
|
||||
c.AddOutput(ctx, xtime.BeijingTimeStr(),
|
||||
c.AddOutput(ctx,
|
||||
c.node.Name,
|
||||
"完成",
|
||||
)
|
||||
|
@ -32,10 +32,6 @@ func (d *deployNode) Run(ctx context.Context) error {
|
||||
d.AddOutput(ctx, d.node.Name, "查询部署记录失败", err.Error())
|
||||
return err
|
||||
}
|
||||
if output != nil && output.Succeed {
|
||||
d.AddOutput(ctx, d.node.Name, "已部署过")
|
||||
return nil
|
||||
}
|
||||
// 获取部署对象
|
||||
// 获取证书
|
||||
certSource := d.node.GetConfigString("certificate")
|
||||
@ -52,6 +48,15 @@ func (d *deployNode) Run(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// 未部署过,开始部署
|
||||
// 部署过但是证书更新了,重新部署
|
||||
// 部署过且证书未更新,直接返回
|
||||
|
||||
if d.deployed(output) && cert.Created.Before(output.Updated) {
|
||||
d.AddOutput(ctx, d.node.Name, "已部署过且证书未更新")
|
||||
return nil
|
||||
}
|
||||
|
||||
accessRepo := repository.NewAccessRepository()
|
||||
access, err := accessRepo.GetById(context.Background(), d.node.GetConfigString("access"))
|
||||
if err != nil {
|
||||
@ -86,11 +91,16 @@ func (d *deployNode) Run(ctx context.Context) error {
|
||||
d.AddOutput(ctx, d.node.Name, "部署成功")
|
||||
|
||||
// 记录部署结果
|
||||
outputId := ""
|
||||
if output != nil {
|
||||
outputId = output.Id
|
||||
}
|
||||
output = &domain.WorkflowOutput{
|
||||
Workflow: GetWorkflowId(ctx),
|
||||
NodeId: d.node.Id,
|
||||
Node: d.node,
|
||||
Succeed: true,
|
||||
Meta: domain.Meta{Id: outputId},
|
||||
}
|
||||
|
||||
if err := d.outputRepo.Save(ctx, output, nil, nil); err != nil {
|
||||
@ -102,3 +112,7 @@ func (d *deployNode) Run(ctx context.Context) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *deployNode) deployed(output *domain.WorkflowOutput) bool {
|
||||
return output != nil && output.Succeed
|
||||
}
|
||||
|
@ -8,51 +8,38 @@ import (
|
||||
"github.com/usual2970/certimate/internal/utils/xtime"
|
||||
)
|
||||
|
||||
type RunLog struct {
|
||||
NodeName string `json:"node_name"`
|
||||
Err string `json:"err"`
|
||||
Outputs []RunLogOutput `json:"outputs"`
|
||||
}
|
||||
|
||||
type RunLogOutput struct {
|
||||
Time string `json:"time"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
type NodeProcessor interface {
|
||||
Run(ctx context.Context) error
|
||||
Log(ctx context.Context) *RunLog
|
||||
Log(ctx context.Context) *domain.RunLog
|
||||
AddOutput(ctx context.Context, title, content string, err ...string)
|
||||
}
|
||||
|
||||
type Logger struct {
|
||||
log *RunLog
|
||||
log *domain.RunLog
|
||||
}
|
||||
|
||||
func NewLogger(node *domain.WorkflowNode) *Logger {
|
||||
return &Logger{
|
||||
log: &RunLog{
|
||||
log: &domain.RunLog{
|
||||
NodeName: node.Name,
|
||||
Outputs: make([]RunLogOutput, 0),
|
||||
Outputs: make([]domain.RunLogOutput, 0),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Logger) Log(ctx context.Context) *RunLog {
|
||||
func (l *Logger) Log(ctx context.Context) *domain.RunLog {
|
||||
return l.log
|
||||
}
|
||||
|
||||
func (l *Logger) AddOutput(ctx context.Context, title, content string, err ...string) {
|
||||
output := RunLogOutput{
|
||||
output := domain.RunLogOutput{
|
||||
Time: xtime.BeijingTimeStr(),
|
||||
Title: title,
|
||||
Content: content,
|
||||
}
|
||||
if len(err) > 0 {
|
||||
output.Error = err[0]
|
||||
l.log.Err = err[0]
|
||||
l.log.Error = err[0]
|
||||
}
|
||||
l.log.Outputs = append(l.log.Outputs, output)
|
||||
}
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
|
||||
"github.com/usual2970/certimate/internal/domain"
|
||||
"github.com/usual2970/certimate/internal/utils/xtime"
|
||||
)
|
||||
|
||||
type startNode struct {
|
||||
@ -21,7 +20,7 @@ func NewStartNode(node *domain.WorkflowNode) *startNode {
|
||||
|
||||
// 开始节点没有任何操作
|
||||
func (s *startNode) Run(ctx context.Context) error {
|
||||
s.AddOutput(ctx, xtime.BeijingTimeStr(),
|
||||
s.AddOutput(ctx,
|
||||
s.node.Name,
|
||||
"完成",
|
||||
)
|
||||
|
@ -8,15 +8,20 @@ import (
|
||||
|
||||
type workflowProcessor struct {
|
||||
workflow *domain.Workflow
|
||||
logs []RunLog
|
||||
logs []domain.RunLog
|
||||
}
|
||||
|
||||
func NewWorkflowProcessor(workflow *domain.Workflow) *workflowProcessor {
|
||||
return &workflowProcessor{
|
||||
workflow: workflow,
|
||||
logs: make([]domain.RunLog, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func (w *workflowProcessor) Log(ctx context.Context) []domain.RunLog {
|
||||
return w.logs
|
||||
}
|
||||
|
||||
func (w *workflowProcessor) Run(ctx context.Context) error {
|
||||
ctx = WithWorkflowId(ctx, w.workflow.Id)
|
||||
return w.runNode(ctx, w.workflow.Content)
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
|
||||
type WorkflowRepository interface {
|
||||
Get(ctx context.Context, id string) (*domain.Workflow, error)
|
||||
SaveRunLog(ctx context.Context, log *domain.WorkflowRunLog) error
|
||||
}
|
||||
|
||||
type WorkflowService struct {
|
||||
@ -43,11 +44,29 @@ func (s *WorkflowService) Run(ctx context.Context, req *domain.WorkflowRunReq) e
|
||||
|
||||
processor := nodeprocessor.NewWorkflowProcessor(workflow)
|
||||
if err := processor.Run(ctx); err != nil {
|
||||
log := &domain.WorkflowRunLog{
|
||||
Workflow: workflow.Id,
|
||||
Log: processor.Log(ctx),
|
||||
Succeed: false,
|
||||
Error: err.Error(),
|
||||
}
|
||||
if err := s.repo.SaveRunLog(ctx, log); err != nil {
|
||||
app.GetApp().Logger().Error("failed to save run log", "err", err)
|
||||
}
|
||||
return fmt.Errorf("failed to run workflow: %w", err)
|
||||
}
|
||||
|
||||
// 保存执行日志
|
||||
|
||||
log := &domain.WorkflowRunLog{
|
||||
Workflow: workflow.Id,
|
||||
Log: processor.Log(ctx),
|
||||
Succeed: true,
|
||||
}
|
||||
if err := s.repo.SaveRunLog(ctx, log); err != nil {
|
||||
app.GetApp().Logger().Error("failed to save run log", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -3,23 +3,21 @@ import { ColumnDef, flexRender, getCoreRowModel, getPaginationRowModel, Paginati
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { Button } from "../ui/button";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
interface DataTableProps<TData extends { id: string }, TValue> {
|
||||
columns: ColumnDef<TData, TValue>[];
|
||||
data: TData[];
|
||||
pageCount: number;
|
||||
onPageChange?: (pageIndex: number, pageSize?: number) => Promise<void>;
|
||||
onRowClick?: (id: string) => void;
|
||||
}
|
||||
|
||||
export function DataTable<TData extends { id: string }, TValue>({ columns, data, onPageChange, pageCount }: DataTableProps<TData, TValue>) {
|
||||
export function DataTable<TData extends { id: string }, TValue>({ columns, data, onPageChange, pageCount, onRowClick }: DataTableProps<TData, TValue>) {
|
||||
const [{ pageIndex, pageSize }, setPagination] = useState<PaginationState>({
|
||||
pageIndex: 0,
|
||||
pageSize: 10,
|
||||
});
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
const pagination = {
|
||||
pageIndex,
|
||||
pageSize,
|
||||
@ -43,11 +41,11 @@ export function DataTable<TData extends { id: string }, TValue>({ columns, data,
|
||||
}, [pageIndex]);
|
||||
|
||||
const handleRowClick = (id: string) => {
|
||||
navigate(`/workflow/detail?id=${id}`);
|
||||
onRowClick?.(id);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div>
|
||||
<div className="rounded-md">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
@ -88,14 +86,19 @@ export function DataTable<TData extends { id: string }, TValue>({ columns, data,
|
||||
</div>
|
||||
<div className="flex items-center justify-end mt-5">
|
||||
<div className="flex items-center space-x-2 dark:text-stone-200">
|
||||
{table.getCanPreviousPage() && (
|
||||
<Button variant="outline" size="sm" onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()}>
|
||||
上一页
|
||||
</Button>
|
||||
)}
|
||||
|
||||
{table.getCanNextPage && (
|
||||
<Button variant="outline" size="sm" onClick={() => table.nextPage()} disabled={!table.getCanNextPage()}>
|
||||
下一页
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
92
ui/src/components/workflow/WorkflowLog.tsx
Normal file
92
ui/src/components/workflow/WorkflowLog.tsx
Normal file
@ -0,0 +1,92 @@
|
||||
import { WorkflowRunLog } from "@/domain/workflow";
|
||||
import { logs } from "@/repository/workflow";
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
import { useState } from "react";
|
||||
import { DataTable } from "./DataTable";
|
||||
import { useSearchParams } from "react-router-dom";
|
||||
import { Check, X } from "lucide-react";
|
||||
import WorkflowLogDetail from "./WorkflowLogDetail";
|
||||
|
||||
const WorkflowLog = () => {
|
||||
const [data, setData] = useState<WorkflowRunLog[]>([]);
|
||||
const [pageCount, setPageCount] = useState<number>(0);
|
||||
|
||||
const [searchParams] = useSearchParams();
|
||||
const id = searchParams.get("id");
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
const [selectedLog, setSelectedLog] = useState<WorkflowRunLog>();
|
||||
|
||||
const fetchData = async (page: number, pageSize?: number) => {
|
||||
const resp = await logs({ page: page, perPage: pageSize, id: id ?? "" });
|
||||
setData(resp.items);
|
||||
setPageCount(resp.totalPages);
|
||||
};
|
||||
|
||||
const columns: ColumnDef<WorkflowRunLog>[] = [
|
||||
{
|
||||
accessorKey: "succeed",
|
||||
header: "状态",
|
||||
cell: ({ row }) => {
|
||||
const succeed: boolean = row.getValue("succeed");
|
||||
if (succeed) {
|
||||
return (
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="text-white bg-green-500 w-8 h-8 rounded-full flex items-center justify-center">
|
||||
<Check size={18} />
|
||||
</div>
|
||||
<div className="text-sone-700">通过</div>
|
||||
</div>
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
<div className="flex items-center space-x-2">
|
||||
<div className="text-white bg-red-500 w-8 h-8 rounded-full flex items-center justify-center">
|
||||
<X size={18} />
|
||||
</div>
|
||||
<div className="text-stone-700">失败</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "error",
|
||||
header: "原因",
|
||||
cell: ({ row }) => {
|
||||
let error: string = row.getValue("error");
|
||||
if (!error) {
|
||||
error = "";
|
||||
}
|
||||
return <div className="min-w-[250px] truncate">{error}</div>;
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "created",
|
||||
header: "时间",
|
||||
cell: ({ row }) => {
|
||||
const date: string = row.getValue("created");
|
||||
return new Date(date).toLocaleString();
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const handleRowClick = (id: string) => {
|
||||
setOpen(true);
|
||||
const log = data.find((item) => item.id === id);
|
||||
setSelectedLog(log);
|
||||
};
|
||||
return (
|
||||
<div className="w-full md:w-[960px]">
|
||||
<div>
|
||||
<div className="text-muted-foreground mb-5">日志</div>
|
||||
<DataTable columns={columns} data={data} onPageChange={fetchData} pageCount={pageCount} onRowClick={handleRowClick} />
|
||||
</div>
|
||||
|
||||
<WorkflowLogDetail open={open} onOpenChange={setOpen} log={selectedLog} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default WorkflowLog;
|
||||
|
81
ui/src/components/workflow/WorkflowLogDetail.tsx
Normal file
81
ui/src/components/workflow/WorkflowLogDetail.tsx
Normal file
@ -0,0 +1,81 @@
|
||||
import { WorkflowOutput, WorkflowRunLog, WorkflowRunLogItem } from "@/domain/workflow";
|
||||
import { Sheet, SheetContent, SheetDescription, SheetHeader, SheetTitle } from "../ui/sheet";
|
||||
import { Check, X } from "lucide-react";
|
||||
|
||||
type WorkflowLogDetailProps = {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
log?: WorkflowRunLog;
|
||||
};
|
||||
const WorkflowLogDetail = ({ open, onOpenChange, log }: WorkflowLogDetailProps) => {
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||
<SheetContent className="sm:max-w-5xl">
|
||||
<SheetHeader>
|
||||
<SheetTitle>日志</SheetTitle>
|
||||
</SheetHeader>
|
||||
|
||||
<div className="flex flex-col">
|
||||
{log?.succeed ? (
|
||||
<div className="mt-5 flex justify-between bg-green-100 p-5 rounded-md items-center">
|
||||
<div className="flex space-x-2 items-center">
|
||||
<div className="w-8 h-8 bg-green-500 flex items-center justify-center rounded-full text-white">
|
||||
<Check size={18} />
|
||||
</div>
|
||||
<div className="text-stone-700">成功</div>
|
||||
</div>
|
||||
|
||||
<div className="text-muted-foreground">{new Date(log.created).toLocaleString()}</div>
|
||||
</div>
|
||||
) : (
|
||||
<div className="mt-5 flex justify-between bg-green-100 p-5 rounded-md items-center">
|
||||
<div className="flex space-x-2 items-center">
|
||||
<div className="w-8 h-8 bg-red-500 flex items-center justify-center rounded-full text-white">
|
||||
<X size={18} />
|
||||
</div>
|
||||
<div className="text-stone-700">失败</div>
|
||||
</div>
|
||||
|
||||
<div className="text-red-500">{log?.error}</div>
|
||||
|
||||
<div className="text-muted-foreground">{log?.created && new Date(log.created).toLocaleString()}</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="bg-black p-5 mt-5 rounded-md text-stone-200 flex flex-col space-y-3">
|
||||
{log?.log.map((item: WorkflowRunLogItem, i) => {
|
||||
return (
|
||||
<div key={i} className="flex flex-col space-y-2">
|
||||
<div className="">{item.nodeName}</div>
|
||||
<div className="flex flex-col space-y-1">
|
||||
{item.outputs.map((output: WorkflowOutput) => {
|
||||
return (
|
||||
<>
|
||||
<div className="flex text-sm space-x-2">
|
||||
<div>[{output.time}]</div>
|
||||
{output.error ? (
|
||||
<>
|
||||
<div className="text-red-500">{output.error}</div>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<div>{output.content}</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
};
|
||||
|
||||
export default WorkflowLogDetail;
|
||||
|
@ -3,6 +3,29 @@ import { nanoid } from "nanoid";
|
||||
import i18n from "@/i18n";
|
||||
import { deployTargets, KVType } from "./domain";
|
||||
|
||||
export type WorkflowRunLog = {
|
||||
id: string;
|
||||
workflow: string;
|
||||
log: WorkflowRunLogItem[];
|
||||
error: string;
|
||||
succeed: boolean;
|
||||
created: string;
|
||||
updated: string;
|
||||
};
|
||||
|
||||
export type WorkflowRunLogItem = {
|
||||
nodeName: string;
|
||||
error: string;
|
||||
outputs: WorkflowOutput[];
|
||||
};
|
||||
|
||||
export type WorkflowOutput = {
|
||||
time: string;
|
||||
title: string;
|
||||
content: string;
|
||||
error: string;
|
||||
};
|
||||
|
||||
export type Workflow = {
|
||||
id: string;
|
||||
name: string;
|
||||
@ -446,3 +469,4 @@ export const workflowNodeDropdownList: WorkflowwNodeDropdwonItem[] = [
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
|
@ -7,12 +7,14 @@ import { useToast } from "@/components/ui/use-toast";
|
||||
import End from "@/components/workflow/End";
|
||||
import NodeRender from "@/components/workflow/NodeRender";
|
||||
import WorkflowBaseInfoEditDialog from "@/components/workflow/WorkflowBaseInfoEditDialog";
|
||||
import WorkflowLog from "@/components/workflow/WorkflowLog";
|
||||
|
||||
import WorkflowProvider from "@/components/workflow/WorkflowProvider";
|
||||
import { allNodesValidated, WorkflowNode } from "@/domain/workflow";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { useWorkflowStore, WorkflowState } from "@/providers/workflow";
|
||||
import { ArrowLeft } from "lucide-react";
|
||||
import { useEffect, useMemo } from "react";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { useNavigate, useSearchParams } from "react-router-dom";
|
||||
|
||||
import { useShallow } from "zustand/shallow";
|
||||
@ -30,11 +32,17 @@ const WorkflowDetail = () => {
|
||||
|
||||
// 从 url 中获取 workflowId
|
||||
const [searchParams] = useSearchParams();
|
||||
const [locId, setLocId] = useState<string>("");
|
||||
const id = searchParams.get("id");
|
||||
|
||||
const [tab, setTab] = useState("workflow");
|
||||
|
||||
useEffect(() => {
|
||||
console.log(id);
|
||||
init(id ?? "");
|
||||
if (id) {
|
||||
setLocId(id);
|
||||
}
|
||||
}, [id]);
|
||||
|
||||
const navigate = useNavigate();
|
||||
@ -85,6 +93,13 @@ const WorkflowDetail = () => {
|
||||
save();
|
||||
};
|
||||
|
||||
const getTabCls = (tabName: string) => {
|
||||
if (tab === tabName) {
|
||||
return "text-primary border-primary";
|
||||
}
|
||||
return "border-transparent hover:text-primary hover:border-b-primary";
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<WorkflowProvider>
|
||||
@ -102,6 +117,26 @@ const WorkflowDetail = () => {
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-between space-x-5 text-muted-foreground text-lg h-full">
|
||||
<div
|
||||
className={cn("h-full flex items-center cursor-pointer border-b-2", getTabCls("workflow"))}
|
||||
onClick={() => {
|
||||
setTab("workflow");
|
||||
}}
|
||||
>
|
||||
<div>流程</div>
|
||||
</div>
|
||||
<div
|
||||
className={cn("h-full flex items-center cursor-pointer border-b-2", getTabCls("history"))}
|
||||
onClick={() => {
|
||||
setTab("history");
|
||||
}}
|
||||
>
|
||||
<div>历史</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="px-5 flex items-center space-x-3">
|
||||
<Show when={!!workflow.enabled}>
|
||||
<Show when={!!workflow.hasDraft} fallback={<Button variant={"secondary"}>立即执行</Button>}>
|
||||
@ -114,8 +149,15 @@ const WorkflowDetail = () => {
|
||||
<Switch className="dark:data-[state=unchecked]:bg-stone-400" checked={workflow.enabled ?? false} onCheckedChange={handleEnableChange} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<Show when={tab === "workflow"}>
|
||||
<div className=" flex flex-col items-center mt-8">{elements}</div>
|
||||
</Show>
|
||||
|
||||
<Show when={!!locId && tab === "history"}>
|
||||
<div className=" flex flex-col items-center mt-8">
|
||||
<WorkflowLog />
|
||||
</div>
|
||||
</Show>
|
||||
|
||||
<ScrollBar orientation="vertical" />
|
||||
<ScrollBar orientation="horizontal" />
|
||||
|
@ -185,6 +185,10 @@ const Workflow = () => {
|
||||
const handleCreateClick = () => {
|
||||
navigate("/workflow/detail");
|
||||
};
|
||||
|
||||
const handleRowClick = (id: string) => {
|
||||
navigate(`/workflow/detail?id=${id}`);
|
||||
};
|
||||
return (
|
||||
<>
|
||||
<div className="flex justify-between items-center">
|
||||
@ -196,7 +200,7 @@ const Workflow = () => {
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<DataTable columns={columns} data={data} onPageChange={fetchData} pageCount={pageCount} />
|
||||
<DataTable columns={columns} data={data} onPageChange={fetchData} pageCount={pageCount} onRowClick={handleRowClick} />
|
||||
</div>
|
||||
|
||||
<CustomAlertDialog
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Workflow, WorkflowNode } from "@/domain/workflow";
|
||||
import { Workflow, WorkflowNode, WorkflowRunLog } from "@/domain/workflow";
|
||||
import { getPb } from "./api";
|
||||
|
||||
export const get = async (id: string) => {
|
||||
@ -39,3 +39,30 @@ export const list = async (req: WorkflowListReq) => {
|
||||
export const remove = async (id: string) => {
|
||||
return await getPb().collection("workflow").delete(id);
|
||||
};
|
||||
|
||||
type WorkflowLogsReq = {
|
||||
id: string;
|
||||
page: number;
|
||||
perPage?: number;
|
||||
};
|
||||
|
||||
export const logs = async (req: WorkflowLogsReq) => {
|
||||
let page = 1;
|
||||
if (req.page) {
|
||||
page = req.page;
|
||||
}
|
||||
let perPage = 10;
|
||||
if (req.perPage) {
|
||||
perPage = req.perPage;
|
||||
}
|
||||
|
||||
const response = await getPb()
|
||||
.collection("workflow_run_log")
|
||||
.getList<WorkflowRunLog>(page, perPage, {
|
||||
sort: "-created",
|
||||
filter: getPb().filter("workflow={:workflowId}", { workflowId: req.id }),
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user