fix(api): показывать подтверждённые метрики OSPF

This commit is contained in:
Denozordec committed 2026-09-30 19:49:47 +07:00
1 parent e3791f3496
commit af05ad5cda
13 files changed
+120 -55

No files matched your search

+2 -1
View File
@@ -12,6 +12,7 @@ test("neighbor cost is scoped to device, interface and area", () => {
test("conflicting or invalid template costs do not choose a fabricated value", () => {
const key = ospfCostKey(1,"ether1","0.0.0.0")
assert.equal(buildOspfCostIndex([iface, iface]).get(key), 7)
assert.equal(buildOspfCostIndex([{ ...iface, cost: 0 }]).get(key), 0)
assert.equal(buildOspfCostIndex([iface, { ...iface, cost: 42 }, iface]).get(key), null)
for (const cost of [0, -1, 65536, 1.5]) assert.equal(buildOspfCostIndex([{ ...iface, cost }]).get(key), null)
for (const cost of [null, -1, 65536, 1.5]) assert.equal(buildOspfCostIndex([{ ...iface, cost }]).get(key), null)
})
+2 -2
View File
@@ -1,10 +1,10 @@
type InterfaceCost = { serverId: number; interface: string; areaId: string; cost: number }
type InterfaceCost = { serverId: number; interface: string; areaId: string; cost: number | null }
export const ospfCostKey = (serverId: number, iface: string, areaId: string) => JSON.stringify([serverId, iface, areaId])
export function buildOspfCostIndex(interfaces: InterfaceCost[]): Map<string, number | null> {
const result = new Map<string, number | null>()
for (const iface of interfaces) {
const key = ospfCostKey(iface.serverId, iface.interface, iface.areaId)
const cost = Number.isInteger(iface.cost) && iface.cost >= 1 && iface.cost <= 65535 ? iface.cost : null
const cost = iface.cost !== null && Number.isInteger(iface.cost) && iface.cost >= 0 && iface.cost <= 65535 ? iface.cost : null
result.set(key, result.has(key) && result.get(key) !== cost ? null : cost)
}
return result
+3 -3
View File
@@ -4,10 +4,10 @@ const counter = z.number().int().nonnegative()
const base = z.object({ id: z.string().trim().min(1), serverId: z.number().int().positive(), serverName: z.string(), serverSite: z.string(), serverCountry: z.string().default("UN") })
export const ospfCatalogSchema = z.object({
neighbors: z.array(base.extend({ address: z.string(), routerId: z.string(), instance: z.string(), area: z.string(), areaId: z.string(), interface: z.string(), state: z.string(), uptime: z.string().nullable(), stateChanges: counter.nullable(), priority: counter.max(255).nullable() })),
interfaces: z.array(base.extend({ instance: z.string(), area: z.string(), areaId: z.string(), interface: z.string(), cost: counter, type: z.string(), disabled: z.boolean(), inactive: z.boolean(), priority: counter, helloInterval: counter, deadInterval: counter, useBfd: z.boolean() })),
instances: z.array(base.extend({ name: z.string(), routerId: z.string(), version: counter, disabled: z.boolean(), inactive: z.boolean(), redistribute: z.string() })),
interfaces: z.array(base.extend({ instance: z.string(), area: z.string(), areaId: z.string(), interface: z.string(), cost: counter.max(65535).nullable(), type: z.string().nullable(), disabled: z.boolean().nullable(), inactive: z.boolean().nullable(), priority: counter.max(255).nullable(), helloInterval: z.number().finite().nonnegative().nullable(), deadInterval: z.number().finite().nonnegative().nullable(), useBfd: z.boolean().nullable() })),
instances: z.array(base.extend({ name: z.string(), routerId: z.string().nullable(), version: z.union([z.literal(2), z.literal(3)]).nullable(), disabled: z.boolean().nullable(), inactive: z.boolean().nullable(), redistribute: z.string() })),
bfdSessions: z.array(base.extend({ localAddr: z.string(), remoteAddr: z.string(), interface: z.string(), state: z.string(), uptime: z.string().nullable(), multihop: z.boolean(), multiplier: counter.max(255).nullable(), txInterval: z.number().finite().nonnegative().nullable(), rxInterval: z.number().finite().nonnegative().nullable(), holdTime: z.number().finite().nonnegative().nullable(), packetsRx: counter.nullable(), packetsTx: counter.nullable(), stateChanges: counter.nullable() })),
routes: z.array(base.extend({ destination: z.string(), type: z.enum(["O", "O IA", "O E1", "O E2"]), cost: counter, nextHop: z.string(), via: z.string(), area: z.string() })),
routes: z.array(base.extend({ destination: z.string(), type: z.enum(["O", "O IA", "O E1", "O E2"]), cost: counter.max(4294967295).nullable(), nextHop: z.string(), via: z.string(), area: z.string() })),
failures: z.array(z.object({ serverId: z.number().int().positive(), serverName: z.string(), error: z.string() })),
}).refine(data => {
const failed = new Set(data.failures.map(f => f.serverId))