18 lines
1.1 KiB
TypeScript
18 lines
1.1 KiB
TypeScript
export async function pollBgpCatalog<S extends { id: number; name: string }, T extends { id: string; serverId: number }>(
|
|
devices: readonly S[], read: (device: S) => Promise<T[]>,
|
|
): Promise<{ sessions: T[]; failures: Array<{ serverId: number; serverName: string; error: string }> }> {
|
|
const results = await Promise.all(devices.map(async device => {
|
|
try {
|
|
const sessions = await read(device)
|
|
if (!Array.isArray(sessions) || sessions.some(s => s.serverId !== device.id || typeof s.id !== 'string' || !s.id.trim())) {
|
|
throw new Error('Некорректный контекст BGP-каталога')
|
|
}
|
|
if (new Set(sessions.map(s => s.id)).size !== sessions.length) throw new Error('Повторяющиеся ID BGP-сессий')
|
|
return { sessions, failures: [] }
|
|
} catch (error) {
|
|
return { sessions: [], failures: [{ serverId: device.id, serverName: device.name, error: error instanceof Error ? error.message : 'Ошибка опроса BGP' }] }
|
|
}
|
|
}))
|
|
return { sessions: results.flatMap(r => r.sessions), failures: results.flatMap(r => r.failures) }
|
|
}
|