//! THE SERVING MAC'S METERING, read live. //! //! `cotton_ai_calls` never leaves the serving Mac (server-only metering, //! architect 14 Oct 2026), so this device's copy holds no row of it and a list //! read from the copy would be empty on every phone. `records.list` and //! `records.get` of the metering reads (`cotton_core::definitions::METERING_READS`) //! go to the serving Mac in one call instead, on every device the same way — //! the Mac's own app included, which reaches its server over the same signed //! transport, as `repo.vars` does. //! //! A read never waits on a Mac this device does not hear (295 rule 15, //! walker294): with the serving Mac not heard now — the copy's own //! reachability fact, a failed pass after the last contact being not heard //! (`sync_state::serving_not_heard`) — it answers at once, typed //! `could_not_read` with `why: on_the_mac`, the table and the Mac's //! `heard_at`, and the transport is never called. The database definition's //! `words.could_not_read.on_the_mac` says it; no sentence is made here. use serde_json::{json, Value}; use crate::phone::owner; use crate::shell::client::ClientController; use crate::{handle::CottonHandle, methods::MethodError}; /// The live answer for a metering read, or `None` for every other read. pub fn records_read( handle: &CottonHandle, method: &str, request: &Value, ) -> Option> { let tool = match method { "records.list" => "list_records", "records.get" => "get_record_by_table", _ => return None, }; let table = request.get("table").and_then(Value::as_str)?; if !cotton_core::definitions::METERING_READS.contains(&table) { return None; } let client = handle.client(); Some(answer(&client, method, table, || read(handle, tool, request))) } /// `details.why` of a metering read whose Mac is not heard now. pub const ON_THE_MAC: &str = "on_the_mac"; /// The metering read's answer: at once and typed when the serving Mac is not /// heard now, without calling `ask`; else `ask`'s answer — the Mac's. pub(crate) fn answer( client: &ClientController, method: &str, table: &str, ask: impl FnOnce() -> Result, ) -> Result { if let Some(heard_at) = crate::phone::sync_state::serving_not_heard(client) { let mut details = json!({ "why": ON_THE_MAC, "method": method, "table": table, "heard_at": heard_at }); crate::phone::stamps::Stamps::of(client).on(&mut details, &["heard_at"]); // The message is for the log; the page draws the definition's line. return Err(MethodError::new( crate::methods::COULD_NOT_READ, format!("{method} {table}: read on the serving Mac, not heard now (last {heard_at:?})"), ) .with_details(details)); } ask() } fn read(handle: &CottonHandle, tool: &str, request: &Value) -> Result { let mut args = crate::phone::tool_arguments(request)?; // The reader is this device: the day a call is on is ITS calendar day. if let Some(zone) = handle.client().time_zone() { args.entry("timezone").or_insert_with(|| Value::from(zone)); } owner::with_fetch(handle, |fetch| { owner::send( fetch, owner::post( cotton_core::definitions::METERING_READ_PATH.to_string(), &json!({ "tool": tool, "args": args }), ), ) }) } #[cfg(test)] #[path = "metering_tests.rs"] mod tests;