Files
jap_learn/src/data_provider/card_states.rs
T

128 lines
3.9 KiB
Rust
Raw Normal View History

2026-08-24 18:16:41 +03:00
use crate::data::entities::{CardStatistics, DeckSettings};
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
2026-08-25 12:04:07 +03:00
use crate::notification::{MaybeError, NotificationData};
2026-08-28 18:52:38 +03:00
use chrono::NaiveDate;
use fsrs::MemoryState;
2026-08-24 18:16:41 +03:00
use log::info;
2026-08-19 23:51:41 +03:00
use rusqlite::{Connection, params};
2026-07-29 13:30:49 +03:00
use std::time::Instant;
2026-04-13 22:25:58 +03:00
2026-08-24 18:16:41 +03:00
pub fn load_stats_of_deck(
set: &DeckSettings,
connection: &Connection,
) -> Result<Vec<CardStatistics>, NotificationData> {
2026-08-01 19:43:07 +03:00
let mut stmt = connection
2026-08-29 21:31:54 +03:00
.prepare("SELECT id, word_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day FROM states WHERE set_id = ?1")
2026-08-24 18:16:41 +03:00
.expect("Failed to prepare state query");
2026-08-01 19:43:07 +03:00
let iter = stmt
.query_map((set.id,), |row| {
Ok(CardStatistics {
id: row.get(0)?,
word_id: row.get(1)?,
set_id: set.id,
score: row.get(2)?,
2026-08-28 18:52:38 +03:00
fsrs_opened: NaiveDate::from_epoch_days(row.get::<usize, i32>(4)?)
.expect("Incorrect date encoding"),
2026-08-30 20:32:16 +03:00
fsrs_memory: get_memory(row.get(5)?),
2026-08-01 19:43:07 +03:00
last_open: row.get(3)?,
2026-08-28 18:52:38 +03:00
fsrs_open_day: NaiveDate::from_epoch_days(row.get::<usize, i32>(6)?)
.expect("Incorrect date encoding"),
2026-08-01 19:43:07 +03:00
})
2026-04-13 22:25:58 +03:00
})
2026-08-24 18:16:41 +03:00
.map_err(|_| DbLoadError)?;
2026-04-13 22:25:58 +03:00
2026-08-24 18:16:41 +03:00
let mut buffer = Vec::with_capacity(512);
2026-08-24 18:46:54 +03:00
iter.filter_map(|res| res.ok()).for_each(|i| buffer.push(i));
2026-04-13 22:25:58 +03:00
2026-08-24 18:16:41 +03:00
Ok(buffer)
2026-04-13 22:25:58 +03:00
}
2026-08-30 20:32:16 +03:00
fn get_memory(value: i64) -> Option<MemoryState> {
if value == 0 {
return None;
}
Some(MemoryState {
2026-08-28 18:52:38 +03:00
stability: f32::from_bits(value as u32),
difficulty: f32::from_bits((value >> 32) as u32),
2026-08-30 20:32:16 +03:00
})
2026-08-28 18:52:38 +03:00
}
2026-08-30 20:32:16 +03:00
fn memory_to_i64(value: &Option<MemoryState>) -> i64 {
let Some(value) = &value else {
return 0;
};
2026-08-28 18:52:38 +03:00
value.stability.to_bits() as i64 | ((value.difficulty.to_bits() as i64) << 32)
}
2026-08-24 23:42:58 +03:00
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError {
2026-08-16 14:23:00 +03:00
let time = Instant::now();
2026-08-25 12:04:07 +03:00
let tx = connection
.transaction()
.expect("Failed to create transaction");
2026-08-19 17:33:22 +03:00
let count = stats.len();
{
let mut stmt = tx
.prepare(
2026-08-29 21:31:54 +03:00
"INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)",
2026-08-19 17:33:22 +03:00
)
2026-08-24 18:16:41 +03:00
.expect("Failed to prepare state query");
2026-08-19 17:33:22 +03:00
for stat in stats.iter() {
stmt.execute(params![
2026-08-15 13:22:59 +03:00
stat.word_id,
stat.set_id,
stat.score,
2026-08-28 18:52:38 +03:00
stat.last_open.timestamp(),
stat.fsrs_opened.to_epoch_days(),
2026-08-30 20:32:16 +03:00
memory_to_i64(&stat.fsrs_memory),
2026-08-28 18:52:38 +03:00
stat.fsrs_open_day.to_epoch_days()
2026-08-19 17:33:22 +03:00
])
2026-08-24 18:16:41 +03:00
.map_err(|_| DbSaveError)?;
2026-08-19 17:33:22 +03:00
}
2026-07-29 13:30:49 +03:00
}
2026-08-19 17:33:22 +03:00
let last_index: u32 = tx.last_insert_rowid() as u32;
2026-08-24 18:16:41 +03:00
tx.commit().expect("Failed to commit");
2026-07-29 13:30:49 +03:00
2026-08-19 17:33:22 +03:00
let start_index = last_index - (count as u32) + 1;
2026-07-29 13:30:49 +03:00
2026-08-15 13:22:59 +03:00
for (index, id) in (start_index..=last_index).enumerate() {
2026-08-19 17:33:22 +03:00
stats[index].id = id.into();
2026-07-29 13:30:49 +03:00
}
2026-08-18 17:31:19 +03:00
2026-08-23 22:48:00 +03:00
info!("Added {} cards for {:?}", stats.len(), time.elapsed());
2026-08-24 18:16:41 +03:00
Ok(())
2026-07-29 13:30:49 +03:00
}
2026-08-24 23:42:58 +03:00
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> MaybeError {
2026-07-29 13:30:49 +03:00
let time = Instant::now();
2026-08-01 19:43:07 +03:00
connection
.execute(
2026-08-29 21:31:54 +03:00
"UPDATE states SET score = ?1, last_opened = ?2 WHERE id = ?3",
2026-08-01 19:43:07 +03:00
(&stat.score, &stat.last_open.timestamp(), &stat.id),
)
2026-08-24 18:16:41 +03:00
.map_err(|_| DbSaveError)?;
2026-08-23 22:48:00 +03:00
info!("Updated stat: {}", time.elapsed().as_millis());
2026-08-24 18:16:41 +03:00
Ok(())
2026-04-14 15:19:46 +03:00
}
2026-08-24 23:42:58 +03:00
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> MaybeError {
2026-08-20 16:22:38 +03:00
if stat.is_empty() {
2026-08-24 18:16:41 +03:00
return Ok(());
2026-08-20 16:22:38 +03:00
}
2026-08-20 16:23:22 +03:00
let ids = stat
.iter()
.map(|s| s.id.into())
.collect::<Vec<String>>()
.join(", ");
2026-08-20 16:22:38 +03:00
connection
2026-08-20 16:23:22 +03:00
.execute(
2026-08-29 21:31:54 +03:00
format!("DELETE FROM states WHERE id in ({ids})").as_str(),
2026-08-20 16:23:22 +03:00
(),
)
2026-08-24 18:16:41 +03:00
.map_err(|_| DbSaveError)?;
Ok(())
2026-08-20 16:23:22 +03:00
}