File size: 14,662 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
"""Comment-level data-center metrics from our own transcripts (#1233, epic #1232).

``GET /api/data-centers/comment-metrics?year=2026&state=AL&city=`` returns the free numbers
that ``public.data_center_comment`` (dbt) supports: how many public comments, at how many local
governments and meetings, the concerns they raise (multi-label), the companies and public
figures they name, how many mention AI, how speakers describe themselves, the places they
name, and data-center mentions per meeting by quarter.

Metrics that need a per-comment stance (opposed / in favor / question) are returned as
``null`` with a reason: stance needs the classifier pass that is on hold (epic #1232, phase 2).
Nothing is defaulted to 0 or invented (CLAUDE.md, No Fabricated Data). The response echoes the
applied scope so the UI label matches the data (CLAUDE.md, scope label rule).

All shaping lives in dbt; this route only counts rows of the served marts.
"""

from __future__ import annotations

from typing import Any

import asyncpg
from fastapi import APIRouter, HTTPException, Query
from loguru import logger
from opentelemetry import trace
from pydantic import BaseModel, Field

from api.routes.data_centers import JobClaimScope, build_scope
from api.routes.search_postgres import get_db_pool

router = APIRouter(prefix="/data-centers", tags=["data-centers"])
tracer = trace.get_tracer(__name__)

STANCE_REASON = (
    "Needs a per-comment stance (opposed / in favor / question). That classifier pass is on hold "
    "by the owner (epic #1232, phase 2); until it runs, this number is unknown."
)
BASIS = (
    "Public comments segmented from our own meeting captions (self-introductions and the chair calling "
    "speakers), kept when the speaker isn't staff or the applicant and the comment is about a data center. "
    "Concerns, companies, people, AI and roles are keyword matches (dbt seeds), not a model's reading."
)


class CountItem(BaseModel):
    key: str
    label: str
    count: int
    share: float | None = Field(None, description="count / comments; null when there are no comments.")


class QuarterItem(BaseModel):
    quarter: str = Field(..., description="e.g. '2026-Q1' (a string, not a number).")
    meetings_with_mention: int = Field(..., description="Meetings whose captions say 'data center'.")
    mention_passages: int = Field(..., description="3-minute caption passages that say it.")
    passages_per_meeting: float | None
    comments: int = Field(..., description="Data-center public comments segmented in those meetings.")


class HeatSummary(BaseModel):
    """Average and peak comment heat (1-5) over the speakers who have one; present only when at least 3 do (#1386)."""

    speakers_rated: int
    mean: float
    peak: int


MIN_HEAT_SPEAKERS = 3


class CommentMetricsResponse(BaseModel):
    year: str | None = Field(None, description="Calendar year filter as a string; null for all years.")
    scope: JobClaimScope
    comments: int = Field(..., description="Distinct SPEAKERS (meeting_speaker_position); segments are detail (#1383).")
    segments: int = Field(0, description="Caption segments behind those speakers (data_center_comment rows).")
    shapes: list[CountItem] = Field(
        default_factory=list,
        description="Speakers by how they state a position (stance shape), each counted once, never as both sides.",
    )
    heat: HeatSummary | None = Field(None, description="Mean and peak heat; null unless 3+ speakers have a rating.")
    governments: int
    meetings: int
    meetings_segmented: int = Field(..., description="Meetings in scope with any segmented comment (all topics).")
    meetings_with_no_supporters: int | None = None
    no_supporters_reason: str = STANCE_REASON
    positions: dict[str, int] | None = None
    positions_reason: str = STANCE_REASON
    concerns: list[CountItem]
    multi_concern_share: float | None = Field(
        None, description="Share of comments raising 2+ concerns on the reference list."
    )
    companies: list[CountItem]
    people: list[CountItem]
    ai_mentions: CountItem | None
    roles: list[CountItem]
    places: list[CountItem] = Field(default_factory=list, description="Place kinds named (roads, schools, ...).")
    mentions_per_meeting_by_quarter: list[QuarterItem] = Field(
        ..., description="The trend since 2022-Q1 for the place scope; it ignores `year` on purpose (it is a trend)."
    )
    basis: str = BASIS


def _where(scope: JobClaimScope, year: int | None, alias: str = "") -> tuple[str, list[Any]]:
    a = f"{alias}." if alias else ""
    clauses: list[str] = []
    params: list[Any] = []
    if year is not None:
        params.append(year)
        n = len(params)
        # A range on the date (not extract(year ...)) so the (state_code, meeting_date) index applies.
        clauses.append(f"{a}meeting_date >= make_date(${n}, 1, 1) AND {a}meeting_date < make_date(${n} + 1, 1, 1)")
    if scope.state_code:
        params.append(scope.state_code)
        clauses.append(f"{a}state_code = ${len(params)}")
    if scope.city:
        params.append(f"%{scope.city}%")
        clauses.append(f"{a}jurisdiction_name ILIKE ${len(params)}")
    if scope.jurisdiction_id:
        params.append(scope.jurisdiction_id)
        clauses.append(f"{a}jurisdiction_id = ${len(params)}")
    return (" WHERE " + " AND ".join(clauses)) if clauses else "", params


