Skip to content

Commit 903a18b

Browse files
committed
fix: address Codex review - reuse fetched result, handle empty remote
- Collector: reuse the latest result already fetched for dashboard instead of making a second API call (#5) - Routes: use get_latest_with_error to distinguish empty remote from request failure. Clear cache when remote is reachable but empty (#1)
1 parent 0db7d49 commit 903a18b

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

app/modules/speedtest/collector.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ 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:
55+
# ID-reset detection: reuse the result already fetched above
56+
if cached_count > 0 and last_id > 0 and results:
57+
remote_id = results[0].get("id", 0)
58+
if remote_id < last_id:
5959
log.info(
6060
"Speedtest ID reset detected (cache=%d, remote=%d), rebuilding",
61-
last_id, remote_latest[0]["id"],
61+
last_id, remote_id,
6262
)
6363
self._storage.clear_cache()
6464
cached_count = 0

app/modules/speedtest/routes.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,13 @@ def api_speedtest():
121121
last_id = ss.get_latest_speedtest_id()
122122
# ID-reset detection: compare remote max ID with cache max ID
123123
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:
124+
remote_latest, fetch_err = client.get_latest_with_error(1)
125+
if fetch_err is None and not remote_latest:
126+
# Remote is reachable but empty — server was wiped
127+
log.info("Remote has no results but cache has %d, clearing", cached_count)
128+
ss.clear_cache()
129+
cached_count = 0
130+
elif remote_latest and remote_latest[0].get("id", 0) < last_id:
126131
log.info(
127132
"Speedtest ID reset detected (cache max=%d, remote max=%d), rebuilding",
128133
last_id, remote_latest[0]["id"],

0 commit comments

Comments
 (0)