From 7a84ff4a7428c9962830d444afb23d3c53722c5c Mon Sep 17 00:00:00 2001 From: Sipachev Igor Date: Tue, 24 Jan 2023 18:31:48 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D0=B0=D0=BB=D1=8C=D0=BD=D1=8B=D0=B5=20=D0=B4?= =?UTF-8?q?=D0=B5=D0=B9=D1=81=D1=82=D0=B2=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/sm/responses/processes.ts | 34 ++++++-- api/sm/responses/response.ts | 6 +- api/sm/schemas/kill-process.ts | 10 +++ api/sm/schemas/pause-process.ts | 10 +++ api/sm/schemas/play-process.ts | 10 +++ api/sm/schemas/stop-process.ts | 10 +++ api/sm/sm-client.ts | 44 +++++++++- components/elements/processes/index.tsx | 109 ++++++++++-------------- 8 files changed, 153 insertions(+), 80 deletions(-) create mode 100644 api/sm/schemas/kill-process.ts create mode 100644 api/sm/schemas/pause-process.ts create mode 100644 api/sm/schemas/play-process.ts create mode 100644 api/sm/schemas/stop-process.ts diff --git a/api/sm/responses/processes.ts b/api/sm/responses/processes.ts index a69a976..95d989e 100644 --- a/api/sm/responses/processes.ts +++ b/api/sm/responses/processes.ts @@ -1,15 +1,17 @@ export interface ProcessExceptionInterface { - message: string, - file: string, - line: number, - code: number, - trace: any[], + message: string + file: string + line: number + code: number + trace: any[] } export interface ProcessProgressInterface { - total: number, - progress: number, - memory: any[], + total: number + progress: number + memory: string + remaining: string + percent: number } export interface ProcessesResponseInterface { @@ -29,4 +31,18 @@ export interface ProcessesResponseInterface { pausedAt: string | null cancelledAt: string | null completedAt: string | null -} \ No newline at end of file + status: Status + canPlay: boolean + canPause: boolean + canRepeat: boolean + canStop: boolean + canKill: boolean +} + +export enum Status { + Wait = 'wait', + Running = 'running', + Cancelled = 'cancelled', + Error = 'error', + Success = 'success', +} diff --git a/api/sm/responses/response.ts b/api/sm/responses/response.ts index dad140b..4a1f553 100644 --- a/api/sm/responses/response.ts +++ b/api/sm/responses/response.ts @@ -1,4 +1,4 @@ -export interface ResponseInterface { - data: any +export interface ResponseInterface { + data: T headers: Headers -} \ No newline at end of file +} diff --git a/api/sm/schemas/kill-process.ts b/api/sm/schemas/kill-process.ts new file mode 100644 index 0000000..97708d8 --- /dev/null +++ b/api/sm/schemas/kill-process.ts @@ -0,0 +1,10 @@ +import {Method, SchemaInterface} from "../../schema-client"; + +class KillProcessSchema implements SchemaInterface { + method = Method.POST + url = '/system-monitoring/processes/{id}/kill' + contentType = null; +} + +export let killProcessSchema = new KillProcessSchema() +export default killProcessSchema diff --git a/api/sm/schemas/pause-process.ts b/api/sm/schemas/pause-process.ts new file mode 100644 index 0000000..c9991a6 --- /dev/null +++ b/api/sm/schemas/pause-process.ts @@ -0,0 +1,10 @@ +import {Method, SchemaInterface} from "../../schema-client"; + +class PauseProcessSchema implements SchemaInterface { + method = Method.POST + url = '/system-monitoring/processes/{id}/pause' + contentType = null; +} + +export let pauseProcessSchema = new PauseProcessSchema() +export default pauseProcessSchema diff --git a/api/sm/schemas/play-process.ts b/api/sm/schemas/play-process.ts new file mode 100644 index 0000000..37054bc --- /dev/null +++ b/api/sm/schemas/play-process.ts @@ -0,0 +1,10 @@ +import {Method, SchemaInterface} from "../../schema-client"; + +class PlayProcessSchema implements SchemaInterface { + method = Method.POST + url = '/system-monitoring/processes/{id}/play' + contentType = null; +} + +export let playProcessSchema = new PlayProcessSchema() +export default playProcessSchema diff --git a/api/sm/schemas/stop-process.ts b/api/sm/schemas/stop-process.ts new file mode 100644 index 0000000..b5df79f --- /dev/null +++ b/api/sm/schemas/stop-process.ts @@ -0,0 +1,10 @@ +import {Method, SchemaInterface} from "../../schema-client"; + +class StopProcessSchema implements SchemaInterface { + method = Method.POST + url = '/system-monitoring/processes/{id}/stop' + contentType = null; +} + +export let stopProcessSchema = new StopProcessSchema() +export default stopProcessSchema diff --git a/api/sm/sm-client.ts b/api/sm/sm-client.ts index 0f64415..8759255 100644 --- a/api/sm/sm-client.ts +++ b/api/sm/sm-client.ts @@ -11,6 +11,10 @@ import {RepeatProcessRequest} from "./requests/repeat-process"; import repeatProcessSchema from "./schemas/repeat-process"; import processOutputSchema from "./schemas/process-output"; import {ProcessOutputRequest} from "./requests/process-ouput"; +import playProcessSchema from "./schemas/play-process"; +import pauseProcessSchema from "./schemas/pause-process"; +import stopProcessSchema from "./schemas/stop-process"; +import killProcessSchema from "./schemas/kill-process"; export class SMClient { schemaClient: SchemaClient @@ -21,10 +25,10 @@ export class SMClient { }) } - async getCommands(): Promise { + async getCommands(): Promise> { let { responseData, headers } = await this.schemaClient.send(commandsSchema, {}) return { - data: responseData as CommandResponseInterface[], + data: responseData, headers: headers } } @@ -37,10 +41,10 @@ export class SMClient { } } - async getProcesses(data: GetProcessesRequest | PaginationRequest): Promise { + async getProcesses(data: GetProcessesRequest | PaginationRequest): Promise> { let { responseData, headers } = await this.schemaClient.send(processesSchema, data) return { - data: responseData as ProcessesResponseInterface[], + data: responseData, headers: headers } } @@ -60,6 +64,38 @@ export class SMClient { headers: headers } } + + async playProcess(id: string): Promise { + let { headers } = await this.schemaClient.send(playProcessSchema, {id}) + return { + data: null, + headers: headers + } + } + + async pauseProcess(id: string): Promise { + let { headers } = await this.schemaClient.send(pauseProcessSchema, {id}) + return { + data: null, + headers: headers + } + } + + async stopProcess(id: string): Promise { + let { headers } = await this.schemaClient.send(stopProcessSchema, {id}) + return { + data: null, + headers: headers + } + } + + async killProcess(id: string): Promise { + let { headers } = await this.schemaClient.send(killProcessSchema, {id}) + return { + data: null, + headers: headers + } + } } export let smClient = new SMClient diff --git a/components/elements/processes/index.tsx b/components/elements/processes/index.tsx index 4b4e786..a6fb5b3 100644 --- a/components/elements/processes/index.tsx +++ b/components/elements/processes/index.tsx @@ -13,62 +13,23 @@ import FactCheckOutlined from "@mui/icons-material/FactCheckOutlined" import {IconButton, LinearProgress, TableContainer, Table, TableBody, TableCell, TableHead, TableRow, TablePagination} from "@mui/material" import ConfirmDialog from "../confirm-dialog"; import smClient from "../../../api/sm/sm-client"; +import {ProcessesResponseInterface, Status} from "../../../api/sm/responses/processes"; -export interface ProcessExceptionInterface { - message: string, - file: string, - line: number, - code: number, - trace: any[], -} - -export interface ProcessProgressInterface { - total: number, - progress: number, - memory: any[], - remaining: string - percent: number -} - -export interface ProcessInterface { - id: string, - lock: string | null, - containerUuid: string | null, - pid: bigint | null, - name: string, - options: Record, - arguments: Record, - exception: ProcessExceptionInterface | null, - progress: ProcessProgressInterface | null, - outputId: string | null, - createdAt: string, - updatedAt: string | null, - startedAt: string | null, - pausedAt: string | null, - cancelledAt: string | null - completedAt: string | null - status: Status - canPlay: boolean - canPause: boolean - canRepeat: boolean - canStop: boolean - canKill: boolean -} - -enum Status { - Wait = 'wait', - Running = 'running', - Cancelled = 'cancelled', - Error = 'error', - Success = 'success', +enum Action { + Repeat, + Stop, + Kill, + Play, + Pause, } export default function Processes() { - const [processes, setProcesses] = useState([]); + const [processes, setProcesses] = useState([]); const [page, setPage] = useState(0); const [count, setCount] = useState(0); const [open, setOpen] = useState(false); - const [selectedProcess, setSelectedProcess] = useState(null); + const [action, setAction] = useState(null); + const [selectedProcess, setSelectedProcess] = useState(null); let refreshLock = false let refreshProcesses = async () => { @@ -84,7 +45,7 @@ export default function Processes() { setCount(Number(headers.get('X-Pagination-Count'))) refreshLock = false } - let output = async (process: ProcessInterface) => { + let output = async (process: ProcessesResponseInterface) => { const { data: output } = await smClient.getProcessOutput({ id: process.id }) @@ -101,19 +62,20 @@ export default function Processes() { return () => clearInterval(timer); }, [page]); - let isFinished = (process: ProcessInterface) => process.cancelledAt || process.completedAt + let isFinished = (process: ProcessesResponseInterface) => process.cancelledAt || process.completedAt const handleChangePage = (event: any, page: number) => { setPage(page); } - const openRepeatDialog = (process: ProcessInterface) => { + const openDialog = (process: ProcessesResponseInterface, action: Action) => { setSelectedProcess(process) setOpen(true) + setAction(action) } let lock = false - const repeat = async (dialogId: string) => { + const agreeCallback = async (dialogId: string) => { if (lock) { return } @@ -122,14 +84,33 @@ export default function Processes() { } lock = true - await smClient.repeatProcess({ - id: selectedProcess.id, - requestId: dialogId - }) + if (action === Action.Repeat) { + await smClient.repeatProcess({ + id: selectedProcess.id, + requestId: dialogId + }) + } + + if (action === Action.Stop) { + await smClient.stopProcess(selectedProcess.id) + } + + if (action === Action.Kill) { + await smClient.killProcess(selectedProcess.id) + } + + if (action === Action.Play) { + await smClient.playProcess(selectedProcess.id) + } + + if (action === Action.Pause) { + await smClient.pauseProcess(selectedProcess.id) + } lock = false setSelectedProcess(null) setOpen(false) + setAction(null) } return ( @@ -146,7 +127,7 @@ export default function Processes() { - {processes.map((process: ProcessInterface, index: number) => ( + {processes.map((process: ProcessesResponseInterface, index: number) => ( {process.name} @@ -156,13 +137,13 @@ export default function Processes() { {process.progress && {`${process.progress.progress}`} / {`${process.progress.total}`} - {process.progress.percent}% [{process.progress.memory}] / {process.progress.remaining} } - {process.canPlay && + {process.canPlay && openDialog(process, Action.Play)} title={`Play`} aria-label="Play"> } - {process.canPause && + {process.canPause && openDialog(process, Action.Pause)} title={`Pause`} aria-label="Pause"> } - {process.canStop && + {process.canStop && openDialog(process, Action.Stop)} title={`Stop`} aria-label="Stop"> } @@ -184,10 +165,10 @@ export default function Processes() { } - {process.canRepeat && openRepeatDialog(process)} title={`Repeat`} aria-label="Repeat"> + {process.canRepeat && openDialog(process, Action.Repeat)} title={`Repeat`} aria-label="Repeat"> } - {process.canKill && + {process.canKill && openDialog(process, Action.Kill)} title={`Kill`} aria-label="Kill"> } {process?.outputId && output(process)} aria-label="Output"> @@ -209,7 +190,7 @@ export default function Processes() { page={page} onPageChange={handleChangePage} /> - { + { setSelectedProcess(null) setOpen(false) }}/>