131 lines
4.7 KiB
Python
131 lines
4.7 KiB
Python
"""PeeringDB Facility (/api/fac) resolver for commercial datacenter compute sites."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from typing import Any
|
|
|
|
import httpx
|
|
|
|
from ..models import LocationCandidate, LocationQuery, ResolverOutput
|
|
from ..text import coerce_str, normalize_country_text, normalize_text, parse_float
|
|
|
|
PEERINGDB_FAC_URL = "https://www.peeringdb.com/api/fac"
|
|
PEERINGDB_USER_AGENT = "Planet-Intelligence-System/1.0"
|
|
PEERINGDB_TIMEOUT_SECONDS = 6.0
|
|
|
|
|
|
_GENERIC_OPERATOR_NAMES = frozenset(
|
|
{
|
|
"google",
|
|
"ibm",
|
|
"microsoft",
|
|
"amazon",
|
|
"aws",
|
|
"meta",
|
|
"facebook",
|
|
"nebius",
|
|
"nebius ai",
|
|
"oracle",
|
|
"alibaba",
|
|
"tencent",
|
|
"bytedance",
|
|
"nvidia",
|
|
"apple",
|
|
"openai",
|
|
"xai",
|
|
"equinix",
|
|
"digital realty",
|
|
"coreweave",
|
|
"lambda",
|
|
}
|
|
)
|
|
|
|
|
|
async def lookup_peeringdb_facility(
|
|
query: LocationQuery,
|
|
*,
|
|
timeout_seconds: float = PEERINGDB_TIMEOUT_SECONDS,
|
|
) -> ResolverOutput:
|
|
"""Query PeeringDB facilities matching the target site/name (never generic operator alone)."""
|
|
extra = query.extra or {}
|
|
search_terms: list[str] = []
|
|
for raw in [
|
|
extra.get("site"),
|
|
*(query.aliases or ()),
|
|
query.name,
|
|
]:
|
|
cleaned = coerce_str(raw)
|
|
norm = normalize_text(cleaned)
|
|
if not cleaned or len(cleaned) < 3 or norm in _GENERIC_OPERATOR_NAMES:
|
|
continue
|
|
if cleaned not in search_terms:
|
|
search_terms.append(cleaned)
|
|
if not search_terms:
|
|
return ResolverOutput()
|
|
|
|
attempted: list[str] = []
|
|
candidates: list[LocationCandidate] = []
|
|
expected_country = normalize_text(normalize_country_text(query.country))
|
|
headers = {"User-Agent": PEERINGDB_USER_AGENT, "Accept": "application/json"}
|
|
api_key = os.environ.get("PEERINGDB_API_KEY", "").strip()
|
|
|
|
async with httpx.AsyncClient(timeout=timeout_seconds, headers=headers) as client:
|
|
for term in search_terms[:2]:
|
|
attempted.append(f"peeringdb_fac:{term}")
|
|
params: dict[str, Any] = {"name__icontains": term, "limit": 4}
|
|
if api_key:
|
|
params["key"] = api_key
|
|
try:
|
|
resp = await client.get(PEERINGDB_FAC_URL, params=params)
|
|
resp.raise_for_status()
|
|
rows = (resp.json() or {}).get("data") or []
|
|
except Exception:
|
|
continue
|
|
|
|
term_norm = normalize_text(term)
|
|
for item in rows:
|
|
if not isinstance(item, dict):
|
|
continue
|
|
lat = parse_float(item.get("latitude"))
|
|
lon = parse_float(item.get("longitude"))
|
|
if (
|
|
lat in (None, 0.0)
|
|
or lon in (None, 0.0)
|
|
or not (-90.0 <= lat <= 90.0)
|
|
or not (-180.0 <= lon <= 180.0)
|
|
):
|
|
continue
|
|
item_country = normalize_country_text(item.get("country"))
|
|
if expected_country and item_country and normalize_text(item_country) != expected_country:
|
|
continue
|
|
fac_name = coerce_str(item.get("name")) or term
|
|
if term_norm and term_norm not in normalize_text(fac_name):
|
|
# Require meaningful overlap when fuzzy matched
|
|
fac_tokens = set(normalize_text(fac_name).split())
|
|
if not (set(term_norm.split()) & fac_tokens):
|
|
continue
|
|
city = coerce_str(item.get("city")) or None
|
|
addr = coerce_str(item.get("address1"))
|
|
candidates.append(
|
|
LocationCandidate(
|
|
latitude=lat,
|
|
longitude=lon,
|
|
display_name=fac_name,
|
|
precision="precise" if addr else "site",
|
|
confidence=0.80,
|
|
query=f"peeringdb_fac:{term}",
|
|
source="peeringdb_facility",
|
|
source_note=f"PeeringDB facility #{item.get('id')}: {fac_name}" + (f" ({addr})" if addr else ""),
|
|
matched_fields=("site", "peeringdb"),
|
|
needs_confirmation=True,
|
|
city=city,
|
|
region=coerce_str(item.get("state")) or None,
|
|
country=item_country or query.country,
|
|
matched_location_name=fac_name,
|
|
raw_payload={"peeringdb_fac_id": item.get("id"), "address": addr},
|
|
)
|
|
)
|
|
|
|
return ResolverOutput(candidates=tuple(candidates), attempted_queries=tuple(attempted))
|