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
//! Pluggable output sinks for worker event delivery.
//!
//! The `OutputSink` trait defines the interface that both `DataWorker` and
//! `MarketWorker` use to emit data.  Multiple sinks can be active
//! simultaneously via `OutputSinkSet`, which fans out every call.
//!
//! # Implemented sinks
//!
//! | Sink | Status | Description |
//! |------|--------|-------------|
//! | `ChannelSink` | Working | Wraps existing `TopicRegistry` broadcast channels |
//! | `TerminalSink` | Stub | Debug/tracing terminal output |
//! | `ParquetSink` | Working | Buffers `MarketSnapshot`s, decomposes and flushes to per-datatype Parquet files |
//!
//! # Adding a new sink
//!
//! 1. Implement `OutputSink` for your type.
//! 2. Add a variant to `OutputSinkConfig` in `config::workers::common`.
//! 3. Handle the new variant in `build_sinks()`.

use crate::config::workers::OutputSinkConfig;
use crate::snapshots::MarketSnapshot;
use crate::sources::ExchangeEvent;
use crate::workers::topic_publisher::{TopicMessage, TopicRegistry};

// ─────────────────────────────────────────────────────────────────────────────
// OutputSink trait
// ─────────────────────────────────────────────────────────────────────────────

/// A destination for worker output.
///
/// Workers call `emit_raw` for unsynchronised events and `emit_snapshot`
/// for grid-aligned snapshots.  Not every sink needs to support both —
/// the default implementations are no-ops.
pub trait OutputSink: Send + Sync {
    /// Emit a raw, unsynchronised event (used by `DataWorker`).
    fn emit_raw(
        &self,
        topic: &str,
        event: &ExchangeEvent,
        received_at_ns: u64,
    ) -> anyhow::Result<()> {
        let _ = (topic, event, received_at_ns);
        Ok(())
    }

    /// Emit a synchronised snapshot (used by `MarketWorker`).
    fn emit_snapshot(&self, snapshot: &MarketSnapshot) -> anyhow::Result<()> {
        let _ = snapshot;
        Ok(())
    }

    /// Flush any buffered data.  No-op for non-buffered sinks.
    fn flush(&self) -> anyhow::Result<()> {
        Ok(())
    }

    /// Human-readable sink name for logging.
    fn name(&self) -> &'static str;
}

// ─────────────────────────────────────────────────────────────────────────────
// ChannelSink — wraps existing TopicRegistry
// ─────────────────────────────────────────────────────────────────────────────

/// Publishes raw events to broadcast channels via [`TopicRegistry`].
///
/// This is the primary sink — it preserves the existing pub/sub
/// architecture where downstream consumers subscribe to specific
/// topics and receive `TopicMessage` clones.
pub struct ChannelSink {
    registry: TopicRegistry,
}

impl ChannelSink {
    /// Create a new channel sink wrapping the given registry.
    pub fn new(registry: TopicRegistry) -> Self {
        Self { registry }
    }

    /// Borrow the underlying registry (for downstream subscriptions).
    pub fn registry(&self) -> &TopicRegistry {
        &self.registry
    }
}

impl OutputSink for ChannelSink {
    fn emit_raw(
        &self,
        topic: &str,
        event: &ExchangeEvent,
        received_at_ns: u64,
    ) -> anyhow::Result<()> {
        let msg = TopicMessage {
            topic: topic.to_string(),
            received_at_ns,
            exchange: match event {
                ExchangeEvent::Bybit(_) => "bybit".to_string(),
                ExchangeEvent::Coinbase(_) => "coinbase".to_string(),
                ExchangeEvent::Kraken(_) => "kraken".to_string(),
                ExchangeEvent::Binance(_) => "binance".to_string(),
            },
            payload: event.clone(),
        };

        // Ignore publish errors (no receivers = best-effort).
        if let Err(e) = self.registry.publish(topic, msg) {
            tracing::warn!(topic = topic, error = %e, "channel_sink.publish_failed");
        }
        Ok(())
    }

    fn emit_snapshot(&self, _snapshot: &MarketSnapshot) -> anyhow::Result<()> {
        // TODO: publish snapshots to a dedicated "snapshots" topic.
        Ok(())
    }

    fn name(&self) -> &'static str {
        "channel"
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// TerminalSink — stub
// ─────────────────────────────────────────────────────────────────────────────

/// Prints events to the terminal via `tracing::debug!`.
///
/// **Status: stub** — emits a one-line summary per event.  Future versions
/// will support configurable verbosity and pretty-printing.
pub struct TerminalSink;

impl OutputSink for TerminalSink {
    fn emit_raw(
        &self,
        topic: &str,
        _event: &ExchangeEvent,
        received_at_ns: u64,
    ) -> anyhow::Result<()> {
        tracing::debug!(
            topic = topic,
            received_at_ns = received_at_ns,
            "terminal_sink.raw_event"
        );
        Ok(())
    }

