atelier_data 0.0.15

Data Artifacts and I/O for the atelier-rs engine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! Synchronised market data worker.
//!
//! `MarketWorker` composes an `IngestionCore` with a
//! `MarketSynchronizer` to produce grid-aligned `MarketSnapshot`s
//! from raw exchange events.
//!
//! # Design
//!
//! ```text
//! ┌──────────────┐     mpsc     ┌──────────────┐   sync   ┌──────────┐
//! │ IngestionCore│─────────────→│ MarketWorker  │────────→│ Sinks    │
//! │ (WSS + reconn│  TopicMsg    │ (feed_event + │ Snapshot │ (channel │
//! │  + classify) │              │  synchronizer)│          │  / term) │
//! └──────────────┘              └──────────────┘          └──────────┘
//! ```
//!
//! The worker:
//! 1. Spawns an `IngestionCore` in a background task.
//! 2. Receives classified `TopicMessage`s.
//! 3. Converts raw exchange events into normalised types
//!    (`Trade`, `Orderbook`, etc.) and feeds them into the synchroniser.
//! 4. Drains ready `MarketSnapshot`s and emits them to output sinks.

use tokio::sync::mpsc;
use tokio::sync::watch;

use crate::config::markets::market_config::SyncMode;
use crate::config::workers::{MarketWorkerConfig, OutputSinkConfig};
use crate::levels::Level;
use crate::orders::OrderSide;
use crate::synchronizers::{ClockMode, MarketSynchronizer};
use crate::sources::ExchangeEvent;

use super::ingestion_core::{IngestionCore, IngestionReport};
use super::output::{build_sinks, OutputSinkSet};
use super::topic_publisher::{TopicMessage, TopicRegistry};

// ─────────────────────────────────────────────────────────────────────────────
// MarketWorkerReport
// ─────────────────────────────────────────────────────────────────────────────

/// Summary statistics returned when a `MarketWorker` finishes.
#[derive(Debug, Clone)]
pub struct MarketWorkerReport {
    /// Ingestion-level statistics (events, reconnects, gaps).
    pub ingestion: IngestionReport,
    /// Total grid-aligned snapshots produced.
    pub snapshots_produced: u64,
    /// Number of flush cycles completed.
    pub flushes: u32,
}

// ─────────────────────────────────────────────────────────────────────────────
// MarketWorker
// ─────────────────────────────────────────────────────────────────────────────

/// A single-symbol synchronised market data worker.
///
/// Connects to the configured exchange, ingests raw events via
/// `IngestionCore`, feeds them through a `MarketSynchronizer`,
/// and emits grid-aligned `MarketSnapshot`s to the configured sinks.
pub struct MarketWorker {
    /// The ingestion engine (handles WSS connection + reconnection).
    core: IngestionCore,
    /// Time synchroniser producing grid-aligned snapshots.
    sync: MarketSynchronizer,
    /// Fan-out output sinks.
    sinks: OutputSinkSet,
    /// Grid ticks before a flush cycle.
    flush_threshold: usize,
    /// Trading pair symbol.
    symbol: String,
    /// Exchange name.
    exchange_name: String,
    /// Channel capacity for the core → worker pipe.
    channel_capacity: usize,
    /// Which clock mode drives snapshot production.
    clock_mode: ClockMode,
    /// Grid period in nanoseconds (used for the timer in ExternalClock mode).
    period_ns: u64,
}

impl MarketWorker {
    /// Create a MarketWorker from a [`MarketWorkerConfig`].
    pub fn from_config(config: MarketWorkerConfig) -> anyhow::Result<Self> {
        let period_ns = config.sync.period_ns();
        let clock_mode = match config.sync.sync_mode {
            SyncMode::OnOrderbook => ClockMode::OrderbookDriven,
            SyncMode::OnTrade => ClockMode::TradeDriven,
            SyncMode::OnLiquidation => ClockMode::LiquidationDriven,
            SyncMode::OnTime => ClockMode::ExternalClock,
        };
        let sync = MarketSynchronizer::with_clock_mode(period_ns, clock_mode);
        let flush_threshold = config.sync.flush_threshold;

        let channel_capacity = config.common.channel_capacity();
        let symbol = config.common.symbol.clone();
        let exchange_name = config.common.exchange.clone();

        let core = IngestionCore::new(config.common.clone())?;

        // Build registry for channel sink (if requested).
        let has_channel = config
            .output
            .iter()
            .any(|s| matches!(s, OutputSinkConfig::Channel));

        let registry = if has_channel {
            Some(TopicRegistry::from_config(
                &config.common.exchange,
                &config.common.symbol,
                &config.common.datatypes,
                channel_capacity,
            ))
        } else {
            None
        };

        let sinks = build_sinks(&config.output, registry);

        Ok(Self {
            core,
            sync,
            sinks,
            flush_threshold,
            symbol,
            exchange_name,
            channel_capacity,
            clock_mode,
            period_ns,
        })
    }

