import type { FlowMapService, FlowMapServiceEdge, FlowMapServicePath } from "@mmapp/contracts/traffic-flow" import type { Server } from "@/lib/data" export function isolatedIxServers(servers: Pick[], links: readonly (readonly [string, string])[]) { const neighbors = new Map() for (const [from, to] of links) { neighbors.set(from, [...(neighbors.get(from) ?? []), to]) neighbors.set(to, [...(neighbors.get(to) ?? []), from]) } const connected = new Set(servers.filter(server => server.type === "jump-host" || server.type === "exit-node").map(server => server.id)) for (const id of connected) for (const neighbor of neighbors.get(id) ?? []) connected.add(neighbor) return new Set(servers.filter(server => server.type === "ix-node" && !connected.has(server.id)).map(server => server.id)) } export function scopeMapDestinations( services: FlowMapService[], edges: FlowMapServiceEdge[], paths: FlowMapServicePath[], includes: (id: string) => boolean, userId?: string, retainUnassigned = false, ) { const visiblePaths = paths.filter(path => includes(path.enId || path.viaId) && (!userId || path.clientId === userId)) const visibleEdges = userId ? [] : edges.filter(edge => includes(edge.fromId)) if (userId) { const bySource = new Map() for (const path of visiblePaths) { const fromId = path.enId || path.viaId const key = JSON.stringify([fromId, path.serviceId, Boolean(path.enId)]) const edge = bySource.get(key) ?? { fromId, toId: path.serviceId, bytes: 0, bps: 0, bpsFwd: 0, bpsRev: 0, ...(path.enId ? {} : { observedOnly: true as const }), } edge.bytes += path.bytes edge.bps += path.bps bySource.set(key, edge) } visibleEdges.push(...bySource.values()) } const totals = new Map() const allByService = new Map() if (retainUnassigned) for (const edge of edges) allByService.set(edge.toId, (allByService.get(edge.toId) ?? 0) + edge.bytes) let bytes = 0 for (const edge of visibleEdges) { const total = totals.get(edge.toId) ?? { bytes: 0, bps: 0 } total.bytes += edge.bytes total.bps += edge.bps bytes += edge.bytes totals.set(edge.toId, total) } if (retainUnassigned) for (const service of services) { const unassigned = service.bytes - (allByService.get(service.id) ?? 0) if (unassigned <= 0) continue const total = totals.get(service.id) ?? { bytes: 0, bps: 0 } total.bytes += unassigned total.bps += service.bps * unassigned / service.bytes bytes += unassigned totals.set(service.id, total) } const shareOfVisible = Boolean(userId) || services.some(service => (totals.get(service.id)?.bytes ?? 0) !== service.bytes) return { shareOfVisible, services: services.flatMap(service => { const total = totals.get(service.id) if (!total) return [] const { trafficTypes, serviceEvidence, lastBucketAt, ...destination } = service const visibleShare = bytes ? total.bytes / bytes : 0 return [{ ...destination, ...total, share: shareOfVisible ? visibleShare : service.share, ...(total.bytes === service.bytes ? { trafficTypes, serviceEvidence, lastBucketAt } : {}) }] }).sort((a, b) => b.bytes - a.bytes || a.label.localeCompare(b.label)), edges: visibleEdges, paths: visiblePaths, } }