feat: 输出日志单独成步并记住上次配置
This commit is contained in:
@@ -5,6 +5,7 @@ import { del, formatTime, getJSON, postJSON } from '../api'
|
||||
const props = defineProps({
|
||||
task: { type: Object, required: true },
|
||||
logOpen: { type: Boolean, default: false },
|
||||
focusLog: { type: Boolean, default: false },
|
||||
})
|
||||
const emit = defineEmits(['changed'])
|
||||
|
||||
@@ -27,7 +28,7 @@ const countText = computed(() => {
|
||||
})
|
||||
|
||||
watch(() => [props.task.message, props.task.done, props.task.status], () => {
|
||||
if (expanded.value) loadLog()
|
||||
if (props.focusLog || expanded.value) loadLog()
|
||||
})
|
||||
|
||||
function onExpand(names) {
|
||||
@@ -36,7 +37,7 @@ function onExpand(names) {
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
if (props.logOpen) loadLog()
|
||||
if (props.logOpen || props.focusLog) loadLog()
|
||||
})
|
||||
|
||||
async function loadLog() {
|
||||
@@ -94,8 +95,12 @@ async function remove() {
|
||||
<n-button v-if="task.active && !task.stop_requested" size="small" @click="stop">停止任务</n-button>
|
||||
<n-button v-if="!task.active" size="small" @click="confirming = true">删除任务</n-button>
|
||||
</div>
|
||||
<p class="muted">成果目录:{{ task.output }}</p>
|
||||
<n-collapse :default-expanded-names="logOpen ? ['log'] : []" @update:expanded-names="onExpand">
|
||||
<p v-if="!focusLog" class="muted">成果目录:{{ task.output }}</p>
|
||||
<div v-if="focusLog" class="log-box focus">
|
||||
<n-alert v-if="logError" type="error">{{ logError }}</n-alert>
|
||||
<pre>{{ log || '等待任务输出……' }}</pre>
|
||||
</div>
|
||||
<n-collapse v-else :default-expanded-names="logOpen ? ['log'] : []" @update:expanded-names="onExpand">
|
||||
<n-collapse-item title="运行日志" name="log">
|
||||
<n-alert v-if="logError" type="error">{{ logError }}</n-alert>
|
||||
<div class="log-box"><pre>{{ log || '等待任务输出……' }}</pre></div>
|
||||
@@ -133,6 +138,7 @@ header span, .message { margin: 0; }
|
||||
border-radius: 6px;
|
||||
padding: 8px;
|
||||
}
|
||||
.log-box.focus { max-height: min(52vh, 480px); min-height: 220px; }
|
||||
pre {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
|
||||
+130
-25
@@ -38,7 +38,6 @@ const step = ref(1)
|
||||
const options = ref([])
|
||||
const marketError = ref('')
|
||||
const error = ref('')
|
||||
const success = ref('')
|
||||
const submitting = ref(false)
|
||||
const opening = ref(false)
|
||||
const editingBoundary = ref(false)
|
||||
@@ -46,9 +45,11 @@ const editingProject = ref(false)
|
||||
const settingsOpen = ref(false)
|
||||
const loadingMarkets = ref(false)
|
||||
const lastTask = ref(null)
|
||||
const FORM_KEY = 'marketgrid.market-form'
|
||||
let marketTicket = 0
|
||||
let marketTimer = 0
|
||||
let pollTimer = 0
|
||||
let persistReady = false
|
||||
|
||||
const neededSourceKeys = computed(() => {
|
||||
const keys = new Set(alwaysSources.value)
|
||||
@@ -68,7 +69,8 @@ const fileItems = computed(() => {
|
||||
}
|
||||
return items
|
||||
})
|
||||
const canStart = computed(() => draft.ready && options.value.length > 0 && !submitting.value)
|
||||
const taskBusy = computed(() => !!lastTask.value?.active)
|
||||
const canStart = computed(() => draft.ready && options.value.length > 0 && !submitting.value && !taskBusy.value)
|
||||
const clipOptions = [
|
||||
{ label: '与网格相交', value: 'intersects' },
|
||||
{ label: '中心点在网格内', value: 'center' },
|
||||
@@ -85,21 +87,86 @@ function onFileAction(item, key) {
|
||||
if (item.key === 'GIS_PROJECT_SOURCE' && key === 'project') editingProject.value = true
|
||||
}
|
||||
|
||||
function persistForm() {
|
||||
if (!persistReady) return
|
||||
const payload = {
|
||||
paths: { ...draft.paths },
|
||||
allMarkets: draft.allMarkets,
|
||||
chosen: [...draft.chosen],
|
||||
maxWorkers: draft.maxWorkers,
|
||||
scale: draft.scale,
|
||||
summary: draft.summary,
|
||||
maxRaster: draft.maxRaster,
|
||||
summaryRaster: draft.summaryRaster,
|
||||
clipMode: draft.clipMode,
|
||||
padding: draft.padding,
|
||||
lastTaskId: draft.lastTaskId,
|
||||
layerPlan: JSON.parse(JSON.stringify(layerPlan)),
|
||||
step: step.value,
|
||||
}
|
||||
localStorage.setItem(FORM_KEY, JSON.stringify(payload))
|
||||
}
|
||||
|
||||
function restoreForm() {
|
||||
let saved
|
||||
try {
|
||||
saved = JSON.parse(localStorage.getItem(FORM_KEY) || 'null')
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
if (!saved || typeof saved !== 'object') return
|
||||
if (saved.paths && typeof saved.paths === 'object') Object.assign(draft.paths, saved.paths)
|
||||
if (typeof saved.allMarkets === 'boolean') draft.allMarkets = saved.allMarkets
|
||||
if (Array.isArray(saved.chosen)) draft.chosen = saved.chosen
|
||||
for (const key of ['maxWorkers', 'scale', 'maxRaster', 'summaryRaster', 'padding']) {
|
||||
if (typeof saved[key] === 'number') draft[key] = saved[key]
|
||||
}
|
||||
if (typeof saved.summary === 'boolean') draft.summary = saved.summary
|
||||
if (saved.clipMode === 'intersects' || saved.clipMode === 'center') draft.clipMode = saved.clipMode
|
||||
if (typeof saved.lastTaskId === 'string') draft.lastTaskId = saved.lastTaskId
|
||||
if (saved.layerPlan && typeof saved.layerPlan === 'object') {
|
||||
for (const [key, value] of Object.entries(saved.layerPlan)) {
|
||||
if (!value || typeof value !== 'object') continue
|
||||
const enabled = !!value.enabled
|
||||
layerPlan[key] = { enabled, visible: enabled && !!value.visible }
|
||||
}
|
||||
}
|
||||
if (saved.step >= 1 && saved.step <= 4) step.value = saved.step
|
||||
draft.ready = true
|
||||
}
|
||||
|
||||
function setLayerEnabled(key, enabled) {
|
||||
layerPlan[key].enabled = enabled
|
||||
if (!enabled) layerPlan[key].visible = false
|
||||
}
|
||||
|
||||
function nextStep() {
|
||||
function validateThrough(through) {
|
||||
error.value = ''
|
||||
if (step.value === 2) {
|
||||
if (through >= 1 && !outputLayers.value.some((item) => layerPlan[item.key]?.enabled)) {
|
||||
error.value = '请至少选择一个要生成的图层。'
|
||||
step.value = 1
|
||||
return false
|
||||
}
|
||||
if (through >= 2) {
|
||||
for (const item of fileItems.value) {
|
||||
if (!draft.paths[item.key]) {
|
||||
error.value = `请先填写${item.label}。`
|
||||
return
|
||||
step.value = 2
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
function goStep(target) {
|
||||
if (target < 1 || target > 4 || target === step.value) return
|
||||
if (target > step.value && !validateThrough(target - 1)) return
|
||||
step.value = target
|
||||
}
|
||||
|
||||
function nextStep() {
|
||||
if (!validateThrough(step.value) || step.value >= 3) return
|
||||
step.value += 1
|
||||
}
|
||||
|
||||
@@ -112,11 +179,11 @@ async function ensureCatalog() {
|
||||
for (const item of outputLayers.value) {
|
||||
if (!layerPlan[item.key]) layerPlan[item.key] = { enabled: true, visible: item.visible }
|
||||
}
|
||||
if (!draft.ready) {
|
||||
for (const item of data.sources) draft.paths[item.key] = item.path
|
||||
draft.paths[data.nr_csv.key] = data.nr_csv.path
|
||||
draft.ready = true
|
||||
for (const item of data.sources) {
|
||||
if (!(item.key in draft.paths)) draft.paths[item.key] = item.path
|
||||
}
|
||||
if (data.nr_csv && !(data.nr_csv.key in draft.paths)) draft.paths[data.nr_csv.key] = data.nr_csv.path
|
||||
draft.ready = true
|
||||
}
|
||||
|
||||
async function loadMarkets(path) {
|
||||
@@ -159,11 +226,28 @@ async function refreshTask() {
|
||||
if (!draft.lastTaskId) return
|
||||
try {
|
||||
lastTask.value = await getJSON(`/api/tasks/${encodeURIComponent(draft.lastTaskId)}`)
|
||||
} catch {
|
||||
// 轮询失败时保留上一帧。
|
||||
} catch (reason) {
|
||||
if (String(reason.message || '').includes('不存在')) {
|
||||
lastTask.value = null
|
||||
draft.lastTaskId = ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreTask() {
|
||||
if (!draft.lastTaskId) {
|
||||
try {
|
||||
const data = await getJSON('/api/tasks')
|
||||
const running = (data.tasks || []).find((item) => item.kind === 'market' && item.active)
|
||||
if (running) draft.lastTaskId = running.id
|
||||
} catch {
|
||||
// 没有任务列表时仍显示已保存的配置。
|
||||
}
|
||||
}
|
||||
await refreshTask()
|
||||
if (lastTask.value?.active) step.value = 4
|
||||
}
|
||||
|
||||
function numbersReady() {
|
||||
return [draft.maxWorkers, draft.scale, draft.maxRaster, draft.summaryRaster, draft.padding]
|
||||
.every((value) => typeof value === 'number')
|
||||
@@ -171,7 +255,10 @@ function numbersReady() {
|
||||
|
||||
async function submit() {
|
||||
error.value = ''
|
||||
success.value = ''
|
||||
if (taskBusy.value) {
|
||||
step.value = 4
|
||||
return
|
||||
}
|
||||
if (!canStart.value) return
|
||||
if (!numbersReady()) {
|
||||
error.value = '请填写完整参数。'
|
||||
@@ -206,7 +293,7 @@ async function submit() {
|
||||
},
|
||||
})
|
||||
draft.lastTaskId = result.id
|
||||
success.value = '任务已启动,可在任务中心查看进度。'
|
||||
step.value = 4
|
||||
await refreshTask()
|
||||
} catch (reason) {
|
||||
error.value = reason.message
|
||||
@@ -220,14 +307,21 @@ watch(() => draft.paths.MARKET_GPKG, (path) => {
|
||||
marketTimer = setTimeout(() => loadMarkets(path), 300)
|
||||
})
|
||||
|
||||
watch(draft, persistForm, { deep: true })
|
||||
watch(layerPlan, persistForm, { deep: true })
|
||||
watch(step, persistForm)
|
||||
|
||||
onMounted(async () => {
|
||||
restoreForm()
|
||||
try {
|
||||
await ensureCatalog()
|
||||
await loadMarkets(draft.paths.MARKET_GPKG)
|
||||
await restoreTask()
|
||||
} catch (reason) {
|
||||
error.value = reason.message
|
||||
}
|
||||
await refreshTask()
|
||||
persistReady = true
|
||||
persistForm()
|
||||
pollTimer = setInterval(refreshTask, 2000)
|
||||
})
|
||||
|
||||
@@ -249,12 +343,16 @@ onUnmounted(() => {
|
||||
</n-button>
|
||||
</teleport>
|
||||
<n-steps :current="step" size="small">
|
||||
<n-step title="图层选择" />
|
||||
<n-step title="导入数据" />
|
||||
<n-step title="选择网格" />
|
||||
<n-step title="图层选择" class="step-hit" @click="goStep(1)" />
|
||||
<n-step title="导入数据" class="step-hit" @click="goStep(2)" />
|
||||
<n-step title="选择网格" class="step-hit" @click="goStep(3)" />
|
||||
<n-step title="输出日志" class="step-hit" @click="goStep(4)" />
|
||||
</n-steps>
|
||||
<n-alert v-if="error" type="error">{{ error }}</n-alert>
|
||||
<n-alert v-if="success" type="success">{{ success }}</n-alert>
|
||||
<n-alert v-if="taskBusy && step !== 4" type="info">
|
||||
正在处理{{ lastTask.message ? `:${lastTask.message}` : '' }}。
|
||||
<n-button text type="primary" @click="step = 4">查看日志</n-button>
|
||||
</n-alert>
|
||||
<div v-show="step === 1" class="layer-form">
|
||||
<div class="layer-head">
|
||||
<span>生成</span>
|
||||
@@ -309,9 +407,18 @@ onUnmounted(() => {
|
||||
<p v-else-if="options.length" class="muted">将处理全部 {{ options.length }} 个网格</p>
|
||||
<p v-else class="muted">请先导入可用的网格边界。</p>
|
||||
</template>
|
||||
<n-button type="primary" :disabled="!canStart" :loading="submitting" @click="submit">开始生成</n-button>
|
||||
<n-button type="primary" :disabled="!canStart" :loading="submitting" @click="submit">{{ taskBusy ? '正在处理' : '开始生成' }}</n-button>
|
||||
<p v-if="taskBusy" class="muted">当前任务还在处理。完成后可以按这次的选择再生成一次。</p>
|
||||
</div>
|
||||
</n-spin>
|
||||
<div v-show="step === 4" class="log-step">
|
||||
<TaskCard v-if="lastTask" :task="lastTask" focus-log @changed="refreshTask" />
|
||||
<p v-else class="muted">还没有处理记录。在「选择网格」里开始生成后,日志会显示在这里。</p>
|
||||
<div v-if="lastTask && !lastTask.active && lastTask.output" class="result-open">
|
||||
<n-button type="primary" :loading="opening" @click="openResult">打开目录</n-button>
|
||||
<span class="muted">{{ lastTask.output }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="wizard-nav">
|
||||
<n-button v-if="step > 1" @click="step -= 1">上一步</n-button>
|
||||
<n-button v-if="step < 3" type="primary" @click="nextStep">下一步</n-button>
|
||||
@@ -356,11 +463,6 @@ onUnmounted(() => {
|
||||
</n-space>
|
||||
</template>
|
||||
</n-modal>
|
||||
<div v-if="lastTask && !lastTask.active && lastTask.output" class="result-open">
|
||||
<n-button type="primary" :loading="opening" @click="openResult">打开目录</n-button>
|
||||
<span class="muted">{{ lastTask.output }}</span>
|
||||
</div>
|
||||
<TaskCard v-if="lastTask" :task="lastTask" log-open @changed="refreshTask" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -375,7 +477,10 @@ onUnmounted(() => {
|
||||
}
|
||||
.layer-head { color: #66788a; font-size: 12px; border-bottom: 1px solid #f0f3f6; }
|
||||
.layer-row + .layer-row { border-top: 1px solid #f0f3f6; }
|
||||
.wizard-nav { display: flex; justify-content: flex-end; gap: 8px; }
|
||||
.wizard-nav { display: flex; justify-content: space-between; gap: 8px; }
|
||||
.wizard-nav:not(:has(button + button)) { justify-content: flex-end; }
|
||||
.step-hit { cursor: pointer; }
|
||||
.log-step { display: flex; flex-direction: column; gap: 12px; }
|
||||
.result-open { display: flex; align-items: center; gap: 12px; }
|
||||
.result-open .muted { word-break: break-all; }
|
||||
.market-box { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
|
||||
|
||||
Reference in New Issue
Block a user