SPB Git

Repository Public

group.ts 2.0 KB 46 lines typescript

1import type { Schemas } from "@/api/client";23type Volume = Schemas["Volume"];4export interface NodeVolumes {5  nodeId: string;6  alias: string;7  volumes: Volume[];8  /** Sum over volumes that report both figures; `counted` says how many did. */9  usedBytes: number;10  totalBytes: number;11  counted: number;12}1314/** Used share of a volume in percent, or undefined when either figure was not observed. */15export function usedPct(v: Pick<Volume, "used_bytes" | "total_bytes">): number | undefined {16  if (v.used_bytes === undefined || v.total_bytes === undefined || v.total_bytes <= 0) return undefined;17  return (v.used_bytes / v.total_bytes) * 100;18}1920/** Volumes grouped by node (alias order), each group sorted by mount point. */21export function groupByNode(volumes: readonly Volume[]): NodeVolumes[] {22  const groups = new Map<string, NodeVolumes>();23  for (const v of volumes) {24    let g = groups.get(v.node_id);25    if (!g) groups.set(v.node_id, (g = { nodeId: v.node_id, alias: v.node_alias, volumes: [], usedBytes: 0, totalBytes: 0, counted: 0 }));26    g.volumes.push(v);27    // network mounts and vanished volumes are not local capacity28    if (v.kind !== "network" && !v.gone_at && v.used_bytes !== undefined && v.total_bytes !== undefined) {29      g.usedBytes += v.used_bytes;30      g.totalBytes += v.total_bytes;31      g.counted++;32    }33  }34  const out = [...groups.values()].sort((a, b) => a.alias.localeCompare(b.alias, "en", { numeric: true, sensitivity: "base" }));35  for (const g of out) g.volumes.sort((a, b) => a.mount.localeCompare(b.mount));36  return out;37}3839/** Host field of the NAS form: an IPv4 or IPv6 literal (the contract takes an address, not a name). */40export function isIpAddress(value: string): boolean {41  const v = value.trim();42  const v4 = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(v);43  if (v4) return v4.slice(1).every((p) => Number(p) <= 255);44  return v.includes(":") && /^[0-9a-f:.]+$/i.test(v) && v.split(":").length >= 3 && v.split(":").length <= 8 && !/:::/.test(v);45}46
Files main
Menu

Theme