import {bgpCatalogSchema} from "@/lib/bgp/response" export interface DashboardBgp {prefixSum:number|null;establishedCount:number|null} /** A failed device cannot be counted as having no sessions in the global KPI. */ export function parseDashboardBgp(raw:unknown):DashboardBgp { const catalog=bgpCatalogSchema.safeParse(raw) if(!catalog.success)throw Error("Некорректный ответ каталога BGP") if(catalog.data.failures.length)throw Error("Не опрошены устройства BGP: "+catalog.data.failures.map(f=>f.serverName+": "+f.error).join("; ")) let prefixSum=0,establishedCount=0,missingPrefix=false,unknownState=false for(const session of catalog.data.sessions){ const state=session.state.toLowerCase() if(state==="established"){establishedCount++;if(session.prefixesRx===null)missingPrefix=true;else prefixSum+=session.prefixesRx} else if(!["active","idle","connect","opensent","openconfirm"].includes(state))unknownState=true } if(!Number.isSafeInteger(prefixSum))throw Error("Суммарный счётчик BGP превышает допустимый диапазон") return {prefixSum:unknownState||missingPrefix?null:prefixSum,establishedCount:unknownState?null:establishedCount} }