Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Count URLs once a day

Daoud Clarke1cf07128ea07
diff --git a/mwmbl/count_urls.py b/mwmbl/count_urls.py
index 1493b4b..9afc1a5 100644
--- a/mwmbl/count_urls.py
+++ b/mwmbl/count_urls.py
@@ -1,7 +1,8 @@
import os
-from datetime import date, timedelta
+from datetime import date, timedelta, datetime
from logging import getLogger
from pathlib import Path
+from time import sleep
from urllib.parse import urlparse
from django.conf import settings
@@ -28,6 +29,17 @@ def get_redis():
return Redis.from_url(os.environ.get("REDIS_URL", "redis://127.0.0.1:6379"), decode_responses=True)
+def count_urls_continuously():
+ while True:
+ start_time = datetime.utcnow()
+ count_urls()
+ end_time = datetime.utcnow()
+ total_time = (end_time - start_time)
+ time_remaining = 60 * 60 * 24 - total_time.total_seconds()
+ logger.info(f"Counting took {total_time}. Sleeping for {timedelta(seconds=time_remaining)}.")
+ sleep(time_remaining)
+
+
def count_urls():
index_path = Path(settings.DATA_PATH) / settings.INDEX_NAME
redis = get_redis()
diff --git a/mwmbl/crawler/stats.py b/mwmbl/crawler/stats.py
index df20e76..1e2dbf0 100644
--- a/mwmbl/crawler/stats.py
+++ b/mwmbl/crawler/stats.py
@@ -8,6 +8,7 @@ from urllib.parse import urlparse
from pydantic import BaseModel
from redis import Redis
+from mwmbl.count_urls import get_counts
from mwmbl.crawler.batch import HashedBatch
from mwmbl.indexer.update_urls import get_datetime_from_timestamp
@@ -29,6 +30,9 @@ class MwmblStats(BaseModel):
users_crawled_daily: dict[str, int]
top_users: dict[str, int]
top_domains: dict[str, int]
+ urls_in_index_daily: dict[str, int]
+ domains_in_index_daily: dict[str, int]
+ results_in_index_daily: dict[str, int]
class StatsManager:
@@ -101,6 +105,7 @@ class StatsManager:
host_counts = self.redis.zrevrange(host_key, 0, 100, withscores=True)
urls_crawled_today = list(urls_crawled_daily.values())[-1]
+ index_stats = get_counts()
return MwmblStats(
urls_crawled_today=urls_crawled_today,
urls_crawled_daily=urls_crawled_daily,
@@ -108,6 +113,7 @@ class StatsManager:
users_crawled_daily=users_crawled_daily,
top_users=user_counts,
top_domains=host_counts,
+ **index_stats,
)
diff --git a/mwmbl/main.py b/mwmbl/main.py
index c7e3003..aefe80a 100644
--- a/mwmbl/main.py
+++ b/mwmbl/main.py
@@ -5,7 +5,7 @@ import uvicorn
from django.core.management import call_command
from redis import Redis
-from mwmbl.count_urls import count_urls
+from mwmbl.count_urls import count_urls_continuously
from mwmbl.indexer.update_urls import update_urls_continuously
from mwmbl.redis_url_queue import RedisURLQueue
@@ -31,7 +31,7 @@ def run():
elif mwmbl_app == "copy_indexes":
background.copy_indexes_continuously()
elif mwmbl_app == "count_urls":
- count_urls()
+ count_urls_continuously()
elif mwmbl_app == "server":
uvicorn.run("mwmbl.asgi:application", host="0.0.0.0", port=5000)
else: