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":