rename data provider file names

This commit is contained in:
micialware committed 2026-08-29 21:32:54 +03:00
1 parent a642a8fda4
commit af6cf10765
7 files changed
+10 -10

No files matched your search

+125
View File
@@ -0,0 +1,125 @@
use crate::data::entities::{CardStatistics, DeckSettings};
use crate::notification::NotificationData::{DbLoadError, DbSaveError};
use crate::notification::{MaybeError, NotificationData};
use chrono::NaiveDate;
use fsrs::MemoryState;
use log::info;
use reqwest::get;
use rusqlite::{Connection, params};
use std::time::Instant;
pub fn load_stats_of_deck(
set: &DeckSettings,
connection: &Connection,
) -> Result<Vec<CardStatistics>, NotificationData> {
let mut stmt = connection
.prepare("SELECT id, word_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day FROM states WHERE set_id = ?1")
.expect("Failed to prepare state query");
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)?,
fsrs_opened: NaiveDate::from_epoch_days(row.get::<usize, i32>(4)?)
.expect("Incorrect date encoding"),
fsrs_memory: Some(get_memory(row.get(5)?)),
last_open: row.get(3)?,
fsrs_open_day: NaiveDate::from_epoch_days(row.get::<usize, i32>(6)?)
.expect("Incorrect date encoding"),
})
})
.map_err(|_| DbLoadError)?;
let mut buffer = Vec::with_capacity(512);
iter.filter_map(|res| res.ok()).for_each(|i| buffer.push(i));
Ok(buffer)
}
fn get_memory(value: i64) -> MemoryState {
MemoryState {
stability: f32::from_bits(value as u32),
difficulty: f32::from_bits((value >> 32) as u32),
}
}
fn memory_to_i64(value: &MemoryState) -> i64 {
value.stability.to_bits() as i64 | ((value.difficulty.to_bits() as i64) << 32)
}
pub fn add_stat_list(stats: &mut [CardStatistics], connection: &mut Connection) -> MaybeError {
let time = Instant::now();
let tx = connection
.transaction()
.expect("Failed to create transaction");
let count = stats.len();
{
let mut stmt = tx
.prepare(
"INSERT INTO states (word_id, set_id, score, last_opened, fsrs_opened, fsrs_memory, fsrs_open_day) VALUES (?1, ?2, ?3, ?4)",
)
.expect("Failed to prepare state query");
for stat in stats.iter() {
stmt.execute(params![
stat.word_id,
stat.set_id,
stat.score,
stat.last_open.timestamp(),
stat.fsrs_opened.to_epoch_days(),
memory_to_i64(&stat.fsrs_memory.unwrap_or(MemoryState {
difficulty: 0.0,
stability: 0.0
})),
stat.fsrs_open_day.to_epoch_days()
])
.map_err(|_| DbSaveError)?;
}
}
let last_index: u32 = tx.last_insert_rowid() as u32;
tx.commit().expect("Failed to commit");
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());
Ok(())
}
pub fn update_stat_score(stat: &CardStatistics, connection: &Connection) -> MaybeError {
let time = Instant::now();
connection
.execute(
"UPDATE states SET score = ?1, last_opened = ?2 WHERE id = ?3",
(&stat.score, &stat.last_open.timestamp(), &stat.id),
)
.map_err(|_| DbSaveError)?;
info!("Updated stat: {}", time.elapsed().as_millis());
Ok(())
}
pub fn delete_stats(stat: &[CardStatistics], connection: &Connection) -> MaybeError {
if stat.is_empty() {
return Ok(());
}
let ids = stat
.iter()
.map(|s| s.id.into())
.collect::<Vec<String>>()
.join(", ");
connection
.execute(
format!("DELETE FROM states WHERE id in ({ids})").as_str(),
(),
)
.map_err(|_| DbSaveError)?;
Ok(())
}