Skip to content

Commit

Permalink
chore: Add session to have the creation stamp
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh0412 committed Mar 17, 2024
1 parent 874b95f commit 446e569
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 16 deletions.
7 changes: 5 additions & 2 deletions src/components/deck/deck_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ impl DeckListView {

fn get_all_decks_and_stats(&self, collection: &Collection) -> Vec<Deck> {
let decks = get_decks(&collection.storage.conn);
let timing_at_stamp =
CollectionBuilder::timing_for_timestamp(chrono::Local::now().timestamp());
let timing_at_stamp = CollectionBuilder::timing_for_timestamp(
&collection.storage.conn,
chrono::Local::now().timestamp(),
);

let decks_stats =
Deck::get_decks_stats(&collection.storage.conn, timing_at_stamp.days_elapsed).unwrap();

Expand Down
8 changes: 8 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ pub fn init_db(conn: &Connection) -> Result<()> {
[],
)?;

conn.execute(
"CREATE TABLE IF NOT EXISTS sessions (
id INTEGER PRIMARY KEY,
creation_stamp INTEGER NOT NULL
)",
[],
)?;

Ok(())
}
17 changes: 11 additions & 6 deletions src/models/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use std::path::PathBuf;

use chrono::Local;
use gpui::{AppContext, Global};
use rusqlite::Connection;

use crate::{
errors::Result, repositories::flash_card::CardQueue, storage::sqlite::SqliteStorage, FlashCard,
errors::Result,
repositories::{flash_card::CardQueue, session::Session},
storage::sqlite::SqliteStorage,
FlashCard,
};

use super::{
Expand All @@ -23,9 +27,10 @@ impl CollectionBuilder {
}
}

pub(crate) fn timing_for_timestamp(now: i64) -> SchedTimingToday {
pub(crate) fn timing_for_timestamp(conn: &Connection, now: i64) -> SchedTimingToday {
// Get current utc offset from the system
let days_elapsed = now / 86_400;
let creation_stamp = Session::get_creation_stamp(conn).unwrap();
let days_elapsed = (now - creation_stamp) / 86_400;
let next_day_at = (days_elapsed + 1) * 86_400;

SchedTimingToday {
Expand All @@ -46,7 +51,7 @@ impl Builder for CollectionBuilder {
.unwrap_or_else(|| PathBuf::from(":memory:"));

let storage = SqliteStorage::open_or_create(&col_path)?;
let timing = Self::timing_for_timestamp(Local::now().timestamp());
let timing = Self::timing_for_timestamp(&storage.conn, Local::now().timestamp());

let col = Collection {
storage,
Expand Down Expand Up @@ -74,7 +79,7 @@ impl Collection {
pub fn apply_state(&self, card: &mut FlashCard, next: CardState) {
match next {
CardState::New(next_new_state) => {
card.due = next_new_state.position as i32;
card.due = next_new_state.position as u32;
card.set_queue(CardQueue::New);
}
CardState::Learning(next_learning_state) => {
Expand All @@ -84,7 +89,7 @@ impl Collection {
CardState::Review(next_review_state) => {
card.set_queue(CardQueue::Review);
card.interval = next_review_state.scheduled_days;
card.due = (self.timing.days_elapsed + next_review_state.scheduled_days) as i32;
card.due = (self.timing.days_elapsed + next_review_state.scheduled_days) as u32;
card.memory_state = next_review_state.memory_state;
}
_ => {}
Expand Down
4 changes: 1 addition & 3 deletions src/repositories/deck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,6 @@ impl Deck {
":day_cutoff" : day_elapsed,
};

println!("Day elapsed: {}", day_elapsed);

conn.prepare(include_str!("query_decks_stats.sql"))?
.query_and_then(params, row_to_deck_stat)?
.collect()
Expand Down Expand Up @@ -366,7 +364,7 @@ mod test {
let mut new_card = FlashCard::new(1, "Front", "Back", None);
new_card.save(&conn).unwrap();

let stats = Deck::get_decks_stats(&conn).unwrap();
let stats = Deck::get_decks_stats(&conn, 1).unwrap();

assert_eq!(stats.len(), 1);
let deck_stat = stats.get(&deck.id.unwrap()).unwrap();
Expand Down
18 changes: 13 additions & 5 deletions src/repositories/flash_card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use rusqlite::{

use time::OffsetDateTime;

use super::card_data::CardData;
use crate::models::collection::CollectionBuilder;

use super::{card_data::CardData, session::Session};

#[derive(PartialEq, Debug, Clone)]
pub enum Status {
Expand Down Expand Up @@ -51,7 +53,7 @@ pub struct FlashCard {
last_studied_time: Option<SystemTime>,
ef: f32,
pub interval: u32,
pub due: i32,
pub due: u32,
queue: CardQueue,
pub data: CardData,
pub memory_state: Option<MemoryState>,
Expand Down Expand Up @@ -115,8 +117,14 @@ impl FlashCard {
F: FnMut(&FlashCard) -> (),
{
let mut stmt = conn.prepare(include_str!("query_cards_in_deck_by_queue.sql"))?;
let timing_at_stamp =
CollectionBuilder::timing_for_timestamp(conn, chrono::Local::now().timestamp());

let mut rows = stmt.query(params![deck_id, queue as i8])?;
let mut rows = stmt.query(params![
deck_id,
queue as i8,
timing_at_stamp.days_elapsed as u32
])?;
while let row = rows.next()? {
if let None = row {
break;
Expand Down Expand Up @@ -228,8 +236,8 @@ impl FlashCard {
last_studied_time,
ef: row.get(6)?,
interval: row.get(7)?,
due: row.get(8).ok().unwrap_or_default(),
queue: row.get(9)?,
queue: row.get(8)?,
due: row.get(9)?,
memory_state: data.memory_state(),
data,
})
Expand Down
1 change: 1 addition & 0 deletions src/repositories/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod card_data;
pub mod deck;
pub mod flash_card;
pub mod session;
1 change: 1 addition & 0 deletions src/repositories/query_cards_in_deck_by_queue.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ FROM
WHERE
deck_id = ?
AND queue = ?
AND due <= ?
11 changes: 11 additions & 0 deletions src/repositories/session.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use rusqlite::{Connection, Result};

pub struct Session;

impl Session {
pub fn get_creation_stamp(conn: &Connection) -> Result<i64> {
conn.prepare_cached("select creation_stamp from sessions")?
.query_row([], |row| row.get(0))
.map_err(Into::into)
}
}

0 comments on commit 446e569

Please sign in to comment.