Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Wire up seed pages crawled user stat

blueheron786824214578ebc
diff --git a/mwmbl/indexer/seed_crawl.py b/mwmbl/indexer/seed_crawl.py
index 417dd64..bc41751 100644
--- a/mwmbl/indexer/seed_crawl.py
+++ b/mwmbl/indexer/seed_crawl.py
@@ -29,7 +29,7 @@ take minutes, and in series with the periodic tasks they would hold up the usage
the Polar report. One crawl runs at a time, each user has at most one
queued or running, and at most SEED_CRAWL_MAX_QUEUED are, so nobody can fill the queue.
-The new pages a crawl added are counted on the user for their stats, and what it found on
+The new pages a search and its crawl added are counted on the user for their stats, and what it found on
each of Staan's domains is scored for crawlers as seed domains - see
mwmbl.indexer.seed_domains.
"""
@@ -218,6 +218,20 @@ def crawl_round(frontier: Frontier, batch: list[str], redis) -> list[Document]:
return documents
+def credit_seed_search_pages(user_id: int, count: int) -> None:
+ """Add pages a Seed Search or its crawl put into the index to the user's all-time total."""
+ if count:
+ MwmblUser.objects.filter(id=user_id).update(seed_search_pages_indexed=F("seed_search_pages_indexed") + count)
+
+
+def record_seed_search(user_id: int, pages_added: int) -> None:
+ """Count a search made with Seed Search on, and the new pages it wrote to the index, for the user's stats."""
+ MwmblUser.objects.filter(id=user_id).update(
+ seed_searches=F("seed_searches") + 1,
+ seed_search_pages_indexed=F("seed_search_pages_indexed") + pages_added,
+ )
+
+
def start_seed_crawl(
user_id: int, query: str, index_pages: list[Document], staan_results: list[Document], new_staan_urls: set[str]
) -> CrawlOutcome:
@@ -501,10 +515,9 @@ def _run_round(redis, record_key: str, token: int, fields: dict[str, str], index
return
logger.info("Seed crawl for user %d added %d new pages", user_id, len(new_documents))
# After the round is recorded, so a round run twice is not counted twice.
- if new_documents:
- MwmblUser.objects.filter(id=user_id).update(
- seed_search_pages_indexed=F("seed_search_pages_indexed") + len(new_documents)
- )
+ # Not the new seeds: the search that queued this crawl wrote them, and credited them then.
+ crawled_pages = [document for document in new_documents if document.url not in new_seed_urls]
+ credit_seed_search_pages(user_id, len(crawled_pages))
def _finish(
diff --git a/mwmbl/migrations/0045_mwmbluser_seed_searches.py b/mwmbl/migrations/0045_mwmbluser_seed_searches.py
new file mode 100644
index 0000000..3cf1e32
--- /dev/null
+++ b/mwmbl/migrations/0045_mwmbluser_seed_searches.py
@@ -0,0 +1,17 @@
+# Generated by Django 5.2.13 on 2026-10-16 19:24
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("mwmbl", "0044_mwmbluser_combined_search_enabled"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="mwmbluser",
+ name="seed_searches",
+ field=models.IntegerField(default=0),
+ ),
+ ]
diff --git a/mwmbl/models.py b/mwmbl/models.py
index 8b90be1..318d5b2 100644
--- a/mwmbl/models.py
+++ b/mwmbl/models.py
@@ -15,6 +15,8 @@ class MwmblUser(AbstractUser):
# all time. A total on the user rather than a UserStats column, because a UserStats row
# created for it would enter the crawl leaderboards with no crawl results behind it.
seed_search_pages_indexed = models.IntegerField(default=0)
+ # How many searches the user has made with Seed Search on (Combined Search with crawl=true), all time.
+ seed_searches = models.IntegerField(default=0)
# On for new accounts. Accounts created before this field existed start off (see the migration).
combined_search_enabled = models.BooleanField(default=True)
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.py
index 2b1c75f..1efe2cd 100644
--- a/mwmbl/platform/api.py
+++ b/mwmbl/platform/api.py
@@ -1212,15 +1212,20 @@ def get_current_user(request):
summary="Get current user's contribution stats",
description=(
"Returns the authenticated user's contribution stats: how many results they "
- "have indexed, broken down per day for the last 30 days, and how many pages their "
- "seed crawls (Combined Search with `crawl=true`) have added to the index in all."
+ "have indexed, broken down per day for the last 30 days, how many Seed Searches "
+ "(Combined Search with `crawl=true`) they have made, and how many new pages those "
+ "searches and their crawls have added to the index, both in all."
),
tags=["Users"],
)
def get_current_user_stats(request):
user = request.user
stats = stats_manager.get_user_stats(user.username)
- return {**stats, "seed_search_pages_indexed": user.seed_search_pages_indexed}
+ return {
+ **stats,
+ "seed_search_pages_indexed": user.seed_search_pages_indexed,
+ "seed_searches": user.seed_searches,
+ }
# ---------------------------------------------------------------------------
diff --git a/mwmbl/platform/schemas.py b/mwmbl/platform/schemas.py
index 575d05f..1938fca 100644
--- a/mwmbl/platform/schemas.py
+++ b/mwmbl/platform/schemas.py
@@ -23,6 +23,7 @@ class UserStatsResponse(Schema):
results_indexed_today: int
results_indexed_daily: dict[str, int]
seed_search_pages_indexed: int
+ seed_searches: int
class SubscriptionResponse(Schema):
diff --git a/mwmbl/tinysearchengine/combined_search.py b/mwmbl/tinysearchengine/combined_search.py
index 4592eb3..4d4755a 100644
--- a/mwmbl/tinysearchengine/combined_search.py
+++ b/mwmbl/tinysearchengine/combined_search.py
@@ -48,6 +48,7 @@ from mwmbl.indexer.seed_crawl import (
get_active_seed_crawl_query,
get_seed_crawl,
get_seed_crawl_summary,
+ record_seed_search,
start_seed_crawl,
)
from mwmbl.indexer.seed_domains import published_seed_domains
@@ -309,6 +310,7 @@ def init_router(ranker) -> None:
crawl_outcome = None
active_crawl_query = None
if crawl:
+ await sync_to_async(record_seed_search)(user.id, len(new_staan_urls))
crawl_outcome = await sync_to_async(start_seed_crawl)(
user.id, q, retrieval.pages, staan_results, new_staan_urls
)
diff --git a/test/test_combined_search.py b/test/test_combined_search.py
index 9417b18..b4724ca 100644
--- a/test/test_combined_search.py
+++ b/test/test_combined_search.py
@@ -554,6 +554,30 @@ def test_crawl_schedules_a_seed_crawl_of_what_the_index_lacked(
assert seed_crawl.get_seed_crawl(user.id, "tokio")["status"] == "queued"
+def test_a_seed_search_counts_itself_and_its_pages_in_the_users_totals(
+ client, user, access_token, fresh_quota, redis_cache, crawl_sources
+):
+ crawl_sources(new_urls=("https://a.example/", "https://b.example/", "https://c.example/"))
+
+ _crawl(client, access_token)
+ user.refresh_from_db()
+
+ assert user.seed_search_pages_indexed == 3
+ assert user.seed_searches == 1
+
+
+def test_a_search_without_the_crawl_flag_credits_nothing(client, user, access_token, fresh_quota, stub_sources):
+ stub_sources(new_urls=("https://a.example/",))
+
+ _get(client, access_token)
+ user.refresh_from_db()
+
+ assert user.seed_search_pages_indexed == 0
+ assert user.seed_searches == 0
+
+
@pytest.mark.django_db
def test_no_crawl_without_the_flag(client, access_token, fresh_quota, redis_cache, crawl_sources):
crawl_sources()
diff --git a/test/test_seed_crawl.py b/test/test_seed_crawl.py
index 7c833fe..4533922 100644
--- a/test/test_seed_crawl.py
+++ b/test/test_seed_crawl.py
@@ -443,15 +443,36 @@ def test_new_pages_are_added_to_the_users_total(fake_web, redis_cache, user, ind
assert user.seed_search_pages_indexed == 3
+def test_seeds_the_search_found_new_are_not_counted_again_by_the_crawl(fake_web, redis_cache, user, index_path):
+ _index_staan_snippet_of_the_seed(index_path)
+ _run(user, index_path, new_seed_urls=[SEED])
+ user.refresh_from_db()
+
+ assert SEED in [page["url"] for page in seed_crawl.get_seed_crawl(user.id, "tokio")["pages"]]
+ assert user.seed_search_pages_indexed == 2
+
+
+def test_recording_a_seed_search_adds_to_the_users_totals(user):
+ seed_crawl.record_seed_search(user.id, 3)
+ seed_crawl.record_seed_search(user.id, 0)
+ user.refresh_from_db()
+
+ assert user.seed_searches == 2
+ assert user.seed_search_pages_indexed == 3
+
+
@pytest.mark.django_db
def test_the_users_stats_report_their_seed_search_total(user):
- User.objects.filter(id=user.id).update(seed_search_pages_indexed=12)
+ User.objects.filter(id=user.id).update(seed_search_pages_indexed=12, seed_searches=4)
token = str(RefreshToken.for_user(user).access_token)
response = Client().get("/api/v1/platform/user/stats", HTTP_AUTHORIZATION=f"Bearer {token}")
assert response.status_code == 200
assert response.json()["seed_search_pages_indexed"] == 12
+ assert response.json()["seed_searches"] == 4
# ---------------------------------------------------------------------------