ea4adc7ca4
- 项目首页保留部署状态与日志入口,发布和管理功能收进更多菜单。 - 统一记录步骤与日志,限制轮询、日志渲染和助手短会话开销。 - 接入只读 Agent 问答、来源与个人历史,补充权限和并发回归。
410 lines
10 KiB
Vue
410 lines
10 KiB
Vue
<script setup lang="ts">
|
||
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'
|
||
import { Refresh } from '@element-plus/icons-vue'
|
||
import { devopsApi } from '../services/devopsApi'
|
||
import type { JenkinsBuildLog, PipelineStep, ReleaseRun } from '../types/devops'
|
||
import StatusPill from './StatusPill.vue'
|
||
|
||
const props = defineProps<{ steps: PipelineStep[]; run?: ReleaseRun }>()
|
||
const selectedNode = ref('')
|
||
const outputMode = ref<'step' | 'jenkins'>('step')
|
||
const autoTail = ref(true)
|
||
const logLoading = ref(false)
|
||
const logError = ref('')
|
||
const logText = ref('')
|
||
const foldedLines = ref(0)
|
||
const logMeta = ref<JenkinsBuildLog | null>(null)
|
||
const terminal = ref<HTMLElement>()
|
||
let timer: ReturnType<typeof setTimeout> | undefined
|
||
let generation = 0
|
||
let disposed = false
|
||
const currentStep = computed(
|
||
() =>
|
||
props.steps.find((step) => ['failed', 'running', 'queued'].includes(step.status)) ||
|
||
[...props.steps].reverse().find((step) => step.status !== 'pending') ||
|
||
props.steps[0],
|
||
)
|
||
const selectedStep = computed(
|
||
() => props.steps.find((step) => step.nodeId === selectedNode.value) || currentStep.value,
|
||
)
|
||
const active = computed(() => props.run?.status === 'queued' || props.run?.status === 'running')
|
||
const hasBuild = computed(
|
||
() => props.run?.jenkinsBuildNumber !== undefined && props.run?.jenkinsBuildNumber !== null,
|
||
)
|
||
const stepOutput = computed(() => {
|
||
const step = selectedStep.value
|
||
if (!step) return ''
|
||
return [
|
||
step.errorSummary,
|
||
step.message,
|
||
step.logExcerpt,
|
||
!step.errorSummary && !step.message && !step.logExcerpt ? step.summary : '',
|
||
]
|
||
.filter(Boolean)
|
||
.join('\n\n')
|
||
})
|
||
const text = computed(() => (outputMode.value === 'step' ? stepOutput.value : logText.value))
|
||
function stopTimer() {
|
||
clearTimeout(timer)
|
||
timer = undefined
|
||
}
|
||
function schedule() {
|
||
stopTimer()
|
||
if (
|
||
disposed ||
|
||
outputMode.value !== 'jenkins' ||
|
||
!autoTail.value ||
|
||
!hasBuild.value ||
|
||
(!active.value && !logMeta.value?.hasMore)
|
||
)
|
||
return
|
||
timer = setTimeout(() => {
|
||
if (document.hidden) {
|
||
schedule()
|
||
return
|
||
}
|
||
void loadLog(false)
|
||
}, 4500)
|
||
}
|
||
function selectStep(step: PipelineStep) {
|
||
selectedNode.value = step.nodeId
|
||
outputMode.value = 'step'
|
||
}
|
||
async function loadLog(replace: boolean) {
|
||
if (!props.run || !hasBuild.value || logLoading.value) return
|
||
stopTimer()
|
||
const runId = props.run.id
|
||
const request = generation
|
||
logLoading.value = true
|
||
logError.value = ''
|
||
try {
|
||
const log = await devopsApi.getJenkinsLog(runId, replace ? 0 : logMeta.value?.nextStart || 0)
|
||
if (disposed || request !== generation) return
|
||
const merged = replace ? log.text : logText.value + log.text
|
||
const lines = merged.split('\n')
|
||
const removed = Math.max(0, lines.length - 600)
|
||
foldedLines.value = (replace ? 0 : foldedLines.value) + removed
|
||
logText.value = lines.slice(removed).join('\n')
|
||
logMeta.value = log
|
||
await nextTick()
|
||
if (autoTail.value && terminal.value) terminal.value.scrollTop = terminal.value.scrollHeight
|
||
} catch (error) {
|
||
if (!disposed && request === generation)
|
||
logError.value = error instanceof Error ? error.message : '日志读取失败,请重试'
|
||
} finally {
|
||
if (!disposed && request === generation) {
|
||
logLoading.value = false
|
||
schedule()
|
||
}
|
||
}
|
||
}
|
||
watch(
|
||
() => [props.run?.id, props.run?.jenkinsBuildNumber],
|
||
() => {
|
||
generation += 1
|
||
stopTimer()
|
||
logLoading.value = false
|
||
logError.value = ''
|
||
logText.value = ''
|
||
logMeta.value = null
|
||
foldedLines.value = 0
|
||
selectedNode.value = ''
|
||
outputMode.value = hasBuild.value ? 'jenkins' : 'step'
|
||
if (hasBuild.value) void loadLog(true)
|
||
},
|
||
{ immediate: true },
|
||
)
|
||
watch(outputMode, () => {
|
||
if (outputMode.value === 'jenkins' && !logMeta.value) void loadLog(true)
|
||
schedule()
|
||
})
|
||
watch([autoTail, active], schedule)
|
||
onBeforeUnmount(() => {
|
||
disposed = true
|
||
generation += 1
|
||
stopTimer()
|
||
})
|
||
</script>
|
||
|
||
<template>
|
||
<div class="execution-viewer">
|
||
<nav class="step-list" aria-label="发布步骤">
|
||
<h3>执行步骤</h3>
|
||
<button
|
||
v-for="(step, index) in steps"
|
||
:key="step.nodeId"
|
||
type="button"
|
||
:class="{ selected: selectedStep?.nodeId === step.nodeId && outputMode === 'step' }"
|
||
:aria-pressed="selectedStep?.nodeId === step.nodeId && outputMode === 'step'"
|
||
@click="selectStep(step)"
|
||
>
|
||
<span class="step-index">{{ index + 1 }}</span
|
||
><span class="step-name"
|
||
>{{ step.name }}<small>{{ step.duration }}</small></span
|
||
><StatusPill :state="step.status" />
|
||
</button>
|
||
<p v-if="!steps.length" class="field-hint">暂无执行步骤。</p>
|
||
</nav>
|
||
<section class="output-panel">
|
||
<div class="output-toolbar">
|
||
<div class="output-tabs" role="group" aria-label="输出类型">
|
||
<button :aria-pressed="outputMode === 'step'" @click="outputMode = 'step'">
|
||
步骤输出</button
|
||
><button
|
||
:aria-pressed="outputMode === 'jenkins'"
|
||
:disabled="!hasBuild"
|
||
@click="outputMode = 'jenkins'"
|
||
>
|
||
构建日志
|
||
</button>
|
||
</div>
|
||
<div v-if="outputMode === 'jenkins'" class="toolbar log-actions">
|
||
<ElCheckbox v-model="autoTail" size="small">自动跟随</ElCheckbox
|
||
><ElButton
|
||
text
|
||
:icon="Refresh"
|
||
:loading="logLoading"
|
||
aria-label="重新读取构建日志"
|
||
title="重新读取构建日志"
|
||
@click="loadLog(true)"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<div v-if="outputMode === 'step' && selectedStep" class="selected-step-header">
|
||
<strong>{{ selectedStep.name }}</strong
|
||
><StatusPill :state="selectedStep.status" />
|
||
</div>
|
||
<div v-if="logError && outputMode === 'jenkins'" class="log-error" role="alert">
|
||
{{ logError
|
||
}}<ElButton text type="danger" :disabled="logLoading" @click="loadLog(!logMeta)"
|
||
>重试读取</ElButton
|
||
>
|
||
</div>
|
||
<div v-if="foldedLines && outputMode === 'jenkins'" class="folded-note">
|
||
已折叠前 {{ foldedLines }} 行,保留最近 600 行。
|
||
</div>
|
||
<pre
|
||
v-if="text"
|
||
ref="terminal"
|
||
class="terminal"
|
||
tabindex="0"
|
||
:aria-label="outputMode === 'jenkins' ? '构建日志,可滚动' : '步骤输出,可滚动'"
|
||
>{{ text }}</pre
|
||
>
|
||
<div v-else class="output-empty" role="status">
|
||
{{
|
||
logLoading
|
||
? '正在读取日志…'
|
||
: outputMode === 'jenkins'
|
||
? '构建尚未产生输出。'
|
||
: '此步骤暂无输出。'
|
||
}}
|
||
</div>
|
||
<div class="output-footer">
|
||
<span>{{
|
||
outputMode === 'jenkins'
|
||
? active && autoTail
|
||
? '执行中 · 每 4.5 秒更新日志'
|
||
: '构建日志'
|
||
: '步骤状态来自服务端'
|
||
}}</span
|
||
><ElButton
|
||
v-if="outputMode === 'jenkins' && logMeta?.hasMore"
|
||
text
|
||
size="small"
|
||
:loading="logLoading"
|
||
@click="loadLog(false)"
|
||
>继续读取</ElButton
|
||
><span v-if="outputMode === 'step' && !hasBuild">构建开始后可查看日志</span>
|
||
</div>
|
||
</section>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.execution-viewer {
|
||
display: grid;
|
||
grid-template-columns: 240px minmax(0, 1fr);
|
||
gap: 20px;
|
||
align-items: start;
|
||
}
|
||
.step-list {
|
||
min-width: 0;
|
||
}
|
||
.step-list h3 {
|
||
margin: 0 0 13px;
|
||
font-size: 12px;
|
||
color: var(--text-muted);
|
||
font-weight: 500;
|
||
}
|
||
.step-list button {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 9px;
|
||
width: 100%;
|
||
padding: 12px 10px;
|
||
margin-bottom: 5px;
|
||
border: 1px solid transparent;
|
||
border-radius: 6px;
|
||
background: transparent;
|
||
text-align: left;
|
||
cursor: pointer;
|
||
}
|
||
.step-list button:hover {
|
||
background: #f8f8fb;
|
||
}
|
||
.step-list button.selected {
|
||
background: #f5f3fa;
|
||
border-color: #e2ddee;
|
||
}
|
||
.step-index {
|
||
display: grid;
|
||
place-items: center;
|
||
width: 20px;
|
||
height: 20px;
|
||
flex-shrink: 0;
|
||
border: 1px solid #e2e3e9;
|
||
border-radius: 50%;
|
||
color: #8b8c97;
|
||
font-size: 10px;
|
||
}
|
||
.step-name {
|
||
flex: 1;
|
||
min-width: 0;
|
||
overflow-wrap: anywhere;
|
||
color: #4c5361;
|
||
font-size: 11px;
|
||
}
|
||
.step-name small {
|
||
display: block;
|
||
margin-top: 5px;
|
||
font-size: 10px;
|
||
color: var(--text-muted);
|
||
}
|
||
.step-list :deep(.el-tag) {
|
||
font-size: 10px;
|
||
height: 20px;
|
||
padding: 0 5px;
|
||
}
|
||
.output-panel {
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-width: 0;
|
||
min-height: 400px;
|
||
border: 1px solid var(--border);
|
||
border-radius: 8px;
|
||
overflow: hidden;
|
||
}
|
||
.output-toolbar {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
flex-wrap: wrap;
|
||
gap: 8px;
|
||
padding: 10px 13px;
|
||
background: #fafafd;
|
||
border-bottom: 1px solid var(--border);
|
||
}
|
||
.output-tabs {
|
||
display: flex;
|
||
gap: 14px;
|
||
}
|
||
.output-tabs button {
|
||
padding: 6px 0;
|
||
border: 0;
|
||
color: var(--text-muted);
|
||
background: transparent;
|
||
font-size: 11px;
|
||
cursor: pointer;
|
||
}
|
||
.output-tabs button[aria-pressed='true'] {
|
||
color: var(--accent);
|
||
font-weight: 600;
|
||
}
|
||
.output-tabs button:disabled {
|
||
opacity: 0.5;
|
||
cursor: default;
|
||
}
|
||
.log-actions {
|
||
gap: 2px;
|
||
}
|
||
.log-actions :deep(.el-checkbox__label) {
|
||
font-size: 10px;
|
||
}
|
||
.selected-step-header {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
padding: 18px;
|
||
font-size: 12px;
|
||
}
|
||
.terminal {
|
||
min-height: 330px;
|
||
max-height: 58vh;
|
||
margin: 0;
|
||
padding: 20px;
|
||
background: #20232e;
|
||
color: #d9dbe6;
|
||
font-family: 'SFMono-Regular', Consolas, monospace;
|
||
font-size: 11px;
|
||
line-height: 1.9;
|
||
overflow: auto;
|
||
white-space: pre-wrap;
|
||
overflow-wrap: anywhere;
|
||
tab-size: 2;
|
||
overscroll-behavior: contain;
|
||
}
|
||
.output-empty {
|
||
display: grid;
|
||
place-items: center;
|
||
min-height: 320px;
|
||
color: var(--text-muted);
|
||
font-size: 12px;
|
||
}
|
||
.output-footer {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 10px;
|
||
margin-top: auto;
|
||
min-height: 40px;
|
||
padding: 10px 13px;
|
||
border-top: 1px solid var(--border);
|
||
color: var(--text-muted);
|
||
font-size: 10px;
|
||
}
|
||
.log-error {
|
||
padding: 10px 16px;
|
||
color: #a94e4e;
|
||
background: #fff6f6;
|
||
font-size: 12px;
|
||
}
|
||
.folded-note {
|
||
padding: 9px 16px;
|
||
background: #f4f3f8;
|
||
color: var(--text-muted);
|
||
font-size: 10px;
|
||
}
|
||
@media (max-width: 760px) {
|
||
.execution-viewer {
|
||
grid-template-columns: 1fr;
|
||
}
|
||
.step-list {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 5px;
|
||
}
|
||
.step-list h3 {
|
||
flex-basis: 100%;
|
||
}
|
||
.step-list button {
|
||
width: auto;
|
||
flex: 1 1 180px;
|
||
}
|
||
.terminal {
|
||
max-height: 50vh;
|
||
}
|
||
}
|
||
</style>
|