2023-05-16 09:45:45 +08:00
|
|
|
import {
|
2023-06-09 13:05:05 +08:00
|
|
|
ActionContextProvider,
|
2023-08-23 17:30:21 +08:00
|
|
|
cx,
|
2023-05-16 09:45:45 +08:00
|
|
|
SchemaComponent,
|
2023-08-06 10:17:48 +08:00
|
|
|
useAPIClient,
|
2023-05-16 09:45:45 +08:00
|
|
|
useCompile,
|
|
|
|
useDocumentTitle,
|
|
|
|
useResourceActionContext,
|
|
|
|
} from '@nocobase/client';
|
2022-10-30 11:54:14 +08:00
|
|
|
import { str2moment } from '@nocobase/utils/client';
|
2023-08-06 10:17:48 +08:00
|
|
|
import { Breadcrumb, Dropdown, Space, Tag } from 'antd';
|
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
|
|
import { Link, useNavigate } from 'react-router-dom';
|
2023-07-07 14:35:22 +08:00
|
|
|
import { CanvasContent } from './CanvasContent';
|
2023-08-23 17:30:21 +08:00
|
|
|
import { ExecutionStatusOptionsMap, JobStatusOptions } from './constants';
|
2023-07-07 14:35:22 +08:00
|
|
|
import { FlowContext, useFlowContext } from './FlowContext';
|
2023-05-16 09:45:45 +08:00
|
|
|
import { lang, NAMESPACE } from './locale';
|
|
|
|
import { instructions } from './nodes';
|
2023-07-16 12:46:25 +08:00
|
|
|
import useStyles from './style';
|
2023-07-07 14:35:22 +08:00
|
|
|
import { linkNodes } from './utils';
|
2023-08-06 10:17:48 +08:00
|
|
|
import { DownOutlined } from '@ant-design/icons';
|
2023-08-23 17:30:21 +08:00
|
|
|
import { StatusButton } from './components/StatusButton';
|
2022-10-30 11:54:14 +08:00
|
|
|
|
2023-02-20 11:52:06 +08:00
|
|
|
function attachJobs(nodes, jobs: any[] = []): void {
|
2022-10-30 11:54:14 +08:00
|
|
|
const nodesMap = new Map();
|
2023-05-16 09:45:45 +08:00
|
|
|
nodes.forEach((item) => {
|
|
|
|
item.jobs = [];
|
|
|
|
nodesMap.set(item.id, item);
|
|
|
|
});
|
|
|
|
jobs.forEach((item) => {
|
|
|
|
const node = nodesMap.get(item.nodeId);
|
|
|
|
node.jobs.push(item);
|
|
|
|
item.node = {
|
|
|
|
id: node.id,
|
2023-10-25 23:14:54 +08:00
|
|
|
key: node.key,
|
2023-05-16 09:45:45 +08:00
|
|
|
title: node.title,
|
|
|
|
type: node.type,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
nodes.forEach((item) => {
|
|
|
|
item.jobs = item.jobs.sort((a, b) => a.id - b.id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function JobModal() {
|
|
|
|
const compile = useCompile();
|
|
|
|
const { viewJob: job, setViewJob } = useFlowContext();
|
2023-07-16 12:46:25 +08:00
|
|
|
const { styles } = useStyles();
|
|
|
|
|
2023-05-16 09:45:45 +08:00
|
|
|
const { node = {} } = job ?? {};
|
|
|
|
const instruction = instructions.get(node.type);
|
|
|
|
|
|
|
|
return (
|
2023-06-09 13:05:05 +08:00
|
|
|
<ActionContextProvider value={{ visible: Boolean(job), setVisible: setViewJob }}>
|
2023-05-16 09:45:45 +08:00
|
|
|
<SchemaComponent
|
|
|
|
schema={{
|
|
|
|
type: 'void',
|
|
|
|
properties: {
|
|
|
|
[`${job?.id}-modal`]: {
|
|
|
|
type: 'void',
|
|
|
|
'x-decorator': 'Form',
|
|
|
|
'x-decorator-props': {
|
|
|
|
initialValue: job,
|
|
|
|
},
|
|
|
|
'x-component': 'Action.Modal',
|
|
|
|
title: (
|
2023-07-16 12:46:25 +08:00
|
|
|
<div className={styles.nodeTitleClass}>
|
2023-05-16 09:45:45 +08:00
|
|
|
<Tag>{compile(instruction?.title)}</Tag>
|
|
|
|
<strong>{node.title}</strong>
|
|
|
|
<span className="workflow-node-id">#{node.id}</span>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
properties: {
|
|
|
|
status: {
|
|
|
|
type: 'number',
|
|
|
|
title: `{{t("Status", { ns: "${NAMESPACE}" })}}`,
|
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-component': 'Select',
|
|
|
|
enum: JobStatusOptions,
|
|
|
|
'x-read-pretty': true,
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: 'string',
|
|
|
|
title: `{{t("Executed at", { ns: "${NAMESPACE}" })}}`,
|
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-component': 'DatePicker',
|
|
|
|
'x-component-props': {
|
|
|
|
showTime: true,
|
|
|
|
},
|
|
|
|
'x-read-pretty': true,
|
|
|
|
},
|
|
|
|
result: {
|
|
|
|
type: 'object',
|
|
|
|
title: `{{t("Node result", { ns: "${NAMESPACE}" })}}`,
|
|
|
|
'x-decorator': 'FormItem',
|
|
|
|
'x-component': 'Input.JSON',
|
|
|
|
'x-component-props': {
|
2023-08-15 11:32:51 +08:00
|
|
|
className: styles.nodeJobResultClass,
|
2023-05-16 09:45:45 +08:00
|
|
|
},
|
|
|
|
'x-read-pretty': true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
/>
|
2023-06-09 13:05:05 +08:00
|
|
|
</ActionContextProvider>
|
2023-05-16 09:45:45 +08:00
|
|
|
);
|
2022-10-30 11:54:14 +08:00
|
|
|
}
|
|
|
|
|
2023-08-06 10:17:48 +08:00
|
|
|
function ExecutionsDropdown(props) {
|
|
|
|
const { execution } = useFlowContext();
|
|
|
|
const apiClient = useAPIClient();
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const { styles } = useStyles();
|
|
|
|
const [executionsBefore, setExecutionsBefore] = useState([]);
|
|
|
|
const [executionsAfter, setExecutionsAfter] = useState([]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!execution) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
apiClient
|
|
|
|
.resource('executions')
|
|
|
|
.list({
|
|
|
|
filter: {
|
|
|
|
key: execution.key,
|
|
|
|
id: {
|
|
|
|
$lt: execution.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: '-createdAt',
|
|
|
|
pageSize: 10,
|
|
|
|
fields: ['id', 'status', 'createdAt'],
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
setExecutionsBefore(data.data);
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
}, [execution]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!execution) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
apiClient
|
|
|
|
.resource('executions')
|
|
|
|
.list({
|
|
|
|
filter: {
|
|
|
|
key: execution.key,
|
|
|
|
id: {
|
|
|
|
$gt: execution.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sort: 'createdAt',
|
|
|
|
pageSize: 10,
|
|
|
|
fields: ['id', 'status', 'createdAt'],
|
|
|
|
})
|
|
|
|
.then(({ data }) => {
|
|
|
|
setExecutionsAfter(data.data.reverse());
|
|
|
|
})
|
|
|
|
.catch(() => {});
|
|
|
|
}, [execution]);
|
|
|
|
|
|
|
|
const onClick = useCallback(
|
|
|
|
({ key }) => {
|
|
|
|
if (key != execution.id) {
|
|
|
|
navigate(`/admin/settings/workflow/executions/${key}`);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[execution],
|
|
|
|
);
|
|
|
|
|
|
|
|
return execution ? (
|
|
|
|
<Dropdown
|
|
|
|
menu={{
|
|
|
|
onClick,
|
2023-08-23 17:30:21 +08:00
|
|
|
defaultSelectedKeys: [`${execution.id}`],
|
|
|
|
className: cx(styles.dropdownClass, styles.executionsDropdownRowClass),
|
2023-08-06 10:17:48 +08:00
|
|
|
items: [...executionsAfter, execution, ...executionsBefore].map((item) => {
|
|
|
|
return {
|
|
|
|
key: item.id,
|
|
|
|
label: (
|
2023-08-23 17:30:21 +08:00
|
|
|
<>
|
2023-08-06 10:17:48 +08:00
|
|
|
<span className="id">{`#${item.id}`}</span>
|
|
|
|
<time>{str2moment(item.createdAt).format('YYYY-MM-DD HH:mm:ss')}</time>
|
2023-08-23 17:30:21 +08:00
|
|
|
</>
|
|
|
|
),
|
|
|
|
icon: (
|
|
|
|
<span>
|
|
|
|
<StatusButton statusMap={ExecutionStatusOptionsMap} status={item.status} />
|
2023-08-06 10:17:48 +08:00
|
|
|
</span>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
}),
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Space>
|
|
|
|
<strong>{`#${execution.id}`}</strong>
|
|
|
|
<DownOutlined />
|
|
|
|
</Space>
|
|
|
|
</Dropdown>
|
|
|
|
) : null;
|
|
|
|
}
|
|
|
|
|
2022-10-30 11:54:14 +08:00
|
|
|
export function ExecutionCanvas() {
|
|
|
|
const compile = useCompile();
|
2022-11-11 23:37:41 +08:00
|
|
|
const { data, loading } = useResourceActionContext();
|
2022-10-30 11:54:14 +08:00
|
|
|
const { setTitle } = useDocumentTitle();
|
2023-05-16 09:45:45 +08:00
|
|
|
const [viewJob, setViewJob] = useState(null);
|
2022-10-30 11:54:14 +08:00
|
|
|
useEffect(() => {
|
|
|
|
const { workflow } = data?.data ?? {};
|
2022-11-13 18:12:56 +08:00
|
|
|
setTitle?.(`${workflow?.title ? `${workflow.title} - ` : ''}${lang('Execution history')}`);
|
2022-10-30 11:54:14 +08:00
|
|
|
}, [data?.data]);
|
|
|
|
|
|
|
|
if (!data?.data) {
|
|
|
|
if (loading) {
|
2023-04-25 13:12:14 +08:00
|
|
|
return <div>{lang('Loading')}</div>;
|
2022-10-30 11:54:14 +08:00
|
|
|
} else {
|
2022-11-11 23:37:41 +08:00
|
|
|
return <div>{lang('Load failed')}</div>;
|
2022-10-30 11:54:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 13:12:14 +08:00
|
|
|
const { jobs = [], workflow: { nodes = [], revisions = [], ...workflow } = {}, ...execution } = data?.data ?? {};
|
2022-10-30 11:54:14 +08:00
|
|
|
|
2023-02-20 11:52:06 +08:00
|
|
|
linkNodes(nodes);
|
|
|
|
attachJobs(nodes, jobs);
|
2022-10-30 11:54:14 +08:00
|
|
|
|
2023-04-25 13:12:14 +08:00
|
|
|
const entry = nodes.find((item) => !item.upstream);
|
2022-10-30 11:54:14 +08:00
|
|
|
|
|
|
|
const statusOption = ExecutionStatusOptionsMap[execution.status];
|
|
|
|
|
|
|
|
return (
|
2023-04-25 13:12:14 +08:00
|
|
|
<FlowContext.Provider
|
|
|
|
value={{
|
|
|
|
workflow: workflow.type ? workflow : null,
|
|
|
|
nodes,
|
|
|
|
execution,
|
2023-05-16 09:45:45 +08:00
|
|
|
viewJob,
|
2023-06-09 13:05:05 +08:00
|
|
|
setViewJob,
|
2023-04-25 13:12:14 +08:00
|
|
|
}}
|
|
|
|
>
|
2022-10-30 11:54:14 +08:00
|
|
|
<div className="workflow-toolbar">
|
|
|
|
<header>
|
2023-07-15 13:23:02 +08:00
|
|
|
<Breadcrumb
|
|
|
|
items={[
|
|
|
|
{ title: <Link to={`/admin/settings/workflow/workflows`}>{lang('Workflow')}</Link> },
|
|
|
|
{ title: <Link to={`/admin/settings/workflow/workflows/${workflow.id}`}>{workflow.title}</Link> },
|
2023-08-06 10:17:48 +08:00
|
|
|
{ title: <ExecutionsDropdown /> },
|
2023-07-15 13:23:02 +08:00
|
|
|
]}
|
|
|
|
/>
|
2022-10-30 11:54:14 +08:00
|
|
|
</header>
|
|
|
|
<aside>
|
|
|
|
<Tag color={statusOption.color}>{compile(statusOption.label)}</Tag>
|
|
|
|
<time>{str2moment(execution.updatedAt).format('YYYY-MM-DD HH:mm:ss')}</time>
|
|
|
|
</aside>
|
|
|
|
</div>
|
2023-05-16 09:45:45 +08:00
|
|
|
<CanvasContent entry={entry} />
|
|
|
|
<JobModal />
|
2022-10-30 11:54:14 +08:00
|
|
|
</FlowContext.Provider>
|
|
|
|
);
|
|
|
|
}
|