Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

API: new method to fetch user stats for user stats page.

blueheron786d5912a931bcc
diff --git a/mwmbl/crawler/stats.py b/mwmbl/crawler/stats.py
index d7cddb4..2f86b41 100644
--- a/mwmbl/crawler/stats.py
+++ b/mwmbl/crawler/stats.py
@@ -234,6 +234,27 @@ class StatsManager:
**index_stats,
)
+ def get_user_stats(self, username: str) -> dict:
+ """Per-user stats for the last 30 days.
+
+ Reads the per-user results-indexed sorted set for each day. Crawled and
+ blacklisted counts are only tracked per-user on the legacy hash path, not
+ per-username, so this exposes the user's indexed results only.
+ """
+ date = utc_today()
+ results_indexed_daily = {}
+ for i in range(29, -1, -1):
+ date_i = date - timedelta(days=i)
+ user_result_count_key = USER_RESULTS_COUNT_KEY.format(date=date_i)
+ count = self.redis.zscore(user_result_count_key, username)
+ results_indexed_daily[str(date_i)] = int(count) if count else 0
+
+ return {
+ "username": username,
+ "results_indexed_daily": results_indexed_daily,
+ "results_indexed_today": results_indexed_daily[str(date)],
+ }
+
def get_domain_stats(self) -> list[DomainStats]:
today = utc_today()
host_all_key = HOST_COUNT_ALL_KEY.format(date=today)
diff --git a/mwmbl/platform/api.py b/mwmbl/platform/api.py
index e252d38..3b1c6ca 100644
--- a/mwmbl/platform/api.py
+++ b/mwmbl/platform/api.py
@@ -68,6 +68,7 @@ from mwmbl.platform.schemas import (
UpdateDomainSubmission,
UpdateSpendLimitRequest,
UserProfileResponse,
+ UserStatsResponse,
UserVoteHistory,
VoteRemoveRequest,
VoteRequest,
@@ -1132,6 +1133,22 @@ def get_current_user(request):
)
+ "/user/stats",
+ auth=JWTAuth(),
+ response=UserStatsResponse,
+ 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."
+ ),
+ tags=["Users"],
+)
+def get_current_user_stats(request):
+ user = request.user
+ return stats_manager.get_user_stats(user.username)
+
+
# ---------------------------------------------------------------------------
# Billing
# ---------------------------------------------------------------------------
diff --git a/mwmbl/platform/schemas.py b/mwmbl/platform/schemas.py
index ac28fd1..a5ce602 100644
--- a/mwmbl/platform/schemas.py
+++ b/mwmbl/platform/schemas.py
@@ -14,6 +14,14 @@ class UserProfileResponse(Schema):
email_confirmed: bool
+class UserStatsResponse(Schema):
+ """Per-user contribution stats for the logged-in user."""
+
+ username: str
+ results_indexed_today: int
+ results_indexed_daily: dict[str, int]
+
+
class SubscriptionResponse(Schema):
status: str
max_monthly_spend_cents: int