feat(console): CHV production-grade frontend — worker availability, attachment guard, queue hint, build:console fix, vitest
This commit is contained in:
1 parent
3cf1562176
commit
31fdcc51e0
13 files changed
+473
-54
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { chvAvailability, chvQueueHint, countChvWorkers, isWorkerAlive } from "./chv";
|
||||
|
||||
// 纯逻辑测试:只测 chv.ts,不渲染组件、不发请求、不读 localStorage。
|
||||
// alive 规则与 WorkersSection 现有口径一致:alive 字段优先,
|
||||
// 缺失时用 last_seen 距今 <= 120 秒判定在线。
|
||||
describe("isWorkerAlive", () => {
|
||||
it("alive 字段优先", () => {
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", alive: true })).toBe(true);
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", alive: false, last_seen: Date.now() / 1000 })).toBe(false);
|
||||
});
|
||||
it("无 alive 时用 last_seen 120 秒门槛", () => {
|
||||
const now = Date.now() / 1000;
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen: now - 10 })).toBe(true);
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor", last_seen: now - 121 })).toBe(false);
|
||||
expect(isWorkerAlive({ executor: "cloud-hypervisor" })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("countChvWorkers", () => {
|
||||
it("只数 cloud-hypervisor,不数 docker/local", () => {
|
||||
const now = Date.now() / 1000;
|
||||
expect(countChvWorkers([
|
||||
{ executor: "cloud-hypervisor", alive: true },
|
||||
{ executor: "cloud-hypervisor", last_seen: now - 9999 },
|
||||
{ executor: "docker", alive: true },
|
||||
])).toEqual({ online: 1, total: 2 });
|
||||
});
|
||||
});
|
||||
|
||||
describe("chvAvailability", () => {
|
||||
it("有清单时按清单数", () => {
|
||||
expect(chvAvailability([{ executor: "cloud-hypervisor", alive: true }], "", "")).toEqual(
|
||||
{ state: "known", online: 1, total: 1 },
|
||||
);
|
||||
});
|
||||
it("无清单但聚合文案时是未知,不是 0", () => {
|
||||
expect(chvAvailability(null, "在线 2 / 离线 1(共 3;完整 worker 清单仅全局管理员可见)", "")).toEqual(
|
||||
{ state: "unknown", reason: "aggregate-only" },
|
||||
);
|
||||
});
|
||||
it("无 worker 注册时是已知 0", () => {
|
||||
expect(chvAvailability(null, "无 worker 注册", "")).toEqual({ state: "known", online: 0, total: 0 });
|
||||
});
|
||||
it("加载失败时是未知", () => {
|
||||
expect(chvAvailability(null, "", "加载失败:x")).toEqual({ state: "unknown", reason: "load-failed" });
|
||||
});
|
||||
it("尚未加载时是未知", () => {
|
||||
expect(chvAvailability(null, "未加载", "")).toEqual({ state: "unknown", reason: "not-loaded" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("chvQueueHint", () => {
|
||||
it("仅 CHV + 排队 + 已知零在线时给提示", () => {
|
||||
const knownZero = { state: "known" as const, online: 0, total: 0 };
|
||||
expect(chvQueueHint("cloud-hypervisor", "queued", knownZero)).toContain("可能原因");
|
||||
expect(chvQueueHint("docker", "queued", knownZero)).toBe("");
|
||||
expect(chvQueueHint("cloud-hypervisor", "running", knownZero)).toBe("");
|
||||
expect(chvQueueHint("cloud-hypervisor", "queued", { state: "unknown" as const, reason: "aggregate-only" })).toBe("");
|
||||
expect(chvQueueHint("cloud-hypervisor", "queued", { state: "known" as const, online: 1, total: 1 })).toBe("");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { Worker } from "./types";
|
||||
import { workerAgeText } from "./client";
|
||||
|
||||
// CHV 可用性纯逻辑:不发请求、不拼 URL,只从 ctx 已有的
|
||||
// /workers 数据推断。非管理员的 /workers 只返回聚合数
|
||||
// (workers_total/online/offline),没有按执行器的分项,
|
||||
// 此时 CHV 在线数是未知,绝不能把未知写成 0,也绝不能
|
||||
// 把聚合在线数当成 CHV 在线数。
|
||||
export function isWorkerAlive(w: Worker): boolean {
|
||||
if (w.alive !== undefined && w.alive !== null) return !!w.alive;
|
||||
const info = workerAgeText(w);
|
||||
return info.age !== null && info.age <= 120;
|
||||
}
|
||||
|
||||
export function countChvWorkers(workers: Worker[]): { online: number; total: number } {
|
||||
const chv = workers.filter((w) => w.executor === "cloud-hypervisor");
|
||||
return { online: chv.filter(isWorkerAlive).length, total: chv.length };
|
||||
}
|
||||
|
||||
export type ChvAvailability =
|
||||
| { state: "known"; online: number; total: number }
|
||||
| { state: "unknown"; reason: "not-loaded" | "aggregate-only" | "load-failed" };
|
||||
|
||||
export function chvAvailability(
|
||||
workers: Worker[] | null,
|
||||
workersSummary: string,
|
||||
workersError: string,
|
||||
): ChvAvailability {
|
||||
if (workers) return { state: "known", ...countChvWorkers(workers) };
|
||||
if (workersError) return { state: "unknown", reason: "load-failed" };
|
||||
if (workersSummary.includes("无 worker 注册")) return { state: "known", online: 0, total: 0 };
|
||||
if (workersSummary.includes("完整 worker 清单仅全局管理员可见")) {
|
||||
return { state: "unknown", reason: "aggregate-only" };
|
||||
}
|
||||
return { state: "unknown", reason: "not-loaded" };
|
||||
}
|
||||
|
||||
// 排队原因提示:只在“已知零 CHV worker 在线 + CHV 任务排队”时
|
||||
// 给出,文案明确写“可能原因”,且不编造后端未返回的
|
||||
// VM 状态/IP/快照。未知时返回空串(不提示、不猜测)。
|
||||
export function chvQueueHint(
|
||||
executor: string | undefined | null,
|
||||
status: string | undefined | null,
|
||||
availability: ChvAvailability,
|
||||
): string {
|
||||
if (executor !== "cloud-hypervisor" || status !== "queued") return "";
|
||||
if (availability.state === "known" && availability.online === 0) {
|
||||
return "可能原因:当前无 cloud-hypervisor worker 在线,任务在排队(基于 /workers 推断;VM 状态/IP/快照等后端未返回字段不展示)。请先确认 KVM 宿主上的 CHV worker 已起,worker 上线后可认领。";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
Reference in new issue
Block a user