Docker images / prepare-release (push) Successful in 12s
Docker images / backend-test (push) Successful in 10m46s
Docker images / backend-image (push) Successful in 1m59s
Docker images / frontend-image (push) Successful in 2m16s
Docker images / updater-image (push) Successful in 41s
Docker images / netflow-agent-image (push) Successful in 31s
Docker images / notify-webhook (push) Successful in 2s
Docker images / publish-release (push) Successful in 10s
68 lines
3.1 KiB
TypeScript
68 lines
3.1 KiB
TypeScript
import type { FlowMapService, FlowMapServiceEdge, FlowMapServicePath } from "@mmapp/contracts/traffic-flow"
|
|
import type { Server } from "@/lib/data"
|
|
|
|
export function isolatedIxServers(servers: Pick<Server, "id" | "type">[], links: readonly (readonly [string, string])[]) {
|
|
const neighbors = new Map<string, string[]>()
|
|
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<string, FlowMapServiceEdge>()
|
|
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<string, { bytes: number; bps: number }>()
|
|
const allByService = new Map<string, number>()
|
|
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)
|
|
}
|
|
return {
|
|
services: services.flatMap(service => {
|
|
const total = totals.get(service.id)
|
|
if (!total) return []
|
|
const { trafficTypes, serviceEvidence, ...destination } = service
|
|
return [{ ...destination, ...total, share: bytes ? total.bytes / bytes : 0,
|
|
...(total.bytes === service.bytes ? { trafficTypes, serviceEvidence } : {}) }]
|
|
}).sort((a, b) => b.bytes - a.bytes || a.label.localeCompare(b.label)),
|
|
edges: visibleEdges,
|
|
paths: visiblePaths,
|
|
}
|
|
}
|