Create fsrs upadte method
This commit is contained in:
1 parent
663fa377b1
commit
e7019903b0
5 files changed
+73
-10
No files matched your search
@@ -1,7 +1,10 @@
|
||||
use crate::data::entities::{CardStatistics, DeckSettings};
|
||||
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
|
||||
use crate::notification::{MaybeError, NotificationData};
|
||||
use chrono::NaiveDate;
|
||||
use fsrs::MemoryState;
|
||||
use log::info;
|
||||
use reqwest::get;
|
||||
use rusqlite::{Connection, params};
|
||||
use std::time::Instant;
|
||||
|
||||
@@ -10,7 +13,7 @@ pub fn load_stats_of_deck(
|
||||
connection: &Connection,
|
||||
) -> Result<Vec<CardStatistics>, NotificationData> {
|
||||
let mut stmt = connection
|
||||
.prepare("SELECT id, word_id, score, last_opened FROM card_stats WHERE set_id = ?1")
|
||||
.prepare("SELECT id, word_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day FROM card_stats WHERE set_id = ?1")
|
||||
.expect("Failed to prepare state query");
|
||||
let iter = stmt
|
||||
.query_map((set.id,), |row| {
|
||||
@@ -19,7 +22,12 @@ pub fn load_stats_of_deck(
|
||||
word_id: row.get(1)?,
|
||||
set_id: set.id,
|
||||
score: row.get(2)?,
|
||||
fsrs_opened: NaiveDate::from_epoch_days(row.get::<usize, i32>(4)?)
|
||||
.expect("Incorrect date encoding"),
|
||||
fsrs_memory: Some(get_memory(row.get(5)?)),
|
||||
last_open: row.get(3)?,
|
||||
fsrs_open_day: NaiveDate::from_epoch_days(row.get::<usize, i32>(6)?)
|
||||
.expect("Incorrect date encoding"),
|
||||
})
|
||||
})
|
||||
.map_err(|_| DbLoadError)?;
|
||||
@@ -30,6 +38,17 @@ pub fn load_stats_of_deck(
|
||||
Ok(buffer)
|
||||
}
|
||||
|
||||
fn get_memory(value: i64) -> MemoryState {
|
||||
MemoryState {
|
||||
stability: f32::from_bits(value as u32),
|
||||
difficulty: f32::from_bits((value >> 32) as u32),
|
||||
}
|
||||
}
|
||||
|
||||
fn memory_to_i64(value: &MemoryState) -> i64 {
|
||||
value.stability.to_bits() as i64 | ((value.difficulty.to_bits() as i64) << 32)
|
||||
}
|
||||
|
||||
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError {
|
||||
let time = Instant::now();
|
||||
let tx = connection
|
||||
@@ -40,7 +59,7 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
|
||||
{
|
||||
let mut stmt = tx
|
||||
.prepare(
|
||||
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4)",
|
||||
"INSERT INTO card_stats (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)",
|
||||
)
|
||||
.expect("Failed to prepare state query");
|
||||
|
||||
@@ -49,7 +68,13 @@ pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection)
|
||||
stat.word_id,
|
||||
stat.set_id,
|
||||
stat.score,
|
||||
stat.last_open.timestamp()
|
||||
stat.last_open.timestamp(),
|
||||
stat.fsrs_opened.to_epoch_days(),
|
||||
memory_to_i64(&stat.fsrs_memory.unwrap_or(MemoryState {
|
||||
difficulty: 0.0,
|
||||
stability: 0.0
|
||||
})),
|
||||
stat.fsrs_open_day.to_epoch_days()
|
||||
])
|
||||
.map_err(|_| DbSaveError)?;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user