Files
jap_learn/src/data_provider/card_stats.rs
T

92 lines
2.5 KiB
Rust
Raw Normal View History

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-08-23 22:48:00 +03:00
use log::info;
2026-08-24 11:40:31 +03:00
use crate::data::entities::{CardStatistics, DeckSettings};
2026-04-13 22:25:58 +03:00
2026-08-18 15:16:51 +03:00
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
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")
.unwrap();
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-01 19:43:07 +03:00
.unwrap();
2026-04-13 22:25:58 +03:00
let mut buffer = vec![];
for word in iter {
buffer.push(word.unwrap());
}
buffer
}
2026-08-19 17:33:22 +03:00
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) {
2026-08-16 14:23:00 +03:00
let time = Instant::now();
2026-08-19 17:33:22 +03:00
let tx = connection.transaction().unwrap();
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)",
)
.unwrap();
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-19 23:51:41 +03:00
.unwrap();
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;
tx.commit().unwrap();
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-07-29 13:30:49 +03:00
}
2026-08-01 19:43:07 +03:00
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
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-19 23:37:59 +03:00
.unwrap();
2026-08-23 22:48:00 +03:00
info!("Updated stat: {}", time.elapsed().as_millis());
2026-04-14 15:19:46 +03:00
}
2026-08-20 16:22:38 +03:00
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) {
if stat.is_empty() {
return;
}
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-20 16:22:38 +03:00
.unwrap();
2026-08-20 16:23:22 +03:00
}