"""Public benchmark data only. Customer billing imports never enter this registry.""" from io import BytesIO from pathlib import Path from zipfile import ZipFile, ZIP_DEFLATED import csv import hashlib import json ROOT=Path(__file__).resolve().parent REGISTRY={ "pt-grid-monthly":("data/pt_monthly.csv","REN Portugal national grid demand","GWh","data/pt_provenance.json"), "de-grid-monthly":("data/de_monthly.csv","SMARD Germany national grid demand","GWh","data/de_provenance.json"), "au-nem-monthly":("data/au_monthly.csv","AEMO Australia five-region NEM demand proxy","GWh","data/au_provenance.json"), "model-test-metrics":("evidence/full_model_benchmark/outputs/test_metrics.csv","Archived model test scores","mixed metrics",None), "model-test-predictions":("evidence/full_model_benchmark/outputs/test_predictions_with_truth.csv","Archived test predictions and observed truth","GWh",None), "grokked-extension-predictions":("evidence/sarima_rl_extension/outputs/test_forecasts.csv","DE/AU SARIMA-bias-RL extension predictions","GWh",None), } def manifest(): output=[] for key,(relative,title,unit,provenance) in REGISTRY.items(): path=ROOT/relative if not path.is_file(): continue rows=list(csv.DictReader(path.open())) item={"dataset_id":key,"title":title,"unit":unit,"rows":len(rows),"bytes":path.stat().st_size, "sha256":hashlib.sha256(path.read_bytes()).hexdigest(),"download_path":f"/api/datasets/{key}/download"} if provenance and (ROOT/provenance).is_file(): item["provenance"]=json.loads((ROOT/provenance).read_text()) output.append(item) return {"status":"public_auditable_datasets","datasets":output, "privacy":"Official grid snapshots and already recorded benchmark evidence only. User bills, normalized customer usage and private jobs are excluded.", "archive_scope":"Historical snapshots through September 2026; not a live provider-tariff history. Hosted LLM lanes contain delegated diagnostics rather than independently authenticated API competitor measurements."} def dataset_inspect(payload): selected=payload.get("dataset_id") catalog=manifest() if not selected: return catalog if selected not in REGISTRY or not (ROOT/REGISTRY[selected][0]).is_file(): raise ValueError("Unknown dataset_id; request the manifest first.") offset=payload.get("offset",0);limit=payload.get("limit",100) if isinstance(offset,bool) or isinstance(limit,bool) or not isinstance(offset,int) or not isinstance(limit,int) or offset<0 or not 1<=limit<=1000: raise ValueError("Use integer offset>=0 and limit 1–1000.") path=ROOT/REGISTRY[selected][0] rows=list(csv.DictReader(path.open())) metadata=next(x for x in catalog["datasets"] if x["dataset_id"]==selected) return {**metadata,"records":rows[offset:offset+limit],"next_offset":offset+limit if offset+limit