spb/datacenterindex
Public
HTML 53.9%
TypeScript 44.5%
JavaScript 0.6%
SQL 0.5%
1/** Facility tenants: carriers / cloud providers present at a facility → facility_tenants + operator flags. */2import { sql } from "@dci/db";3import type { IngestContext, Tx } from "./common.js";4import { resolveOperator } from "./operators.js";56export interface TenantInput {7 names: string[];8 role: "carrier" | "cloud" | "network" | "tenant";9}1011/** Attach tenants; returns the distinct operator count attached for this role. */12export async function attachTenants(tx: Tx, ctx: IngestContext, facilityId: string, input: TenantInput): Promise<number> {13 const seen = new Set<string>();14 for (const raw of input.names) {15 const name = raw?.trim();16 if (!name || name.length < 2 || name.length > 120) continue;17 try {18 const asnMatch = name.match(/\bAS(\d{2,7})\b/i);19 const clean = name.replace(/\s*\(?AS\d{2,7}\)?\s*/i, "").trim() || name;20 const op = await resolveOperator(tx, ctx, { name: clean, roleHint: input.role === "cloud" ? "cloud" : input.role === "carrier" ? "carrier" : null });21 if (seen.has(op.id)) continue;22 seen.add(op.id);23 await tx.execute(sql`insert into facility_tenants (facility_id, operator_id, role, asn, source_id) values (${facilityId}, ${op.id}, ${input.role}, ${asnMatch ? Number(asnMatch[1]) : null}, ${ctx.run.sourceId})24 on conflict (facility_id, operator_id, role) do update set asn = coalesce(excluded.asn, facility_tenants.asn), source_id = excluded.source_id`);25 } catch (e) {26 console.warn(`[ingest] tenant "${name}" skipped: ${(e as Error).message}`);27 }28 }29 return seen.size;30}3132/** Recompute carriers_count / networks_count from facility_tenants. */33export async function refreshTenantCounts(tx: Tx, facilityId: string): Promise<void> {34 await tx.execute(sql`update facilities f set35 carriers_count = (select count(distinct operator_id) from facility_tenants t where t.facility_id = f.id and t.role = 'carrier'),36 networks_count = (select count(distinct operator_id) from facility_tenants t where t.facility_id = f.id and t.role in ('carrier','network','cloud','tenant'))37 where f.id = ${facilityId}`);38}39