[format][library] Books: EPUB/FB2/TXT/MOBI/AZW3 into one sanitized model, CFI anchors, the event-log library, stats, CSV import, export
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
//! Reading stats per year and all time (no comparison with anyone).
|
||||
|
||||
use crate::library::{Goal, GoalKind, Library, Outcome};
|
||||
use crate::year_of;
|
||||
use serde::Serialize;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
|
||||
pub struct Stats {
|
||||
/// `None`: all time.
|
||||
pub year: Option<i32>,
|
||||
pub books: u32,
|
||||
pub did_not_finish: u32,
|
||||
/// Pages of the books finished (from their page counts).
|
||||
pub pages: u64,
|
||||
pub active_minutes: u64,
|
||||
/// Pages turned in sessions per active hour.
|
||||
pub pace: Option<f64>,
|
||||
pub moods: BTreeMap<String, u32>,
|
||||
pub genres: BTreeMap<String, u32>,
|
||||
/// (book id, pages)
|
||||
pub longest: Option<(String, u32)>,
|
||||
pub shortest: Option<(String, u32)>,
|
||||
pub goal: Option<GoalProgress>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize)]
|
||||
pub struct GoalProgress {
|
||||
pub kind: GoalKind,
|
||||
pub target: u32,
|
||||
pub done: u64,
|
||||
}
|
||||
|
||||
/// Stats for one year (`Some(y)`, by finish date and session day) or all time (`None`).
|
||||
pub fn stats(lib: &Library, year: Option<i32>) -> Stats {
|
||||
let mut s = Stats { year, ..Stats::default() };
|
||||
let in_year = |date: Option<&str>| match year {
|
||||
None => true,
|
||||
Some(y) => date.and_then(year_of) == Some(y),
|
||||
};
|
||||
for b in lib.books.values() {
|
||||
for r in &b.reads {
|
||||
let Some(outcome) = r.outcome else { continue };
|
||||
if !in_year(r.finished.as_deref()) {
|
||||
continue;
|
||||
}
|
||||
match outcome {
|
||||
Outcome::DidNotFinish => s.did_not_finish += 1,
|
||||
Outcome::Read => {
|
||||
s.books += 1;
|
||||
if let Some(p) = b.info.pages {
|
||||
s.pages += p as u64;
|
||||
if s.longest.as_ref().is_none_or(|(_, l)| p > *l) {
|
||||
s.longest = Some((b.id.clone(), p));
|
||||
}
|
||||
if s.shortest.as_ref().is_none_or(|(_, l)| p < *l) {
|
||||
s.shortest = Some((b.id.clone(), p));
|
||||
}
|
||||
}
|
||||
for m in &b.moods {
|
||||
*s.moods.entry(m.clone()).or_default() += 1;
|
||||
}
|
||||
for g in &b.info.genres {
|
||||
*s.genres.entry(g.clone()).or_default() += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut active_ms = 0u64;
|
||||
let mut pages_turned = 0u64;
|
||||
for x in lib.sessions.values() {
|
||||
if in_year(Some(&x.date)) {
|
||||
active_ms += x.active_ms;
|
||||
pages_turned += x.pages as u64;
|
||||
}
|
||||
}
|
||||
s.active_minutes = active_ms / 60_000;
|
||||
if active_ms >= 60_000 {
|
||||
s.pace = Some(pages_turned as f64 / (active_ms as f64 / 3_600_000.0));
|
||||
}
|
||||
if let Some(y) = year
|
||||
&& let Some(Goal { kind, target, .. }) = lib.goals.get(&y)
|
||||
{
|
||||
let done = match kind {
|
||||
GoalKind::Books => s.books as u64,
|
||||
GoalKind::Pages => s.pages,
|
||||
};
|
||||
s.goal = Some(GoalProgress { kind: *kind, target: *target, done });
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
/// Active reading time for one page view (docs/01_books.md): the time on the page, at most two minutes.
|
||||
pub const IDLE_CAP_MS: u64 = 2 * 60 * 1000;
|
||||
|
||||
pub fn active_ms(on_page_ms: u64) -> u64 {
|
||||
on_page_ms.min(IDLE_CAP_MS)
|
||||
}
|
||||
Reference in New Issue
Block a user