def build_metric_queries(scope: JobClaimScope, year: int | None) -> dict[str, tuple[str, list[Any]]]:
    """SQL per metric block. Every query reads only served relations."""
    where, params = _where(scope, year)
    where_d, params_d = _where(scope, year, "d")
    totals = (
        "SELECT count(DISTINCT meeting_speaker_id) AS comments, count(*) AS segments, "
        "count(DISTINCT jurisdiction_id) AS governments, "
        "count(DISTINCT video_id) AS meetings, "
        "count(DISTINCT meeting_speaker_id) FILTER (WHERE reference_concern_count >= 2) AS multi_concern, "
        "count(DISTINCT meeting_speaker_id) FILTER (WHERE mentions_ai) AS ai "
        f"FROM data_center_comment{where}"
    )
    unnest_sql = (
        "SELECT k AS key, count(DISTINCT meeting_speaker_id) AS n FROM data_center_comment, unnest({col}) AS k"
        f"{where} GROUP BY k ORDER BY n DESC, k"
    )
    places = (
        "SELECT p.place_kind AS key, count(DISTINCT d.meeting_speaker_id) AS n "
        "FROM data_center_comment_place p JOIN data_center_comment d "
        f"ON d.data_center_comment_id = p.data_center_comment_id{where_d} GROUP BY 1 ORDER BY 2 DESC"
    )
    shapes = (
        "SELECT p.stance_shape AS key, count(*) AS n FROM meeting_speaker_position p "
        f"WHERE p.meeting_speaker_id IN (SELECT meeting_speaker_id FROM data_center_comment{where}) "
        "GROUP BY 1 ORDER BY n DESC, 1"
    )
    heat = (
        "SELECT count(p.heat) AS rated, avg(p.heat)::float8 AS mean, max(p.heat) AS peak "
        "FROM meeting_speaker_position p "
        f"WHERE p.meeting_speaker_id IN (SELECT meeting_speaker_id FROM data_center_comment{where})"
    )
    seg_where, seg_params = _where(scope, year)
    segmented = f"SELECT count(*) AS n FROM segmented_meeting{seg_where}"
    q_where, q_params = _where(scope, None)
    q_extra = " AND " if q_where else " WHERE "
    quarters = (
        "WITH m AS (SELECT video_id, meeting_date FROM data_center_meeting_mention"
        f"{q_where}{q_extra}meeting_date >= DATE '2022-01-01'), "
        "q AS (SELECT to_char(meeting_date, 'YYYY') || '-Q' || extract(quarter FROM meeting_date)::int AS quarter, "
        "count(DISTINCT video_id) AS meetings, count(*) AS passages FROM m GROUP BY 1), "
        "c AS (SELECT to_char(meeting_date, 'YYYY') || '-Q' || extract(quarter FROM meeting_date)::int AS quarter, "
        f"count(DISTINCT meeting_speaker_id) AS comments FROM data_center_comment{q_where}{q_extra}meeting_date >= DATE '2022-01-01' GROUP BY 1) "
        "SELECT q.quarter, q.meetings, q.passages, coalesce(c.comments, 0) AS comments "
        "FROM q LEFT JOIN c USING (quarter) ORDER BY q.quarter"
    )
    return {
        "totals": (totals, params),
        "concerns": (unnest_sql.format(col="concern_keys"), params),
        "companies": (unnest_sql.format(col="company_keys"), params),
        "people": (unnest_sql.format(col="person_keys"), params),
        "roles": (unnest_sql.format(col="role_keys"), params),
        "places": (places, params_d),
        "shapes": (shapes, params),
        "heat": (heat, params),
        "segmented": (segmented, seg_params),
        "quarters": (quarters, q_params),
    }


