Skip to content

Commit 0db7d49

Browse files
committed
fix: move ID-reset detection before cache-size branch
Codex review: reset check was only in the delta path (>=50 rows), missing small caches where INSERT OR IGNORE would silently mix data. Now runs for any non-empty cache before deciding backfill vs delta.
1 parent 62d73e9 commit 0db7d49

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

app/modules/speedtest/collector.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,21 @@ def collect(self) -> CollectorResult:
5252
try:
5353
last_id = self._storage.get_latest_speedtest_id()
5454
cached_count = self._storage.get_speedtest_count()
55+
# ID-reset detection: compare remote max ID with cache max ID
56+
if cached_count > 0 and last_id > 0:
57+
remote_latest = self._client.get_latest(1)
58+
if remote_latest and remote_latest[0].get("id", 0) < last_id:
59+
log.info(
60+
"Speedtest ID reset detected (cache=%d, remote=%d), rebuilding",
61+
last_id, remote_latest[0]["id"],
62+
)
63+
self._storage.clear_cache()
64+
cached_count = 0
5565
is_backfill = cached_count < 50
5666
if is_backfill:
5767
new_results = self._client.get_results(per_page=2000)
5868
else:
5969
new_results = self._client.get_newer_than(last_id)
60-
# ID-reset detection: remote max ID < cache max ID = server replaced
61-
if not new_results and last_id > 0:
62-
remote_latest = self._client.get_latest(1)
63-
if remote_latest and remote_latest[0].get("id", 0) < last_id:
64-
log.info(
65-
"Speedtest ID reset detected (cache=%d, remote=%d), rebuilding",
66-
last_id, remote_latest[0]["id"],
67-
)
68-
self._storage.clear_cache()
69-
new_results = self._client.get_results(per_page=2000)
70-
is_backfill = True
7170
if new_results:
7271
genuinely_new = [r for r in new_results if r.get("id", 0) > last_id]
7372
self._storage.save_speedtest_results(new_results)

app/modules/speedtest/routes.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,21 @@ def api_speedtest():
118118
# Detect server switch and clear stale cache
119119
ss.check_source_url(stt_url)
120120
cached_count = ss.get_speedtest_count()
121+
last_id = ss.get_latest_speedtest_id()
122+
# ID-reset detection: compare remote max ID with cache max ID
123+
if cached_count > 0 and last_id > 0:
124+
remote_latest = client.get_latest(1)
125+
if remote_latest and remote_latest[0].get("id", 0) < last_id:
126+
log.info(
127+
"Speedtest ID reset detected (cache max=%d, remote max=%d), rebuilding",
128+
last_id, remote_latest[0]["id"],
129+
)
130+
ss.clear_cache()
131+
cached_count = 0
121132
if cached_count < 50:
122133
new_results = client.get_results(per_page=2000)
123134
else:
124-
last_id = ss.get_latest_speedtest_id()
125135
new_results = client.get_newer_than(last_id)
126-
# ID-reset detection: if delta returned nothing, check if
127-
# the remote's latest ID is lower than our cache's max ID.
128-
# This means the server was replaced (IDs reset).
129-
if not new_results and last_id > 0:
130-
remote_latest = client.get_latest(1)
131-
if remote_latest and remote_latest[0].get("id", 0) < last_id:
132-
log.info(
133-
"Speedtest ID reset detected (cache max=%d, remote max=%d), rebuilding",
134-
last_id, remote_latest[0]["id"],
135-
)
136-
ss.clear_cache()
137-
new_results = client.get_results(per_page=2000)
138136
if new_results:
139137
ss.save_speedtest_results(new_results)
140138
log.info("Cached %d new speedtest results", len(new_results))

0 commit comments

Comments
 (0)