diff --git a/proto/backend.proto b/proto/backend.proto index f6a7e7030..7c23a4a74 100644 --- a/proto/backend.proto +++ b/proto/backend.proto @@ -545,9 +545,9 @@ message SchedTimingTodayOut { } message DeckTreeIn { - bool include_counts = 1; + // if non-zero, counts for the provided timestamp will be included + int64 now = 1; int64 top_deck_id = 2; - int32 today_delta = 3; } message DeckTreeNode { diff --git a/pylib/anki/decks.py b/pylib/anki/decks.py index 05cf0d8a5..9c8fa0d3e 100644 --- a/pylib/anki/decks.py +++ b/pylib/anki/decks.py @@ -140,9 +140,7 @@ class DeckManager: return from_json_bytes(self.col.backend.new_deck_legacy(filtered)) def deck_tree(self) -> pb.DeckTreeNode: - return self.col.backend.deck_tree( - include_counts=False, top_deck_id=0, today_delta=0 - ) + return self.col.backend.deck_tree(top_deck_id=0, now=0) @classmethod def find_deck_in_tree( diff --git a/pylib/anki/schedv2.py b/pylib/anki/schedv2.py index 8e0d42b10..8fc8f720e 100644 --- a/pylib/anki/schedv2.py +++ b/pylib/anki/schedv2.py @@ -241,9 +241,7 @@ order by due""" def deck_due_tree(self, top_deck_id: int = 0) -> DeckTreeNode: """Returns a tree of decks with counts. If top_deck_id provided, counts are limited to that node.""" - return self.col.backend.deck_tree( - include_counts=True, top_deck_id=top_deck_id, today_delta=0 - ) + return self.col.backend.deck_tree(top_deck_id=top_deck_id, now=intTime()) # Getting the next card ########################################################################## diff --git a/rslib/src/backend/mod.rs b/rslib/src/backend/mod.rs index 2a8679217..540e2184d 100644 --- a/rslib/src/backend/mod.rs +++ b/rslib/src/backend/mod.rs @@ -526,12 +526,12 @@ impl BackendService for Backend { None }; self.with_col(|col| { - let today = if input.include_counts { - Some(col.current_due_day(input.today_delta)?) - } else { + let now = if input.now == 0 { None + } else { + Some(TimestampSecs(input.now)) }; - col.deck_tree(today, lim) + col.deck_tree(now, lim) }) } diff --git a/rslib/src/decks/counts.rs b/rslib/src/decks/counts.rs index 824ed6806..3eb25f4fd 100644 --- a/rslib/src/decks/counts.rs +++ b/rslib/src/decks/counts.rs @@ -12,12 +12,13 @@ pub(crate) struct DueCounts { } impl Collection { + /// Get due counts for decks at the given timestamp. pub(crate) fn due_counts( &mut self, + days_elapsed: u32, + learn_cutoff: u32, limit_to: Option<&str>, ) -> Result> { - let days_elapsed = self.timing_today()?.days_elapsed; - let learn_cutoff = self.learn_cutoff(); if let Some(limit) = limit_to { self.storage .due_counts_limited(self.sched_ver(), days_elapsed, learn_cutoff, limit) diff --git a/rslib/src/decks/tree.rs b/rslib/src/decks/tree.rs index f98fb6cf6..d42015180 100644 --- a/rslib/src/decks/tree.rs +++ b/rslib/src/decks/tree.rs @@ -8,6 +8,7 @@ use crate::{ deckconf::{DeckConf, DeckConfID}, decks::DeckID, err::Result, + timestamp::TimestampSecs, }; use serde_tuple::Serialize_tuple; use std::{ @@ -208,13 +209,13 @@ impl From for LegacyDueCounts { impl Collection { /// Get the deck tree. - /// If today is provided, due counts for the provided day will be populated. + /// If now is provided, due counts for the provided timestamp will be populated. /// If top_deck_id is provided, only the node starting at the provided deck ID will /// have the counts populated. Currently the entire tree is returned in this case, but /// this may change in the future. pub fn deck_tree( &mut self, - today: Option, + now: Option, top_deck_id: Option, ) -> Result { let names = self.storage.get_all_deck_names()?; @@ -227,12 +228,12 @@ impl Collection { .map(|d| (d.id, d)) .collect(); - add_collapsed_and_filtered(&mut tree, &decks_map, today.is_none()); + add_collapsed_and_filtered(&mut tree, &decks_map, now.is_none()); if self.default_deck_is_empty()? { hide_default_deck(&mut tree); } - if let Some(today) = today { + if let Some(now) = now { let limit = top_deck_id.and_then(|did| { if let Some(deck) = decks_map.get(&did) { Some(deck.name.as_str()) @@ -240,7 +241,9 @@ impl Collection { None } }); - let counts = self.due_counts(limit)?; + let days_elapsed = self.timing_for_timestamp(now)?.days_elapsed; + let learn_cutoff = (now.0 as u32) + self.learn_ahead_secs(); + let counts = self.due_counts(days_elapsed, learn_cutoff, limit)?; let dconf: HashMap<_, _> = self .storage .all_deck_config()? @@ -250,7 +253,7 @@ impl Collection { add_counts(&mut tree, &counts); apply_limits( &mut tree, - today, + days_elapsed, &decks_map, &dconf, (std::u32::MAX, std::u32::MAX), @@ -262,14 +265,12 @@ impl Collection { pub fn current_deck_tree(&mut self) -> Result> { let target = self.get_current_deck_id(); - let today = self.current_due_day(0)?; - let tree = self.deck_tree(Some(today), Some(target))?; + let tree = self.deck_tree(Some(TimestampSecs::now()), Some(target))?; Ok(get_subnode(tree, target)) } pub(crate) fn legacy_deck_tree(&mut self) -> Result { - let today = self.current_due_day(0)?; - let tree = self.deck_tree(Some(today), None)?; + let tree = self.deck_tree(Some(TimestampSecs::now()), None)?; Ok(LegacyDueCounts::from(tree)) } @@ -293,6 +294,7 @@ impl Collection { #[cfg(test)] mod test { + use super::*; use crate::{collection::open_test_collection, deckconf::DeckConfID, err::Result}; #[test] @@ -349,7 +351,7 @@ mod test { note.fields[0] = "{{c1::}} {{c2::}} {{c3::}} {{c4::}}".into(); col.add_note(&mut note, child_deck.id)?; - let tree = col.deck_tree(Some(0), None)?; + let tree = col.deck_tree(Some(TimestampSecs::now()), None)?; assert_eq!(tree.children[0].new_count, 4); assert_eq!(tree.children[0].children[0].new_count, 4); @@ -360,7 +362,7 @@ mod test { col.add_or_update_deck(&mut parent_deck)?; // with the default limit of 20, there should still be 4 due - let tree = col.deck_tree(Some(0), None)?; + let tree = col.deck_tree(Some(TimestampSecs::now()), None)?; assert_eq!(tree.children[0].new_count, 4); assert_eq!(tree.children[0].children[0].new_count, 4); @@ -369,7 +371,7 @@ mod test { conf.inner.new_per_day = 4; col.add_or_update_deck_config(&mut conf, false)?; - let tree = col.deck_tree(Some(0), None)?; + let tree = col.deck_tree(Some(TimestampSecs::now()), None)?; assert_eq!(tree.children[0].new_count, 3); assert_eq!(tree.children[0].children[0].new_count, 3); diff --git a/rslib/src/sched/mod.rs b/rslib/src/sched/mod.rs index bf9c942c4..c176ff2d7 100644 --- a/rslib/src/sched/mod.rs +++ b/rslib/src/sched/mod.rs @@ -78,8 +78,4 @@ impl Collection { SchedulerVersion::V2 => self.set_v2_rollover(hour as u32), } } - - pub(crate) fn learn_cutoff(&self) -> u32 { - TimestampSecs::now().0 as u32 + self.learn_ahead_secs() - } }