From 0936a2da94f33e810aa7eae871d473fa0c97224d Mon Sep 17 00:00:00 2001 From: Danilo Riedel Date: Thu, 26 Nov 2026 00:10:07 -0300 Subject: [PATCH] feat(worker): list scraped pages in the build report pages.files --- tests/test_jobs.py | 36 ++++++++++++++++++++++++++++++++++-- worker/jobs.py | 31 +++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/tests/test_jobs.py b/tests/test_jobs.py index ee4be6d..cc4d6a8 100644 --- a/tests/test_jobs.py +++ b/tests/test_jobs.py @@ -336,7 +336,10 @@ def test_build_report_summary_aggregates_snapshot_and_report(): summary = _build_report_summary(_sample_snapshot(), _sample_report_files(), steps) assert set(summary) == {"pages", "assets", "totals", "steps_seconds", "top_savings"} - assert summary["pages"] == {"count": 1, "bytes": 2000} + assert summary["pages"] == { + "count": 1, "bytes": 2000, + "files": [{"path": "index.html", "bytes": 2000}], + } assets = summary["assets"] assert list(assets) == ["images", "css", "js", "fonts", "videos", "html", "other"] @@ -401,7 +404,32 @@ def test_build_report_summary_without_report_marks_everything_unchanged(): "files": 2, "original_bytes": 1500, "final_bytes": 1500, "saved_pct": 0.0, } assert not summary["top_savings"] - assert summary["pages"] == {"count": 1, "bytes": 1000} + assert summary["pages"] == { + "count": 1, "bytes": 1000, + "files": [{"path": "index.html", "bytes": 1000}], + } + + +def test_build_report_summary_pages_files_sorted_and_capped(): + """pages.files is sorted by path and capped at 200; count/bytes cover all.""" + from jobs import _build_report_summary + + snapshot = { + f"pages/{i:03d}.html": {"bytes": 100, "kind": "html"} + for i in reversed(range(205)) + } + + summary = _build_report_summary(snapshot, [], {}) + + pages = summary["pages"] + assert pages["count"] == 205 + assert pages["bytes"] == 20500 + files = pages["files"] + assert len(files) == 200 + paths = [f["path"] for f in files] + assert paths == sorted(paths) + assert paths[0] == "pages/000.html" + assert all(f == {"path": p, "bytes": 100} for f, p in zip(files, paths)) def test_build_report_summary_empty_snapshot_saved_pct_zero(): @@ -416,6 +444,7 @@ def test_build_report_summary_empty_snapshot_saved_pct_zero(): assert all( bucket["count"] == 0 for bucket in summary["assets"].values() ) + assert summary["pages"] == {"count": 0, "bytes": 0, "files": []} # --------------------------------------------------------------------------- @@ -471,6 +500,9 @@ def fake_run(cmd, **_kwargs): "count": 1, "original_bytes": 1000, "final_bytes": 300, } assert report["pages"]["count"] == 1 + assert report["pages"]["files"] == [ + {"path": "index.html", "bytes": len(" hi ")}, + ] assert report["top_savings"] == [ {"path": "photo.jpg", "original_bytes": 1000, "final_bytes": 300}, ] diff --git a/worker/jobs.py b/worker/jobs.py index b9ac9f4..db817d0 100644 --- a/worker/jobs.py +++ b/worker/jobs.py @@ -316,6 +316,11 @@ def _build_opt_cmd(workdir: pathlib.Path, opts: dict, hostname: str = "") -> lis "other": "other", } +# Cap on the pages.files list in the build report — the result travels +# through Redis/the API and is stored by clients, so it must stay bounded. +# count/bytes still reflect ALL pages. +_PAGES_FILES_CAP = 200 + def _snapshot_workdir(workdir: pathlib.Path) -> dict[str, dict]: """ @@ -393,6 +398,22 @@ def _accumulate_entry( savings.append({"path": path, "original_bytes": original, "final_bytes": final}) +def _page_files(snapshot: dict[str, dict]) -> list[dict]: + """ + Per-page list for the build report: one {"path", "bytes"} entry per HTML + file in the snapshot (bytes = scraped/original size), sorted by path and + capped at _PAGES_FILES_CAP — the result travels through Redis/the API and + is stored by clients, so the list must stay bounded while the pages + count/bytes still cover ALL pages. + """ + pages = [ + {"path": rel, "bytes": info["bytes"]} + for rel, info in sorted(snapshot.items()) + if info["kind"] == "html" + ] + return pages[:_PAGES_FILES_CAP] + + def _build_report_summary( snapshot: dict[str, dict], report_files: list, @@ -409,7 +430,9 @@ def _build_report_summary( snapshot walk and the bundle contributes sum(sources) → bundle size; - everything else is unchanged: final bytes = snapshot bytes; - HTML files additionally feed "pages" (count + scraped/original bytes; - the minification delta itself lives in assets.html). + the minification delta itself lives in assets.html) and "pages.files", + a per-page list of {path, bytes} (scraped size) sorted by path and + capped at _PAGES_FILES_CAP entries; count/bytes cover ALL pages. """ buckets = { name: {"count": 0, "original_bytes": 0, "final_bytes": 0} @@ -465,7 +488,11 @@ def _build_report_summary( html_bucket = buckets["html"] return { - "pages": {"count": html_bucket["count"], "bytes": html_bucket["original_bytes"]}, + "pages": { + "count": html_bucket["count"], + "bytes": html_bucket["original_bytes"], + "files": _page_files(snapshot), + }, "assets": assets, "totals": { "files": total_files,