feat(ui): enhance workflow logs display

This commit is contained in:
Fu Diwei 2025-03-19 10:09:30 +08:00
parent fd6e41c566
commit 882f802585
16 changed files with 168 additions and 69 deletions

View File

@ -8,8 +8,9 @@ type WorkflowLog struct {
Meta
WorkflowId string `json:"workflowId" db:"workflowId"`
RunId string `json:"workflorunIdwId" db:"runId"`
NodeId string `json:"nodeId"`
NodeName string `json:"nodeName"`
NodeId string `json:"nodeId" db:"nodeId"`
NodeName string `json:"nodeName" db:"nodeName"`
Timestamp int64 `json:"timestamp" db:"timestamp"` // 毫秒级时间戳
Level string `json:"level" db:"level"`
Message string `json:"message" db:"message"`
Data map[string]any `json:"data" db:"data"`

View File

@ -90,14 +90,6 @@ func (d *DeployerProvider) WithLogger(logger *slog.Logger) deployer.Deployer {
}
func (d *DeployerProvider) Deploy(ctx context.Context, certPem string, privkeyPem string) (*deployer.DeployResult, error) {
// 上传证书到 SCM
upres, err := d.sslUploader.Upload(ctx, certPem, privkeyPem)
if err != nil {
return nil, xerrors.Wrap(err, "failed to upload certificate file")
} else {
d.logger.Info("ssl certificate uploaded", slog.Any("result", upres))
}
// 根据部署资源类型决定部署方式
switch d.config.ResourceType {
case RESOURCE_TYPE_CERTIFICATE:

View File

@ -22,7 +22,7 @@ func (r *WorkflowLogRepository) ListByWorkflowRunId(ctx context.Context, workflo
records, err := app.GetApp().FindRecordsByFilter(
domain.CollectionNameWorkflowLog,
"runId={:runId}",
"-created",
"timestamp",
0, 0,
dbx.Params{"runId": workflowRunId},
)
@ -66,6 +66,7 @@ func (r *WorkflowLogRepository) Save(ctx context.Context, workflowLog *domain.Wo
record.Set("runId", workflowLog.RunId)
record.Set("nodeId", workflowLog.NodeId)
record.Set("nodeName", workflowLog.NodeName)
record.Set("timestamp", workflowLog.Timestamp)
record.Set("level", workflowLog.Level)
record.Set("message", workflowLog.Message)
record.Set("data", workflowLog.Data)
@ -102,6 +103,7 @@ func (r *WorkflowLogRepository) castRecordToModel(record *core.Record) (*domain.
RunId: record.GetString("runId"),
NodeId: record.GetString("nodeId"),
NodeName: record.GetString("nodeName"),
Timestamp: int64(record.GetInt("timestamp")),
Level: record.GetString("level"),
Message: record.GetString("message"),
Data: logdata,

View File

@ -80,6 +80,7 @@ func (w *workflowInvoker) processNode(ctx context.Context, node *domain.Workflow
log.RunId = w.runId
log.NodeId = current.Id
log.NodeName = current.Name
log.Timestamp = record.Time.UnixMilli()
log.Level = record.Level.String()
log.Message = record.Message
log.Data = record.Data

View File

@ -42,7 +42,7 @@ func (n *applyNode) Process(ctx context.Context) error {
// 检测是否可以跳过本次执行
if skippable, skipReason := n.checkCanSkip(ctx, lastOutput); skippable {
n.logger.Warn(fmt.Sprintf("skip this application, because %s", skipReason))
n.logger.Info(fmt.Sprintf("skip this application, because %s", skipReason))
return nil
} else if skipReason != "" {
n.logger.Info(fmt.Sprintf("continue to apply, because %s", skipReason))
@ -124,7 +124,7 @@ func (n *applyNode) checkCanSkip(ctx context.Context, lastOutput *domain.Workflo
renewalInterval := time.Duration(currentNodeConfig.SkipBeforeExpiryDays) * time.Hour * 24
expirationTime := time.Until(lastCertificate.ExpireAt)
if expirationTime > renewalInterval {
return true, fmt.Sprintf("the certificate has already been issued (expires in %dD, next renewal in %dD)", int(expirationTime.Hours()/24), currentNodeConfig.SkipBeforeExpiryDays)
return true, fmt.Sprintf("the certificate has already been issued (expires in %dd, next renewal in %dd)", int(expirationTime.Hours()/24), currentNodeConfig.SkipBeforeExpiryDays)
}
}
}

View File

@ -55,7 +55,7 @@ func (n *deployNode) Process(ctx context.Context) error {
// 检测是否可以跳过本次执行
if lastOutput != nil && certificate.CreatedAt.Before(lastOutput.UpdatedAt) {
if skippable, skipReason := n.checkCanSkip(ctx, lastOutput); skippable {
n.logger.Warn(fmt.Sprintf("skip this deployment, because %s", skipReason))
n.logger.Info(fmt.Sprintf("skip this deployment, because %s", skipReason))
return nil
} else if skipReason != "" {
n.logger.Info(fmt.Sprintf("continue to deploy, because %s", skipReason))

View File

@ -40,7 +40,7 @@ func (n *uploadNode) Process(ctx context.Context) error {
// 检测是否可以跳过本次执行
if skippable, skipReason := n.checkCanSkip(ctx, lastOutput); skippable {
n.logger.Warn(fmt.Sprintf("skip this upload, because %s", skipReason))
n.logger.Info(fmt.Sprintf("skip this upload, because %s", skipReason))
return nil
} else if skipReason != "" {
n.logger.Info(fmt.Sprintf("continue to upload, because %s", skipReason))

View File

@ -3,6 +3,7 @@ package migrations
import (
"encoding/json"
"strings"
"time"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
@ -86,6 +87,18 @@ func init() {
"system": false,
"type": "text"
},
{
"hidden": false,
"id": "number2782324286",
"max": null,
"min": null,
"name": "timestamp",
"onlyInt": false,
"presentable": false,
"required": false,
"system": false,
"type": "number"
},
{
"autogeneratePattern": "",
"hidden": false,
@ -192,13 +205,15 @@ func init() {
for _, log := range logs {
for _, logRecord := range log.Records {
record := core.NewRecord(collection)
createdAt, _ := time.Parse(time.RFC3339, logRecord.Time)
record.Set("workflowId", workflowRun.Get("workflowId"))
record.Set("runId", workflowRun.Get("id"))
record.Set("nodeId", log.NodeId)
record.Set("nodeName", log.NodeName)
record.Set("timestamp", createdAt.UnixMilli())
record.Set("level", logRecord.Level)
record.Set("message", strings.TrimSpace(logRecord.Content+" "+logRecord.Error))
record.Set("created", log.Records)
record.Set("created", createdAt)
if err := app.Save(record); err != nil {
return err
}

View File

@ -1,8 +1,34 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { RightOutlined as RightOutlinedIcon, SelectOutlined as SelectOutlinedIcon } from "@ant-design/icons";
import {
CheckCircleOutlined as CheckCircleOutlinedIcon,
CheckOutlined as CheckOutlinedIcon,
ClockCircleOutlined as ClockCircleOutlinedIcon,
CloseCircleOutlined as CloseCircleOutlinedIcon,
RightOutlined as RightOutlinedIcon,
SelectOutlined as SelectOutlinedIcon,
SettingOutlined as SettingOutlinedIcon,
StopOutlined as StopOutlinedIcon,
SyncOutlined as SyncOutlinedIcon,
} from "@ant-design/icons";
import { useRequest } from "ahooks";
import { Alert, Button, Collapse, Divider, Empty, Skeleton, Space, Spin, Table, type TableProps, Tooltip, Typography, notification } from "antd";
import {
Button,
Collapse,
Divider,
Dropdown,
Empty,
Flex,
Skeleton,
Space,
Spin,
Table,
type TableProps,
Tooltip,
Typography,
notification,
theme,
} from "antd";
import dayjs from "dayjs";
import { ClientResponseError } from "pocketbase";
@ -23,25 +49,14 @@ export type WorkflowRunDetailProps = {
};
const WorkflowRunDetail = ({ data, ...props }: WorkflowRunDetailProps) => {
const { t } = useTranslation();
return (
<div {...props}>
<Show when={data.status === WORKFLOW_RUN_STATUSES.SUCCEEDED}>
<Alert showIcon type="success" message={<Typography.Text type="success">{t("workflow_run.props.status.succeeded")}</Typography.Text>} />
</Show>
<Show when={data.status === WORKFLOW_RUN_STATUSES.FAILED}>
<Alert showIcon type="error" message={<Typography.Text type="danger">{t("workflow_run.props.status.failed")}</Typography.Text>} />
</Show>
<div className="my-4">
<Show when={!!data}>
<WorkflowRunLogs runId={data.id} runStatus={data.status} />
</div>
</Show>
<Show when={data.status === WORKFLOW_RUN_STATUSES.SUCCEEDED}>
<Show when={!!data && data.status === WORKFLOW_RUN_STATUSES.SUCCEEDED}>
<Divider />
<WorkflowRunArtifacts runId={data.id} />
</Show>
</div>
@ -51,9 +66,10 @@ const WorkflowRunDetail = ({ data, ...props }: WorkflowRunDetailProps) => {
const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: string }) => {
const { t } = useTranslation();
type Log = Pick<WorkflowLogModel, "level" | "message" | "data" | "created">;
type LogGroup = { id: string; name: string; records: Log[] };
const { token: themeToken } = theme.useToken();
type Log = Pick<WorkflowLogModel, "timestamp" | "level" | "message" | "data">;
type LogGroup = { id: string; name: string; records: Log[] };
const [listData, setListData] = useState<LogGroup[]>([]);
const { loading } = useRequest(
() => {
@ -61,13 +77,12 @@ const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: strin
},
{
refreshDeps: [runId, runStatus],
pollingInterval: runStatus === WORKFLOW_RUN_STATUSES.PENDING || runStatus === WORKFLOW_RUN_STATUSES.RUNNING ? 5000 : 0,
pollingInterval: runStatus === WORKFLOW_RUN_STATUSES.PENDING || runStatus === WORKFLOW_RUN_STATUSES.RUNNING ? 3000 : 0,
pollingWhenHidden: false,
throttleWait: 500,
onBefore: () => {
setListData([]);
},
onSuccess: (res) => {
if (res.items.length === listData.flatMap((e) => e.records).length) return;
setListData(
res.items.reduce((acc, e) => {
let group = acc.at(-1);
@ -75,7 +90,7 @@ const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: strin
group = { id: e.nodeId, name: e.nodeName, records: [] };
acc.push(group);
}
group.records.push({ level: e.level, message: e.message, data: e.data, created: e.created });
group.records.push({ timestamp: e.timestamp, level: e.level, message: e.message, data: e.data });
return acc;
}, [] as LogGroup[])
);
@ -92,7 +107,52 @@ const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: strin
}
);
const renderLogRecord = (record: Log) => {
const [showTimestamp, setShowTimestamp] = useState(true);
const [showWhitespace, setShowWhitespace] = useState(true);
const renderBadge = () => {
switch (runStatus) {
case WORKFLOW_RUN_STATUSES.PENDING:
return (
<Flex gap="small">
<ClockCircleOutlinedIcon />
{t("workflow_run.props.status.pending")}
</Flex>
);
case WORKFLOW_RUN_STATUSES.RUNNING:
return (
<Flex gap="small" style={{ color: themeToken.colorInfo }}>
<SyncOutlinedIcon spin />
{t("workflow_run.props.status.running")}
</Flex>
);
case WORKFLOW_RUN_STATUSES.SUCCEEDED:
return (
<Flex gap="small" style={{ color: themeToken.colorSuccess }}>
<CheckCircleOutlinedIcon />
{t("workflow_run.props.status.succeeded")}
</Flex>
);
case WORKFLOW_RUN_STATUSES.FAILED:
return (
<Flex gap="small" style={{ color: themeToken.colorError }}>
<CloseCircleOutlinedIcon />
{t("workflow_run.props.status.failed")}
</Flex>
);
case WORKFLOW_RUN_STATUSES.CANCELED:
return (
<Flex gap="small" style={{ color: themeToken.colorWarning }}>
<StopOutlinedIcon />
{t("workflow_run.props.status.canceled")}
</Flex>
);
}
return <></>;
};
const renderRecord = (record: Log) => {
let message = <>{record.message}</>;
if (record.data != null && Object.keys(record.data).length > 0) {
message = (
@ -100,8 +160,8 @@ const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: strin
<summary>{record.message}</summary>
{Object.entries(record.data).map(([key, value]) => (
<div key={key} className="flex space-x-2 " style={{ wordBreak: "break-word" }}>
<div>{key}:</div>
<div>{JSON.stringify(value)}</div>
<div className="whitespace-nowrap">{key}:</div>
<div className={!showWhitespace ? "whitespace-pre-line" : ""}>{JSON.stringify(value)}</div>
</div>
))}
</details>
@ -110,13 +170,14 @@ const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: strin
return (
<div className="flex space-x-2 text-xs" style={{ wordBreak: "break-word" }}>
<div className="whitespace-nowrap text-stone-400">[{dayjs(record.created).format("YYYY-MM-DD HH:mm:ss")}]</div>
{showTimestamp ? <div className="whitespace-nowrap text-stone-400">[{dayjs(record.timestamp).format("YYYY-MM-DD HH:mm:ss")}]</div> : <></>}
<div
className={mergeCls(
"font-mono",
record.level === "DEBUG" ? "text-stone-400" : "",
record.level === "WARN" ? "text-yellow-600" : "",
record.level === "ERROR" ? "text-red-600" : ""
record.level === "ERROR" ? "text-red-600" : "",
!showWhitespace ? "whitespace-pre-line" : ""
)}
>
{message}
@ -129,6 +190,35 @@ const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: strin
<>
<Typography.Title level={5}>{t("workflow_run.logs")}</Typography.Title>
<div className="rounded-md bg-black text-stone-200">
<div className="flex items-center gap-2 p-4">
<div className="grow overflow-hidden">{renderBadge()}</div>
<div>
<Dropdown
menu={{
items: [
{
key: "show-timestamp",
label: t("workflow_run.logs.menu.show_timestamps"),
icon: <CheckOutlinedIcon className={showTimestamp ? "visible" : "invisible"} />,
onClick: () => setShowTimestamp(!showTimestamp),
},
{
key: "show-whitespace",
label: t("workflow_run.logs.menu.show_whitespaces"),
icon: <CheckOutlinedIcon className={showWhitespace ? "visible" : "invisible"} />,
onClick: () => setShowWhitespace(!showWhitespace),
},
],
}}
trigger={["click"]}
>
<Button icon={<SettingOutlinedIcon />} ghost />
</Dropdown>
</div>
</div>
<Divider className="my-0 bg-stone-800" />
<Show
when={!loading || listData.length > 0}
fallback={
@ -155,7 +245,7 @@ const WorkflowRunLogs = ({ runId, runStatus }: { runId: string; runStatus: strin
header: { color: "inherit" },
},
label: group.name,
children: <div className="flex flex-col space-y-1">{group.records.map((record) => renderLogRecord(record))}</div>,
children: <div className="flex flex-col space-y-1">{group.records.map((record) => renderRecord(record))}</div>,
};
})}
/>
@ -221,9 +311,6 @@ const WorkflowRunArtifacts = ({ runId }: { runId: string }) => {
},
{
refreshDeps: [runId],
onBefore: () => {
setTableData([]);
},
onSuccess: (res) => {
setTableData(res.items);
},

View File

@ -10,8 +10,6 @@ import AddNode from "./AddNode";
import WorkflowElement from "../WorkflowElement";
import { type SharedNodeProps } from "./_SharedNode";
const { useToken } = theme;
export type BrandNodeProps = SharedNodeProps;
const BranchNode = ({ node, disabled }: BrandNodeProps) => {
@ -19,7 +17,7 @@ const BranchNode = ({ node, disabled }: BrandNodeProps) => {
const { addBranch } = useWorkflowStore(useZustandShallowSelector(["addBranch"]));
const token = useToken();
const { token: themeToken } = theme.useToken();
const renderBranch = (node: WorkflowNode, branchNodeId?: string, branchIndex?: number) => {
const elements: JSX.Element[] = [];
@ -38,7 +36,7 @@ const BranchNode = ({ node, disabled }: BrandNodeProps) => {
<div
className="relative flex gap-x-16 before:absolute before:inset-x-[128px] before:top-0 before:h-[2px] before:bg-stone-200 before:content-[''] after:absolute after:inset-x-[128px] after:bottom-0 after:h-[2px] after:bg-stone-200 after:content-['']"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
>
<Button
@ -64,13 +62,13 @@ const BranchNode = ({ node, disabled }: BrandNodeProps) => {
<div
className="absolute -left-px -top-1 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
<div
className="absolute -bottom-1 -left-px z-50 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
</>
@ -80,13 +78,13 @@ const BranchNode = ({ node, disabled }: BrandNodeProps) => {
<div
className="absolute -right-px -top-1 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
<div
className="absolute -bottom-1 -right-px z-50 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
</>

View File

@ -7,12 +7,10 @@ import AddNode from "./AddNode";
import WorkflowElement from "../WorkflowElement";
import { type SharedNodeProps } from "./_SharedNode";
const { useToken } = theme;
export type BrandNodeProps = SharedNodeProps;
const ExecuteResultBranchNode = ({ node, disabled }: BrandNodeProps) => {
const token = useToken();
const { token: themeToken } = theme.useToken();
const renderBranch = (node: WorkflowNode, branchNodeId?: string, branchIndex?: number) => {
const elements: JSX.Element[] = [];
@ -31,7 +29,7 @@ const ExecuteResultBranchNode = ({ node, disabled }: BrandNodeProps) => {
<div
className="relative flex gap-x-16 before:absolute before:inset-x-[128px] before:top-0 before:h-[2px] before:bg-stone-200 before:content-[''] after:absolute after:inset-x-[128px] after:bottom-0 after:h-[2px] after:bg-stone-200 after:content-['']"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
>
{node.branches?.map((branch, index) => (
@ -44,13 +42,13 @@ const ExecuteResultBranchNode = ({ node, disabled }: BrandNodeProps) => {
<div
className="absolute -left-px -top-1 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
<div
className="absolute -bottom-1 -left-px z-50 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
</>
@ -60,13 +58,13 @@ const ExecuteResultBranchNode = ({ node, disabled }: BrandNodeProps) => {
<div
className="absolute -right-px -top-1 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
<div
className="absolute -bottom-1 -right-px z-50 h-2 w-1/2"
style={{
backgroundColor: token.token.colorBgContainer,
backgroundColor: themeToken.colorBgContainer,
}}
></div>
</>

View File

@ -1,6 +1,7 @@
export interface WorkflowLogModel extends Omit<BaseModel, "updated"> {
nodeId: string;
nodeName: string;
timestamp: ReturnType<typeof Date.prototype.getTime>;
level: "DEBUG" | "INFO" | "WARN" | "ERROR";
message: string;
data: Record<string, any>;

View File

@ -19,8 +19,10 @@
"workflow_run.props.ended_at": "Ended at",
"workflow_run.logs": "Logs",
"workflow_run.artifacts": "Artifacts",
"workflow_run.logs.menu.show_timestamps": "Show timestamps",
"workflow_run.logs.menu.show_whitespaces": "Show whitespaces",
"workflow_run.artifacts": "Artifacts",
"workflow_run_artifact.props.type": "Type",
"workflow_run_artifact.props.type.certificate": "Certificate",
"workflow_run_artifact.props.name": "Name"

View File

@ -19,8 +19,10 @@
"workflow_run.props.ended_at": "完成时间",
"workflow_run.logs": "日志",
"workflow_run.artifacts": "输出产物",
"workflow_run.logs.menu.show_timestamps": "显示日期时间",
"workflow_run.logs.menu.show_whitespaces": "显示转义换行符",
"workflow_run.artifacts": "输出产物",
"workflow_run_artifact.props.type": "类型",
"workflow_run_artifact.props.type.certificate": "证书",
"workflow_run_artifact.props.name": "名称"

View File

@ -39,7 +39,7 @@ export const listByWorkflowRunId = async (workflowRunId: string) => {
const list = await pb.collection(COLLECTION_NAME_CERTIFICATE).getFullList<CertificateModel>({
batch: 65535,
filter: pb.filter("workflowRunId={:workflowRunId}", { workflowRunId: workflowRunId }),
// sort: "created",
sort: "created",
requestKey: null,
});

View File

@ -8,7 +8,7 @@ export const listByWorkflowRunId = async (workflowRunId: string) => {
const list = await pb.collection(COLLECTION_NAME_WORKFLOW_LOG).getFullList<WorkflowLogModel>({
batch: 65535,
filter: pb.filter("runId={:runId}", { runId: workflowRunId }),
// sort: "created",
sort: "timestamp",
requestKey: null,
});