Files
MikrotikManager/lib/network-map-destinations.ts
T
Denozordec 545a9bbcd2
Docker images / prepare-release (push) Successful in 13s
Docker images / backend-test (push) Successful in 11m8s
Docker images / frontend-image (push) Successful in 2m25s
Docker images / updater-image (push) Successful in 41s
Docker images / netflow-agent-image (push) Successful in 34s
Docker images / backend-image (push) Successful in 2m1s
Docker images / publish-release (push) Successful in 13s
Docker images / notify-webhook (push) Successful in 2s
feat(network-map): добавить сворачивание групп и категории трафика
2026-12-03 09:58:24 +07:00

113 lines
5.5 KiB
TypeScript

import type { FlowMapService, FlowMapServiceEdge, FlowMapServicePath } from "@mmapp/contracts/traffic-flow"
import { mapTrafficCategory, unknownTrafficCategory } from "./map-traffic-category"
export function mapCategoryId(label: string) {
return "category:" + label
}
/** Source node and route certainty stay separate when several destinations share a link. */
export function groupDestinationEdges(edges: FlowMapServiceEdge[], target: (serviceId: string) => string | undefined) {
const grouped = new Map<string, FlowMapServiceEdge>()
const missingDirection = new Set<string>()
for (const edge of edges) {
const toId = target(edge.toId)
if (!toId) continue
const key = JSON.stringify([edge.fromId, toId, Boolean(edge.observedOnly)])
const total = grouped.get(key) ?? {
fromId: edge.fromId, toId, bytes: 0, bps: 0, bpsFwd: 0, bpsRev: 0,
...(edge.observedOnly && { observedOnly: true as const }),
}
total.bytes += edge.bytes
total.bps += edge.bps
total.bpsFwd += edge.bpsFwd
total.bpsRev += edge.bpsRev
if (edge.bps > 0 && !edge.bpsFwd && !edge.bpsRev) missingDirection.add(key)
grouped.set(key, total)
}
for (const key of missingDirection) {
const total = grouped.get(key)!
total.bpsFwd = 0
total.bpsRev = 0
}
return [...grouped.values()]
}
/** Every byte belongs to a single bucket, including the part without classification evidence. */
function categoryVolumes(service: FlowMapService, bytes: number, evidence: FlowMapService["serviceEvidence"]) {
const label = mapTrafficCategory(service)
if (!evidence?.length || label === unknownTrafficCategory) return new Map([[label, bytes]])
const known = Math.min(bytes, evidence.reduce((sum, row) => sum + (row.basis === "unknown" ? 0 : row.bytes), 0))
const volumes = new Map<string, number>()
if (known) volumes.set(label, known)
if (bytes > known || !bytes) volumes.set(unknownTrafficCategory, bytes - known)
return volumes
}
export function groupMapCategories(services: FlowMapService[], edges: FlowMapServiceEdge[], paths: FlowMapServicePath[]) {
const categories = new Map<string, FlowMapService>()
const members = new Map<string, FlowMapService[]>()
const categoryPaths = new Map<string, FlowMapServicePath[]>()
const byService = new Map<string, string>()
const partialEdges: FlowMapServiceEdge[] = []
for (const service of services) {
const servicePaths = paths.filter(path => path.serviceId === service.id)
const evidence = service.serviceEvidence ?? servicePaths.flatMap(path => path.serviceEvidence ?? [])
const volumes = categoryVolumes(service, service.bytes, evidence)
const onlyLabel = volumes.size === 1 ? [...volumes.keys()][0] : undefined
if (onlyLabel) byService.set(service.id, mapCategoryId(onlyLabel))
for (const [label, bytes] of volumes) {
const id = mapCategoryId(label)
const fraction = service.bytes ? bytes / service.bytes : 1
const item = { ...service, bytes, share: service.share * fraction, bps: service.bps * fraction }
const category = categories.get(id) ?? { id, label, category: "Категория трафика", bytes: 0, bps: 0, share: 0 }
category.bytes += item.bytes
category.bps += item.bps
category.share += item.share
categories.set(id, category)
const items = members.get(id) ?? []
items.push(item)
members.set(id, items)
}
const routed = new Map<string, Map<string, number>>()
for (const path of servicePaths) {
// Older snapshots can associate a path with a single service category, never split a mixed service by guesswork.
const parts = path.serviceEvidence?.length ? categoryVolumes(service, path.bytes, path.serviceEvidence) : onlyLabel ? new Map([[onlyLabel, path.bytes]]) : []
const source = JSON.stringify([path.enId || path.viaId, !path.enId])
const totals = routed.get(source) ?? new Map<string, number>()
for (const [label, bytes] of parts) {
const id = mapCategoryId(label)
const items = categoryPaths.get(id) ?? []
items.push(bytes === path.bytes ? path : {
...path, bytes, bps: path.bps * bytes / path.bytes, trafficTypes: undefined,
serviceEvidence: path.serviceEvidence?.filter(row => mapTrafficCategory(service, {serviceEvidence:[row]}) === label),
})
categoryPaths.set(id, items)
totals.set(label, (totals.get(label) ?? 0) + bytes)
}
routed.set(source, totals)
}
for (const edge of edges.filter(item => item.toId === service.id)) {
if (onlyLabel) {
partialEdges.push({ ...edge, toId: mapCategoryId(onlyLabel) })
continue
}
const source = JSON.stringify([edge.fromId, Boolean(edge.observedOnly)])
for (const [label, bytes] of routed.get(source) ?? []) {
if (!edge.bytes || bytes > edge.bytes) continue
const whole = bytes === edge.bytes
partialEdges.push({ ...edge, toId: mapCategoryId(label), bytes, bps: edge.bps * bytes / edge.bytes,
bpsFwd: whole ? edge.bpsFwd : 0, bpsRev: whole ? edge.bpsRev : 0 })
}
}
}
return {
services: [...categories.values()].sort((a, b) => b.bytes - a.bytes || a.label.localeCompare(b.label, "ru")),
edges: groupDestinationEdges(partialEdges, id => id), members, byService, paths: categoryPaths,
}
}
export function smallMapServices(services: FlowMapService[], knownShare: boolean, selectedId: string | null, filteredId: string | null) {
return services.filter(service => knownShare && service.share <= 0.1 && service.id !== selectedId && service.id !== filteredId)
}