feat: 未知进度不显示进度条并增加任务详情

This commit is contained in:
2026-10-25 05:46:55 +08:00
parent 4cf64957c5
commit 85e6653da4
5 changed files with 194 additions and 10 deletions
+11
View File
@@ -81,6 +81,7 @@ class DownloadJob:
"completed": self.completed, "completed": self.completed,
"failed": self.failed, "failed": self.failed,
"writtenRows": self.written_rows, "writtenRows": self.written_rows,
"fileBytes": _file_bytes(self.csv_path),
"csvPath": str(self.csv_path), "csvPath": str(self.csv_path),
"failures": self.failures[-20:], "failures": self.failures[-20:],
"createdAt": self.created_at, "createdAt": self.created_at,
@@ -122,6 +123,15 @@ class DownloadJob:
await subscriber.put(event) await subscriber.put(event)
def _file_bytes(path: str | Path) -> int:
if not path:
return 0
try:
return Path(path).stat().st_size
except OSError:
return 0
def _set_event() -> asyncio.Event: def _set_event() -> asyncio.Event:
event = asyncio.Event() event = asyncio.Event()
event.set() event.set()
@@ -353,6 +363,7 @@ class JobManager:
"completed": finished or data.get("completed") or 0, "completed": finished or data.get("completed") or 0,
"failed": data.get("failed") or 0, "failed": data.get("failed") or 0,
"writtenRows": written or data.get("writtenRows") or 0, "writtenRows": written or data.get("writtenRows") or 0,
"fileBytes": _file_bytes(data.get("csvPath") or ""),
"csvPath": data.get("csvPath"), "csvPath": data.get("csvPath"),
"failures": data.get("failures") or [], "failures": data.get("failures") or [],
"createdAt": data.get("createdAt"), "createdAt": data.get("createdAt"),
+39
View File
@@ -402,6 +402,45 @@ def test_refresh_saved_session_rewrites_cookie(tmp_path: Path, monkeypatch):
assert stored[0]["cookies"][0]["value"] == "kept" assert stored[0]["cookies"][0]["value"] == "kept"
def test_snapshot_reports_csv_size(tmp_path: Path):
csv_path = tmp_path / "out.csv"
csv_path.write_bytes(b"abcdef")
job = DownloadJob(
id="20260921120009",
directory=tmp_path,
network="NR",
city_code="JM",
city_name="江门",
month="2026-09",
groups=["rsrp"],
concurrency=2,
grid_ids=["1", "2"],
csv_path=csv_path,
created_at="2026-09-21T12:00:09",
whole_city=True,
listing_done=False,
discovered=4,
)
snap = job.snapshot()
assert snap["fileBytes"] == 6
assert snap["listingDone"] is False
assert snap["total"] == 4
missing = DownloadJob(
id="20260921120010",
directory=tmp_path,
network="LTE",
city_code="JM",
city_name="江门",
month="2026-09",
groups=["rsrp"],
concurrency=1,
grid_ids=[],
csv_path=tmp_path / "missing.csv",
created_at="",
)
assert missing.snapshot()["fileBytes"] == 0
def test_task_folder_uses_second_timestamp(tmp_path: Path, monkeypatch): def test_task_folder_uses_second_timestamp(tmp_path: Path, monkeypatch):
monkeypatch.setattr(job_module, "DATA_DIR", tmp_path / "jobs") monkeypatch.setattr(job_module, "DATA_DIR", tmp_path / "jobs")
@@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from "vue"; import { computed, onMounted, onUnmounted, ref } from "vue";
import { NButton, NEmpty, NIcon, NInputNumber, NProgress, NTag, NTooltip, useMessage } from "naive-ui"; import { NButton, NEmpty, NIcon, NInputNumber, NProgress, NTag, NTooltip, useMessage } from "naive-ui";
import { FolderOpenOutline, HelpCircleOutline, PauseOutline, PlayOutline, StopCircleOutline } from "@vicons/ionicons5"; import { ChevronBackOutline, FolderOpenOutline, HelpCircleOutline, PauseOutline, PlayOutline, StopCircleOutline } from "@vicons/ionicons5";
import { ApiError, openLocal } from "../../shared/http"; import { ApiError, openLocal } from "../../shared/http";
import { cancelJob, loadDownloadSettings, loadJobs, pauseJob, resumeJob, saveDownloadSettings, type DownloadJob } from "./api"; import { cancelJob, loadDownloadSettings, loadJobs, pauseJob, resumeJob, saveDownloadSettings, type DownloadJob } from "./api";
@@ -10,11 +10,13 @@ const message = useMessage();
const jobs = ref<DownloadJob[]>([]); const jobs = ref<DownloadJob[]>([]);
const maxJobs = ref(2); const maxJobs = ref(2);
const tab = ref<"active" | "done">("active"); const tab = ref<"active" | "done">("active");
const openedId = ref("");
let timer = 0; let timer = 0;
const activeJobs = computed(() => jobs.value.filter((job) => job.status !== "done")); const activeJobs = computed(() => jobs.value.filter((job) => job.status !== "done"));
const doneJobs = computed(() => jobs.value.filter((job) => job.status === "done")); const doneJobs = computed(() => jobs.value.filter((job) => job.status === "done"));
const visible = computed(() => (tab.value === "active" ? activeJobs.value : doneJobs.value)); const visible = computed(() => (tab.value === "active" ? activeJobs.value : doneJobs.value));
const opened = computed(() => jobs.value.find((job) => job.id === openedId.value) ?? null);
const statusText: Record<string, string> = { const statusText: Record<string, string> = {
queued: "等待中", queued: "等待中",
@@ -56,11 +58,35 @@ function scanning(job: DownloadJob) {
return Boolean(job.wholeCity) && job.listingDone === false; return Boolean(job.wholeCity) && job.listingDone === false;
} }
function hasProgress(job: DownloadJob) {
return !scanning(job) && job.total > 0;
}
function percent(job: DownloadJob) { function percent(job: DownloadJob) {
if (scanning(job) || !job.total) return 0; if (!hasProgress(job)) return 0;
return Math.min(100, Math.round((job.completed / job.total) * 100)); return Math.min(100, Math.round((job.completed / job.total) * 100));
} }
function formatSize(bytes?: number) {
const size = bytes || 0;
if (size < 1024) return `${size} B`;
if (size < 1024 * 1024) return `${(size / 1024).toFixed(1)} KB`;
if (size < 1024 * 1024 * 1024) return `${(size / 1024 / 1024).toFixed(1)} MB`;
return `${(size / 1024 / 1024 / 1024).toFixed(2)} GB`;
}
function gridLabel(job: DownloadJob) {
return job.gridSize === "GRID50" ? "50米" : "20米";
}
function openDetail(job: DownloadJob) {
openedId.value = job.id;
}
function closeDetail() {
openedId.value = "";
}
function countText(job: DownloadJob) { function countText(job: DownloadJob) {
const written = `已写入 ${job.writtenRows} 行`; const written = `已写入 ${job.writtenRows} 行`;
const progress = scanning(job) const progress = scanning(job)
@@ -124,10 +150,48 @@ async function openDir(job: DownloadJob) {
<template> <template>
<section class="page"> <section class="page">
<header class="page-head"> <header class="page-head">
<div> <div class="head-title">
<h2>下载管理</h2> <n-button v-if="opened" quaternary size="small" @click="closeDetail">
<template #icon><n-icon><ChevronBackOutline /></n-icon></template>
返回
</n-button>
<h2>{{ opened ? opened.name : "下载管理" }}</h2>
</div> </div>
<div class="head-actions"> <div v-if="opened" class="head-actions">
<n-tag size="small" :type="opened.status === 'running' ? 'info' : opened.status === 'done' ? 'success' : opened.status === 'paused' ? 'warning' : 'default'">
{{ statusLabel(opened) }}
</n-tag>
<template v-if="opened.status === 'running'">
<n-button v-if="opened.message !== '正在停止'" size="small" @click="pause(opened)">
<template #icon><n-icon><PauseOutline /></n-icon></template>
暂停
</n-button>
<n-button size="small" @click="stop(opened)">
<template #icon><n-icon><StopCircleOutline /></n-icon></template>
停止
</n-button>
</template>
<template v-else-if="opened.status === 'paused'">
<n-button size="small" type="primary" secondary @click="resume(opened)">
<template #icon><n-icon><PlayOutline /></n-icon></template>
继续
</n-button>
<n-button size="small" @click="stop(opened)">
<template #icon><n-icon><StopCircleOutline /></n-icon></template>
停止
</n-button>
</template>
<n-button v-else-if="opened.status === 'queued'" size="small" @click="stop(opened)">取消</n-button>
<n-button v-else-if="opened.status !== 'done'" size="small" type="primary" secondary @click="resume(opened)">
<template #icon><n-icon><PlayOutline /></n-icon></template>
继续
</n-button>
<n-button size="small" quaternary @click="openFile(opened)">打开</n-button>
<n-button size="small" quaternary @click="openDir(opened)">
<template #icon><n-icon><FolderOpenOutline /></n-icon></template>
</n-button>
</div>
<div v-else class="head-actions">
<span class="limit-label">同时任务</span> <span class="limit-label">同时任务</span>
<n-tooltip> <n-tooltip>
<template #trigger> <template #trigger>
@@ -145,7 +209,42 @@ async function openDir(job: DownloadJob) {
/> />
</div> </div>
</header> </header>
<div class="tabs"> <div v-if="opened" class="task-list">
<div class="detail">
<n-progress
v-if="hasProgress(opened)"
:percentage="percent(opened)"
:processing="opened.status === 'running'"
/>
<div class="form-row"><label>任务号</label><span>{{ opened.id }}</span></div>
<div class="form-row"><label>状态</label><span>{{ statusLabel(opened) }}</span></div>
<div class="form-row"><label>说明</label><span>{{ opened.message || "—" }}</span></div>
<div class="form-row"><label>网络</label><span>{{ opened.network === "NR" ? "5G" : "4G" }}</span></div>
<div class="form-row"><label>地市</label><span>{{ opened.cityName }}</span></div>
<div class="form-row"><label>月份</label><span>{{ opened.month }}</span></div>
<div class="form-row"><label>栅格</label><span>{{ gridLabel(opened) }}</span></div>
<div class="form-row"><label>范围</label><span>{{ opened.wholeCity ? "整个地市" : "区域表" }}</span></div>
<div class="form-row"><label>并发</label><span>{{ opened.concurrency || 1 }}</span></div>
<div class="form-row">
<label>进度</label>
<span v-if="scanning(opened)">已完成 {{ opened.completed }},已读取 {{ opened.discovered || 0 }}</span>
<span v-else>{{ opened.completed }}/{{ opened.total }}</span>
</div>
<div class="form-row"><label>失败</label><span>{{ opened.failed }}</span></div>
<div class="form-row"><label>已写入</label><span>{{ opened.writtenRows }} 行</span></div>
<div class="form-row"><label>文件大小</label><span>{{ formatSize(opened.fileBytes) }}</span></div>
<div class="form-row"><label>开始时间</label><span>{{ opened.createdAt.replace("T", " ") }}</span></div>
<div v-if="opened.duplicateCount" class="form-row"><label>重复</label><span>已去掉 {{ opened.duplicateCount }} 个</span></div>
<div class="form-row form-top">
<label>保存位置</label>
<span class="path">{{ opened.csvPath }}</span>
</div>
<ul v-if="opened.failures?.length" class="fail-list">
<li v-for="item in opened.failures" :key="item.id">{{ item.id }}:{{ item.message }}</li>
</ul>
</div>
</div>
<div v-else class="tabs">
<button class="tab" :class="{ on: tab === 'active' }" type="button" @click="tab = 'active'"> <button class="tab" :class="{ on: tab === 'active' }" type="button" @click="tab = 'active'">
未完成<em>{{ activeJobs.length }}</em> 未完成<em>{{ activeJobs.length }}</em>
</button> </button>
@@ -153,19 +252,20 @@ async function openDir(job: DownloadJob) {
已完成<em>{{ doneJobs.length }}</em> 已完成<em>{{ doneJobs.length }}</em>
</button> </button>
</div> </div>
<div class="task-list"> <div v-if="!opened" class="task-list">
<article <article
v-for="job in visible" v-for="job in visible"
:key="job.id" :key="job.id"
class="task" class="task clickable"
:class="{ focus: job.id === props.focusId }" :class="{ focus: job.id === props.focusId }"
@click="openDetail(job)"
> >
<div class="task-top"> <div class="task-top">
<div> <div>
<h3>{{ job.name }}</h3> <h3>{{ job.name }}</h3>
<div class="sub">{{ countText(job) }}</div> <div class="sub">{{ countText(job) }}</div>
</div> </div>
<div class="task-actions"> <div class="task-actions" @click.stop>
<n-tag size="small" :type="job.status === 'running' ? 'info' : job.status === 'done' ? 'success' : job.status === 'paused' ? 'warning' : 'default'"> <n-tag size="small" :type="job.status === 'running' ? 'info' : job.status === 'done' ? 'success' : job.status === 'paused' ? 'warning' : 'default'">
{{ statusLabel(job) }} {{ statusLabel(job) }}
</n-tag> </n-tag>
@@ -203,6 +303,7 @@ async function openDir(job: DownloadJob) {
</div> </div>
</div> </div>
<n-progress <n-progress
v-if="hasProgress(job)"
style="margin-top: 10px" style="margin-top: 10px"
:percentage="percent(job)" :percentage="percent(job)"
:show-indicator="true" :show-indicator="true"
@@ -210,7 +311,7 @@ async function openDir(job: DownloadJob) {
/> />
<div class="task-foot"> <div class="task-foot">
<span class="sub">{{ job.message }}</span> <span class="sub">{{ job.message }}</span>
<span class="sub">失败 {{ job.failed }}</span> <span class="sub">失败 {{ job.failed }} · {{ formatSize(job.fileBytes) }}</span>
</div> </div>
<ul v-if="job.failures?.length" class="fail-list"> <ul v-if="job.failures?.length" class="fail-list">
<li v-for="item in job.failures.slice(0, 3)" :key="item.id">{{ item.id }}:{{ item.message }}</li> <li v-for="item in job.failures.slice(0, 3)" :key="item.id">{{ item.id }}:{{ item.message }}</li>
+3
View File
@@ -21,6 +21,8 @@ export interface DownloadJob {
network: string; network: string;
cityName: string; cityName: string;
month: string; month: string;
gridSize?: string;
concurrency?: number;
wholeCity?: boolean; wholeCity?: boolean;
listingDone?: boolean; listingDone?: boolean;
discovered?: number; discovered?: number;
@@ -28,6 +30,7 @@ export interface DownloadJob {
completed: number; completed: number;
failed: number; failed: number;
writtenRows: number; writtenRows: number;
fileBytes?: number;
duplicateCount?: number; duplicateCount?: number;
csvPath: string; csvPath: string;
failures: { id: string; message: string }[]; failures: { id: string; message: string }[];
+30
View File
@@ -253,6 +253,13 @@ select {
border-bottom: 1px solid var(--line); border-bottom: 1px solid var(--line);
} }
.head-title {
display: flex;
align-items: center;
gap: 8px;
min-width: 0;
}
.page-head h2 { .page-head h2 {
margin: 0; margin: 0;
font-size: 18px; font-size: 18px;
@@ -358,6 +365,29 @@ select {
padding: 12px 14px; padding: 12px 14px;
} }
.task.clickable {
cursor: pointer;
}
.task.clickable:hover {
border-color: var(--accent);
}
.detail {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 10px;
padding: 16px;
}
.detail .n-progress {
margin-bottom: 16px;
}
.path {
word-break: break-all;
}
.task.focus { .task.focus {
border-color: var(--accent); border-color: var(--accent);
box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent);