import type { Server, ServerType } from "./data" import { MAP_CARD_H, MAP_CARD_W, MAP_LANES, MAP_WAN_H, MAP_WAN_W, NETWORK_MAP_PIPELINE_Y } from "./network-map-layout" export interface MapPoint { x: number; y: number } export interface DensityGroup extends MapPoint { id: string kind: "role" | "wan" type: ServerType site: string label: string count: number problemCount: number unknownCount: number serverIds: string[] routerId?: string expanded: boolean } export function densityWanKey(serverId: string, index: number) { return `${serverId}:${index}` } const groupLimit = 6 const roleLane = { "home-router": "home", "jump-host": "jh", "exit-node": "en", "ix-node": "en" } as const const roleLabel = { "home-router": "Домашние роутеры", "jump-host": "Узлы доступа", "exit-node": "Интернет-выходы", "ix-node": "IX" } as const type Lane = "home" | "jh" | "en" | "wan" type Row = { point: MapPoint; height: number; place: (point: MapPoint) => void } type Block = { id: string; center: number; rows: Row[]; grouped?: boolean } // Keep a group's header with its children; the world may grow below the original viewport. function placeColumn(blocks: Block[], width: number) { const gap = 12 blocks.sort((a, b) => a.center - b.center || a.id.localeCompare(b.id)) const needsLayout = blocks.some(block => block.grouped) || blocks.some(({ rows: [row] }, i) => { for (let j = i - 1; j >= 0; j--) { const previous = blocks[j].rows[0] if (row.point.y - previous.point.y >= row.height) break if (Math.abs(row.point.x - previous.point.x) < width) return true } return false }) if (!needsLayout) { for (const { rows: [row] } of blocks) row.place(row.point) return } let bottom = 40 for (const { center, rows } of blocks) { const height = rows.reduce((sum, row) => sum + row.height, gap * (rows.length - 1)) let top = Math.max(bottom + gap, center - height / 2) for (const row of rows) { row.place({ x: row.point.x, y: top + row.height / 2 }) top += row.height + gap } bottom = top - gap } } export function projectNetworkMapDensity( servers: readonly Server[], baseNodePos: Record, baseWanPos: Record, expanded: ReadonlySet, reveal: { serverIds?: ReadonlySet; wanKeys?: ReadonlySet } = {}, ) { const groups: DensityGroup[] = [] const hiddenServers = new Map() const hiddenWans = new Map() const nodePos = { ...baseNodePos } const wanSatPos = { ...baseWanPos } const columns: Record = { home: [], jh: [], en: [], wan: [] } const cohorts = new Map() const siteOf = (s: Server) => s.site?.trim() === "—" ? "" : s.site?.trim() || "" const serverPoint = (s: Server) => baseNodePos[s.id] ?? { x: MAP_LANES[roleLane[s.type]].cx, y: NETWORK_MAP_PIPELINE_Y } const serverRow = (s: Server): Row => ({ point: serverPoint(s), height: MAP_CARD_H, place: p => { nodePos[s.id] = p }, }) const groupRow = (g: DensityGroup): Row => ({ point: g, height: MAP_CARD_H, place: p => { g.x = p.x; g.y = p.y }, }) const revealWan = (s: Server) => s.wanUplinks?.some((_, index) => reveal.wanKeys?.has(densityWanKey(s.id, index))) for (const s of servers) { const id = `role:${s.type}:${siteOf(s)}` const cohort = cohorts.get(id) if (cohort) cohort.push(s) else cohorts.set(id, [s]) } for (const [id, members] of cohorts) { const first = members[0], column = columns[roleLane[first.type]] if (members.length <= groupLimit) { for (const s of members) column.push({ id: s.id, center: serverPoint(s).y, rows: [serverRow(s)] }) continue } const site = siteOf(first) const center = members.reduce((ys, s) => ys + serverPoint(s).y, 0) / members.length const open = expanded.has(id) || members.some(s => reveal.serverIds?.has(s.id) || revealWan(s)) const group: DensityGroup = { id, kind: "role", type: first.type, site, label: `${site || "Без площадки"} · ${roleLabel[first.type]}`, count: members.length, expanded: open, x: serverPoint(first).x, y: center, problemCount: members.filter(s => s.status === "offline" || s.status === "degraded").length, unknownCount: members.filter(s => s.status === "unknown").length, serverIds: members.map(s => s.id), } groups.push(group) const rows = [groupRow(group)] if (open) { for (const s of members.sort((a, b) => a.name.localeCompare(b.name, "ru", { numeric: true }) || a.id.localeCompare(b.id))) rows.push(serverRow(s)) } else { for (const s of members) hiddenServers.set(s.id, id) } column.push({ id, center, rows, grouped: true }) } for (const lane of ["home", "jh", "en"] as const) placeColumn(columns[lane], MAP_CARD_W) const groupById = new Map(groups.map(g => [g.id, g])) for (const [serverId, groupId] of hiddenServers) { const { x, y } = groupById.get(groupId)! nodePos[serverId] = { x, y } } for (const router of servers) { const uplinks = router.wanUplinks ?? [] if (router.type !== "home-router" || !uplinks.length) continue wanSatPos[router.id] = [...(baseWanPos[router.id] ?? [])] const ownerGroup = hiddenServers.get(router.id) if (ownerGroup) { uplinks.forEach((_, index) => { hiddenWans.set(densityWanKey(router.id, index), ownerGroup) wanSatPos[router.id][index] = nodePos[router.id] }) continue } const rows: Row[] = uplinks.map((_, index) => ({ point: baseWanPos[router.id]?.[index] ?? { x: MAP_LANES.wan.cx, y: nodePos[router.id].y }, height: MAP_WAN_H, place: p => { wanSatPos[router.id][index] = p }, })) if (rows.length <= groupLimit) { rows.forEach((row, i) => columns.wan.push({ id: densityWanKey(router.id, i), center: row.point.y, rows: [row] })) continue } const id = "wan:" + router.id const open = expanded.has(id) || Boolean(revealWan(router)) const group: DensityGroup = { id, kind: "wan", type: router.type, site: siteOf(router), label: "WAN · " + router.name, routerId: router.id, count: rows.length, expanded: open, x: rows[0].point.x, y: nodePos[router.id].y, serverIds: [router.id], // The WAN catalog has no per-uplink operational status. problemCount: 0, unknownCount: rows.length, } groups.push(group) const header = groupRow(group) if (!open) { uplinks.forEach((_, index) => hiddenWans.set(densityWanKey(router.id, index), id)) header.place = p => { group.y = p.y; rows.forEach(row => row.place(p)) } } columns.wan.push({ id, center: group.y, rows: open ? [header, ...rows] : [header], grouped: true }) } placeColumn(columns.wan, MAP_WAN_W) return { groups, hiddenServers, hiddenWans, nodePos, wanSatPos } }