"""What each kind of document actually spans, written down rather than assumed. The Federal Reserve did not start publishing these things on the same day. The statement goes back furthest, the projections begin in 2007 and their accessible tables in 2012, the dot plot in 2012, the implementation note in December 2015. A reader who assumes a uniform history will quietly mistake *this document did not exist yet* for *this meeting did not produce one*, so the spans are published as a table and the reason for each start date is written beside it. """ from __future__ import annotations from datetime import UTC, datetime from pathlib import Path import polars as pl from .config import BACKFILL_FROM_YEAR NOTES = { "statement": ( "Policy statements reach back to 1994 on the Board's historical pages. This build " f"starts at {BACKFILL_FROM_YEAR}, the first year of the Summary of Economic Projections, " "so the subsets are comparable." ), "implementation_note": ( "The implementation note was introduced with the December 2015 liftoff. Before then " "the administered rates were stated inside the statement itself and are not extracted " "separately." ), "minutes": ( "Minutes are published three weeks after the meeting. Their knowledge instant is the " "release date the calendar states, at end of day in Washington, because the minutes " "page carries no time of its own." ), "sep": ( "The Summary of Economic Projections begins in October 2007, but the accessible " "version with data tables begins in January 2010. Earlier projections exist only as " "PDF charts and are left unparsed rather than guessed at. Medians were not published " "before September 2015 and are null for those releases." ), "dot_plot": ( "The policy path chart begins in January 2012 and is read from the accessible " "version's data table, never from the image. The December 2012 projections are " "published only as the participant compilation, which carries no dot counts, so that " "one release has projections and no dots." ), "votes": ( "Rosters are read from the statement. From 2026 the Committee prints only its " "dissenters and a tally, so `vote_for` for those meetings comes from the tally line." ), "press_conference": ( "Press conferences began in April 2011 and became a feature of every meeting in 2019. " "The page states no time, so the event is dated to the end of the meeting's last day." ), } SOURCES = { "statement": ("events", "STATEMENT"), "implementation_note": ("events", "IMPLEMENTATION_NOTE"), "minutes": ("events", "MINUTES"), "press_conference": ("events", "PRESS_CONFERENCE"), "sep": ("sep", None), "dot_plot": ("dot_plot", None), "votes": ("votes", None), } SCHEMA = { "document_type": pl.String, "first_available_date": pl.Date, "last_available_date": pl.Date, "rows": pl.Int64, "meetings": pl.Int64, "coverage_notes": pl.String, "generated_at": pl.Datetime(time_unit="us", time_zone="UTC"), } def coverage_table(frames: dict[str, pl.DataFrame]) -> pl.DataFrame: now = datetime.now(UTC) rows = [] for name, (table, event_type) in SOURCES.items(): frame = frames.get(table) if frame is None or frame.is_empty(): continue if event_type is not None: frame = frame.filter(pl.col("event_type") == event_type) if frame.is_empty(): continue instants = frame["knowledge_at"] if "knowledge_at" in frame.columns else frame["knowledge_date"] rows.append( { "document_type": name, "first_available_date": instants.min().date(), "last_available_date": instants.max().date(), "rows": frame.height, "meetings": frame["meeting_id"].n_unique(), "coverage_notes": NOTES.get(name, ""), "generated_at": now, } ) return pl.DataFrame(rows, schema=SCHEMA) if rows else pl.DataFrame(schema=SCHEMA) BUILD_SCHEMA = { "build_at": pl.Datetime(time_unit="us", time_zone="UTC"), "parser_version": pl.String, "build_version": pl.String, "package_version": pl.String, "recipe_hash": pl.String, "source_snapshot_at": pl.Datetime(time_unit="us", time_zone="UTC"), "row_counts": pl.String, "quarantine_count": pl.Int64, } def write_coverage(frames: dict[str, pl.DataFrame], *, metadata_dir: Path) -> int: metadata_dir.mkdir(parents=True, exist_ok=True) table = coverage_table(frames) table.write_parquet(metadata_dir / "coverage.parquet", compression="zstd") return table.height def write_build_record( frames: dict[str, pl.DataFrame], *, metadata_dir: Path, quarantined: int ) -> None: """One row saying what this build was, so a published tree can explain itself. `source_snapshot_at` is the newest instant anything in the dataset became public, which is the answer to "how current is this?" -- a question the build time cannot answer, because a run on a quiet Tuesday finds nothing new and is no less recent for it. """ import json from . import __version__ from .manifest import recipe_hash from .schema import BUILD_VERSION, PARSER_VERSION events = frames.get("events") newest = ( events["knowledge_at"].max() if events is not None and not events.is_empty() else None ) record = pl.DataFrame( [ { "build_at": datetime.now(UTC), "parser_version": PARSER_VERSION, "build_version": BUILD_VERSION, "package_version": __version__, "recipe_hash": recipe_hash(), "source_snapshot_at": newest, "row_counts": json.dumps( {name: frame.height for name, frame in sorted(frames.items())} ), "quarantine_count": quarantined, } ], schema=BUILD_SCHEMA, ) metadata_dir.mkdir(parents=True, exist_ok=True) record.write_parquet(metadata_dir / "build.parquet", compression="zstd") __all__ = ["BUILD_SCHEMA", "NOTES", "SCHEMA", "coverage_table", "write_build_record", "write_coverage"]