//! Why a Snapshot outside the minute was written: one log line per Snapshot, //! written by the collector and read back by `reconcile`, which pairs it with //! a mismatch by symbol and receive time. use std::fmt; use chrono::{DateTime, Utc}; /// What a Snapshot that may differ from the book before it stands for. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub enum Cause { /// A fresh book differed from ours: changes no connection delivered. Loss, /// A fresh book differed from ours before any change of the symbol was /// verified: changes our own subscribe kept from us. SubscriptionGap, /// The connection followed changed: the new one's book, which may hold /// changes the old one never delivered. Switch, /// Its market was removed and is tracked again: its first book since, /// against the last one written before. TrackedAgain, } impl Cause { const ALL: [Cause; 4] = [ Cause::Loss, Cause::SubscriptionGap, Cause::Switch, Cause::TrackedAgain, ]; fn name(self) -> &'static str { match self { Cause::Loss => "loss", Cause::SubscriptionGap => "subscription gap", Cause::Switch => "switch", Cause::TrackedAgain => "tracked again", } } } impl fmt::Display for Cause { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(self.name()) } } const MARK: &str = "Snapshot of "; /// The line's text: `Snapshot of at : `, /// then any detail for a reader. pub fn line(symbol: &str, received_at: DateTime, cause: Cause, detail: &str) -> String { let at = received_at.timestamp_micros(); format!("{MARK}{symbol} at {at}: {cause}{detail}") } /// The symbol, receive time and cause of a log line written from [`line`], /// wherever it sits in the line. pub fn parse(log_line: &str) -> Option<(String, DateTime, Cause)> { let rest = &log_line[log_line.find(MARK)? + MARK.len()..]; let (symbol, rest) = rest.split_once(" at ")?; let (at, rest) = rest.split_once(": ")?; let received_at = DateTime::from_timestamp_micros(at.parse().ok()?)?; let cause = Cause::ALL .into_iter() .find(|cause| rest.starts_with(cause.name()))?; Some((symbol.to_string(), received_at, cause)) } #[cfg(test)] mod tests { use super::*; #[test] fn a_line_reads_back() { let at = DateTime::from_timestamp_micros(1_794_390_726_365_123).unwrap(); for cause in Cause::ALL { let text = line("123", at, cause, " (its time 12:00:00.000)"); let logged = format!("12:00:00.001 polymarket: {text}"); assert_eq!(parse(&logged), Some(("123".to_string(), at, cause))); } // A reconnect's loss says how it was found, after its cause. let text = line( "123", at, Cause::Loss, ", found by a reconnect (its time 12:00:00.000)", ); assert_eq!(parse(&text), Some(("123".to_string(), at, Cause::Loss))); assert_eq!( parse("12:00:00.001 polymarket: connection 3: connected"), None ); assert_eq!(parse("Snapshot of 1 at soon: loss"), None); } }