From a1866e72fd979704435b15a0b6578e929f0dc25e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 30 Jul 2020 08:20:51 +1000 Subject: [PATCH] handle preview repeat in deck list --- rslib/src/decks/counts.rs | 9 +-- rslib/src/storage/deck/due_counts.sql | 44 +++++++------- rslib/src/storage/deck/mod.rs | 86 +++++++++++++-------------- 3 files changed, 65 insertions(+), 74 deletions(-) diff --git a/rslib/src/decks/counts.rs b/rslib/src/decks/counts.rs index 3eb25f4fd..066ccdd7f 100644 --- a/rslib/src/decks/counts.rs +++ b/rslib/src/decks/counts.rs @@ -19,12 +19,7 @@ impl Collection { learn_cutoff: u32, limit_to: Option<&str>, ) -> Result> { - if let Some(limit) = limit_to { - self.storage - .due_counts_limited(self.sched_ver(), days_elapsed, learn_cutoff, limit) - } else { - self.storage - .due_counts(self.sched_ver(), days_elapsed, learn_cutoff) - } + self.storage + .due_counts(self.sched_ver(), days_elapsed, learn_cutoff, limit_to) } } diff --git a/rslib/src/storage/deck/due_counts.sql b/rslib/src/storage/deck/due_counts.sql index 9f38a7b40..5d94aeee7 100644 --- a/rslib/src/storage/deck/due_counts.sql +++ b/rslib/src/storage/deck/due_counts.sql @@ -1,33 +1,36 @@ -select - did, - -- new - sum(queue = ?1), - -- reviews +select did, + sum(queue = :new_queue), sum( - queue = ?2 - and due <= ?3 + queue = :review_queue + and due <= :day_cutoff ), -- learning sum( ( case - -- v2 scheduler - ?4 + :sched_ver when 2 then ( - queue = ?5 - and due < ?6 - ) - or ( - queue = ?7 - and due <= ?3 + -- v2 scheduler + ( + queue = :learn_queue + and due < :learn_cutoff + ) + or ( + queue = :daylearn_queue + and due <= :day_cutoff + ) + or ( + queue = :preview_queue + and due <= :learn_cutoff + ) ) else ( -- v1 scheduler case - when queue = ?5 - and due < ?6 then left / 1000 - when queue = ?7 - and due <= ?3 then 1 + when queue = :learn_queue + and due < :learn_cutoff then left / 1000 + when queue = :daylearn_queue + and due <= :day_cutoff then 1 else 0 end ) @@ -35,5 +38,4 @@ select ) ) from cards -where - queue >= 0 \ No newline at end of file +where queue >= 0 \ No newline at end of file diff --git a/rslib/src/storage/deck/mod.rs b/rslib/src/storage/deck/mod.rs index 0793fe420..3408dc42a 100644 --- a/rslib/src/storage/deck/mod.rs +++ b/rslib/src/storage/deck/mod.rs @@ -13,7 +13,7 @@ use crate::{ timestamp::TimestampMillis, }; use prost::Message; -use rusqlite::{params, Row, NO_PARAMS}; +use rusqlite::{named_params, params, Row, NO_PARAMS}; use std::collections::{HashMap, HashSet}; use unicase::UniCase; @@ -173,54 +173,48 @@ impl SqliteStorage { sched: SchedulerVersion, day_cutoff: u32, learn_cutoff: u32, + top_deck: Option<&str>, ) -> Result> { - self.db - .prepare_cached(concat!(include_str!("due_counts.sql"), " group by did"))? - .query_and_then( - params![ - CardQueue::New as u8, - CardQueue::Review as u8, - day_cutoff, - sched as u8, - CardQueue::Learn as u8, - learn_cutoff, - CardQueue::DayLearn as u8, - ], - row_to_due_counts, - )? - .collect() - } + let sched_ver = sched as u8; + let mut params = named_params! { + ":new_queue": CardQueue::New as u8, + ":review_queue": CardQueue::Review as u8, + ":day_cutoff": day_cutoff, + ":sched_ver": sched_ver, + ":learn_queue": CardQueue::Learn as u8, + ":learn_cutoff": learn_cutoff, + ":daylearn_queue": CardQueue::DayLearn as u8, + ":preview_queue": CardQueue::PreviewRepeat as u8, + } + .to_vec(); - pub(crate) fn due_counts_limited( - &self, - sched: SchedulerVersion, - day_cutoff: u32, - learn_cutoff: u32, - top: &str, - ) -> Result> { - let prefix_start = format!("{}\x1f", top); - let prefix_end = format!("{}\x20", top); - self.db - .prepare_cached(concat!( + let sql; + let prefix_start; + let prefix_end; + let top; + if let Some(top_inner) = top_deck { + // limited to deck node + top = top_inner; + prefix_start = format!("{}\x1f", top); + prefix_end = format!("{}\x20", top); + params.extend(named_params! { + ":top_deck": top, + ":prefix_start": prefix_start, + ":prefix_end": prefix_end, + }); + sql = concat!( include_str!("due_counts.sql"), - " and did in (select id from decks where name = ? ", - "or (name >= ? and name < ?)) group by did " - ))? - .query_and_then( - params![ - CardQueue::New as u8, - CardQueue::Review as u8, - day_cutoff, - sched as u8, - CardQueue::Learn as u8, - learn_cutoff, - CardQueue::DayLearn as u8, - top, - prefix_start, - prefix_end, - ], - row_to_due_counts, - )? + " and did in (select id from decks where name = :top_deck ", + "or (name >= :prefix_start and name < :prefix_end)) group by did " + ); + } else { + // entire tree + sql = concat!(include_str!("due_counts.sql"), " group by did"); + } + + self.db + .prepare_cached(sql)? + .query_and_then_named(¶ms, row_to_due_counts)? .collect() }