Files
jap_learn/src/data_provider/card_stats.rs
T

100 lines
3.0 KiB
Rust
Raw Normal View History

2026-08-24 18:16:41 +03:00
use crate::data::entities::{CardStatistics, DeckSettings};
use crate::notification::NotificationData;
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
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
.prepare("SELECT id, word_id, score, last_opened FROM card_stats 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)?,
last_open: row.get(3)?,
})
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);
for word in iter.filter_map(|result| result.ok()) {
buffer.push(word);
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-24 18:16:41 +03:00
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> Result<(), NotificationData> {
2026-08-16 14:23:00 +03:00
let time = Instant::now();
2026-08-24 18:16:41 +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(
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4)",
)
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,
stat.last_open.timestamp()
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 18:16:41 +03:00
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> Result<(), NotificationData> {
2026-07-29 13:30:49 +03:00
let time = Instant::now();
2026-08-01 19:43:07 +03:00
connection
.execute(
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
(&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 18:16:41 +03:00
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> Result<(), NotificationData> {
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-20 17:48:03 +03:00
format!("DELETE FROM card_stats 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
}