Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Address second review: drop dump.rdb, record agreements last, vote without the gap

Clauded500bf5dc807
diff --git a/.gitignore b/.gitignore
index 6fb741b..596c2a2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -230,4 +230,6 @@ devdata/external-cache-engb.tinysearch
!devdata/judge_train/eval_manifest.json
devdata/moderation_bakeoff/
-vendor/
\ No newline at end of file
+vendor/
+# Redis snapshot written by a local redis-server started in the repo
+dump.rdb
diff --git a/dump.rdb b/dump.rdb
deleted file mode 100644
index 7c93c23..0000000
Binary files a/dump.rdb and /dev/null differ
diff --git a/mwmbl/crawler/app.py b/mwmbl/crawler/app.py
index a627a4c..165319c 100644
--- a/mwmbl/crawler/app.py
+++ b/mwmbl/crawler/app.py
@@ -272,13 +272,13 @@ def _register_routes(r: Router | NinjaAPI):
# with a plain unauthenticated GET), so uploading the object as submitted would
# publish the plaintext key of any client still using the deprecated body field.
filename = upload_object(results.model_copy(update={"api_key": None}), now, api_key.user.username, "results")
- # After the upload: the agreements are analytics, and failing to store them should not
- # cost the batch.
- record_agreements(indexed.agreements, api_key.user, results.crawler_version)
-
# Update stats for the user
stats_manager.record_results(results, api_key.user)
+ # Last, since they are only analytics: a failure to store them still answers with an
+ # error, but after the batch is indexed, uploaded and counted.
+ record_agreements(indexed.agreements, api_key.user, results.crawler_version)
+
return {
"status": "ok",
"url": f"{PUBLIC_URL_PREFIX}{filename}",
diff --git a/mwmbl/indexer/index_batches.py b/mwmbl/indexer/index_batches.py
index 2640d5e..6ae322c 100644
--- a/mwmbl/indexer/index_batches.py
+++ b/mwmbl/indexer/index_batches.py
@@ -100,10 +100,19 @@ def _one_agreement_per_url(page_agreements: Counter) -> set[Agreement]:
"""The URL is on the page of each of its terms, and the pages need not all hold the same
stored copy: two crawlers' versions have different terms, and which one a shared term's
page keeps depends on rank. Keeping the copy most of its pages hold gives one agreement
- per submitted URL, so a URL with divergent copies is not counted several times over."""
+ per submitted URL, so a URL with divergent copies is not counted several times over.
+
+ The vote leaves out the crawl time gap: a page's stored crawl time is the latest of the
+ copies merged on it, so one stored copy can carry different gaps on different pages."""
+ copy_votes = Counter()
+ agreement_by_copy = {}
+ for agreement, count in page_agreements.items():
+ copy = agreement._replace(last_crawled_delta=None)
+ copy_votes[copy] += count
+ agreement_by_copy.setdefault(copy, agreement)
agreements_by_url = {}
- for agreement, _ in page_agreements.most_common():
- agreements_by_url.setdefault(agreement.url, agreement)
+ for copy, _ in copy_votes.most_common():
+ agreements_by_url.setdefault(copy.url, agreement_by_copy[copy])
return set(agreements_by_url.values())
diff --git a/test/test_crawler_agreement.py b/test/test_crawler_agreement.py
index 7b3de6c..59b2eea 100644
--- a/test/test_crawler_agreement.py
+++ b/test/test_crawler_agreement.py
@@ -1,3 +1,4 @@
+from collections import Counter
from datetime import timedelta
from unittest.mock import patch
@@ -11,7 +12,7 @@ from mwmbl.crawler.agreement import AGREEMENT_RETENTION, prune_agreements, recor
from mwmbl.indexer.agreement import Agreement, find_agreements, trigram_similarity
from mwmbl.indexer.blacklist_providers import StaticBlacklistProvider
from mwmbl.indexer.blacklist_snapshot import SnapshotBlacklist
-from mwmbl.indexer.index_batches import index_documents
+from mwmbl.indexer.index_batches import _one_agreement_per_url, index_documents
from mwmbl.models import ApiKey, CrawlerAgreement, generate_api_key
from mwmbl.tinysearchengine.indexer import PAGE_SIZE, Document, TinyIndex
@@ -164,6 +165,18 @@ def test_index_documents_reports_one_agreement_when_stored_copies_differ(index_p
assert agreement.extract_similarity == 1.0
+def test_one_agreement_per_url_votes_on_the_copy_not_the_crawl_time_gap():
+ """A page's stored crawl time is the latest of the copies merged on it, so one stored
+ copy can carry a different gap on each page; it still gets all of its pages' votes."""
+ held_copy = Agreement(URL, existing_user_id=1, title_similarity=1.0, extract_similarity=0.2, last_crawled_delta=10)
+ other_copy = Agreement(URL, existing_user_id=2, title_similarity=1.0, extract_similarity=0.9, last_crawled_delta=10)
+ page_agreements = Counter({held_copy: 2, held_copy._replace(last_crawled_delta=20): 2, other_copy: 3})
+
+ (agreement,) = _one_agreement_per_url(page_agreements)
+
+ assert agreement.existing_user_id == 1
+
+
def test_index_documents_reports_no_agreement_for_a_new_url(index_path, no_blacklist):
indexed = index_documents([Document("Python tutorial", URL, "Learn python today", user_ids=[1])], index_path)