This repository has been archived on 2026-11-16. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
orderbook-collector/src/cause.rs
T
pascal cf6c16c9cf feat: ready to submit, a run to watch, the README, the review's fixes
Sprint 08 (roadmap/08. ready to submit.md), the last milestone.

- scripts/watch.sh runs the collector with a live view of its log, stops
  cleanly on Ctrl-C, then checks the output (reconcile) and shows one
  symbol's book as text (show, a new binary).
- The README for the solution, with the final runs: Polymarket through an
  outage it recovered from on its own, Kalshi's demo with two slow
  connections recycled.
- From a review of the whole codebase: Kalshi takes RSA keys too
  (RSA-PSS-SHA256); a slow Kalshi connection takes the hourly recycle's
  next turn, which it nearly never got before; a market tracked again no
  longer fails reconcile; Book::add checks for overflow; usage and --help
  name the key variables; the first scan says how far it got.
- Tests for every gap the reviews found, an offline run of the whole
  orchestrator among them; ShellCheck in CI.
2026-11-12 22:32:39 +01:00

98 lines
3.2 KiB
Rust

//! 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 <symbol> at <receive time, µs>: <cause>`,
/// then any detail for a reader.
pub fn line(symbol: &str, received_at: DateTime<Utc>, 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<Utc>, 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);
}
}