Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Address review

claude[bot]d33d7f22a2ce
diff --git a/mwmbl/admin_views.py b/mwmbl/admin_views.py
index c67daea..ae38458 100644
--- a/mwmbl/admin_views.py
+++ b/mwmbl/admin_views.py
@@ -258,9 +258,14 @@ def _user_agents(redis_client, days: list[date], counts: dict) -> dict:
shows up at all.
"""
agent_days = days[:USER_AGENT_MAX_DAYS]
- tracked = read_user_agent_counts(redis_client, agent_days, TRACKED_USER_AGENTS)
+ # The limit is the window's union, not one day's trim. Each day is already read down to
+ # TRACKED_USER_AGENTS of its own, so a week of browsers routinely sums to more distinct
+ # strings than that; cutting the sum to one day's limit again would put the readout's own
+ # cut into the gap below, where it reads as the trim's.
+ tracked = read_user_agent_counts(redis_client, agent_days, TRACKED_USER_AGENTS * len(agent_days))
tracked_total = sum(count for _, count in tracked)
- request_total = sum(count for coordinate, count in counts.items() if coordinate[DAY] in set(agent_days))
+ agent_day_set = set(agent_days)
+ request_total = sum(count for coordinate, count in counts.items() if coordinate[DAY] in agent_day_set)
top = [
{
diff --git a/test/test_admin_search_traffic.py b/test/test_admin_search_traffic.py
index 5a28b46..d5669ad 100644
--- a/test/test_admin_search_traffic.py
+++ b/test/test_admin_search_traffic.py
@@ -150,6 +150,25 @@ def test_the_gap_is_the_traffic_no_tracked_agent_accounts_for(redis_client):
assert (agents["request_total"], agents["tracked_total"], agents["gap"]) == (14, 10, 4)
+def test_the_gap_excludes_agents_only_the_readout_dropped(redis_client):
+ """Each day is read down to the trim limit on its own, so the window's union of agents
+ can exceed it without a single request going unattributed. Summing only the window's top
+ TRACKED_USER_AGENTS would report the readout's own cut as a gap."""
+ yesterday = utc_today() - timedelta(days=1)
+ for i in range(traffic.TRACKED_USER_AGENTS):
+ record(redis_client, "v1_search", user_agent=f"Today/{i}")
+ record(redis_client, "v1_search", day=yesterday, user_agent=f"Yesterday/{i}")
+
+ agents = admin_views._user_agents(traffic.get_redis(), *window(2))
+
+ assert agents["tracked"] == 2 * traffic.TRACKED_USER_AGENTS
+ assert (agents["request_total"], agents["tracked_total"], agents["gap"]) == (
+ 2 * traffic.TRACKED_USER_AGENTS,
+ 2 * traffic.TRACKED_USER_AGENTS,
+ 0,
+ )
+
+
def test_view_requires_staff(client, db):
user = User.objects.create_user(username="ordinary_user_2", password="correctpassword")
client.force_login(user)