Skip to main content

e2e_canonical/
e2e_canonical.rs

1use std::path::PathBuf;
2
3use digdigdig3::core::normalization::{CanonicalEvent, Canonicalize};
4use digdigdig3::core::types::StreamEvent;
5use digdigdig3_station::{StorageConfig, StorageManager, StreamKey};
6
7#[tokio::main]
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let cfg = StorageConfig {
10        root: PathBuf::from("./e2e_test_data"),
11        default_retention_days: 365,
12        orderbook_snapshot_interval_secs: 0,
13    };
14    let mgr = StorageManager::new(cfg)?;
15
16    let key = StreamKey {
17        exchange: "binance".to_string(),
18        account: "spot".to_string(),
19        symbol: "BTCUSDT".to_string(),
20        stream_kind: "trade".to_string(),
21    };
22
23    let now_ms = chrono::Utc::now().timestamp_millis();
24    let day_start = now_ms - (now_ms % 86_400_000);
25    let records = mgr.read_range(&key, day_start, now_ms).await?;
26
27    println!("Total Binance trade records: {}", records.len());
28
29    let sample: Vec<_> = records.iter().take(10).collect();
30    let mut ok = 0u32;
31    let mut failed = 0u32;
32
33    for (i, (ts_ms, payload)) in sample.iter().enumerate() {
34        match serde_json::from_slice::<StreamEvent>(payload) {
35            Ok(event) => {
36                match event.canonicalize() {
37                    Some(CanonicalEvent::Trade(t)) => {
38                        let price_ok = t.price > rust_decimal::Decimal::ZERO;
39                        let qty_ok = t.quantity > rust_decimal::Decimal::ZERO;
40                        let ts_ok = t.timestamp_ms > 1_700_000_000_000;
41                        if price_ok && qty_ok && ts_ok {
42                            ok += 1;
43                            println!("[{i}] OK  price={} qty={} ts_ms={} side={:?}",
44                                t.price, t.quantity, t.timestamp_ms, t.side);
45                        } else {
46                            failed += 1;
47                            println!("[{i}] BAD price={} qty={} ts_ms={} (record ts_ms={})",
48                                t.price, t.quantity, t.timestamp_ms, ts_ms);
49                        }
50                    }
51                    Some(other) => {
52                        failed += 1;
53                        println!("[{i}] WRONG VARIANT: {:?}", other);
54                    }
55                    None => {
56                        failed += 1;
57                        println!("[{i}] canonicalize() returned None (record ts_ms={})", ts_ms);
58                    }
59                }
60            }
61            Err(e) => {
62                failed += 1;
63                println!("[{i}] DESERIALIZE FAIL: {e}");
64            }
65        }
66    }
67
68    println!("\nResult: {ok}/10 canonical OK, {failed} failed");
69    Ok(())
70}