From 2c46f11a308d16da24c1170e2d76b470cd3e0722 Mon Sep 17 00:00:00 2001 From: stqfdyr <89149493+stqfdyr@users.noreply.github.com> Date: Fri, 18 Sep 2026 20:28:47 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=9C=AC=E6=9C=88=E7=94=A8?= =?UTF-8?q?=E9=87=8F=E4=BC=98=E5=85=88=E8=AF=BB=20hub=20=E4=B8=8B=E5=8F=91?= =?UTF-8?q?=E7=9A=84=20month=5Fused=EF=BC=8C=E6=97=A7=20hub=20=E4=B8=8A?= =?UTF-8?q?=E4=BB=8D=E6=8C=89=20traffic=5Fmode=20=E8=87=AA=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/api.ts | 2 ++ src/lib/format.test.ts | 1 + src/lib/format.ts | 8 ++++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/lib/api.ts b/src/lib/api.ts index e26f05b..53bc47f 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -52,6 +52,8 @@ export type Node = { total_tx: number month_rx: number month_tx: number + /** This period's usage as the plan meters it (`traffic_mode`). Absent on older hubs. */ + month_used?: number month_start: string day_rx: number day_tx: number diff --git a/src/lib/format.test.ts b/src/lib/format.test.ts index e0858f5..93f3984 100644 --- a/src/lib/format.test.ts +++ b/src/lib/format.test.ts @@ -125,6 +125,7 @@ eq(duration(76 * 86400 + 5), "76 天", "超过一天只写天数") eq(monthUsage({ ...node, traffic_mode: "up" }), 5, "仅上行") eq(monthUsage({ ...node, traffic_mode: "down" }), 3, "仅下行") eq(monthUsage({ ...node, traffic_mode: "max" }), 5, "取较大值") + eq(monthUsage({ ...node, traffic_mode: "up", month_used: 7 }), 7, "hub 算好的值优先") } eq(osName("Debian GNU/Linux 12 (bookworm)"), "Debian 12", "发行版名去掉代号") diff --git a/src/lib/format.ts b/src/lib/format.ts index 24fbe76..9cf94cb 100644 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -77,8 +77,12 @@ export function duration(seconds: number): string { return `${Math.floor(s / 3600)}:${pad(Math.floor((s % 3600) / 60))}:${pad(s % 60)}` } -/** The month's usage counted the way the node's plan meters it. */ -export function monthUsage(node: { month_rx: number; month_tx: number; traffic_mode: string }): number { +/** + * The month's usage counted the way the node's plan meters it. The hub computes + * it; the switch below serves only a hub from before `month_used`. + */ +export function monthUsage(node: { month_rx: number; month_tx: number; month_used?: number; traffic_mode: string }): number { + if (typeof node.month_used === "number") return node.month_used const { month_rx: rx, month_tx: tx } = node switch (node.traffic_mode) { case "up":