40 lines
3.9 KiB
TypeScript
40 lines
3.9 KiB
TypeScript
import { observeFlowRing, applyRingSnapshot, snapshotRings } from "./traffic-flow-engine.js"
|
|
import { createFlowIngestQuality } from "./traffic-flow-ingest-quality.js"
|
|
import assert from "node:assert/strict"
|
|
import { flowCounter, flowCounterSum, observeFlowClient, observeFlowExporter, type FlowClientInput } from "./traffic-flow-catalog-read.js"
|
|
import type { PendingFlowRow } from "./traffic-flow-ingest.js"
|
|
|
|
for (const value of [-1, NaN, Infinity, "bad", "1.5", "9007199254740992", null, undefined, true]) assert.throws(() => flowCounter(value))
|
|
assert.equal(flowCounter("0"), 0); assert.equal(flowCounterSum([0, 5, 10]), 15)
|
|
assert.throws(() => flowCounterSum([Number.MAX_SAFE_INTEGER, 1]))
|
|
const row = { serverId: 1, bytes: 1000, packets: 10, inIface: "client-vlan", bucketAt: "2026-10-19T00:00:00Z" } as PendingFlowRow
|
|
const user: FlowClientInput = { id: "u", login: "user", name: "Name", active: true, bindingCount: 2, selectedBindingCount: 1, bindings: [{ serverId: 1, interfaceName: "client-vlan", peerSpecific: false }] }
|
|
const observed = observeFlowClient(user, [row, { ...row, inIface: "2", bytes: 9999 }, { ...row, serverId: 2, bytes: 8888 }], 5)
|
|
assert.equal(observed.ingressNamedRowsBytes, 1000); assert.equal(observed.ingressNamedRowCount, 1)
|
|
assert.equal(observed.ifIndexRowsNotAttributed, 1); assert.equal(observed.completeSourceSet, false); assert.equal(observed.completeAttribution, false)
|
|
assert.equal(observed.availability, "unknown"); assert.equal(observed.rxMbps, null); assert.equal(observed.txMbps, null); assert.equal(observed.historicalOwnershipVerified, false)
|
|
assert.equal(observeFlowClient(user, [row], 5).completeAttribution, false)
|
|
const peer = observeFlowClient({ ...user, bindings: [{ ...user.bindings[0], peerSpecific: true }] }, [row], 5)
|
|
assert.equal(peer.ingressNamedRowsBytes, null); assert.equal(peer.peerBindingsNotAttributed, 1)
|
|
assert.equal(observeFlowClient({ ...user, active: false }, [], 5).availability, "unknown")
|
|
const source = { id: 1, name: "R", site: "site", country: "RU", host: "192.0.2.1", type: "home-router", enabled: false }
|
|
const exporter = observeFlowExporter({ catalog: { list: [source], byId: new Map([[1, source]]) }, configuredIds: new Set([1]), configurationMembershipVerified: true, window: { rows: [row], since: "", until: "", topRowsPerBucketLimit: 20 } }, 1, 5)
|
|
assert.equal(exporter.retainedRowsBytes, 1000); assert.equal(exporter.retainedRowsPackets, 10); assert.equal(exporter.collectionEnabled, false)
|
|
assert.equal(exporter.availability, "unknown"); assert.equal(exporter.wireRateMbps, null)
|
|
assert.equal(exporter.retainedRowsMeanMbps, 1000 * 8 / 300 / 1000000)
|
|
assert.equal(observeFlowExporter({ catalog: { list: [source], byId: new Map([[1, source]]) }, configuredIds: new Set([1]), configurationMembershipVerified: false, window: { rows: [], since: "", until: "", topRowsPerBucketLimit: 20 } }, 1, 5).storedPeerRecord, true)
|
|
applyRingSnapshot([])
|
|
assert.equal(observeFlowRing(1).available, false); assert.equal(observeFlowRing(1).lastSlotIngressMbps, null)
|
|
const ringQuality=createFlowIngestQuality().snapshot()
|
|
const validRing={key:"1\0__all__",inBps:[...Array(59).fill(0),1000000],outBps:Array(60).fill(0)}
|
|
applyRingSnapshot([validRing],ringQuality)
|
|
assert.equal(observeFlowRing(1).lastSlotIngressMbps, 1); assert.equal(observeFlowRing(1).slotTimestampsVerified, false)
|
|
const originalRings=snapshotRings()
|
|
assert.throws(()=>applyRingSnapshot([validRing,{key:"2\0__all__",inBps:[NaN],outBps:[]}],ringQuality),/Invalid|oversized/)
|
|
assert.deepEqual(snapshotRings(),originalRings,"A rejected frame cannot partially replace a valid ring catalogue")
|
|
assert.equal(observeFlowRing(2).available,false)
|
|
applyRingSnapshot([validRing])
|
|
assert.equal(observeFlowRing(1).available,false,"Legacy missing quality is unobserved, not a healthy zero-loss interval")
|
|
applyRingSnapshot([])
|
|
console.log("NetFlow catalog models: safe counters/overflow, named ingress subset, peer/ifIndex uncertainty, disabled is not offline and observed row means are not wire rates")
|