    fn emit_snapshot(&self, snapshot: &MarketSnapshot) -> anyhow::Result<()> {
        tracing::debug!(
            ts_ns = snapshot.ts_ns,
            has_ob = snapshot.orderbook.is_some(),
            n_trades = snapshot.trades.len(),
            "terminal_sink.snapshot"
        );
        Ok(())
    }

    fn name(&self) -> &'static str {
        "terminal"
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// ParquetSink — snapshot buffering + Parquet flush
// ─────────────────────────────────────────────────────────────────────────────

/// Writes [`MarketSnapshot`]s to local Parquet files.
///
/// Snapshots are buffered in memory via [`OutputSink::emit_snapshot`] and flushed to
/// per-datatype Parquet files when [`OutputSink::flush`] is called.  The decomposition
/// logic mirrors [`crate::synchronizers::MarketSynchronizer`]: one subdirectory
/// per data type (`orderbooks/`, `trades/`, `liquidations/`, `fundings/`,
/// `open_interests/`), each containing timestamped Parquet files with Snappy
/// compression.
///
/// Raw events (`emit_raw`) are **not** persisted — converting exchange-
/// specific events to normalised types would duplicate the classifier logic.
/// Use the [`TerminalSink`] or [`ChannelSink`] for raw event output.
///
/// # Feature flag
///
/// Requires `--features parquet`.  When the feature is disabled the sink
/// compiles as a no-op stub.
#[cfg(feature = "parquet")]
pub struct ParquetSink {
    output_dir: String,
    snapshot_buffer: std::sync::Mutex<Vec<MarketSnapshot>>,
}

#[cfg(feature = "parquet")]
impl ParquetSink {
    /// Create a new Parquet sink writing to the given directory.
    pub fn new(output_dir: String) -> Self {
        Self {
            output_dir,
            snapshot_buffer: std::sync::Mutex::new(Vec::new()),
        }
    }
}

#[cfg(feature = "parquet")]
impl OutputSink for ParquetSink {
    fn emit_raw(
        &self,
        topic: &str,
        _event: &ExchangeEvent,
        _received_at_ns: u64,
    ) -> anyhow::Result<()> {
        tracing::trace!(topic = topic, "parquet_sink.emit_raw (raw events not persisted)");
        Ok(())
    }

    fn emit_snapshot(&self, snapshot: &MarketSnapshot) -> anyhow::Result<()> {
        let mut buf = self.snapshot_buffer.lock().unwrap();
        buf.push(snapshot.clone());
        Ok(())
    }

    fn flush(&self) -> anyhow::Result<()> {
        use crate::funding::io::funding_parquet::write_funding_parquet_timestamped;
        use crate::liquidations::io::liq_parquet::write_liquidations_parquet_timestamped;
        use crate::open_interest::io::oi_parquet::write_oi_parquet_timestamped;
        use crate::orderbooks::io::ob_parquet::write_ob_parquet;
        use crate::trades::io::trades_parquet::write_trades_parquet_timestamped;

        let mut buf = self.snapshot_buffer.lock().unwrap();
        if buf.is_empty() {
            return Ok(());
        }

        // Decompose snapshots into per-type vectors (same as MarketSynchronizer::flush_to_parquet).
        let mut orderbooks = Vec::new();
        let mut trades = Vec::new();
        let mut liquidations = Vec::new();
        let mut funding_rates = Vec::new();
        let mut open_interests = Vec::new();

        for snap in buf.iter() {
            if let Some(ob) = &snap.orderbook {
                orderbooks.push(ob.clone());
            }
            trades.extend(snap.trades.iter().cloned());
            liquidations.extend(snap.liquidations.iter().cloned());
            funding_rates.extend(snap.funding_rate.iter().cloned());
            open_interests.extend(snap.open_interest.iter().cloned());
        }

        let output_path = std::path::Path::new(&self.output_dir);

        if !orderbooks.is_empty() {
            let dir = output_path.join("orderbooks");
            std::fs::create_dir_all(&dir)?;
            let p = write_ob_parquet(&orderbooks, &dir, "sync")?;
            tracing::info!(path = %p.display(), n = orderbooks.len(), "parquet_sink.wrote_orderbooks");
        }
        if !trades.is_empty() {
            let dir = output_path.join("trades");
            std::fs::create_dir_all(&dir)?;
            let p = write_trades_parquet_timestamped(&trades, &dir, "sync")?;
            tracing::info!(path = %p.display(), n = trades.len(), "parquet_sink.wrote_trades");
        }
        if !liquidations.is_empty() {
            let dir = output_path.join("liquidations");
            std::fs::create_dir_all(&dir)?;
            let p = write_liquidations_parquet_timestamped(&liquidations, &dir, "sync")?;
            tracing::info!(path = %p.display(), n = liquidations.len(), "parquet_sink.wrote_liquidations");
        }
        if !funding_rates.is_empty() {
            let dir = output_path.join("fundings");
            std::fs::create_dir_all(&dir)?;
            let p = write_funding_parquet_timestamped(&funding_rates, &dir, "sync")?;
            tracing::info!(path = %p.display(), n = funding_rates.len(), "parquet_sink.wrote_funding");
        }
        if !open_interests.is_empty() {
            let dir = output_path.join("open_interests");
            std::fs::create_dir_all(&dir)?;
            let p = write_oi_parquet_timestamped(&open_interests, &dir, "sync")?;
            tracing::info!(path = %p.display(), n = open_interests.len(), "parquet_sink.wrote_oi");
        }

        let n = buf.len();
        buf.clear();

        tracing::info!(
            dir = self.output_dir.as_str(),
            snapshot_count = n,
            "parquet_sink.flushed"
        );

        Ok(())
    }

