Files
jap_learn/src/data_provider/card_stats.rs
T

115 lines
3.2 KiB
Rust
Raw Normal View History

2026-08-18 17:31:19 +03:00
use crate::lang::{CardStatistics, DeckSettings, INVALID_ID};
2026-04-14 15:19:46 +03:00
use rusqlite::Connection;
2026-07-29 13:30:49 +03:00
use std::time::Instant;
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-15 13:22:59 +03:00
pub fn add_stat_list(stat: &mut [CardStatistics], connection: &Connection) {
2026-08-16 14:23:00 +03:00
let time = Instant::now();
2026-08-01 19:43:07 +03:00
let inserting = stat
.iter()
.map(|stat| {
format!(
"({}, {}, {}, {})",
2026-08-15 13:22:59 +03:00
stat.word_id,
stat.set_id,
stat.score,
stat.last_open.timestamp()
2026-08-01 19:43:07 +03:00
)
})
.collect::<Vec<_>>()
.join(", ");
let query = format!(
"INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES {}",
inserting
);
let count = connection.execute(query.as_str(), ());
2026-07-29 13:30:49 +03:00
if count.is_err() {
return;
}
2026-08-01 19:43:07 +03:00
let last_index: u32 = connection
.query_one(
"SELECT seq from sqlite_sequence WHERE name == ?1",
("card_stats".to_string(),),
|row| row.get(0),
)
.unwrap();
2026-07-29 13:30:49 +03:00
2026-08-15 13:22:59 +03:00
let start_index = last_index - (stat.len() 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-18 12:47:06 +03:00
stat[index].id = id.into();
2026-07-29 13:30:49 +03:00
}
2026-08-18 17:31:19 +03:00
2026-08-16 14:23:00 +03:00
println!("Added {} cards for {:?}", stat.len(), time.elapsed());
2026-07-29 13:30:49 +03:00
}
2026-08-01 23:40:56 +03:00
// pub fn add_stat(stat: &mut CardStatistics, connection: &Connection) {
// let time = Instant::now();
// let index = connection
// .query_row(
// "INSERT INTO card_stats (word_id, set_id, score, last_opened) VALUES (?1, ?2, ?3, ?4) RETURNING id",
// (
// &stat.word_id,
// &stat.set_id,
// &stat.score,
// &stat.last_open.timestamp(),
// ),
// |row| row.get(0)
// )
// .unwrap_or_else(|e| {println!("{}", e); 0});
//
// stat.id = index;
// println!("Added stat: {}", time.elapsed().as_millis());
// }
2026-04-13 22:25:58 +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),
)
.unwrap_or_else(|e| {
println!("{}", e);
0
});
2026-07-29 13:30:49 +03:00
println!("Updated stat: {}", time.elapsed().as_millis());
2026-04-14 15:19:46 +03:00
}
2026-04-15 07:53:32 +03:00
pub fn delete_stat(stat: &CardStatistics, connection: &Connection) {
2026-08-18 17:31:19 +03:00
if !stat.id.is_valid() {
2026-04-13 22:25:58 +03:00
return;
}
connection
2026-04-15 07:53:32 +03:00
.execute("DELETE FROM card_stats WHERE id = ?1", (&stat.id,))
2026-04-13 22:25:58 +03:00
.unwrap_or_else(|e| {
println!("{}", e);
0
});
}