Read the portraits folders once and stop the card pass offline (#77)

The statistics list the portraits folder and its cards subfolder once
instead of checking about 340 files. The tall-portrait pass is skipped
when the catalog refresh failed and stops at the first network error;
the core tells after each attempt whether a portrait was saved, so the
worker's throttle can tell the page after any attempt, not only saves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nessarandClaude Opus 5.5 committed 2026-11-21 02:26:14 +01:00
1 parent 485d7b9065
commit 18b06f8ea6
5 files changed
+183 -53

No files matched your search

+56 -31
View File
@@ -81,9 +81,9 @@ impl RefreshGate {
/// statistics to show them.
const PORTRAITS_TOLD_EVERY: Duration = Duration::from_secs(1);
/// Spaces out what saved portraits cause: the first save is told at once, the next ones at most
/// once per `every`, and one left untold when the pass ends is told then. No timer: a save or
/// the end of the pass is what tells.
/// Spaces out what saved portraits cause: the first save is told at once, the next ones when
/// `every` has passed since the last message, and one still untold when the pass ends is told
/// then. No timer: each attempt of the pass, saved or not, and its end are what tell.
struct Throttle {
every: Duration,
last_told: Option<Instant>,
@@ -99,18 +99,19 @@ impl Throttle {
}
}
/// A portrait was saved at `now`: true when it must be told now.
fn saved(&mut self, now: Instant) -> bool {
if self
/// An attempt ended at `now`, with a portrait saved or not: true when the page must be told
/// now, as a save is untold and `every` has passed since the last message.
fn attempted(&mut self, saved: bool, now: Instant) -> bool {
self.untold |= saved;
let due = self
.last_told
.is_some_and(|last| now.duration_since(last) < self.every)
{
self.untold = true;
return false;
.is_none_or(|last| now.duration_since(last) >= self.every);
if self.untold && due {
self.last_told = Some(now);
self.untold = false;
return true;
}
self.last_told = Some(now);
self.untold = false;
true
false
}
/// The pass ended: true when a save is still untold.
@@ -170,13 +171,17 @@ fn spawn_guarded<T: Send + 'static>(
pub fn spawn_follower(tracker: Arc<Tracker>, fallback_riot_id: String, tx: Sender<Msg>) {
let champions = tracker.clone();
spawn_guarded(tx.clone(), Msg::Crashed, move |tx| {
let _ = champions.refresh_champions();
let refreshed = champions.refresh_champions();
let _ = tx.try_send(Msg::Champions);
// Offline, the catalog call failed and so would every portrait request.
if refreshed.is_err() {
return;
}
// The tall card portraits after the square ones, which the Statistiques table uses. They
// show as they arrive, without a restart, the page reloading at most once a second.
let mut throttle = Throttle::new(PORTRAITS_TOLD_EVERY);
let _ = champions.refresh_card_portraits(&mut || {
if throttle.saved(Instant::now()) {
let _ = champions.refresh_card_portraits(&mut |saved| {
if throttle.attempted(saved, Instant::now()) {
let _ = tx.try_send(Msg::Champions);
}
});
@@ -282,12 +287,20 @@ pub fn spawn_refresh(tracker: Arc<Tracker>, tx: Sender<Msg>) {
});
}
#[cfg(test)]
thread_local! {
/// The scopes computed on this thread, to check what a change makes the page compute.
pub static COMPUTED: std::cell::RefCell<Vec<Scope>> = const { std::cell::RefCell::new(Vec::new()) };
}
/// The statistics of `puuid` for `scope`, with the error as the page shows it.
pub fn compute_statistics(
tracker: &Tracker,
puuid: &str,
scope: &Scope,
) -> Result<Statistics, String> {
#[cfg(test)]
COMPUTED.with(|computed| computed.borrow_mut().push(scope.clone()));
tracker
.statistics(puuid, scope)
.map_err(|e| format!("Statistiques indisponibles : {e}"))
@@ -431,27 +444,39 @@ mod tests {
}
#[test]
fn saved_portraits_are_told_at_most_once_a_second_and_the_last_ones_at_the_end() {
fn saved_portraits_are_told_once_a_second_after_any_attempt_and_the_last_ones_at_the_end() {
let start = Instant::now();
let at = |ms: u64| start + Duration::from_millis(ms);
let mut throttle = Throttle::new(Duration::from_secs(1));
assert!(throttle.saved(at(0)), "the first save is told at once");
assert!(!throttle.saved(at(300)));
assert!(!throttle.saved(at(999)));
assert!(throttle.saved(at(1000)));
assert!(!throttle.saved(at(1500)));
// The save at 1.5 s is told when the pass ends.
assert!(throttle.finished());
// The last save was told: nothing more at the end.
let mut throttle = Throttle::new(Duration::from_secs(1));
assert!(throttle.saved(at(0)));
assert!(!throttle.saved(at(10)));
assert!(throttle.saved(at(2000)));
assert!(
throttle.attempted(true, at(0)),
"the first save is told at once"
);
assert!(!throttle.attempted(true, at(300)));
assert!(!throttle.attempted(true, at(999)));
assert!(throttle.attempted(true, at(1000)));
assert!(!throttle.attempted(true, at(1500)));
// A failed attempt tells nothing before the second is over, then the save at 1.5 s is.
assert!(!throttle.attempted(false, at(1900)));
assert!(throttle.attempted(false, at(2000)));
assert!(
!throttle.attempted(false, at(5000)),
"nothing is untold any more"
);
assert!(!throttle.finished());
// The last save was told late or never: the end of the pass tells it.
let mut throttle = Throttle::new(Duration::from_secs(1));
assert!(throttle.attempted(true, at(0)));
assert!(!throttle.attempted(true, at(10)));
assert!(!throttle.attempted(false, at(20)));
assert!(throttle.finished());
// Nothing saved, nothing told.
assert!(!Throttle::new(Duration::from_secs(1)).finished());
let mut throttle = Throttle::new(Duration::from_secs(1));
assert!(!throttle.attempted(false, at(0)));
assert!(!throttle.attempted(false, at(5000)));
assert!(!throttle.finished());
}
#[test]