/** * /ai-infrastructure — AI / HPC index. Counts use ai_evidence confirmed | likely (or the legacy is_ai flag); * "associated" facilities are reported separately and never counted as AI infrastructure. */ import type { AiIndex } from "@dci/core"; import { pg, facilityJoins, facilitySummaryCols, facilityView, knownMwAgg, pipelineMwAgg, countedAgg, hasMwAgg, mwExpr, projectJoins, projectSummaryCols, projectLive, CONSTRUCTION_SET, type Sql } from "../lib/sql.js"; import { int, num, reqStr, type Row } from "../lib/rows.js"; import { asStatus, facilitySummary, projectSummary, round2, share } from "../lib/dto.js"; import { eventsWhere } from "./events.js"; export const AI_EVIDENCE_NOTE = "AI evidence levels: confirmed = a source explicitly describes the site as AI / HPC / GPU / accelerated-computing infrastructure; likely = AI-ready, high-density, liquid- or direct-to-chip cooling or GPU signals in the source; associated = only an AI tenant, customer or company mention (reported separately, NOT counted as AI infrastructure); unknown = no signal. A single keyword never confirms a site. Counts and MW cover confirmed + likely only; MW figures are published site-scoped figures (containment-aware), never extrapolated."; const AI_F = (sql: Sql) => sql`(f.ai_evidence in ('confirmed', 'likely') or f.is_ai or f.facility_type = 'ai')`; const AI_P = (sql: Sql) => sql`(p.ai_evidence in ('confirmed', 'likely') or p.is_ai)`; export async function aiIndex(): Promise { const sql = pg(); const known = knownMwAgg(sql), pipe = pipelineMwAgg(sql), counted = countedAgg(sql), hasMw = hasMwAgg(sql); const [fs, ps, topOps, topMetros, topCountries, stages, recentProjects, facilities, recentAnnouncements] = await Promise.all([ sql`select count(*) filter (where ${counted} and ${AI_F(sql)})::int as facilities, count(*) filter (where ${counted} and f.ai_evidence = 'confirmed')::int as confirmed, count(*) filter (where ${counted} and f.ai_evidence = 'likely')::int as likely, count(*) filter (where ${counted} and f.ai_evidence = 'associated')::int as associated, sum(${pipe}) filter (where ${AI_F(sql)} and f.status = any(${CONSTRUCTION_SET}))::float as construction_mw, sum(${known}) filter (where ${AI_F(sql)} and f.status in ('operational', 'partially_operational', 'expansion'))::float as known_mw, count(*) filter (where ${counted} and ${AI_F(sql)} and ${hasMw})::int as with_mw, count(distinct f.country_iso2) filter (where ${AI_F(sql)})::int as f_countries, count(distinct f.operator_id) filter (where ${AI_F(sql)})::int as f_operators from ${facilityView(sql)} f`, sql`select count(*)::int as projects, sum(p.planned_mw)::float as planned_mw, count(distinct p.country_iso2)::int as countries, count(distinct p.operator_id)::int as operators from projects p where ${projectLive(sql)} and ${AI_P(sql)}`, sql`select * from (select o.id, o.slug, o.name, (select count(*) from ${facilityView(sql)} f where f.operator_id = o.id and ${counted} and ${AI_F(sql)})::int as facilities, (select count(*) from projects p where p.operator_id = o.id and ${projectLive(sql)} and ${AI_P(sql)})::int as projects, (select sum(p.planned_mw) from projects p where p.operator_id = o.id and ${projectLive(sql)} and ${AI_P(sql)})::float as planned_mw from operators o where exists (select 1 from facilities f where f.operator_id = o.id and f.merged_into is null and ${AI_F(sql)}) or exists (select 1 from projects p where p.operator_id = o.id and ${projectLive(sql)} and ${AI_P(sql)})) t order by t.facilities + t.projects desc, t.planned_mw desc nulls last, t.name limit 10`, sql`select * from (select m.id, m.slug, m.name, m.country_iso2, (select count(*) from ${facilityView(sql)} f where f.metro_id = m.id and ${counted} and ${AI_F(sql)})::int as facilities, (select count(*) from projects p where p.metro_id = m.id and ${projectLive(sql)} and ${AI_P(sql)})::int as projects, (select sum(p.planned_mw) from projects p where p.metro_id = m.id and ${projectLive(sql)} and ${AI_P(sql)})::float as planned_mw from metros m where exists (select 1 from facilities f where f.metro_id = m.id and f.merged_into is null and ${AI_F(sql)}) or exists (select 1 from projects p where p.metro_id = m.id and ${projectLive(sql)} and ${AI_P(sql)})) t order by t.facilities + t.projects desc, t.planned_mw desc nulls last, t.name limit 10`, sql`select * from (select c.iso2, c.slug, c.name, (select count(*) from ${facilityView(sql)} f where f.country_iso2 = c.iso2 and ${counted} and ${AI_F(sql)})::int as facilities, (select count(*) from projects p where p.country_iso2 = c.iso2 and ${projectLive(sql)} and ${AI_P(sql)})::int as projects, (select sum(p.planned_mw) from projects p where p.country_iso2 = c.iso2 and ${projectLive(sql)} and ${AI_P(sql)})::float as planned_mw from countries c where exists (select 1 from facilities f where f.country_iso2 = c.iso2 and f.merged_into is null and ${AI_F(sql)}) or exists (select 1 from projects p where p.country_iso2 = c.iso2 and ${projectLive(sql)} and ${AI_P(sql)})) t order by t.facilities + t.projects desc, t.planned_mw desc nulls last, t.name limit 10`, sql`select p.status, count(*)::int as n, sum(p.planned_mw)::float as mw from projects p where ${projectLive(sql)} and ${AI_P(sql)} group by 1 order by n desc`, sql`select ${projectSummaryCols(sql)} from projects p ${projectJoins(sql)} where ${projectLive(sql)} and ${AI_P(sql)} order by p.last_update desc limit 10`, sql`select ${facilitySummaryCols(sql)} from facilities f ${facilityJoins(sql)} where f.merged_into is null and ${AI_F(sql)} order by ${mwExpr(sql)} desc nulls last, f.completeness desc limit 30`, eventsWhere(sql`e.is_ai = true`, 15), ]); const f = fs[0] ?? {}; const p = ps[0] ?? {}; const facilitiesN = int(f.facilities); return { stats: { facilities: facilitiesN, confirmed: int(f.confirmed), likely: int(f.likely), associated: int(f.associated), projects: int(p.projects), plannedMw: round2(num(p.planned_mw)), constructionMw: round2(num(f.construction_mw)), countries: Math.max(int(f.f_countries), int(p.countries)), operators: Math.max(int(f.f_operators), int(p.operators)), mwCoverage: share(int(f.with_mw), facilitiesN), }, topOperators: topOps.map((r) => ({ id: reqStr(r.id), slug: reqStr(r.slug), name: reqStr(r.name), facilities: int(r.facilities), projects: int(r.projects), plannedMw: round2(num(r.planned_mw)) })), topMetros: topMetros.map((r) => ({ id: reqStr(r.id), slug: reqStr(r.slug), name: reqStr(r.name), countryIso2: reqStr(r.country_iso2), facilities: int(r.facilities), projects: int(r.projects), plannedMw: round2(num(r.planned_mw)) })), topCountries: topCountries.map((r) => ({ iso2: reqStr(r.iso2), slug: reqStr(r.slug), name: reqStr(r.name), facilities: int(r.facilities), projects: int(r.projects), plannedMw: round2(num(r.planned_mw)) })), pipelineByStage: stages.map((r) => ({ status: asStatus(r.status), count: int(r.n), mw: round2(num(r.mw)) })), recentAnnouncements, recentProjects: recentProjects.map(projectSummary), facilities: facilities.map(facilitySummary), evidenceNote: AI_EVIDENCE_NOTE, }; }