Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(metrics): show real totals for the "All" time range (#6834)

Lukas54db0d356e61
diff --git a/changelog.d/6834.bugfix.md b/changelog.d/6834.bugfix.md
new file mode 100644
index 0000000000..5ff71a6f8e
--- /dev/null
+++ b/changelog.d/6834.bugfix.md
@@ -0,0 +1 @@
+The metrics page's "All" time range shows real totals again; it showed zero usage because the rate-limit section failed for that period.
diff --git a/src/local_deep_research/metrics/token_counter.py b/src/local_deep_research/metrics/token_counter.py
index 6aa9f0031e..9b8086fde4 100644
--- a/src/local_deep_research/metrics/token_counter.py
+++ b/src/local_deep_research/metrics/token_counter.py
@@ -1358,7 +1358,10 @@ class TokenCounter:
# Get rate limit attempts
rate_limit_query = session.query(RateLimitAttempt)
- # Apply time filter
+ # Apply time filter. The cutoff is read again below for the
+ # engine queries, so it needs a value when there is no time
+ # condition: 0, no cutoff, which is what period "all" means.
+ cutoff_time = 0
if time_condition is not None:
# RateLimitAttempt uses timestamp as float, not datetime
if period == "7d":
diff --git a/tests/metrics/test_token_counting_attribution.py b/tests/metrics/test_token_counting_attribution.py
index a6e6cea714..b8e14d9031 100644
--- a/tests/metrics/test_token_counting_attribution.py
+++ b/tests/metrics/test_token_counting_attribution.py
@@ -37,6 +37,7 @@ is fixed, which is the signal to delete the marker.
import asyncio
import threading
+import time
from contextlib import contextmanager
import pytest
@@ -563,20 +564,6 @@ def test_search_metrics_aggregate_per_engine_within_one_user(user_dbs):
}
- strict=True,
- reason=(
- "period='all' RETURNS ZEROS. In "
- "token_counter._get_metrics_from_encrypted_db, `cutoff_time` is only "
- "assigned inside `if time_condition is not None:` (~line 1320), but "
- "is read unconditionally at ~lines 1382 and 1395 for the "
- "tracked_engines / engine_types queries. get_time_filter_condition "
- "returns None for period='all', so the read raises UnboundLocalError, "
- "the function-wide `except Exception` swallows it, and the caller "
- "gets _get_empty_metrics(). Every other period works, so the "
- "dashboard's 'All time' view reports zero usage."
- ),
-)
def test_overall_metrics_period_all_returns_real_totals(user_dbs):
seed_token_rows(
user_dbs,
@@ -601,6 +588,43 @@ def test_overall_metrics_period_all_returns_real_totals(user_dbs):
assert all_time["by_model"] != []
+def test_period_all_counts_rate_limit_attempts_of_every_age(user_dbs):
+ """'all' has no cutoff: an attempt older than a year still counts."""
+ now = time.time()
+ session = user_dbs.session("alice")
+ session.add_all(
+ [
+ RateLimitAttempt(
+ engine_type="searxng",
+ timestamp=now - 60,
+ wait_time=1.0,
+ retry_count=0,
+ success=True,
+ ),
+ RateLimitAttempt(
+ engine_type="arxiv",
+ timestamp=now - 400 * 24 * 3600,
+ wait_time=3.0,
+ retry_count=1,
+ success=False,
+ error_type="RateLimitError",
+ ),
+ ]
+ )
+ session.commit()
+ session.close()
+ counter = TokenCounter()
+
+ last_year = counter.get_overall_metrics(period="1y", username="alice")
+ assert last_year["rate_limiting"]["total_attempts"] == 1
+ assert last_year["rate_limiting"]["tracked_engines"] == 1
+
+ all_time = counter.get_overall_metrics(period="all", username="alice")
+ assert all_time["rate_limiting"]["total_attempts"] == 2
+ assert all_time["rate_limiting"]["rate_limit_events"] == 1
+ assert all_time["rate_limiting"]["tracked_engines"] == 2
+
+
@pytest.mark.xfail(
strict=True,
reason=(