An open source, non-profit web search engine
Record counts from new crawler
Daoud Clarkece04f4c22352
diff --git a/crawl.py b/crawl.pyindex 1ac4953..31b0c2d 100644--- a/crawl.py+++ b/crawl.py@@ -123,7 +123,7 @@ def run_indexing():score=doc.score, term=doc.term, state=doc.state) for doc in new_items]results = Results(api_key=API_KEY, results=result_items)logger.info(f"Posting {len(result_items)} results")- response = requests.post("https://beta.mwmbl.org/api/v1/crawler/results", json=results.dict())+ response = requests.post("https://mwmbl.org/api/v1/crawler/results", json=results.dict())logger.info(f"Response: {response.text}")response.raise_for_status()diff --git a/devdata/index-v2.tinysearch b/devdata/index-v2.tinysearchindex 72050d9..ae3aa95 100644Binary files a/devdata/index-v2.tinysearch and b/devdata/index-v2.tinysearch differdiff --git a/mwmbl/crawler/app.py b/mwmbl/crawler/app.pyindex e058aef..6c19728 100644--- a/mwmbl/crawler/app.py+++ b/mwmbl/crawler/app.py@@ -185,6 +185,9 @@ def create_router(batch_cache: BatchCache, queued_batches: RedisURLQueue, versionow = datetime.now(timezone.utc)filename = upload_object(results, now, api_key.user.username, "results")+ # Update stats for the user+ stats_manager.record_results(results, api_key.user.username)+return {'status': 'ok','url': f'{PUBLIC_URL_PREFIX}{filename}',diff --git a/mwmbl/crawler/stats.py b/mwmbl/crawler/stats.pyindex 03254d6..181dd35 100644--- a/mwmbl/crawler/stats.py+++ b/mwmbl/crawler/stats.py@@ -12,7 +12,7 @@ from pydantic import BaseModelfrom redis import Redisfrom mwmbl.count_urls import get_counts, get_domain_result_count-from mwmbl.crawler.batch import HashedBatch+from mwmbl.crawler.batch import HashedBatch, Resultsfrom mwmbl.crawler.urls import URLDatabasefrom mwmbl.indexer.batch_cache import BatchCachefrom mwmbl.indexer.update_urls import get_datetime_from_timestamp@@ -27,6 +27,9 @@ HOST_COUNT_KEY = "host-count-{date}"HOST_COUNT_ALL_KEY = "host-count-all-{date}"HOST_COUNT_LINK_KEY = "host-count-link-{date}"HOST_COUNT_LINK_NEW_KEY = "host-count-link-new-{date}"+RESULTS_COUNT_KEY = "results-count-{date}"+USER_RESULTS_COUNT_KEY = "user-results-count-{date}"+SHORT_EXPIRE_SECONDS = 60 * 60 * 24LONG_EXPIRE_SECONDS = 60 * 60 * 24 * 30@@ -47,6 +50,8 @@ class MwmblStats(BaseModel):users_crawled_daily: dict[str, int]top_users: list[tuple[str, int]]top_domains: list[tuple[str, int]]+ results_indexed_daily: dict[str, int]+ top_user_results: list[tuple[str, int]]urls_in_index_daily: dict[str, int]domains_in_index_daily: dict[str, int]results_in_index_daily: dict[str, int]@@ -138,6 +143,7 @@ class StatsManager:urls_crawled_daily = {}users_crawled_daily = {}+ results_indexed_daily = {}for i in range(29, -1, -1):date_i = date - timedelta(days=i)url_count_key = URL_DATE_COUNT_KEY.format(date=date_i)@@ -150,6 +156,12 @@ class StatsManager:user_day_count = self.redis.scard(user_day_count_key)users_crawled_daily[str(date_i)] = user_day_count+ result_count_key = RESULTS_COUNT_KEY.format(date=date_i)+ result_count = self.redis.get(result_count_key)+ if result_count is None:+ result_count = 0+ results_indexed_daily[str(date_i)] = result_count+hour_counts = []for i in range(date_time.hour + 1):hour = datetime(date_time.year, date_time.month, date_time.day, i)@@ -167,6 +179,10 @@ class StatsManager:urls_crawled_today = list(urls_crawled_daily.values())[-1]index_stats = get_counts()++ user_results_count_key = USER_RESULTS_COUNT_KEY.format(date=date_time.date())+ user_results_counts = self.redis.zrevrange(user_results_count_key, 0, 100, withscores=True)+return MwmblStats(urls_crawled_today=urls_crawled_today,urls_crawled_daily=urls_crawled_daily,@@ -174,6 +190,8 @@ class StatsManager:users_crawled_daily=users_crawled_daily,top_users=user_counts,top_domains=host_counts,+ results_indexed_daily=results_indexed_daily,+ top_user_results=user_results_counts,**index_stats,)@@ -215,6 +233,16 @@ class StatsManager:)return domain_stats+ def record_results(self, results: Results, username: str) -> None:+ num_results = len(results.results)+ result_count_key = RESULTS_COUNT_KEY.format(date=datetime.utcnow().date())+ self.redis.incrby(result_count_key, num_results)+ self.redis.expire(result_count_key, LONG_EXPIRE_SECONDS)++ user_result_count_key = USER_RESULTS_COUNT_KEY.format(date=datetime.utcnow().date())+ self.redis.zincrby(user_result_count_key, num_results, username)+ self.redis.expire(user_result_count_key, SHORT_EXPIRE_SECONDS)+def get_test_batches():for path in glob("./devdata/batches/**/*.json.gz", recursive=True):