    fn name(&self) -> &'static str {
        "parquet"
    }
}

// Stub when the `parquet` feature is not enabled.
#[cfg(not(feature = "parquet"))]
pub struct ParquetSink {
    #[allow(dead_code)]
    output_dir: String,
}

#[cfg(not(feature = "parquet"))]
impl ParquetSink {
    /// Create a new Parquet sink (no-op without the `parquet` feature).
    pub fn new(output_dir: String) -> Self {
        Self { output_dir }
    }
}

#[cfg(not(feature = "parquet"))]
impl OutputSink for ParquetSink {
    fn emit_raw(
        &self,
        topic: &str,
        _event: &ExchangeEvent,
        _received_at_ns: u64,
    ) -> anyhow::Result<()> {
        tracing::trace!(topic = topic, "parquet_sink.raw_event (parquet feature not enabled)");
        Ok(())
    }

    fn emit_snapshot(&self, _snapshot: &MarketSnapshot) -> anyhow::Result<()> {
        tracing::trace!("parquet_sink.snapshot (parquet feature not enabled)");
        Ok(())
    }

    fn name(&self) -> &'static str {
        "parquet"
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// OutputSinkSet — fan-out wrapper
// ─────────────────────────────────────────────────────────────────────────────

/// Fan-out wrapper that delegates every call to all contained sinks.
pub struct OutputSinkSet {
    sinks: Vec<Box<dyn OutputSink>>,
}

impl OutputSinkSet {
    /// Create an empty sink set.
    pub fn new() -> Self {
        Self { sinks: Vec::new() }
    }

    /// Add a sink to the set.
    pub fn push(&mut self, sink: Box<dyn OutputSink>) {
        self.sinks.push(sink);
    }

    /// Number of sinks in the set.
    pub fn len(&self) -> usize {
        self.sinks.len()
    }

    /// Whether the set is empty.
    pub fn is_empty(&self) -> bool {
        self.sinks.is_empty()
    }

    /// Emit a raw event to all sinks.
    pub fn emit_raw(
        &self,
        topic: &str,
        event: &ExchangeEvent,
        received_at_ns: u64,
    ) -> anyhow::Result<()> {
        for sink in &self.sinks {
            if let Err(e) = sink.emit_raw(topic, event, received_at_ns) {
                tracing::warn!(
                    sink = sink.name(),
                    topic = topic,
                    error = %e,
                    "output_sink_set.emit_raw_failed"
                );
            }
        }
        Ok(())
    }

    /// Emit a snapshot to all sinks.
    pub fn emit_snapshot(&self, snapshot: &MarketSnapshot) -> anyhow::Result<()> {
        for sink in &self.sinks {
            if let Err(e) = sink.emit_snapshot(snapshot) {
                tracing::warn!(
                    sink = sink.name(),
                    error = %e,
                    "output_sink_set.emit_snapshot_failed"
                );
            }
        }
        Ok(())
    }

    /// Flush all sinks.
    pub fn flush(&self) -> anyhow::Result<()> {
        for sink in &self.sinks {
            if let Err(e) = sink.flush() {
                tracing::warn!(
                    sink = sink.name(),
                    error = %e,
                    "output_sink_set.flush_failed"
                );
            }
        }
        Ok(())
    }
}

impl Default for OutputSinkSet {
    fn default() -> Self {
        Self::new()
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Factory
// ─────────────────────────────────────────────────────────────────────────────

/// Build an [`OutputSinkSet`] from config entries.
///
/// The `registry` parameter is consumed by the `Channel` sink — pass
/// `None` if no channel sink is configured (the function will create
/// a dummy registry if Channel is requested but none is provided).
pub fn build_sinks(
    configs: &[OutputSinkConfig],
    registry: Option<TopicRegistry>,
) -> OutputSinkSet {
    let mut set = OutputSinkSet::new();
    let mut registry = registry;

    for config in configs {
        match config {
            OutputSinkConfig::Channel => {
                if let Some(reg) = registry.take() {
                    tracing::info!("output.channel_sink_created");
                    set.push(Box::new(ChannelSink::new(reg)));
                } else {
                    tracing::warn!("output.channel_sink_requested_but_no_registry");
                }
            }
            OutputSinkConfig::Terminal => {
                tracing::info!("output.terminal_sink_created");
                set.push(Box::new(TerminalSink));
            }
            OutputSinkConfig::Parquet { dir } => {
                tracing::info!(dir = dir.as_str(), "output.parquet_sink_created");
                set.push(Box::new(ParquetSink::new(dir.clone())));
            }
        }
    }

    set
}