From ccf9373203bc9472693d1e7f0c89dd7a05828cf4 Mon Sep 17 00:00:00 2001 From: Fdefined <55788435+FU-design@users.noreply.github.com> Date: Thu, 18 Jul 2024 23:30:44 +0800 Subject: [PATCH] BIGTOP-4164: Close sse connection when task log window closed (#21) --- bigtop-manager-ui/src/api/request.ts | 2 + bigtop-manager-ui/src/api/sse/index.ts | 11 +- .../src/components/job-info/job.vue | 138 +++-------------- .../src/components/job-info/task-log.vue | 141 ++++++++++++++++++ bigtop-manager-ui/src/utils/tools.ts | 8 + 5 files changed, 177 insertions(+), 123 deletions(-) create mode 100644 bigtop-manager-ui/src/components/job-info/task-log.vue diff --git a/bigtop-manager-ui/src/api/request.ts b/bigtop-manager-ui/src/api/request.ts index 3a471b52..6b431304 100644 --- a/bigtop-manager-ui/src/api/request.ts +++ b/bigtop-manager-ui/src/api/request.ts @@ -79,6 +79,8 @@ request.interceptors.response.use( message.error(i18n.global.t('common.error_network')) } else if (error.code === AxiosError.ETIMEDOUT) { message.error(i18n.global.t('common.error_timeout')) + } else if (error.code === AxiosError.ERR_CANCELED) { + return } else { console.log(error) message.error(i18n.global.t('common.error_unknown')) diff --git a/bigtop-manager-ui/src/api/sse/index.ts b/bigtop-manager-ui/src/api/sse/index.ts index c07ad277..52ee663e 100644 --- a/bigtop-manager-ui/src/api/sse/index.ts +++ b/bigtop-manager-ui/src/api/sse/index.ts @@ -17,20 +17,25 @@ * under the License. */ +import axios, { type AxiosProgressEvent, type CancelTokenSource } from 'axios' import request from '@/api/request.ts' -import { AxiosProgressEvent } from 'axios' export const getLogs = ( clusterId: number, id: number, func: Function -): Promise => { - return request({ +): { promise: Promise; cancel: () => void } => { + const source: CancelTokenSource = axios.CancelToken.source() + + const promise = request({ method: 'get', url: `/sse/clusters/${clusterId}/tasks/${id}/log`, responseType: 'stream', timeout: 0, + cancelToken: source.token, onDownloadProgress: (progressEvent: AxiosProgressEvent) => func(progressEvent) }) + + return { promise, cancel: source.cancel } } diff --git a/bigtop-manager-ui/src/components/job-info/job.vue b/bigtop-manager-ui/src/components/job-info/job.vue index 9ca85680..d2a2b34a 100644 --- a/bigtop-manager-ui/src/components/job-info/job.vue +++ b/bigtop-manager-ui/src/components/job-info/job.vue @@ -18,12 +18,10 @@ --> + + + + diff --git a/bigtop-manager-ui/src/utils/tools.ts b/bigtop-manager-ui/src/utils/tools.ts index 2a884bf1..033fc370 100644 --- a/bigtop-manager-ui/src/utils/tools.ts +++ b/bigtop-manager-ui/src/utils/tools.ts @@ -47,3 +47,11 @@ export const copyText = (text: string): Promise => { } }) } + +export const scrollToBottom = (container: HTMLElement | null) => { + if (!container) { + return + } + const { clientHeight, scrollHeight } = container + container.scrollTop = scrollHeight - clientHeight +}