diff --git a/scripts/sitegen/insights.py b/scripts/sitegen/insights.py index 6446e681e..ff525e62a 100644 --- a/scripts/sitegen/insights.py +++ b/scripts/sitegen/insights.py @@ -162,29 +162,29 @@ def _goat_window(start: dt.datetime, end: dt.datetime, auth: dict[str, str]) -> def _goat_hits(start: dt.datetime, end: dt.datetime, auth: dict[str, str]) -> list[tuple[str, int]]: - """Counts by path over the whole span, summed day by day: GoatCounter - keeps hourly counts, so windows add up. The API pages with + """Counts by path over the whole span, summed window by window: + GoatCounter keeps hourly counts, so windows add up. The API pages with exclude_paths, which lengthens the address by every path already read - (GOAT_MAX_EXCLUDED). A day with too many paths is read again in - halves, down to one hour. Days, not weeks: a window that overflows is - read twice, and four weeks cost 432 of the 500 requests GoatCounter - allows per window.""" + (GOAT_MAX_EXCLUDED). A window with too many paths is read again at half + the size, down to one hour, and the windows after it keep that size: a + busy day is followed by busy days, and reading each one whole first + paid every page twice and spent the 500 requests GoatCounter allows + per hour on four weeks.""" totals: dict[str, int] = defaultdict(int) - day = dt.timedelta(days=1) - pending = [(low, min(low + day, end)) for low in - (start + n * day for n in range(-(-(end - start) // day)))] - while pending: - low, high = pending.pop() + hour = dt.timedelta(hours=1) + size = dt.timedelta(days=1) + low = start + while low < end: + high = min(low + size, end) counts = _goat_window(low, high, auth) if counts is None: - hours = int((high - low).total_seconds()) // 3600 - if hours <= 1: + if high - low <= hour: raise SystemExit(f"GoatCounter holds more than {GOAT_MAX_EXCLUDED} paths in the hour from {low}") - middle = low + dt.timedelta(hours=hours // 2) - pending += [(low, middle), (middle, high)] + size = max(hour, (high - low) // 2 // hour * hour) continue for path, count in counts.items(): totals[path] += count + low = high return list(totals.items()) diff --git a/tests/test_site_insights.py b/tests/test_site_insights.py index 48b66a75a..78712daa9 100644 --- a/tests/test_site_insights.py +++ b/tests/test_site_insights.py @@ -273,6 +273,18 @@ class GoatCounterPaging(unittest.TestCase): self.assertEqual(got, {f"/p{n}": n + 1 for n in range(250)}) self.assertGreater(server.calls, 4) + def test_busy_days_pay_the_overflow_once(self) -> None: + # 150 paths every day, at most 100 a window: each day takes two half + # days of one page each, and only the first day is read whole first. + hourly = {(f"/d{day}p{n}", self.START + dt.timedelta(days=day, hours=n % 24)): 1 + for day in range(28) for n in range(150)} + server = FakeGoatCounter(hourly) + with mock.patch.object(insights, "GOAT_MAX_EXCLUDED", 100): + got = self.hits(server) + self.assertEqual(len(got), 28 * 150) + self.assertEqual(sum(got.values()), 28 * 150) + self.assertEqual(server.calls, 1 + 28 * 2) + def test_a_long_rate_limit_ends_the_run_instead_of_sleeping(self) -> None: headers = email.message.Message() headers["X-Rate-Limit-Reset"] = "2355"