98 lines
2.9 KiB
TypeScript
98 lines
2.9 KiB
TypeScript
import { aggregateCommunityRouteCounts, type EvoCatalogRaw } from "./evobgp-catalog.js"
|
|
|
|
export function formatEvoBgpCatalog(catalog: EvoCatalogRaw, serversByCommunityStr: Map<string, number>, now = new Date()) {
|
|
const modules = catalog.modules.items
|
|
const modMap = new Map(modules.map((m) => [m.id, m]))
|
|
const commItems = catalog.communities.items
|
|
const commLabel = new Map<string, string>()
|
|
for (const c of commItems) {
|
|
const label = (c.community && c.title)
|
|
? `${c.title} (${c.community})`
|
|
: (c.title ?? c.community ?? c.id)
|
|
commLabel.set(c.id, label)
|
|
}
|
|
|
|
const fmtTime = now.toLocaleString("ru-RU")
|
|
const fetchedAt = now.toISOString()
|
|
|
|
const domains = catalog.domains.items.map((rowItem) => {
|
|
const mod = modMap.get(rowItem.module_id)
|
|
const cid = rowItem.entry.community_id
|
|
return {
|
|
id: `${rowItem.module_id}:${rowItem.entry.id}`,
|
|
domain: rowItem.entry.fqdn,
|
|
resolvedIp: "—",
|
|
asn: "—",
|
|
purpose: mod?.name ?? "—",
|
|
filter: cid ? (commLabel.get(cid) ?? cid) : "—",
|
|
updated: fmtTime,
|
|
enabled: true,
|
|
}
|
|
})
|
|
|
|
const ipRanges = catalog.ip_ranges.items.map((rowItem) => {
|
|
const mod = modMap.get(rowItem.module_id)
|
|
const cid = rowItem.entry.community_id
|
|
return {
|
|
id: `${rowItem.module_id}:${rowItem.entry.id}`,
|
|
cidr: rowItem.entry.prefix,
|
|
asn: "—",
|
|
country: "—",
|
|
purpose: mod?.name ?? "—",
|
|
filter: commLabel.get(cid) ?? cid,
|
|
updated: fmtTime,
|
|
enabled: true,
|
|
}
|
|
})
|
|
|
|
const asns = catalog.asns.items.map((rowItem) => {
|
|
const cid = rowItem.entry.community_id
|
|
const e = rowItem.entry
|
|
const name = (e.asn_name ?? "").trim()
|
|
const prefixes = e.prefix_count ?? null
|
|
let updated = fmtTime
|
|
const rawResolved = (e.asn_resolved_at ?? "").trim()
|
|
if (rawResolved) {
|
|
const d = new Date(rawResolved)
|
|
if (!Number.isNaN(d.getTime())) updated = d.toLocaleString("ru-RU")
|
|
}
|
|
return {
|
|
id: `${rowItem.module_id}:${e.id}`,
|
|
asn: `AS${e.asn}`,
|
|
org: name || "—",
|
|
country: "—",
|
|
prefixes,
|
|
filter: cid ? (commLabel.get(cid) ?? cid) : "—",
|
|
updated,
|
|
enabled: true,
|
|
}
|
|
})
|
|
|
|
const routeCountsByCommUuid = aggregateCommunityRouteCounts(catalog)
|
|
|
|
const communities = commItems.map((c) => {
|
|
const valueStr = (c.community ?? "").trim()
|
|
return {
|
|
id: c.id,
|
|
value: c.community ?? c.id,
|
|
name: c.title ?? c.community ?? c.id,
|
|
description: "",
|
|
type: "custom" as const,
|
|
filterIds: [] as string[],
|
|
serverCount: valueStr ? (serversByCommunityStr.get(valueStr) ?? 0) : 0,
|
|
prefixCount: routeCountsByCommUuid.has(c.id) ? routeCountsByCommUuid.get(c.id)! : 0,
|
|
action: "permit" as const,
|
|
enabled: true,
|
|
}
|
|
})
|
|
|
|
return {
|
|
fetchedAt,
|
|
modules: modules.map((m) => ({ id: m.id, name: m.name, type: m.type })),
|
|
domains,
|
|
ipRanges,
|
|
asns,
|
|
communities,
|
|
}
|
|
}
|