An open source, non-profit web search engine
Set expiry
Daoud Clarkeb3a481e0047a
diff --git a/mwmbl/count_urls.py b/mwmbl/count_urls.pyindex aa5bfc9..1493b4b 100644--- a/mwmbl/count_urls.py+++ b/mwmbl/count_urls.py@@ -1,7 +1,8 @@import os-from datetime import date+from datetime import date, timedeltafrom logging import getLoggerfrom pathlib import Path+from urllib.parse import urlparsefrom django.conf import settingsfrom redis import Redis@@ -9,7 +10,13 @@ from redis import Redisfrom mwmbl.tinysearchengine.indexer import TinyIndex, Documentfrom mwmbl.utils import batch-INDEX_URL_COUNT_KEY = "index-count-{date}"+INDEX_RESULT_COUNT_KEY = "index-result-count-{date}"+INDEX_DOMAIN_COUNT_KEY = "index-domain-count-{date}"+INDEX_URL_COUNT_KEY = "index-url-count-{date}"++INDEX_URL_HLL_KEY = "index-hll-{date}"+INDEX_DOMAIN_HLL_KEY = "index-domain-hll-{date}"+SHORT_EXPIRE_SECONDS = 60 * 60 * 24LONG_EXPIRE_SECONDS = 60 * 60 * 24 * 30NUM_PAGES_IN_BATCH = 1024@@ -21,33 +28,74 @@ def get_redis():return Redis.from_url(os.environ.get("REDIS_URL", "redis://127.0.0.1:6379"), decode_responses=True)-def count_urls() -> int:+def count_urls():index_path = Path(settings.DATA_PATH) / settings.INDEX_NAMEredis = get_redis()logger.info(f"Counting URLs in index {index_path}")- key = INDEX_URL_COUNT_KEY.format(date=date.today())+ today = date.today()+ url_hll_key = INDEX_URL_HLL_KEY.format(date=today)+ domain_hll_key = INDEX_DOMAIN_HLL_KEY.format(date=today)+ num_results = 0with TinyIndex(item_factory=Document, index_path=index_path) as tiny_index:# Count using a Redis hyperloglog to avoid memory issues.- for page_batch in batch(range(tiny_index.num_pages), NUM_PAGES_IN_BATCH):+ for page_indexes in batch(range(tiny_index.num_pages), NUM_PAGES_IN_BATCH):urls = set()- for page in page_batch:- urls |= {doc.url for doc in tiny_index.get_page(page)}- redis.pfadd(key, *urls)- logger.info(f"Counted {page} pages of {tiny_index.num_pages}.")+ domains = set()+ for i in page_indexes:+ docs = tiny_index.get_page(i)+ urls |= {doc.url for doc in docs}+ domains |= {urlparse(doc.url).netloc for doc in docs}+ num_results += len(docs)+ redis.pfadd(url_hll_key, *urls)+ redis.pfadd(domain_hll_key, *domains)+ logger.info(f"Counted {i} pages of {tiny_index.num_pages}.")++ redis.expire(url_hll_key, SHORT_EXPIRE_SECONDS)+ url_count = redis.pfcount(url_hll_key)+ logger.info("Counted %d unique URLs", url_count)++ redis.expire(domain_hll_key, SHORT_EXPIRE_SECONDS)+ domain_count = redis.pfcount(domain_hll_key)+ logger.info("Counted %d unique domains", domain_count)- count = redis.pfcount(key)- redis.expire(key, LONG_EXPIRE_SECONDS)- logger.info("Counted %d unique URLs", count)+ _set_count(INDEX_URL_COUNT_KEY, redis, today, url_count)+ _set_count(INDEX_DOMAIN_COUNT_KEY, redis, today, domain_count)+ _set_count(INDEX_RESULT_COUNT_KEY, redis, today, num_results)- return count+def _set_count(key, redis, today, count):+ redis.set(key.format(date=today), count)+ redis.expire(key.format(date=today), LONG_EXPIRE_SECONDS)-def get_url_count(date_: date) -> int:++def get_counts() -> dict[str, list[int]]:redis = get_redis()- key = INDEX_URL_COUNT_KEY.format(date_)- return redis.pfcount(key)++ today = date.today()++ urls_in_index_daily = []+ domains_in_index_daily = []+ results_in_index_daily = []+ for i in range(29, -1, -1):+ date_i = today - timedelta(days=i)++ key = INDEX_URL_COUNT_KEY+ urls_in_index_daily.append(_get_count(redis, key, date_i))+ domains_in_index_daily.append(_get_count(redis, INDEX_DOMAIN_COUNT_KEY, date_i))+ results_in_index_daily.append(_get_count(redis, INDEX_RESULT_COUNT_KEY, date_i))++ return {+ "urls_in_index_daily": urls_in_index_daily,+ "domains_in_index_daily": domains_in_index_daily,+ "results_in_index_daily": results_in_index_daily,+ }+++def _get_count(redis, key, date_i):+ c = int(redis.get(key.format(date=date_i)) or 0)+ return cif __name__ == "__main__":@@ -56,3 +104,5 @@ if __name__ == "__main__":logging.basicConfig(level=logging.INFO)count_urls()+ counts = get_counts()+ print("Counts", counts, sep="\n")