feat(pipeline-tasks): 任务详情添加实时任务状态展示

This commit is contained in:
Ivan Li 2021-03-24 20:34:52 +08:00
parent 46f50b6deb
commit a896673035
3 changed files with 109 additions and 6 deletions

View File

@ -23,3 +23,9 @@
@apply p-1 my-px; @apply p-1 my-px;
@apply bg-gray-100; @apply bg-gray-100;
} }
.taskStatusIcon {
@apply text-lg;
}
.taskInfo {
@apply grid grid-flow-col;
}

View File

@ -5,6 +5,8 @@ declare namespace PipelineTaskDetailsScssNamespace {
details: string; details: string;
navContainer: string; navContainer: string;
navItem: string; navItem: string;
taskInfo: string;
taskStatusIcon: string;
unitLogs: string; unitLogs: string;
unitLogsContainer: string; unitLogsContainer: string;
} }

View File

@ -1,9 +1,9 @@
import { gql, useSubscription, useQuery } from '@apollo/client'; import { gql, useSubscription, useQuery } from '@apollo/client';
import { h } from 'preact'; import { h } from 'preact';
import { useMemo } from 'preact/hooks'; import { useMemo } from 'preact/hooks';
import { find, propEq } from 'ramda'; import { find, last, propEq } from 'ramda';
import { observer, useLocalObservable, useObserver } from 'mobx-react'; import { observer, useLocalObservable, useObserver } from 'mobx-react';
import { makeAutoObservable } from 'mobx'; import { autorun, makeAutoObservable } from 'mobx';
import styles from './pipeline-task-details.scss'; import styles from './pipeline-task-details.scss';
import { import {
PipelineTaskLogs, PipelineTaskLogs,
@ -12,6 +12,13 @@ import {
PipelineTask, PipelineTask,
PipelineTaskLogMessage PipelineTaskLogMessage
} from '../../generated/graphql'; } from '../../generated/graphql';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
faExclamationCircle,
faPauseCircle,
faSpinner
} from '@fortawesome/free-solid-svg-icons';
import classNames from 'classnames';
interface Props { interface Props {
taskId: string; taskId: string;
} }
@ -19,8 +26,21 @@ interface Props {
class Store { class Store {
logs: PipelineTaskLogs[] = []; logs: PipelineTaskLogs[] = [];
task?: PipelineTask; task?: PipelineTask;
currUnit?: PipelineUnits;
constructor() { constructor() {
makeAutoObservable(this); makeAutoObservable(this);
autorun(() => {
const units = this.task?.units.filter(unit =>
this.logs.find(log => log.unit === unit)
);
if (units) {
this.setCurrUnit(last(units));
}
});
}
private setCurrUnit(unit?: PipelineUnits) {
this.currUnit = unit;
} }
addLogsFromTask(task: PipelineTask) { addLogsFromTask(task: PipelineTask) {
@ -58,6 +78,9 @@ const FIND_PIPELINE_TASK = gql`
id id
units units
commit commit
status
startedAt
endedAt
logs { logs {
unit unit
logs logs
@ -66,6 +89,19 @@ const FIND_PIPELINE_TASK = gql`
} }
`; `;
const PIPELINE_TASK_CHANGED = gql`
subscription Pipelinetaskchanged($taskId: String!) {
task: pipelineTaskChanged(id: $taskId) {
id
units
commit
status
startedAt
endedAt
}
}
`;
const PIPELINE_TASK_LOG = gql` const PIPELINE_TASK_LOG = gql`
subscription PipelineTaskLog($taskId: String!) { subscription PipelineTaskLog($taskId: String!) {
log: pipelineTaskLog(taskId: $taskId) { log: pipelineTaskLog(taskId: $taskId) {
@ -77,10 +113,15 @@ const PIPELINE_TASK_LOG = gql`
`; `;
export const PipelineTaskDetails = ({ taskId }: Props) => { export const PipelineTaskDetails = ({ taskId }: Props) => {
const { data } = useQuery<{ task: PipelineTask }, { taskId: string }>( const { data, subscribeToMore } = useQuery<
FIND_PIPELINE_TASK, { task: PipelineTask },
{ variables: { taskId } } { taskId: string }
); >(FIND_PIPELINE_TASK, { variables: { taskId } });
subscribeToMore({
document: PIPELINE_TASK_CHANGED,
variables: { taskId }
});
const store = useLocalObservable(() => new Store()); const store = useLocalObservable(() => new Store());
@ -109,6 +150,7 @@ export const PipelineTaskDetails = ({ taskId }: Props) => {
}); });
return useObserver(() => ( return useObserver(() => (
<section className={styles.details}> <section className={styles.details}>
<TaskInfo store={store} />
<UnitList store={store} /> <UnitList store={store} />
<LogListOfUnit store={store} /> <LogListOfUnit store={store} />
@ -143,3 +185,56 @@ const LogListOfUnit = observer(({ store }: { store: Store }) => {
</div> </div>
); );
}); });
const TaskInfo = observer(({ store }: { store: Store }) => {
const task = store.task;
if (!task) {
return <header></header>;
}
return (
<header className={styles.taskInfo}>
<StatusIcon status={task.status} />
<p>{task.startedAt}</p>
<p>{task.status}</p>
</header>
);
});
const StatusIcon = ({ status }: { status: TaskStatuses }) => {
switch (status) {
case TaskStatuses.Working:
return (
<FontAwesomeIcon
icon={faSpinner}
className={classNames([
'animate-spin',
styles.taskStatusIcon,
'text-purple-500'
])}
/>
);
case TaskStatuses.Failed:
return (
<FontAwesomeIcon
icon={faExclamationCircle}
className={classNames([styles.taskStatusIcon, 'text-red-700'])}
/>
);
case TaskStatuses.Success:
return (
<FontAwesomeIcon
icon={faExclamationCircle}
className={classNames([styles.taskStatusIcon, 'text-green-500'])}
/>
);
case TaskStatuses.Pending:
return (
<FontAwesomeIcon
icon={faPauseCircle}
className={classNames([styles.taskStatusIcon, 'text-yellow-500'])}
/>
);
default:
return null;
}
};