//! `sync.status`'s one read of where this device's sync stands //! (`cotton_core::rules::sync_status`): what this device's file says — //! its role, the serving Mac's name, the photos and documents it has not //! uploaded, and per table on the detail page — read in the same snapshot as //! the outbox count, beside what the passes and the keep-fetch sweep keep in //! memory. The line is the core's; this file reads, and says nothing. use std::collections::BTreeMap; use cotton_core::rules::sync_status::{Pile, Role, SyncWords, TableFacts}; use cotton_store::driver::Driver; use rusqlite::types::Value as Sql; use crate::shell::file::ReadFile; /// What this device's file says about its sync. #[derive(Debug, Default)] pub struct FileFacts { pub role: Role, pub serving: Option, /// The serving device is this one: a database founded here that no Mac /// has taken, so this device has no peer — never its own. pub serving_here: bool, /// Bytes this device filed and has not uploaded. pub to_send: Pile, /// Per table, only when the detail page asks. pub tables: Option>, /// The words the line is said in: this database's `lines` rows, the /// kit's for any it has none of. pub words: SyncWords, } /// Read inside the caller's transaction. A part that does not read is /// unknown, never zero. pub fn file_facts(file: &ReadFile<'_>, this_device: Option<&str>, detail: bool) -> FileFacts { FileFacts { role: this_device.map_or(Role::Full, |id| role(file.driver, id)), serving: crate::phone::sync_state::serving_name_in(file), serving_here: this_device.is_some_and(|here| { crate::phone::sync_state::serving_id(file).ok().flatten().as_deref() == Some(here) }), to_send: unsent_bytes(file.driver, file.workspace_id), tables: detail.then(|| tables(file.driver)).flatten(), words: SyncWords::of_rows(&cotton_tools::presentation::line_rows(file.driver, file.workspace_id)), } } /// A reader's device row says `reader`; every other device sends. fn role(db: &Driver, device: &str) -> Role { let scope = db .get("SELECT scope FROM cotton_devices WHERE id = ?", &[Sql::Text(device.to_string())]) .ok() .flatten() .and_then(|row| row.text("scope").map(str::to_string)); match scope.as_deref() { Some("reader") => Role::Reader, _ => Role::Full, } } fn unsent_bytes(db: &Driver, workspace: &str) -> Pile { db.get( "SELECT COUNT(*) AS n, COALESCE(SUM(size_bytes), 0) AS bytes FROM _media \ WHERE workspace_id = ? AND state = 'pending'", &[Sql::Text(workspace.to_string())], ) .ok() .flatten() .map_or_else(Pile::default, |row| Pile { count: row.int("n").and_then(|n| u64::try_from(n).ok()), bytes: row.int("bytes").and_then(|n| u64::try_from(n).ok()), }) } /// Every table this file holds receipts for: its rows, and the newest /// receipt's stamp (an HLC opens with its 13 milliseconds). fn tables(db: &Driver) -> Option> { let names = db.all("SELECT DISTINCT tbl FROM _writes", &[]).ok()?; let mut out = BTreeMap::new(); for name in names.iter().filter_map(|row| row.text("tbl").map(str::to_string)) { let quoted = name.replace('"', "\"\""); let Ok(Some(rows)) = db.get(&format!("SELECT COUNT(*) AS n FROM \"{quoted}\""), &[]) else { continue }; let last = db .get("SELECT MAX(hlc) AS hlc FROM _writes WHERE tbl = ?", &[Sql::Text(name.clone())]) .ok() .flatten() .and_then(|row| row.text("hlc").and_then(|hlc| hlc.get(..13)).and_then(|ms| ms.parse().ok())); out.insert( name, TableFacts { rows: rows.int("n").and_then(|n| u64::try_from(n).ok()).unwrap_or(0), last_receipt_at: last }, ); } Some(out) }