interfaces.alert_down / groups.alert_down (migration 9); interface_down rule OptIn; non-opted ports' open alerts close silently as retired. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lx9FNLG2CqeBbVGGd9bFsP
209 lines
9.2 KiB
TypeScript
209 lines
9.2 KiB
TypeScript
// Stateful mock of the M5 group endpoints. One store per page. Shapes come from the generated schema.
|
|
import type { components } from '../../src/api/schema.d.ts'
|
|
import { NOW } from './traffic.ts'
|
|
|
|
type S = components['schemas']
|
|
type Member = S['GroupMemberView']
|
|
type Pt = [number, number | null]
|
|
export type Reply = { status: number; body: unknown }
|
|
|
|
export interface GroupResolver {
|
|
slug: (name: string) => string
|
|
iface: (device: string, ifname: string) => string
|
|
info: (id: string) => S['GroupMemberIface'] | undefined
|
|
/** raw series of one interface over a window */
|
|
series: (id: string, from: number, to: number, step: number) => { in: Pt[]; out: Pt[] } | null
|
|
}
|
|
|
|
interface Group {
|
|
id: string
|
|
name: string
|
|
kind: S['GroupBody']['kind']
|
|
alertDown: boolean
|
|
members: Member[]
|
|
/** share of the month's 5-minute slots that had data (mock constant) */
|
|
coverage: number
|
|
}
|
|
|
|
const iso = (t: number) => new Date(t * 1000).toISOString().replace('.000Z', 'Z')
|
|
const ago = (days: number) => iso(NOW - days * 86400)
|
|
const problem = (status: number, title: string, detail: string): Reply => ({ status, body: { type: 'about:blank', title, status, detail } })
|
|
|
|
export const GROUP_IDS = { acme: '01K9GRPACME000000000000001', core: '01K9GRPCORE000000000000002', globex: '01K9GRPGLOBEX00000000003', empty: '01K9GRPEMPTY00000000000004' } as const
|
|
|
|
/** Interface pairs that are the two ends of one link (a group holding both would count it twice). */
|
|
function links(r: GroupResolver): [string, string][] {
|
|
return [
|
|
[r.iface('yyz1-core1', 'et-0/1/1'), r.iface('yyz1-dist1', 'Ethernet51')],
|
|
[r.iface('yyz1-core1', 'et-0/1/2'), r.iface('yyz1-dist2', 'Ethernet51')],
|
|
]
|
|
}
|
|
|
|
function seed(r: GroupResolver): Group[] {
|
|
const m = (device: string, ifname: string, direction: Member['direction'], fromDays: number, toDays?: number): Member => {
|
|
const id = r.iface(device, ifname)
|
|
return { direction, effective_from: ago(fromDays), effective_to: toDays === undefined ? null : ago(toDays), interface: r.info(id) as S['GroupMemberIface'] }
|
|
}
|
|
return [
|
|
{
|
|
id: GROUP_IDS.acme,
|
|
name: 'Customer: Acme Corp',
|
|
kind: 'customer',
|
|
alertDown: false,
|
|
coverage: 0.97,
|
|
members: [m('yyz1-acc01', 'Gi1/0/1', 'toward', 400), m('yyz1-acc01', 'Gi1/0/2', 'toward', 400), m('yyz1-acc02', 'Gi1/0/5', 'away', 400), m('yyz1-acc01', 'Gi1/0/3', 'toward', 400, 60)],
|
|
},
|
|
{
|
|
id: GROUP_IDS.core,
|
|
name: 'Core uplinks yyz1',
|
|
kind: 'site',
|
|
alertDown: true,
|
|
coverage: 1,
|
|
members: [m('yyz1-core1', 'et-0/1/1', 'toward', 200), m('yyz1-dist1', 'Ethernet51', 'toward', 200)],
|
|
},
|
|
{ id: GROUP_IDS.globex, name: 'Customer: Globex Inc', kind: 'customer', alertDown: false, coverage: 0.82, members: [m('yyz1-acc03', 'Gi1/0/4', 'toward', 12), m('yyz1-acc03', 'Gi1/0/6', 'toward', 12)] },
|
|
{ id: GROUP_IDS.empty, name: 'Site yul1 transit', kind: 'custom', alertDown: false, coverage: 1, members: [] },
|
|
]
|
|
}
|
|
|
|
const open = (g: Group) => g.members.filter((x) => x.effective_to === null)
|
|
|
|
function mergeSpans(spans: [number, number][]): { from: string; to: string }[] {
|
|
const out: [number, number][] = []
|
|
for (const [a, b] of spans) {
|
|
const last = out[out.length - 1]
|
|
if (last && a <= last[1]) last[1] = Math.max(last[1], b)
|
|
else out.push([a, b])
|
|
}
|
|
return out.map(([a, b]) => ({ from: iso(a), to: iso(b) }))
|
|
}
|
|
|
|
function p95(v: (number | null)[]): number | null {
|
|
const a = v.filter((x): x is number => x != null).sort((x, y) => x - y)
|
|
return a.length ? (a[Math.max(0, Math.ceil(0.95 * a.length) - 1)] as number) : null
|
|
}
|
|
|
|
export class GroupStore {
|
|
private groups: Group[]
|
|
private seq = 100
|
|
constructor(private readonly r: GroupResolver) {
|
|
this.groups = seed(r)
|
|
}
|
|
|
|
/** `group:<name>` of the first group with alert_down on that holds the interface now, else ''. */
|
|
alertDownVia(ifaceId: string): string {
|
|
const g = this.groups.find((x) => x.alertDown && open(x).some((m) => m.interface.id === ifaceId))
|
|
return g ? `group:${g.name}` : ''
|
|
}
|
|
|
|
private summary(g: Group): S['GroupSummary'] {
|
|
return { id: g.id, name: g.name, kind: g.kind, member_count: open(g).length, alert_down: g.alertDown }
|
|
}
|
|
|
|
private flags(g: Group): S['GroupFlag'][] {
|
|
const ids = new Set(open(g).map((x) => x.interface.id))
|
|
return links(this.r)
|
|
.filter(([a, b]) => ids.has(a) && ids.has(b))
|
|
.map(([a, b]) => ({ a, b, kind: 'both_ends_of_link' as const }))
|
|
}
|
|
|
|
private detail(g: Group): S['GroupDetail'] {
|
|
return { ...this.summary(g), members: g.members, flags: this.flags(g) }
|
|
}
|
|
|
|
save(id: string | null, b: Partial<S['GroupBody']>): Reply {
|
|
const name = (b.name ?? '').trim()
|
|
if (!name) return { status: 422, body: { type: 'about:blank', title: 'Unprocessable Entity', status: 422, errors: [{ location: 'body.name', message: 'must not be empty' }] } }
|
|
const kind = b.kind ?? 'custom'
|
|
if (id === null) {
|
|
const g: Group = { id: `01K9GRPNEW${String(++this.seq).padStart(15, '0')}`, name, kind, alertDown: b.alert_down ?? false, members: [], coverage: 1 }
|
|
this.groups.push(g)
|
|
return { status: 201, body: this.summary(g) }
|
|
}
|
|
const g = this.groups.find((x) => x.id === id)
|
|
if (!g) return problem(404, 'Not Found', `group "${id}" does not exist`)
|
|
Object.assign(g, { name, kind, alertDown: b.alert_down ?? g.alertDown })
|
|
return { status: 200, body: this.summary(g) }
|
|
}
|
|
|
|
addMember(g: Group, b: Partial<S['GroupMemberBody']>): Reply {
|
|
const info = b.interface_id ? this.r.info(b.interface_id) : undefined
|
|
if (!info) return problem(422, 'Unprocessable Entity', `interface "${b.interface_id ?? ''}" does not exist`)
|
|
if (open(g).some((x) => x.interface.id === info.id)) return problem(409, 'Conflict', 'interface is already a member of this group')
|
|
const m: Member = { direction: b.direction ?? 'toward', effective_from: b.effective_from ?? iso(NOW), effective_to: null, interface: info }
|
|
g.members.push(m)
|
|
return { status: 201, body: m }
|
|
}
|
|
|
|
graph(g: Group, q: URLSearchParams): Reply {
|
|
const to = q.get('to') ? Date.parse(q.get('to') as string) / 1000 : NOW
|
|
const from = q.get('from') ? Date.parse(q.get('from') as string) / 1000 : to - 86400
|
|
const step = Number(q.get('step')) || 300
|
|
const inS: Pt[] = []
|
|
const outS: Pt[] = []
|
|
const gaps: [number, number][] = []
|
|
const parts = open(g).map((m) => ({ m, s: this.r.series(m.interface.id, from, to, step) }))
|
|
const grid = parts[0]?.s?.in.map((p) => p[0]) ?? []
|
|
grid.forEach((t, i) => {
|
|
let a: number | null = 0
|
|
let b: number | null = 0
|
|
for (const { m, s } of parts) {
|
|
const x = s?.in[i]?.[1] ?? null
|
|
const y = s?.out[i]?.[1] ?? null
|
|
const [rx, ry] = m.direction === 'toward' ? [x, y] : [y, x]
|
|
a = a == null || rx == null ? null : a + rx
|
|
b = b == null || ry == null ? null : b + ry
|
|
}
|
|
inS.push([t, a])
|
|
outS.push([t, b])
|
|
if (a == null || b == null) gaps.push([t, t + step])
|
|
})
|
|
const month = q.get('month') ?? undefined
|
|
const materialised = !!month && month < new Date(NOW * 1000).toISOString().slice(0, 7)
|
|
const body: S['GroupGraphResponse'] = {
|
|
flags: this.flags(g),
|
|
from: iso(grid[0] ?? from),
|
|
to: iso(to),
|
|
group: { id: g.id, name: g.name, kind: g.kind },
|
|
partial: mergeSpans(gaps),
|
|
series: { in: inS, out: outS },
|
|
step_seconds: step,
|
|
...(q.get('p95') === 'true' && {
|
|
p95: { in_bps: p95(inS.map((p) => p[1])), out_bps: p95(outS.map((p) => p[1])), source: materialised ? ('materialised' as const) : ('live' as const), coverage: g.coverage, partial: g.coverage < 0.9, ...(month && { month }) },
|
|
}),
|
|
}
|
|
return { status: 200, body }
|
|
}
|
|
|
|
/** Dispatches /groups endpoints; undefined when `p` is not one of them. */
|
|
route(method: string, p: string, q: URLSearchParams, body: () => unknown): Reply | undefined {
|
|
if (p === '/groups' && method === 'GET') return { status: 200, body: { items: this.groups.map((g) => this.summary(g)) } }
|
|
if (p === '/groups' && method === 'POST') return this.save(null, body() as Partial<S['GroupBody']>)
|
|
const m = /^\/groups\/([^/]+)(\/graph|\/members)?$/.exec(p)
|
|
if (!m) return undefined
|
|
const id = decodeURIComponent(m[1] as string)
|
|
const g = this.groups.find((x) => x.id === id)
|
|
if (!g) return problem(404, 'Not Found', `group "${id}" does not exist`)
|
|
return this.one(g, m[2], method, q, body)
|
|
}
|
|
|
|
private one(g: Group, sub: string | undefined, method: string, q: URLSearchParams, body: () => unknown): Reply | undefined {
|
|
if (sub === '/graph') return this.graph(g, q)
|
|
if (sub === '/members' && method === 'POST') return this.addMember(g, body() as Partial<S['GroupMemberBody']>)
|
|
if (sub === '/members' && method === 'DELETE') {
|
|
const mem = open(g).find((x) => x.interface.id === q.get('interface_id'))
|
|
if (!mem) return problem(404, 'Not Found', 'no open membership for that interface')
|
|
mem.effective_to = iso(NOW)
|
|
return { status: 204, body: undefined }
|
|
}
|
|
if (sub) return undefined
|
|
if (method === 'GET') return { status: 200, body: this.detail(g) }
|
|
if (method === 'PUT') return this.save(g.id, body() as Partial<S['GroupBody']>)
|
|
if (method === 'DELETE') {
|
|
this.groups = this.groups.filter((x) => x !== g)
|
|
return { status: 204, body: undefined }
|
|
}
|
|
return undefined
|
|
}
|
|
}
|