🔨 动态计算使用时间.

This commit is contained in:
lijiahang
2024-03-20 11:43:59 +08:00
parent 30349cb362
commit 3d853eb7d4
4 changed files with 41 additions and 14 deletions

View File

@@ -1,8 +1,8 @@
import type { IDisposable, ITerminalOptions, ITerminalInitOnlyOptions } from 'xterm'; import type { IDisposable, ITerminalOptions, ITerminalInitOnlyOptions } from 'xterm';
import { Terminal } from 'xterm'; import type { Terminal } from 'xterm';
import { FitAddon } from 'xterm-addon-fit'; import type { FitAddon } from 'xterm-addon-fit';
import { SearchAddon } from 'xterm-addon-search'; import type { SearchAddon } from 'xterm-addon-search';
import { CanvasAddon } from 'xterm-addon-canvas'; import type { CanvasAddon } from 'xterm-addon-canvas';
// appender 配置 // appender 配置
export const AppenderOptions: ITerminalOptions & ITerminalInitOnlyOptions = { export const AppenderOptions: ITerminalOptions & ITerminalInitOnlyOptions = {

View File

@@ -105,7 +105,6 @@ export default class LogAppender implements ILogAppender {
// 自适应 // 自适应
fit(): void { fit(): void {
Object.values(this.appenderRel).forEach(s => { Object.values(this.appenderRel).forEach(s => {
console.log(s);
s.addons?.fit?.fit(); s.addons?.fit?.fit();
}); });
} }

View File

@@ -109,7 +109,7 @@ export function formatDuration(start: number, end?: number): string {
} }
const duration = (end - start) / 1000; const duration = (end - start) / 1000;
if (duration < 1) { if (duration < 1) {
return `${duration.toFixed(1)}s`; return `${Number.parseFloat(duration.toFixed(1))}s`;
} }
const minutes = Math.floor(duration / 60); const minutes = Math.floor(duration / 60);
const seconds = Math.floor(duration % 60); const seconds = Math.floor(duration % 60);

View File

@@ -24,16 +24,16 @@
import type { ExecCommandResponse } from '@/api/exec/exec'; import type { ExecCommandResponse } from '@/api/exec/exec';
import { onUnmounted, ref, nextTick } from 'vue'; import { onUnmounted, ref, nextTick } from 'vue';
import { getExecLogStatus } from '@/api/exec/exec-log'; import { getExecLogStatus } from '@/api/exec/exec-log';
import { execStatus } from '@/views/exec/exec-log/types/const'; import { execHostStatus, execStatus } from '@/views/exec/exec-log/types/const';
import LogPanelHost from './log-panel-host.vue'; import LogPanelHost from './log-panel-host.vue';
import LogPanelView from './log-panel-view.vue'; import LogPanelView from './log-panel-view.vue';
import { useDictStore } from '@/store';
const emits = defineEmits(['back']); const emits = defineEmits(['back']);
const logContainer = ref(); const logContainer = ref();
const currentHostExecId = ref(); const currentHostExecId = ref();
const intervalId = ref(); const statusIntervalId = ref();
const finishIntervalId = ref();
const command = ref<ExecCommandResponse>(); const command = ref<ExecCommandResponse>();
// 打开 // 打开
@@ -41,7 +41,9 @@
command.value = record; command.value = record;
currentHostExecId.value = record.hosts[0].id; currentHostExecId.value = record.hosts[0].id;
// 注册状态轮询 // 注册状态轮询
intervalId.value = setInterval(fetchTaskStatus, 5000); statusIntervalId.value = setInterval(fetchTaskStatus, 5000);
// 注册完成时间轮询
finishIntervalId.value = setInterval(setTaskFinishTime, 1000);
// 打开日志 // 打开日志
nextTick(() => { nextTick(() => {
logContainer.value?.open(); logContainer.value?.open();
@@ -66,7 +68,7 @@
if (hostStatus) { if (hostStatus) {
host.status = hostStatus.status; host.status = hostStatus.status;
host.startTime = hostStatus.startTime; host.startTime = hostStatus.startTime;
host.finishTime = hostStatus.finishTime || Date.now(); host.finishTime = hostStatus.finishTime;
host.exitStatus = hostStatus.exitStatus; host.exitStatus = hostStatus.exitStatus;
host.errorMessage = hostStatus.errorMessage; host.errorMessage = hostStatus.errorMessage;
} }
@@ -78,6 +80,24 @@
} }
}; };
// 设置完成时间
const setTaskFinishTime = () => {
const hosts = command.value?.hosts;
if (!hosts) {
return;
}
hosts.forEach(s => {
// 未完成自动设置完成时间为当前时间 用于展示使用时间
if (s.status === execHostStatus.WAITING ||
s.status === execHostStatus.RUNNING) {
if (!s.startTime) {
s.startTime = Date.now();
}
s.finishTime = Date.now();
}
});
};
defineExpose({ open }); defineExpose({ open });
// 选中主机 // 选中主机
@@ -89,16 +109,24 @@
const closeClient = () => { const closeClient = () => {
// 关闭日志 // 关闭日志
logContainer.value?.closeClient(); logContainer.value?.closeClient();
// 关闭状态轮询 // 清理轮询
clearInterval(intervalId.value); clearAllInterval();
}; };
// 清理并且关闭 // 清理并且关闭
const closeAll = () => { const closeAll = () => {
// 关闭日志 // 关闭日志
logContainer.value?.closeAll(); logContainer.value?.closeAll();
// 清理轮询
clearAllInterval();
};
// 清理轮询
const clearAllInterval = () => {
// 关闭状态轮询 // 关闭状态轮询
clearInterval(intervalId.value); clearInterval(statusIntervalId.value);
// 关闭使用时间轮询
clearInterval(finishIntervalId.value);
}; };
onUnmounted(closeAll); onUnmounted(closeAll);