# Labels mirror the dbt seeds (comment_concern_terms, comment_entity_terms, comment_role_terms);
# an unknown key falls back to the key itself, never to an invented label.
CONCERN_LABELS = {
    "water_use": "Water use", "quality_of_life": "Quality of life", "lack_of_transparency": "Lack of transparency",
    "company_distrust": "Company distrust", "rushed_review": "Rushed review",
    "limited_community_benefit": "Limited community benefit", "few_jobs": "Few jobs",
    "taxpayer_subsidies": "Taxpayer subsidies", "community_character": "Community character",
    "farmland_wildlife_loss": "Farmland / wildlife loss", "property_rights": "Property rights",
    "energy_costs_grid": "Electricity costs / grid",
}
ENTITY_LABELS = {
    "google": "Google / Alphabet", "amazon": "Amazon / AWS", "meta": "Meta / Facebook", "microsoft": "Microsoft",
    "openai": "OpenAI / ChatGPT", "xai": "xAI / Grok", "anthropic": "Anthropic / Claude", "nvidia": "Nvidia",
    "oracle": "Oracle", "apple": "Apple", "tesla": "Tesla", "blackstone_qts": "Blackstone / QTS",
    "coreweave": "CoreWeave", "elon_musk": "Elon Musk", "sam_altman": "Sam Altman", "peter_thiel": "Peter Thiel",
    "mark_zuckerberg": "Mark Zuckerberg", "jeff_bezos": "Jeff Bezos", "bill_gates": "Bill Gates",
    "kevin_oleary": "Kevin O'Leary", "jensen_huang": "Jensen Huang", "larry_ellison": "Larry Ellison",
    "sundar_pichai": "Sundar Pichai", "satya_nadella": "Satya Nadella",
}
ROLE_LABELS = {
    "resident": "Resident", "property_owner": "Property owner", "union_worker": "Union worker",
    "business": "Business owner / chamber", "farmer": "Farmer", "advocacy_group": "Advocacy or community group",
    "elected_official": "Elected official",
}
SHAPE_LABELS = {
    "clear_for": "For", "clear_against": "Against", "conditional": "Conditional",
    "for_concept_against_project": "Supports the idea, opposes this project",
    "against_concept_open_to_terms": "Opposes, open to terms", "concerns_only": "Concerns raised",
    "unclear": "Unclear",
}
PLACE_LABELS = {
    "road": "Roads and routes", "school": "Schools", "county": "Counties and townships",
    "neighborhood": "Neighborhoods", "district": "Districts and wards", "site": "Named sites",
    "jurisdiction": "Towns and cities", "project": "Tracked data center projects",
}


def _items(rows: list[Any], labels: dict[str, str], total: int) -> list[CountItem]:
    return [
        CountItem(
            key=r["key"],
            label=labels.get(r["key"], r["key"]),
            count=int(r["n"]),
            share=round(int(r["n"]) / total, 4) if total else None,
        )
        for r in rows
    ]


def _heat_summary(rows: list[Any]) -> HeatSummary | None:
    r = rows[0] if rows else None
    if not r or int(r.get("rated") or 0) < MIN_HEAT_SPEAKERS:
        return None
    return HeatSummary(speakers_rated=int(r["rated"]), mean=round(float(r["mean"]), 2), peak=int(r["peak"]))


def shape_response(
    year: int | None, scope: JobClaimScope, results: dict[str, list[Any]]
) -> CommentMetricsResponse:
    t = results["totals"][0] if results["totals"] else {}
    total = int(t.get("comments") or 0)
    quarters = [
        QuarterItem(
            quarter=r["quarter"],
            meetings_with_mention=int(r["meetings"]),
            mention_passages=int(r["passages"]),
            passages_per_meeting=round(int(r["passages"]) / int(r["meetings"]), 2) if r["meetings"] else None,
            comments=int(r["comments"]),
        )
        for r in results["quarters"]
    ]
    return CommentMetricsResponse(
        year=str(year) if year is not None else None,
        scope=scope,
        comments=total,
        segments=int(t.get("segments") or 0),
        heat=_heat_summary(results.get("heat", [])),
        shapes=_items(results.get("shapes", []), SHAPE_LABELS, total),
        governments=int(t.get("governments") or 0),
        meetings=int(t.get("meetings") or 0),
        meetings_segmented=int(results["segmented"][0]["n"]) if results["segmented"] else 0,
        concerns=_items(results["concerns"], CONCERN_LABELS, total),
        multi_concern_share=round(int(t["multi_concern"]) / total, 4) if total else None,
        companies=_items(results["companies"], ENTITY_LABELS, total),
        people=_items(results["people"], ENTITY_LABELS, total),
        ai_mentions=(
            CountItem(key="ai", label="Any mention of AI", count=int(t["ai"]), share=round(int(t["ai"]) / total, 4))
            if total
            else None
        ),
        roles=_items(results["roles"], ROLE_LABELS, total),
        places=_items(results["places"], PLACE_LABELS, total),
        mentions_per_meeting_by_quarter=quarters,
    )


@router.get("/comment-metrics", response_model=CommentMetricsResponse)
async def data_center_comment_metrics(
    year: int = Query(2026, ge=0, le=2100, description="Meeting year (default 2026); 0 for all years."),
    state: str | None = Query(None, description="2-letter code or full state name."),
    city: str | None = Query(None, description="City / jurisdiction name (substring)."),
    jurisdiction_id: str | None = Query(None),
) -> CommentMetricsResponse:
    scope = build_scope(state, city, jurisdiction_id)
    year_filter = year or None
    queries = build_metric_queries(scope, year_filter)
    with tracer.start_as_current_span("data_center_comment_metrics") as span:
        span.set_attribute("scope.level", scope.level)
        pool = await get_db_pool()
        results: dict[str, list[Any]] = {}
        try:
            async with pool.acquire() as conn:
                for name, (sql, params) in queries.items():
                    results[name] = [dict(r) for r in await conn.fetch(sql, *params)]
        except asyncpg.UndefinedTableError as exc:
            # The marts are new; before the first dbt build there is nothing to count.
            logger.warning("comment-metrics: {}", exc)
            raise HTTPException(status_code=503, detail="Comment-level data isn't built yet.") from exc
    return shape_response(year_filter, scope, results)