diff --git a/entscheiden/motor/src/s2_vorhersage.rs b/entscheiden/motor/src/s2_vorhersage.rs index 54f9c66b..6547a9ee 100644 --- a/entscheiden/motor/src/s2_vorhersage.rs +++ b/entscheiden/motor/src/s2_vorhersage.rs @@ -3,7 +3,7 @@ //! (Knoten des Sitzes aus der Lage, wie `_gedaechtnis` mit `anfrage`) oder `{"id": ..., "vw": "", "knoten": code}`. //! Ausgabe je Zeile: `{"id", "code", "name", "p": [6 Haeufigkeiten], "n", "n_eff", "prior", "pool", "haende"}` (ungerundet) oder `{"id", "fehler"}`. //! Ohne `strom_bis` zaehlt nur basis.bin; mit `strom_bis` zusaetzlich die Haende aus strom.jsonl vor dieser Zeit. -use crate::speicher::{hoch, knoten_aus_anfrage, name, schaetze_in, vw_zahl, Live}; +use crate::speicher::{knoten_aus_anfrage, name, pool_in, schaetze_in, vw_zahl, Live}; use serde_json::{json, Value}; use std::io::{BufRead, Write}; @@ -23,7 +23,7 @@ fn eine_zeile(l: &mut Live, z: &Value) -> Value { }, }; let s = schaetze_in(l, vw_zahl(&vw), code); - let pool = (0..3).find_map(|lvl| l.m.pool_von(lvl, hoch(code, lvl as u8))); + let pool = pool_in(l, code); let haende = vw_zahl(&vw).and_then(|i| l.sp.sp.get(&i)).map_or(0, |x| x.haende); json!({"id": id, "code": code, "name": name(code), "p": s.p, "n": s.n, "n_eff": s.n_eff, "prior": s.prior, "pool": pool, "haende": haende}) } diff --git a/entscheiden/motor/src/speicher.rs b/entscheiden/motor/src/speicher.rs index 5bcb916d..9ebe4e6f 100644 --- a/entscheiden/motor/src/speicher.rs +++ b/entscheiden/motor/src/speicher.rs @@ -778,7 +778,7 @@ pub struct Schaetzung { } impl Modell { - pub(crate) fn pool_von(&self, lvl: usize, c: u16) -> Option<[f64; A]> { + fn pool_von(&self, lvl: usize, c: u16) -> Option<[f64; A]> { self.pool[lvl].get(&c).copied() } /// Haeufigkeit von Typ t (None = Pool) auf Stufe lvl, Knoten c (Code dieser Stufe). @@ -915,15 +915,9 @@ impl Live { pub fn aus_ordner(ordner: &str, strom_bis: Option) -> Option { let mut sp = Speicher::lies(&format!("{ordner}/basis.bin"))?; let m = Modell::lies(&format!("{ordner}/modell.json"))?; - if let Some(bis) = strom_bis { + if strom_bis.is_some() { let f = std::fs::File::open(format!("{ordner}/strom.jsonl")).ok()?; - for z in std::io::BufReader::new(f).split(b'\n').map_while(Result::ok) { - if let Ok(h) = serde_json::from_slice::(&z) { - if h["t"].as_f64().is_some_and(|t| t < bis as f64) { - sp.verbuche(&h); - } - } - } + strom_lesen_vor(&mut sp, std::io::BufReader::new(f), strom_bis); } Some(Live { sp, m, strom: None }) } @@ -951,11 +945,18 @@ pub fn ordner() -> Option { /// Verbucht den Strom Zeile fuer Zeile (Bytezeilen): eine kaputte Zeile (kein UTF-8, kein JSON, abgebrochen) wird uebergangen, /// die folgenden zaehlen weiter. Rueckgabe: Zahl der neu verbuchten Haende. pub fn strom_lesen(sp: &mut Speicher, r: impl std::io::BufRead) -> usize { + strom_lesen_vor(sp, r, None) +} + +/// Wie `strom_lesen`, aber mit `vor` = Some(Unix-Sekunden) nur Haende mit Zeit `t` < `vor` (zeitlicher Schnitt fuer Pruefungen). +pub fn strom_lesen_vor(sp: &mut Speicher, r: impl std::io::BufRead, vor: Option) -> usize { let mut n = 0; for z in r.split(b'\n') { let Ok(z) = z else { break }; if let Ok(h) = serde_json::from_slice::(&z) { - n += sp.verbuche(&h) as usize; + if vor.is_none_or(|v| h["t"].as_f64().is_some_and(|t| t < v as f64)) { + n += sp.verbuche(&h) as usize; + } } } n @@ -1057,7 +1058,7 @@ pub(crate) fn schaetze_in(l: &mut Live, key: Option, c: u16) -> Schaetzung } /// Pool-Haeufigkeiten am Knoten c, bei Luecke von der groeberen Stufe (wie die Schaetzung); ohne Pool gleichverteilt. -fn pool_in(l: &Live, c: u16) -> [f64; A] { +pub(crate) fn pool_in(l: &Live, c: u16) -> [f64; A] { (0..3).find_map(|lvl| l.m.pool_von(lvl, hoch(c, lvl as u8))).unwrap_or_else(gleich) }