Files
NqiTools/desktop/src/features/download/GridDownloadView.vue
T

258 lines
8.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { onMounted, ref, watch } from "vue";
import {
NButton,
NCheckbox,
NCheckboxGroup,
NDatePicker,
NIcon,
NInputNumber,
NSelect,
NTooltip,
useMessage,
} from "naive-ui";
import { HelpCircleOutline } from "@vicons/ionicons5";
import { ApiError, chooseDirectory } from "../../shared/http";
import { createJob, loadMeta, type Meta } from "./api";
const emit = defineEmits<{ started: [id: string] }>();
const message = useMessage();
const meta = ref<Meta | null>(null);
const network = ref("NR");
const cityCode = ref("JM");
const month = ref<string | null>(previousMonth());
const concurrency = ref(8);
const groups = ref<string[]>([]);
const outputDir = ref("");
const file = ref<File | null>(null);
const fileInput = ref<HTMLInputElement | null>(null);
const submitting = ref(false);
const gridSize = ref("GRID20");
const sizeOptions = [
{ label: "20米", value: "GRID20" },
{ label: "50米", value: "GRID50" },
];
const networkOptions = [
{ label: "5G", value: "NR" },
{ label: "4G", value: "LTE" },
];
function previousMonth() {
const date = new Date();
date.setDate(1);
date.setMonth(date.getMonth() - 1);
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;
}
onMounted(async () => {
try {
meta.value = await loadMeta();
syncGroups();
} catch (error) {
message.error(error instanceof ApiError ? error.message : "读取下载项目失败");
}
});
watch(network, syncGroups);
function syncGroups() {
const items = meta.value?.groups[network.value] || [];
groups.value = items.some((item) => item.id === "rsrp") ? ["rsrp"] : [];
}
function onFile(event: Event) {
const input = event.target as HTMLInputElement;
const selected = input.files?.[0] ?? null;
input.value = "";
if (selected && !selected.name.toLowerCase().endsWith(".csv")) {
message.warning("区域表只支持 CSV");
file.value = null;
return;
}
file.value = selected;
}
function clearFile() {
file.value = null;
if (fileInput.value) fileInput.value = "";
}
async function downloadSample() {
const content = "\uFEFF20米栅格ID\r\n541927061\r\n544536471\r\n";
if (!("__TAURI_INTERNALS__" in window)) {
const blob = new Blob([content], { type: "text/csv;charset=utf-8" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "区域表示例.csv";
link.click();
URL.revokeObjectURL(link.href);
return;
}
const { save } = await import("@tauri-apps/plugin-dialog");
const selected = await save({
defaultPath: "区域表示例.csv",
filters: [{ name: "CSV", extensions: ["csv"] }],
});
if (!selected) return;
try {
const { invoke } = await import("@tauri-apps/api/core");
await invoke("write_sample_csv", { path: selected });
message.success("例程已保存");
} catch {
message.error("例程保存失败");
}
}
async function pickDir() {
const selected = await chooseDirectory();
if (selected) outputDir.value = selected;
}
function clearOutput() {
outputDir.value = "";
}
async function start() {
if (file.value && !file.value.name.toLowerCase().endsWith(".csv")) {
message.warning("区域表只支持 CSV");
return;
}
if (!month.value || !/^\d{4}-(0[1-9]|1[0-2])$/.test(month.value)) {
message.warning("请选择月份");
return;
}
const form = new FormData();
form.append("grid_size", gridSize.value);
if (file.value) form.append("file", file.value);
form.append("network", network.value);
form.append("city_code", cityCode.value);
form.append("month", month.value);
form.append("groups", JSON.stringify(groups.value));
form.append("concurrency", String(concurrency.value || 8));
form.append("output_dir", outputDir.value.trim());
submitting.value = true;
try {
const job = await createJob(form);
const removed = job.duplicateCount || 0;
if (job.status === "queued") message.success("已加入等待");
else if (!file.value) message.success("已开始下载整个地市");
else message.success(removed ? `已开始下载,去掉 ${removed} 个重复栅格` : "已开始下载");
emit("started", job.id);
} catch (error) {
message.error(error instanceof ApiError ? error.message : "创建下载失败");
} finally {
submitting.value = false;
}
}
</script>
<template>
<section class="page">
<header class="page-head">
<div>
<h2>栅格下载</h2>
</div>
<div class="head-actions">
<n-button type="primary" :loading="submitting" @click="start">开始下载</n-button>
</div>
</header>
<div class="page-body">
<div class="panel">
<div class="form-row">
<label>栅格</label>
<n-select v-model:value="gridSize" :options="sizeOptions" />
</div>
<div class="form-row">
<label>网络</label>
<n-select v-model:value="network" :options="networkOptions" />
</div>
<div class="form-row">
<label>地市</label>
<n-select
v-model:value="cityCode"
:options="(meta?.cities || []).map((city) => ({ label: city.name, value: city.code }))"
/>
</div>
<div class="form-row">
<label>
月份
<n-tooltip>
<template #trigger>
<n-icon class="hint" :size="15"><HelpCircleOutline /></n-icon>
</template>
选择要下载的月份。
</n-tooltip>
</label>
<n-date-picker v-model:formatted-value="month" type="month" value-format="yyyy-MM" />
</div>
<div class="form-row">
<label>
并发
<n-tooltip>
<template #trigger>
<n-icon class="hint" :size="15"><HelpCircleOutline /></n-icon>
</template>
同时下载的栅格数量。
</n-tooltip>
</label>
<n-input-number v-model:value="concurrency" :min="1" :max="32" />
</div>
<div class="form-row form-top">
<label>
项目
<n-tooltip>
<template #trigger>
<n-icon class="hint" :size="15"><HelpCircleOutline /></n-icon>
</template>
选择要导出的指标。
</n-tooltip>
</label>
<n-checkbox-group v-model:value="groups" class="checks">
<div v-for="item in meta?.groups[network] || []" :key="item.id" class="check-line">
<n-checkbox :value="item.id">{{ item.label }}</n-checkbox>
<n-tooltip>
<template #trigger>
<n-icon class="hint" :size="14" style="margin-left: 4px"><HelpCircleOutline /></n-icon>
</template>
{{ item.columns.join("、") }}
</n-tooltip>
</div>
</n-checkbox-group>
</div>
<div class="form-row">
<label>
区域表
<n-tooltip>
<template #trigger>
<n-icon class="hint" :size="15"><HelpCircleOutline /></n-icon>
</template>
不选文件则边读取边下载该地市的全部栅格,同时下载的数量由并发决定。选择文件时只支持 CSV。
</n-tooltip>
</label>
<div class="captcha-line">
<n-button tag="label">
选择文件
<input ref="fileInput" type="file" accept=".csv,text/csv" hidden @change="onFile" />
</n-button>
<n-button v-if="file" quaternary @click="clearFile">清除</n-button>
<n-button quaternary @click="downloadSample">下载例程</n-button>
<span class="file-name">{{ file?.name || "不选则下载整个地市" }}</span>
</div>
</div>
<div class="form-row">
<label>保存到</label>
<div class="captcha-line">
<div class="path-text" :class="{ placeholder: !outputDir }" :title="outputDir || '默认目录'">
{{ outputDir || "默认目录" }}
</div>
<n-button @click="pickDir">选择</n-button>
<n-button :disabled="!outputDir" @click="clearOutput">清除</n-button>
</div>
</div>
</div>
</div>
</section>
</template>