Files
MikrotikManager/lib/network-map-columns.ts
T
Denozordec 876a61f705
Docker images / prepare-release (push) Successful in 14s
Docker images / backend-test (push) Successful in 12m24s
Docker images / frontend-image (push) Successful in 2m21s
Docker images / updater-image (push) Successful in 41s
Docker images / netflow-agent-image (push) Successful in 32s
Docker images / backend-image (push) Successful in 1m52s
Docker images / publish-release (push) Successful in 17s
Docker images / notify-webhook (push) Successful in 2s
fix(network-map): перенести визуализацию карты из утверждённого прототипа
2026-12-03 11:36:21 +07:00

118 lines
5.3 KiB
TypeScript

import type { Server } from "./data"
import type { projectNetworkMapDensity, MapPoint } from "./network-map-density"
import { densityWanKey } from "./network-map-density"
import { mapCardMetrics } from "./network-map-card-metrics"
export type MapLane = "access" | "home" | "wan" | "jh" | "en" | "dest" | "nested"
export const deviceLane = (type: Server["type"]) => type === "home-router" ? "home" : type === "jump-host" ? "jh" : "en"
const minimum = {access:147, home:158, wan:147, jh:157, en:162, dest:167, nested:167}
const preferredCard = {access:150, home:157, wan:155, jh:157, en:157, dest:174, nested:174}
const laneOrder: MapLane[] = ["access", "home", "wan", "jh", "en", "dest", "nested"]
type Row = {
name: string
kind: Parameters<typeof mapCardMetrics>[1]
order: number
place: (point: MapPoint) => void
}
/** Place the visible rows at their real text size; retain the projected anchors for all folded members. */
export function layoutNetworkMapColumns(
viewWidth: number,
servers: readonly Server[],
projection: ReturnType<typeof projectNetworkMapDensity>,
access: {name:string; individual:boolean} | null,
destinations: ReadonlyArray<{id:string; name:string}>,
nested: ReadonlyArray<{id:string; name:string}>,
) {
const rows: Record<MapLane, Row[]> = {access:[], home:[], wan:[], jh:[], en:[], dest:[], nested:[]}
const nodePos = {...projection.nodePos}
const wanSatPos = Object.fromEntries(Object.entries(projection.wanSatPos).map(([id, points]) => [id, [...points]]))
const groups = projection.groups.map(group => ({...group}))
let accessPosition: MapPoint = {x:0, y:0}
const destPos: Record<string, MapPoint> = {}
const nestedPos: Record<string, MapPoint> = {}
if (access) {
rows.access.push({
name: access.name, kind: access.individual ? "user" : "access", order: 0,
place: point => { accessPosition = point },
})
}
const routers = servers.filter(server => server.type === "home-router")
.sort((a, b) => nodePos[a.id].y - nodePos[b.id].y || a.id.localeCompare(b.id))
const routerOrder = new Map(routers.map((server, index) => [server.id, index]))
for (const server of servers) {
if (!projection.hiddenServers.has(server.id)) {
rows[deviceLane(server.type)].push({
name: server.name, kind: "device", order: nodePos[server.id].y,
place: point => { nodePos[server.id] = point },
})
}
if (server.type !== "home-router") continue
const uplinks = server.wanUplinks ?? []
for (const [index, wan] of uplinks.entries()) {
if (projection.hiddenWans.has(densityWanKey(server.id, index))) continue
rows.wan.push({
name: wan.isp || wan.name, kind: "wan",
// The group header sorts first, followed by its uplinks in router interface order.
order: routerOrder.get(server.id)! + (index + 1) / (uplinks.length + 1),
place: point => { wanSatPos[server.id][index] = point },
})
}
}
for (const group of groups) {
const lane = group.kind === "wan" ? "wan" : deviceLane(group.type)
rows[lane].push({
name: group.label, kind: "group", order: group.routerId ? routerOrder.get(group.routerId)! : group.y,
place: point => Object.assign(group, point),
})
}
destinations.forEach((item, index) => {
rows.dest.push({name:item.name, kind:"destination", order:index, place:point => { destPos[item.id] = point }})
})
nested.forEach((item, index) => {
rows.nested.push({name:item.name, kind:"destination", order:index, place:point => { nestedPos[item.id] = point }})
})
const active = laneOrder.filter(lane => rows[lane].length)
const minimumWidth = active.reduce((sum,id) => sum + minimum[id], 0)
const worldWidth = Math.max(viewWidth,minimumWidth)
const extra = active.length ? (worldWidth-minimumWidth)/active.length : 0
const cardWidths = {...preferredCard}
let left = 0
const columns = active.map(id => {
const width = minimum[id] + extra
const x0 = left
left += width
cardWidths[id] = Math.min(preferredCard[id], width - 17)
const items = rows[id].sort((a, b) => a.order - b.order).map(row => ({
row, height: mapCardMetrics(row.name, row.kind, cardWidths[id]).height,
}))
return {id, x0, x1:left, cx:x0+width/2, items}
})
const gap = 15, top = 86
const tallest = Math.max(0, ...columns.map(({items}) => items.slice(0,5).reduce((sum,item) => sum+item.height+gap,0)))
const bodyHeight = Math.max(225,Math.min(390,tallest))
let worldHeight = Math.max(340, top+bodyHeight+24)
for (const {cx,items} of columns) {
const stack = items.reduce((sum,item) => sum+item.height+gap,-gap)
let y = top + Math.max(0,(bodyHeight-stack)/2)
for (const {row,height} of items) {
row.place({x:cx,y:y+height/2})
y += height+gap
}
worldHeight = Math.max(worldHeight,y-gap+26)
}
const groupById = new Map(groups.map(g => [g.id,g]))
for (const [id, parent] of projection.hiddenServers) {
const {x, y} = groupById.get(parent)!
nodePos[id] = {x, y}
}
for (const server of servers) for (const [i] of (server.wanUplinks ?? []).entries()) {
const parent = projection.hiddenWans.get(densityWanKey(server.id, i))
if (parent) {
const {x, y} = groupById.get(parent)!
wanSatPos[server.id][i] = {x, y}
}
}
return {groups, nodePos, wanSatPos, accessPosition, destPos, nestedPos, columns, cardWidths, worldWidth, worldHeight}
}