Files
MikrotikManager/lib/network-map-metrics.test.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

52 lines
3.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {test} from "node:test"
import assert from "node:assert/strict"
import {buildServerResourceMap,greTunnelProbe} from "./network-map-layout"
import {mergeGreMetricsWithSpeedProbe} from "./map-gre-speed-probe"
import {servers,greTunnels} from "./data"
import {scopeMapDestinations} from "./network-map-scope"
test("live resources preserve zero, partial poll and unknowns without fake history",()=>{
const base={...servers[0],cpuLoad:undefined,totalMemory:undefined,freeMemory:undefined,uptime:undefined,polledAt:undefined}
const unknown=buildServerResourceMap([base])[base.id]
assert.deepEqual(unknown,{cpu:null,cpuHistory:[],ramTotal:null,ramUsed:null,hddUsed:null,hddTotal:null,uptimeSeconds:null,measuredAt:null,fromPoll:true})
const partial=buildServerResourceMap([{...base,cpuLoad:0,uptime:"0s",polledAt:"2020-01-01T00:00:00Z"}])[base.id]
assert.equal(partial.cpu,0);assert.equal(partial.uptimeSeconds,0);assert.equal(partial.measuredAt,"2020-01-01T00:00:00Z");assert.equal(partial.ramUsed,null)
const memory=buildServerResourceMap([{...base,totalMemory:1048576,freeMemory:1048576,uptime:"bad"}])[base.id]
assert.equal(memory.ramUsed,0);assert.equal(memory.ramTotal,1);assert.equal(memory.uptimeSeconds,null)
assert.ok(buildServerResourceMap([base],true)[base.id].cpuHistory.length>0)
})
test("live GRE does not hash metrics and probe values retain zero and fractions",()=>{
const fallback=greTunnelProbe(greTunnels[0]);assert.deepEqual(fallback,{pingMs:null,dlMbps:null,ulMbps:null})
assert.equal(greTunnelProbe(greTunnels[0],true).pingMs!=null,true)
const merged=mergeGreMetricsWithSpeedProbe({id:"p",srcServerId:"1",dstServerId:"2",lastPingRttMs:0,lastTxAvgMbps:0,lastRxAvgMbps:0.125},fallback)
assert.deepEqual(merged,{pingMs:0,dlMbps:0,ulMbps:0.125,hasSpeedMonitor:true})
})
test("a filtered topology does not inherit a service's full-network traffic composition", () => {
const service = {id: "svc:dns", label: "DNS", category: "DNS", bytes: 100, bps: 10, share: 0.4,
trafficTypes: [{transport: "UDP", application: "DNS", bytes: 100}], lastBucketAt: "2026-11-26T18:30:00.000Z"}
const edges = ["1", "2"].map(fromId => ({fromId, toId: service.id, bytes: 50, bps: 5, bpsFwd: 5, bpsRev: 0}))
const partial = scopeMapDestinations([service], edges, [], id => id === "1").services[0]
const complete = scopeMapDestinations([service], edges, [], () => true).services[0]
assert.equal(partial?.trafficTypes, undefined)
assert.equal(partial?.lastBucketAt, undefined)
assert.deepEqual(complete?.trafficTypes, service.trafficTypes)
assert.equal(complete?.lastBucketAt, service.lastBucketAt)
assert.equal(complete.share, 0.4)
assert.equal(partial.share, 1)
})
test("a scoped service uses only the classification of its contributing paths", () => {
const service = {id:"svc:video", label:"Видео", category:"Видео", bytes:100, bps:20, share:1,
serviceEvidence:[{basis:"network" as const, bytes:60}, {basis:"heuristic" as const, bytes:40}]}
const endpoint = {viaId:"1", viaName:"Экспортёр", enId:"", enName:"", serviceId:service.id}
const paths = [
{...endpoint, clientId:"a", clientName:"А", bytes:40, bps:8, serviceEvidence:[{basis:"heuristic" as const,bytes:40}]},
{...endpoint, clientId:"b", clientName:"Б", bytes:60, bps:12, serviceEvidence:[{basis:"network" as const,bytes:60}]},
]
const scoped = scopeMapDestinations([service], [], paths, () => true, "a").services[0]
assert.deepEqual(scoped.serviceEvidence, paths[0].serviceEvidence)
assert.equal(scoped.bytes,40)
const legacy = scopeMapDestinations([service], [], paths.map(path => ({...path, serviceEvidence:undefined})), () => true,"a").services[0]
assert.equal(legacy.serviceEvidence,undefined)
})