    /// Run the synchronised ingestion loop until `shutdown` signals `true`.
    ///
    /// When the clock mode is [`ClockMode::ExternalClock`], a
    /// `tokio::time::interval` drives the grid via `sync.on_time()`.
    /// For all other modes the grid is driven by the data events
    /// themselves (orderbook / trade / liquidation timestamps).
    ///
    /// Returns a [`MarketWorkerReport`] with session statistics.
    pub async fn run(
        self,
        shutdown: watch::Receiver<bool>,
    ) -> anyhow::Result<MarketWorkerReport> {
        // Destructure so `core` can be moved into the spawned task while
        // the remaining fields stay available for the event loop.
        let MarketWorker {
            core,
            mut sync,
            sinks,
            flush_threshold,
            channel_capacity,
            exchange_name,
            symbol,
            clock_mode,
            period_ns,
            ..
        } = self;

        // Pipeline: passthrough for most exchanges, BookInitializer for Binance.
        let exchange_enum: crate::exchanges::Exchange = exchange_name
            .parse()
            .unwrap_or(crate::exchanges::Exchange::Bybit);
        let pipeline = crate::workers::pipeline::build_pipeline(
            exchange_enum,
            &symbol,
            core.datatypes(),
        );

        let (core_tx, core_rx) = mpsc::channel(channel_capacity);
        let core_handle = tokio::spawn(core.run(shutdown.clone(), core_tx));

        let (pipeline_tx, mut rx) = mpsc::channel(channel_capacity);
        let _pipeline_handle = tokio::spawn(
            pipeline.run(core_rx, pipeline_tx, shutdown),
        );

        let mut snapshots_produced: u64 = 0;
        let mut snapshots_since_flush: u64 = 0;
        let mut flushes: u32 = 0;

        /// Drain ready snapshots and emit them to sinks.
        /// Returns the number of new snapshots produced.
        #[inline]
        fn drain_and_emit(
            sync: &mut MarketSynchronizer,
            sinks: &OutputSinkSet,
        ) -> u64 {
            let ready = sync.drain();
            let n = ready.len() as u64;
            for snapshot in &ready {
                let _ = sinks.emit_snapshot(snapshot);
            }
            n
        }

        /// Check if a flush is due and perform it if so.
        ///
        /// Uses a running counter (`snapshots_since_flush`) instead of
        /// exact modulo arithmetic — this guarantees a flush even if
        /// `drain_and_emit` returns more than one snapshot per tick.
        #[inline]
        fn maybe_flush(
            sinks: &OutputSinkSet,
            flush_threshold: usize,
            snapshots_since_flush: &mut u64,
            flushes: &mut u32,
        ) -> anyhow::Result<()> {
            if flush_threshold > 0
                && *snapshots_since_flush >= flush_threshold as u64
            {
                sinks.flush()?;
                *snapshots_since_flush = 0;
                *flushes += 1;
            }
            Ok(())
        }

        // ── Event loop ──────────────────────────────────────────────────
        if clock_mode == ClockMode::ExternalClock {
            // Timer-driven path: a periodic tick calls sync.on_time() to
            // advance the grid. Data events are accumulated passively.
            let tick_ms = (period_ns / 1_000_000).max(1);
            let mut timer = tokio::time::interval(
                std::time::Duration::from_millis(tick_ms),
            );

            tracing::info!(
                tick_ms = tick_ms,
                "market_worker.external_clock_started"
            );

            loop {
                tokio::select! {
                    _ = timer.tick() => {
                        let now_ns = std::time::SystemTime::now()
                            .duration_since(std::time::UNIX_EPOCH)
                            .unwrap()
                            .as_nanos() as u64;

                        sync.on_time(now_ns);

                        let n = drain_and_emit(&mut sync, &sinks);
                        snapshots_produced += n;
                        snapshots_since_flush += n;
                        maybe_flush(&sinks, flush_threshold, &mut snapshots_since_flush, &mut flushes)?;
                    }

                    msg = rx.recv() => {
                        match msg {
                            Some(msg) => feed_event(&mut sync, &exchange_name, &symbol, &msg),
                            None => break, // IngestionCore exited
                        }
                    }
                }
            }
        } else {
            // Data-driven path: events drive the grid.
            while let Some(msg) = rx.recv().await {
                feed_event(&mut sync, &exchange_name, &symbol, &msg);

                let n = drain_and_emit(&mut sync, &sinks);
                snapshots_produced += n;
                snapshots_since_flush += n;
                maybe_flush(&sinks, flush_threshold, &mut snapshots_since_flush, &mut flushes)?;
            }
        }

        // ── Final drain + flush ─────────────────────────────────────────
        sync.finalize();
        let final_snapshots = sync.drain();
        for snapshot in &final_snapshots {
            let _ = sinks.emit_snapshot(snapshot);
            snapshots_produced += 1;
        }
        sinks.flush()?;
        flushes += 1;

        let ingestion = core_handle.await??;

        tracing::info!(
            exchange = exchange_name.as_str(),
            symbol = symbol.as_str(),
            snapshots_produced = snapshots_produced,
            flushes = flushes,
            "market_worker.stopped"
        );

        Ok(MarketWorkerReport {
            ingestion,
            snapshots_produced,
            flushes,
        })
    }

}

