From ac16875c5bdf056500656ba5022871289c452ea0 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Fri, 20 Nov 2026 08:18:16 +0100 Subject: [PATCH] fix: date the site period from its first count --- scripts/sitegen/insights.py | 15 +++++++++++---- tests/test_site_insights.py | 6 ++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/scripts/sitegen/insights.py b/scripts/sitegen/insights.py index 89be93c47..1f28855ff 100644 --- a/scripts/sitegen/insights.py +++ b/scripts/sitegen/insights.py @@ -154,8 +154,9 @@ def _goat_export(first: dt.datetime, auth: dict[str, str]) -> bytes: def read_export(raw: bytes, first: dt.datetime, after: dt.datetime) -> dict: """Paths and events with their counts, the referring domains of page - views, from first up to after (excluded). An action is counted as a - path without a leading slash and carries no referrer.""" + views, from first up to after (excluded), and the first day counted. + An action is counted as a path without a leading slash and carries no + referrer.""" with zipfile.ZipFile(io.BytesIO(raw)) as archive: members = {Path(name).name: name for name in archive.namelist()} @@ -169,15 +170,18 @@ def read_export(raw: bytes, first: dt.datetime, after: dt.datetime) -> dict: low, high = first.strftime("%Y-%m-%dT%H:00:00Z"), after.strftime("%Y-%m-%dT%H:00:00Z") hits: dict[str, int] = defaultdict(int) referrers: dict[str, int] = defaultdict(int) + hours: set[str] = set() for row in stats: if low <= row["hour"] < high: + hours.add(row["hour"]) path = paths[row["path_id"]] hits[path] += row["count"] if path.startswith("/"): referrers[refs[row["ref_id"]] or "(direct)"] += row["count"] actions = sum(count for path, count in hits.items() if not path.startswith("/")) return {"hits": list(hits.items()), "referrers": sorted(referrers.items(), key=lambda row: -row[1]), - "visits": sum(hits.values()) - actions, "actions": actions} + "visits": sum(hits.values()) - actions, "actions": actions, + "first_day": dt.date.fromisoformat(min(hours)[:10]) if hours else None} def fetch_goatcounter(start: dt.date, end: dt.date) -> dict: @@ -487,9 +491,12 @@ SITE_ACTIONS = {"search-none": "Site searches that found nothing", "404": "Broke def _goatcounter(readings: Readings, goat: dict) -> list[str]: + # The site counts visits since 2026-11-06: a period that starts + # earlier is stated from the first day GoatCounter holds. actions = site_actions(goat["hits"]) + start = max(readings.start, goat.get("first_day") or readings.start) out = ["## On the site", "", - f"{_span(readings.start, readings.goat_end or readings.end)}, UTC days: " + f"{_span(start, readings.goat_end or readings.end)}, UTC days: " f"{goat.get('visits', 0):,} page views, {goat.get('actions', 0):,} actions.", ""] for kind, title in SITE_ACTIONS.items(): if kind in actions: diff --git a/tests/test_site_insights.py b/tests/test_site_insights.py index bb7198651..abb6a01a4 100644 --- a/tests/test_site_insights.py +++ b/tests/test_site_insights.py @@ -212,6 +212,11 @@ class TheReport(unittest.TestCase): text = insights.report(readings(goat=goat, goat_end=dt.date(2026, 11, 19))) self.assertIn("2026-10-01 to 2026-11-19 (50 days), UTC days: 4 page views", text) + def test_the_site_period_starts_on_the_first_day_counted(self) -> None: + goat = {"hits": [], "referrers": [], "visits": 4, "actions": 0, "first_day": dt.date(2026, 11, 6)} + text = insights.report(readings(goat=goat, goat_end=dt.date(2026, 11, 19))) + self.assertIn("2026-11-06 to 2026-11-19 (14 days), UTC days", text) + def test_names_come_from_profiles_and_the_collection(self) -> None: db = {"indexes": {"by_name": {"SCPH5501.BIN": ["x"]}}} profiles = {"p": {"files": [{"name": "bios/dsp1b.rom", "aliases": ["DSP1B.bin"]}, "str"]}} @@ -253,6 +258,7 @@ class GoatCounterExport(unittest.TestCase): self.assertEqual(dict(got["hits"]), {"/": 7, "/gone/ [404]": 2, "search-none/kenji": 5}) self.assertEqual(got["referrers"], [("Google", 4), ("(direct)", 3), ("github.com", 2)]) self.assertEqual((got["visits"], got["actions"]), (9, 5)) + self.assertEqual(got["first_day"], dt.date(2026, 10, 20)) self.assertEqual(insights.site_actions(got["hits"])["404"], [("/gone/", 2)]) def test_the_export_is_asked_for_once_and_read_when_ready(self) -> None: