An open source, non-profit web search engine
Stats: record crawlers per day as users crawling per day
blueheron7861e5d7a28929d
diff --git a/mwmbl/crawler/stats.py b/mwmbl/crawler/stats.pyindex 3800210..d7cddb4 100644--- a/mwmbl/crawler/stats.py+++ b/mwmbl/crawler/stats.py@@ -108,6 +108,8 @@ class StatsManager:self.redis.incrby(hour_key, num_crawled_urls)self.redis.expire(hour_key, SHORT_EXPIRE_SECONDS)+ # TODO: remove this if we don't want the old crawlers to contribute+ # to the "number of crawlers crawling today" stat.users_key = USERS_KEY.format(date=date)self.redis.sadd(users_key, hashed_batch.user_id_hash)self.redis.expire(users_key, LONG_EXPIRE_SECONDS)@@ -280,6 +282,13 @@ class StatsManager:self.redis.zincrby(user_result_count_key, num_results, username)self.redis.expire(user_result_count_key, SHORT_EXPIRE_SECONDS)+ # The new crawler submits via the API-key-authenticated results endpoint rather than+ # record_batch, so this is the path that sees real crawlers. Add the account so that+ # users_crawled_daily counts distinct crawlers that contacted us today.+ users_key = USERS_KEY.format(date=utc_today())+ self.redis.sadd(users_key, username)+ self.redis.expire(users_key, LONG_EXPIRE_SECONDS)+def record_blacklisted_removed(self, num_results: int) -> None:"""Record documents removed from the index by the background blacklist purge.diff --git a/test/test_stats_users_crawled.py b/test/test_stats_users_crawled.pynew file mode 100644index 0000000..e2268e0--- /dev/null+++ b/test/test_stats_users_crawled.py@@ -0,0 +1,77 @@+"""+Tests for "users crawled today" — the count of distinct crawlers that contacted us.++Both submission paths feed the same users set that `get_stats` reads back with `scard`+to build `users_crawled_daily`:++- `record_batch` (the deprecated batch flow) adds the crawler's self-claimed+ `user_id_hash`.+- `record_results` (the modern API-key-authenticated results endpoint) adds the+ account's `username`.++Because writer and reader are in different methods, a change to one side that splits+the key format would silently zero the chart, so these tests pin the two writers to the+same set.+"""++from datetime import datetime, timezone+from unittest.mock import patch++import fakeredis++from mwmbl.crawler.batch import HashedBatch, Item, ItemContent, Result, Results+from mwmbl.crawler.stats import StatsManager+from mwmbl.utils import utc_today++NO_INDEX_COUNTS = {+ "urls_in_index_daily": {},+ "domains_in_index_daily": {},+ "results_in_index_daily": {},+}+++def make_results(*urls: str) -> Results:+ return Results(results=[Result(url=url, title="t", extract="e") for url in urls])+++def make_batch(user_id_hash: str) -> HashedBatch:+ content = ItemContent(title="Example", extract="An example page", links=[], extra_links=[])+ item = Item(+ url="https://example.com/",+ status=200,+ timestamp=datetime.now(timezone.utc).timestamp(),+ content=content,+ )+ return HashedBatch(user_id_hash=user_id_hash, timestamp=item.timestamp, items=[item])+++def test_record_results_counts_each_crawler_once_per_day():+ redis = fakeredis.FakeRedis(decode_responses=True)+ stats_manager = StatsManager(redis)++ # A crawler submits twice in the same day with the same API-key user.+ stats_manager.record_results(make_results("https://example.com/"), "alice")+ stats_manager.record_results(make_results("https://example.com/b", "https://example.com/c"), "alice")+ stats_manager.record_results(make_results("https://other.org/"), "bob")++ with patch("mwmbl.crawler.stats.get_counts", return_value=NO_INDEX_COUNTS):+ stats = stats_manager.get_stats()++ today = str(utc_today())+ assert stats.users_crawled_daily[today] == 2+++def test_both_submission_paths_feed_the_same_users_set():+ redis = fakeredis.FakeRedis(decode_responses=True)+ stats_manager = StatsManager(redis)++ # The old batch flow and the new results endpoint each contribute a distinct crawler.+ with patch("mwmbl.crawler.stats.URLDatabase"):+ stats_manager.record_batch(make_batch(user_id_hash="a" * 64))+ stats_manager.record_results(make_results("https://example.com/"), "alice")++ with patch("mwmbl.crawler.stats.get_counts", return_value=NO_INDEX_COUNTS):+ stats = stats_manager.get_stats()++ today = str(utc_today())+ assert stats.users_crawled_daily[today] == 2