// ─────────────────────────────────────────────────────────────────────────────
// Free-function event routing (avoids partial-move issues with `self`)
// ─────────────────────────────────────────────────────────────────────────────

/// Route a [`TopicMessage`] into the synchroniser.
///
/// Converts exchange-specific events into normalised types and calls
/// the appropriate `sync.on_*()` method.
fn feed_event(
    sync: &mut MarketSynchronizer,
    _exchange_name: &str,
    symbol: &str,
    msg: &TopicMessage,
) {
    match &msg.payload {
        ExchangeEvent::Bybit(bybit_event) => feed_bybit(sync, symbol, bybit_event),
        ExchangeEvent::Coinbase(coinbase_event) => feed_coinbase(sync, coinbase_event),
        ExchangeEvent::Kraken(kraken_event) => feed_kraken(sync, kraken_event),
        ExchangeEvent::Binance(binance_event) => feed_binance(sync, symbol, binance_event),
    }
}

fn feed_bybit(
    sync: &mut MarketSynchronizer,
    symbol: &str,
    event: &crate::sources::bybit::events::BybitWssEvent,
) {
    use crate::sources::bybit::events::BybitWssEvent;

    match event {
        BybitWssEvent::TradeData(data) => {
            let trade = crate::trades::Trade {
                trade_ts: data.trade_ts,
                symbol: data.symbol.clone(),
                side: data.side.clone(),
                amount: data.amount.parse::<f64>().unwrap_or(0.0),
                price: data.price.parse::<f64>().unwrap_or(0.0),
                exchange: "bybit".to_string(),
                id: data.trade_id.clone(),
            };
            sync.on_trade(trade);
        }
        BybitWssEvent::OrderbookData(resp) => {
            // BybitOrderbookResponse.orderbook_ts is the exchange timestamp.
            // BybitOrderbookData.bids / .asks are Vec<BybitPriceLevel>.
            // BybitPriceLevel is a tuple struct with .price() and .size() methods.
            let ob = crate::orderbooks::Orderbook {
                orderbook_id: 0,
                orderbook_ts: resp.orderbook_ts,
                symbol: symbol.to_string(),
                exchange: "bybit".to_string(),
                bids: resp
                    .data
                    .bids
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32, OrderSide::Bids, level.price(), level.size(), vec![],
                    ))
                    .collect(),
                asks: resp
                    .data
                    .asks
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32, OrderSide::Asks, level.price(), level.size(), vec![],
                    ))
                    .collect(),
            };
            sync.on_orderbook(symbol, resp.orderbook_ts, ob);
        }
        BybitWssEvent::LiquidationData(data) => {
            let liq = crate::liquidations::Liquidation {
                liquidation_ts: data.liquidation_ts,
                symbol: data.symbol.clone(),
                side: data.side.clone(),
                price: data.price.parse::<f64>().unwrap_or(0.0),
                amount: data.amount.parse::<f64>().unwrap_or(0.0),
                exchange: "bybit".to_string(),
            };
            sync.on_liquidation(liq);
        }
        BybitWssEvent::TickerData(data) => {
            if let Some(ref fr_str) = data.funding_rate {
                if let Ok(fr_val) = fr_str.parse::<f64>() {
                    // next_funding_time is Option<String> (ms as string).
                    let next_ts: u64 = data
                        .next_funding_time
                        .as_deref()
                        .and_then(|s| s.parse::<u64>().ok())
                        .unwrap_or(0);
                    let fr = crate::funding::FundingRate {
                        funding_rate_ts: data.ts.unwrap_or(0),
                        symbol: data.symbol.clone(),
                        funding_rate: fr_val,
                        next_funding_ts: next_ts,
                        exchange: "bybit".to_string(),
                    };
                    sync.on_funding(fr);
                }
            }
            if let Some(ref oi_str) = data.open_interest {
                if let Ok(oi_val) = oi_str.parse::<f64>() {
                    let oi_value: f64 = data
                        .open_interest_value
                        .as_deref()
                        .and_then(|s| s.parse::<f64>().ok())
                        .unwrap_or(0.0);
                    let oi = crate::open_interest::OpenInterest {
                        open_interest_ts: data.ts.unwrap_or(0),
                        symbol: data.symbol.clone(),
                        open_interest: oi_val,
                        open_interest_value: oi_value,
                        exchange: "bybit".to_string(),
                    };
                    sync.on_open_interest(oi);
                }
            }
        }
    }
}

