Files
MikrotikManager/lib/network-map-group-links.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

84 lines
3.2 KiB
TypeScript

import { mapLinkPath, type GreMapEdge, type MapBox } from "./network-map-layout"
export type GroupLinkEnd = {
id: string
name: string
x: number
y: number
halfWidth: number
groupId?: string
}
type ChannelCounts = { count: number; down: number; degraded: number }
type ConnectionType = "gre" | "wan-jh" | "wan" | "ix" | "service" | "observation" | "user"
export type MapGroupConnection = Partial<ChannelCounts> & {
from: GroupLinkEnd
to: GroupLinkEnd
type: ConnectionType
gre?: GreMapEdge
}
/** Unmatched reports from opposite ends of one outer GRE pair can describe the same tunnels. */
export function countGroupGre(edges: Iterable<GreMapEdge>): ChannelCounts {
const total = { count: 0, down: 0, degraded: 0 }
const ambiguous = new Map<string, Map<string, ChannelCounts>>()
for (const edge of edges) {
const { tunnel, ambiguousPeerKey } = edge
let counts = total
if (ambiguousPeerKey) {
const byOwner = ambiguous.get(ambiguousPeerKey) ?? new Map<string, ChannelCounts>()
ambiguous.set(ambiguousPeerKey, byOwner)
counts = byOwner.get(tunnel.serverId) ?? { count: 0, down: 0, degraded: 0 }
byOwner.set(tunnel.serverId, counts)
}
counts.count++
const status = channelStatus(edge)
if (status === "down") counts.down++
else if (status === "degraded") counts.degraded++
}
for (const byOwner of ambiguous.values()) {
const reports = [...byOwner.values()]
const count = Math.max(...reports.map(row => row.count))
const down = Math.min(Math.max(...reports.map(row => row.down)), count - Math.max(...reports.map(row => row.count - row.down)))
// Conflicting reports indicate a problem but do not establish that the whole channel is down.
const affected = Math.max(...reports.map(row => row.down + row.degraded))
total.count += count
total.down += down
total.degraded += affected - down
}
return total
}
export function channelStatus({ tunnel, peerTunnel }: GreMapEdge) {
return peerTunnel && peerTunnel.status !== tunnel.status ? "degraded" : tunnel.status
}
export function collectGroupLinks(rows: Iterable<MapGroupConnection>, obstacles?: MapBox[], nextFaultTrack?: () => number) {
const buckets = new Map<string, MapGroupConnection[]>()
for (const row of rows) {
if (!(row.from.groupId || row.to.groupId) || row.from.id === row.to.id) continue
const key = JSON.stringify([row.type, ...[row.from.id, row.to.id].sort()])
const bucket = buckets.get(key)
if (bucket) bucket.push(row)
else buckets.set(key, [row])
}
return [...buckets].map(([id, bucket]) => {
const { from, to, type } = bucket[0]
const totals = countGroupGre(bucket.flatMap(row => row.gre ? [row.gre] : []))
for (const row of bucket) {
if (row.gre) continue
totals.count += row.count ?? 1
totals.down += row.down ?? 0
totals.degraded += row.degraded ?? 0
}
return {
id, from, to, type, ...totals,
groups: [from.groupId, to.groupId].filter((group): group is string => Boolean(group)),
link: mapLinkPath(from, from.halfWidth, to, to.halfWidth, {obstacles, faultY:totals.down || totals.degraded ? nextFaultTrack?.() : undefined}),
}
})
}
export type MapGroupLink = ReturnType<typeof collectGroupLinks>[number]