Files
NqiTools/desktop/src/features/download/api.ts
T

95 lines
2.6 KiB
TypeScript

import { ApiError, api } from "../../shared/http";
export interface GroupOption {
id: string;
label: string;
columns: string[];
}
export interface Meta {
networks: { value: string; label: string }[];
cities: { name: string; code: string }[];
groups: Record<string, GroupOption[]>;
fixed: { grid: string[]; cell: Record<string, string[]> };
defaultOutputDir?: string;
}
export interface DownloadJob {
id: string;
name: string;
status: string;
message: string;
network: string;
cityName: string;
month: string;
gridSize?: string;
concurrency?: number;
wholeCity?: boolean;
listingDone?: boolean;
discovered?: number;
total: number;
completed: number;
failed: number;
writtenRows: number;
fileBytes?: number;
duplicateCount?: number;
csvPath: string;
failures: { id: string; message: string }[];
createdAt?: string | null;
stopping?: boolean;
}
export function loadMeta() {
return api<Meta>("/api/download/meta");
}
export function loadJobs() {
return api<{ jobs: DownloadJob[] }>("/api/download/jobs");
}
export async function loadActiveDownloadCount() {
try {
const result = await api<{ count?: number }>("/api/download/active");
if (typeof result?.count === "number" && Number.isFinite(result.count)) return result.count;
} catch (error) {
if (error instanceof ApiError && (error.status === 401 || error.status === 0)) throw error;
}
const result = await loadJobs();
return result.jobs.filter((job) => job.status === "running").length;
}
export function createJob(form: FormData) {
return api<DownloadJob>("/api/download/jobs", { method: "POST", body: form });
}
export function resumeJob(id: string) {
return api<DownloadJob>(`/api/download/jobs/${id}/resume`, { method: "POST" });
}
export function pauseJob(id: string) {
return api<DownloadJob>(`/api/download/jobs/${id}/pause`, { method: "POST" });
}
export function updateConcurrency(id: string, concurrency: number) {
return api<DownloadJob>(`/api/download/jobs/${id}/concurrency`, {
method: "PUT",
body: JSON.stringify({ concurrency }),
});
}
export function deleteJob(id: string, files: boolean) {
const flag = files ? "true" : "false";
return api<unknown>(`/api/download/jobs/${encodeURIComponent(id)}?files=${flag}`, { method: "DELETE" });
}
export function loadDownloadSettings() {
return api<{ maxConcurrentJobs: number }>("/api/download/settings");
}
export function saveDownloadSettings(maxConcurrentJobs: number) {
return api<{ maxConcurrentJobs: number }>("/api/download/settings", {
method: "PUT",
body: JSON.stringify({ maxConcurrentJobs }),
});
}