fn feed_coinbase(
    sync: &mut MarketSynchronizer,
    event: &crate::sources::coinbase::events::CoinbaseWssEvent,
) {
    use crate::sources::coinbase::events::CoinbaseWssEvent;

    match event {
        CoinbaseWssEvent::TradeData(data) => {
            // CoinbaseTradeData.time is ISO 8601 string → use timestamp_ms() helper.
            let trade = crate::trades::Trade {
                trade_ts: data.timestamp_ms(),
                symbol: data.product_id.clone(),
                side: data.side.clone(),
                amount: data.size.parse::<f64>().unwrap_or(0.0),
                price: data.price.parse::<f64>().unwrap_or(0.0),
                exchange: "coinbase".to_string(),
                id: data.trade_id.clone(),
            };
            sync.on_trade(trade);
        }
        CoinbaseWssEvent::OrderbookData(resp) => {
            // CoinbaseOrderbookResponse has:
            //   timestamp: String (ISO 8601)
            //   events: Vec<CoinbaseL2Event>
            // Each CoinbaseL2Event has product_id + updates: Vec<CoinbaseL2Update>.
            // Each update has side ("bid"/"offer"), price_level, new_quantity.
            let Some(event) = resp.events.first() else {
                return;
            };
            let ts_ms: u64 = chrono::DateTime::parse_from_rfc3339(&resp.timestamp)
                .map(|dt| dt.timestamp_millis() as u64)
                .unwrap_or(0);

            let mut bids = Vec::new();
            let mut asks = Vec::new();
            for (i, update) in event.updates.iter().enumerate() {
                let price = update.price_level.parse::<f64>().unwrap_or(0.0);
                let volume = update.new_quantity.parse::<f64>().unwrap_or(0.0);
                match update.side.as_str() {
                    "bid" => bids.push(Level::new(
                        i as u32, OrderSide::Bids, price, volume, vec![],
                    )),
                    "offer" | "ask" => asks.push(Level::new(
                        i as u32, OrderSide::Asks, price, volume, vec![],
                    )),
                    _ => {}
                }
            }

            let ob = crate::orderbooks::Orderbook {
                orderbook_id: 0,
                orderbook_ts: ts_ms,
                symbol: event.product_id.clone(),
                exchange: "coinbase".to_string(),
                bids,
                asks,
            };
            sync.on_orderbook(&event.product_id, ts_ms, ob);
        }
    }
}

