aboutsummaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
authorJoris2022-02-12 16:57:19 +0100
committerJoris2022-02-12 16:57:19 +0100
commit3adf3f9697c4e2beb10e652947046d5fddda2ed4 (patch)
tree689e704cf4665cc4cd1949c06d4a7b0553d07501 /src/db
parenta2ba205df12332a360d89991fe508f964d88c73f (diff)
Say when the next card will be available
Diffstat (limited to 'src/db')
-rw-r--r--src/db/db.rs24
1 files changed, 9 insertions, 15 deletions
diff --git a/src/db/db.rs b/src/db/db.rs
index 9ed88ce..4c2d9a2 100644
--- a/src/db/db.rs
+++ b/src/db/db.rs
@@ -7,7 +7,8 @@ use anyhow::Result;
use rand::{rngs::ThreadRng, Rng};
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};
-use std::time::SystemTime;
+
+use crate::util::time;
pub fn init(database: String) -> Result<Connection> {
let mut conn = Connection::open(database)?;
@@ -25,7 +26,7 @@ pub fn init(database: String) -> Result<Connection> {
/// - keep existing cards,
/// - hide unused cards (keep state in case the card is added back afterward).
pub fn synchronize(conn: &Connection, entries: Vec<Entry>) -> Result<()> {
- let now = get_current_time()?;
+ let now = time::now()?;
let state = serde_json::to_string(&space_repetition::init())?;
let mut rng = rand::thread_rng();
@@ -81,22 +82,20 @@ fn delete_read_before(conn: &Connection, t: u64) -> Result<()> {
Ok(())
}
-pub fn pick_ready(conn: &Connection) -> Option<Card> {
- let now = get_current_time().ok()?;
-
+pub fn next_ready(conn: &Connection) -> Option<Card> {
let mut stmt = conn
.prepare(
"
- SELECT question, responses, state
+ SELECT question, responses, state, ready
FROM cards
- WHERE ready <= ? AND deleted IS NULL
+ WHERE deleted IS NULL
ORDER BY ready
LIMIT 1
",
)
.ok()?;
- let mut rows = stmt.query([now]).ok()?;
+ let mut rows = stmt.query([]).ok()?;
let row = rows.next().ok()??;
let state_str: String = row.get(2).ok()?;
let responses_str: String = row.get(1).ok()?;
@@ -105,11 +104,12 @@ pub fn pick_ready(conn: &Connection) -> Option<Card> {
question: row.get(0).ok()?,
responses: serialization::line_to_words(&responses_str),
state: serde_json::from_str(&state_str).ok()?,
+ ready: row.get(3).ok()?,
})
}
pub fn update(conn: &Connection, question: &String, state: &space_repetition::State) -> Result<()> {
- let now = get_current_time()?;
+ let now = time::now()?;
let ready = now + state.get_interval_seconds();
let state_str = serde_json::to_string(state)?;
@@ -124,9 +124,3 @@ pub fn update(conn: &Connection, question: &String, state: &space_repetition::St
Ok(())
}
-
-fn get_current_time() -> Result<u64> {
- Ok(SystemTime::now()
- .duration_since(SystemTime::UNIX_EPOCH)?
- .as_secs())
-}