Files
jap_learn/src/data_provider/card_stats.rs
T

93 lines
2.5 KiB
Rust

use rusqlite::{Connection, params};
use std::time::Instant;
use log::info;
use crate::data::entities::{CardStatistics, DeckSettings};
pub fn load_stats_of_deck(set: &DeckSettings, connection: &Connection) -> Vec<CardStatistics> {
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)?,
})
})
.unwrap();
let mut buffer = vec![];
for word in iter {
buffer.push(word.unwrap());
}
buffer
}
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) {
let time = Instant::now();
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![
stat.word_id,
stat.set_id,
stat.score,
stat.last_open.timestamp()
])
.unwrap();
}
}
let last_index: u32 = tx.last_insert_rowid() as u32;
tx.commit().unwrap();
let start_index = last_index - (count as u32) + 1;
for (index, id) in (start_index..=last_index).enumerate() {
stats[index].id = id.into();
}
info!("Added {} cards for {:?}", stats.len(), time.elapsed());
}
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) {
let time = Instant::now();
connection
.execute(
"UPDATE card_stats SET score = ?1, last_opened = ?2 WHERE id = ?3",
(&stat.score, &stat.last_open.timestamp(), &stat.id),
)
.unwrap();
info!("Updated stat: {}", time.elapsed().as_millis());
}
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) {
if stat.is_empty() {
return;
}
let ids = stat
.iter()
.map(|s| s.id.into())
.collect::<Vec<String>>()
.join(", ");
connection
.execute(
format!("DELETE FROM card_stats WHERE id in ({ids})").as_str(),
(),
)
.unwrap();
}