fn feed_kraken(
    sync: &mut MarketSynchronizer,
    event: &crate::sources::kraken::events::KrakenWssEvent,
) {
    use crate::sources::kraken::events::KrakenWssEvent;

    match event {
        KrakenWssEvent::TradeData(data) => {
            // KrakenTradeData: price/qty are f64 (not strings).
            // timestamp is String → use timestamp_ms() helper.
            let trade = crate::trades::Trade {
                trade_ts: data.timestamp_ms(),
                symbol: data.symbol.clone(),
                side: data.side.clone(),
                amount: data.qty,
                price: data.price,
                exchange: "kraken".to_string(),
                id: data.trade_id.to_string(),
            };
            sync.on_trade(trade);
        }
        KrakenWssEvent::OrderbookData(resp) => {
            // KrakenBookResponse.data: Vec<KrakenBookData> — take first entry.
            // KrakenBookData has symbol, bids, asks, checksum, timestamp.
            // KrakenPriceLevel has price: f64, qty: f64.
            let Some(book) = resp.data.first() else {
                return;
            };
            let ts_ms: u64 = chrono::DateTime::parse_from_rfc3339(&book.timestamp)
                .map(|dt| dt.timestamp_millis() as u64)
                .unwrap_or(0);

            let ob = crate::orderbooks::Orderbook {
                orderbook_id: 0,
                orderbook_ts: ts_ms,
                symbol: book.symbol.clone(),
                exchange: "kraken".to_string(),
                bids: book
                    .bids
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32, OrderSide::Bids, level.price, level.qty, vec![],
                    ))
                    .collect(),
                asks: book
                    .asks
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32, OrderSide::Asks, level.price, level.qty, vec![],
                    ))
                    .collect(),
            };
            sync.on_orderbook(&book.symbol, ts_ms, ob);
        }
    }
}

fn feed_binance(
    sync: &mut MarketSynchronizer,
    symbol: &str,
    event: &crate::sources::binance::events::BinanceWssEvent,
) {
    use crate::sources::binance::events::BinanceWssEvent;

    match event {
        BinanceWssEvent::TradeData(data) => {
            let trade = crate::trades::Trade {
                trade_ts: data.trade_time,
                symbol: data.symbol.clone(),
                side: data.taker_side().to_string(),
                amount: data.quantity.parse::<f64>().unwrap_or(0.0),
                price: data.price.parse::<f64>().unwrap_or(0.0),
                exchange: "binance".to_string(),
                id: data.trade_id.to_string(),
            };
            sync.on_trade(trade);
        }
        BinanceWssEvent::DepthUpdate(upd) => {
            // After BookInitializer, delta events flow here.
            // Convert to Orderbook for the synchronizer.
            let ob = crate::orderbooks::Orderbook {
                orderbook_id: 0,
                orderbook_ts: upd.event_time,
                symbol: symbol.to_string(),
                exchange: "binance".to_string(),
                bids: upd
                    .bids
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32,
                        OrderSide::Bids,
                        level[0].parse::<f64>().unwrap_or(0.0),
                        level[1].parse::<f64>().unwrap_or(0.0),
                        vec![],
                    ))
                    .collect(),
                asks: upd
                    .asks
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32,
                        OrderSide::Asks,
                        level[0].parse::<f64>().unwrap_or(0.0),
                        level[1].parse::<f64>().unwrap_or(0.0),
                        vec![],
                    ))
                    .collect(),
            };
            sync.on_orderbook(symbol, upd.event_time, ob);
        }
        BinanceWssEvent::DepthSnapshot(snap) => {
            // Synthesised by BookInitializer from REST response.
            let ob = crate::orderbooks::Orderbook {
                orderbook_id: 0,
                orderbook_ts: 0, // REST snapshot has no timestamp
                symbol: symbol.to_string(),
                exchange: "binance".to_string(),
                bids: snap
                    .bids
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32,
                        OrderSide::Bids,
                        level[0].parse::<f64>().unwrap_or(0.0),
                        level[1].parse::<f64>().unwrap_or(0.0),
                        vec![],
                    ))
                    .collect(),
                asks: snap
                    .asks
                    .iter()
                    .enumerate()
                    .map(|(i, level)| Level::new(
                        i as u32,
                        OrderSide::Asks,
                        level[0].parse::<f64>().unwrap_or(0.0),
                        level[1].parse::<f64>().unwrap_or(0.0),
                        vec![],
                    ))
                    .collect(),
            };
            sync.on_orderbook(symbol, 0, ob);
        }
    }
}