-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefresh_etl_scenarios.py
More file actions
172 lines (144 loc) · 6.41 KB
/
Copy pathrefresh_etl_scenarios.py
File metadata and controls
172 lines (144 loc) · 6.41 KB
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
#!/usr/bin/env python3
"""
refresh_etl_scenarios.py - regenerate the ETL-side scenario list.
Reads the working CSV
(`etl/ingestion/scenario_listing/model_run_file_source_working.csv`) and
writes `etl/common/etl_scenarios.py` containing a frozenset of every
short_code the ETL pipeline is intended to process.
Filter:
- Excludes rows whose `download_status` column is `skip` or `retired`.
- Includes every other well-formed row (blank `download_status`, `done`,
`needs_review`, anything else).
Consumers (`run_all.py`, `verify_all_sections.py`) get the broader
"what the ETL touches" set. For the narrower "what is live on the
public website" set, use `etl/common/active_scenarios.py`, regenerated
by `refresh_active_scenarios.py` from the API.
Usage:
python etl/ingestion/tools/refresh_etl_scenarios.py
python etl/ingestion/tools/refresh_etl_scenarios.py --working-csv path/to.csv
python etl/ingestion/tools/refresh_etl_scenarios.py --dry-run
"""
from __future__ import annotations
# Make `etl.common` importable when this script is invoked directly with
# `python etl/ingestion/tools/refresh_etl_scenarios.py`. See
# etl/common/__init__.py for the rationale.
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
import argparse # noqa: E402
import csv # noqa: E402
import logging # noqa: E402
from datetime import datetime, timezone # noqa: E402
from typing import List, Set # noqa: E402
from etl.ingestion.lib.config import COLUMN_MAP, WORKING_CSV_PATH # noqa: E402
REPO_ROOT = Path(__file__).resolve().parents[3]
DEFAULT_WORKING_CSV = REPO_ROOT / WORKING_CSV_PATH
DEFAULT_ETL_SCENARIOS_PY = REPO_ROOT / "etl" / "common" / "etl_scenarios.py"
EXCLUDED_DOWNLOAD_STATUSES: Set[str] = {"skip", "retired"}
SHORT_CODE_COL = COLUMN_MAP["short_code"]
DOWNLOAD_STATUS_COL = COLUMN_MAP["download_status"]
log = logging.getLogger("refresh_etl_scenarios")
def _read_working_csv(csv_path: Path) -> List[dict]:
"""Read every row of the working CSV as a dict, preserving order."""
if not csv_path.exists():
raise SystemExit(
f"\nWorking CSV not found: {csv_path}\n"
f"Bootstrap from the reference copy first. See etl/README.md.\n"
)
with csv_path.open(encoding="utf-8-sig") as f:
reader = csv.DictReader(f)
if SHORT_CODE_COL not in (reader.fieldnames or []):
raise SystemExit(
f"\nWorking CSV is missing required column '{SHORT_CODE_COL}'.\n"
f" CSV path: {csv_path}\n"
)
return list(reader)
def _select_short_codes(rows: List[dict]) -> List[str]:
"""Return sorted unique short_codes after the download_status filter."""
keep: Set[str] = set()
excluded = 0
blank = 0
for raw in rows:
sc = (raw.get(SHORT_CODE_COL) or "").strip().lower()
if not sc:
blank += 1
continue
status = (raw.get(DOWNLOAD_STATUS_COL) or "").strip().lower()
if status in EXCLUDED_DOWNLOAD_STATUSES:
excluded += 1
continue
keep.add(sc)
log.info(
"Read %d rows; kept %d, excluded %d by download_status, skipped %d with blank short_code",
len(rows), len(keep), excluded, blank,
)
return sorted(keep)
def _render_etl_scenarios_py(short_codes: List[str], csv_path: Path) -> str:
"""Render the contents of etl/common/etl_scenarios.py."""
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
try:
rel_csv = csv_path.resolve().relative_to(REPO_ROOT)
csv_label = str(rel_csv)
except ValueError:
csv_label = str(csv_path)
excluded_list = ", ".join(sorted(f'"{s}"' for s in EXCLUDED_DOWNLOAD_STATUSES))
if short_codes:
body = "\n".join(f' "{code}",' for code in short_codes)
literal = "frozenset({\n" + body + "\n})"
else:
literal = "frozenset()"
return (
'"""Scenarios the ETL pipeline is intended to process.\n'
"\n"
f"Auto-generated by `etl/ingestion/tools/refresh_etl_scenarios.py`\n"
f"on {now} from `{csv_label}`. Rows whose `download_status` is in\n"
f"{{{excluded_list}}} are excluded; everything else is included.\n"
"Do not edit by hand. Re-run the refresh script to regenerate.\n"
"\n"
"Use this set for anything that runs against raw ETL output in S3 or\n"
"the per-scenario statistics in the DB: `run_all.py --all-scenarios`,\n"
"`verify_all_sections.py --all-scenarios`.\n"
"For the narrower curated/public set (what the website serves), import\n"
"`ACTIVE_SCENARIOS` from `etl.common.active_scenarios` instead.\n"
'"""\n'
"\n"
"from __future__ import annotations\n"
"\n"
f"ETL_SCENARIOS: frozenset[str] = {literal}\n"
)
def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)-5s %(message)s",
datefmt="%Y-%m-%dT%H:%M:%SZ",
)
parser = argparse.ArgumentParser(
description="Refresh etl/common/etl_scenarios.py from the working CSV."
)
parser.add_argument("--working-csv", default=str(DEFAULT_WORKING_CSV),
help=f"Path to the working CSV (default: {DEFAULT_WORKING_CSV})")
parser.add_argument("--etl-py", default=str(DEFAULT_ETL_SCENARIOS_PY),
help=f"Path to write the ETL_SCENARIOS Python module (default: {DEFAULT_ETL_SCENARIOS_PY})")
parser.add_argument("--dry-run", action="store_true",
help="Print the generated module to stdout, don't write.")
args = parser.parse_args()
csv_path = Path(args.working_csv)
rows = _read_working_csv(csv_path)
short_codes = _select_short_codes(rows)
new_py = _render_etl_scenarios_py(short_codes, csv_path)
etl_py = Path(args.etl_py)
current_py = etl_py.read_text() if etl_py.exists() else ""
if args.dry_run:
print("--- new etl/common/etl_scenarios.py ---")
print(new_py)
print("\n--- diff summary ---")
print(f"etl_scenarios.py: {'(no change)' if current_py == new_py else '(would change)'}")
return
if current_py == new_py:
log.info("etl/common/etl_scenarios.py already up to date (%d scenarios)", len(short_codes))
return
etl_py.parent.mkdir(parents=True, exist_ok=True)
etl_py.write_text(new_py)
log.info("Wrote %s (%d scenarios)", etl_py, len(short_codes))
if __name__ == "__main__":
main()