chapaty 1.1.2

An event-driven Rust engine for building and evaluating quantitative trading agents. Features a Gym-style API for algorithmic backtesting and reinforcement learning.
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
use std::{fs, path::Path};

use polars::{
    frame::DataFrame,
    io::cloud::CloudOptions,
    prelude::{
        CsvWriterOptions, IntoLazy, LazyFrame, ParquetWriteOptions, PlPath, SchemaRef, SinkOptions,
        SinkTarget,
    },
};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use strum::{Display, EnumIter, EnumString, IntoStaticStr};
use tonic::async_trait;

use crate::{
    error::{ChapatyError, ChapatyResult, DataError, IoError, SystemError},
    report::polars_ext::{DataFrameExt, LazyFrameExt},
};

// ================================================================================================
// Core Types & Configurations
// ================================================================================================

/// Defines the target export format and holds specific Polars writing options.
#[derive(Debug, Clone)]
pub enum ExportFormat {
    Csv(CsvWriterOptions),
    Parquet(ParquetWriteOptions),
}

impl Default for ExportFormat {
    fn default() -> Self {
        Self::Csv(CsvWriterOptions::default())
    }
}

/// Configuration for exporting reports to the local file system.
#[derive(Debug, Clone)]
pub struct FileConfig<'a> {
    pub dir: &'a Path,
    pub file_stem: Option<String>,
    pub format: ExportFormat,
    pub sink_opts: SinkOptions,
}

impl Default for FileConfig<'_> {
    fn default() -> Self {
        Self {
            dir: Path::new("./chapaty/reports"),
            file_stem: None,
            format: ExportFormat::default(),
            sink_opts: SinkOptions::default(),
        }
    }
}

impl<'a> FileConfig<'a> {
    pub fn with_dir(self, dir: &'a Path) -> Self {
        Self { dir, ..self }
    }

    pub fn with_file_stem(self, file_stem: impl Into<String>) -> Self {
        Self {
            file_stem: Some(file_stem.into()),
            ..self
        }
    }

    pub fn with_format(self, format: ExportFormat) -> Self {
        Self { format, ..self }
    }

    pub fn with_sink_opts(self, sink_opts: SinkOptions) -> Self {
        Self { sink_opts, ..self }
    }
}

/// Configuration for exporting reports to cloud storage (GCS, S3, Azure).
///
/// # Important: Full URIs Required
/// To prevent URL malformation, `CloudConfig` requires the **complete URI**, including
/// the file name and extension (e.g., `gs://bucket/path/to/my_report.csv`).
/// Do not pass a directory URI.
#[derive(Debug, Clone)]
pub struct CloudConfig<'a> {
    pub uri: &'a str,
    pub format: ExportFormat,
    pub cloud_opts: CloudOptions,
    pub sink_opts: SinkOptions,
}

impl<'a> CloudConfig<'a> {
    /// Creates a new `CloudConfig` targeting a specific, complete Cloud URI.
    pub fn new(uri: &'a str) -> Self {
        Self {
            uri,
            format: ExportFormat::default(),
            cloud_opts: CloudOptions::default(),
            sink_opts: SinkOptions::default(),
        }
    }

    pub fn with_format(self, format: ExportFormat) -> Self {
        Self { format, ..self }
    }

    pub fn with_cloud_opts(self, cloud_opts: CloudOptions) -> Self {
        Self { cloud_opts, ..self }
    }

    pub fn with_sink_opts(self, sink_opts: SinkOptions) -> Self {
        Self { sink_opts, ..self }
    }
}

// ================================================================================================
// Traits
// ================================================================================================

/// Defines a common interface for all Report types (Journal, TradeStats, etc.).
pub trait Report {
    /// Access the underlying DataFrame (Immutable).
    fn as_df(&self) -> &DataFrame;

    /// Access the underlying DataFrame (Mutable).
    fn as_df_mut(&mut self) -> &mut DataFrame;
}

pub trait ReportName {
    fn base_name(&self) -> String;
}

pub trait ToSchema {
    /// Returns the canonical schema for this report type.
    fn to_schema() -> SchemaRef;
}

pub trait AsFormattedLazyFrame {
    fn as_formatted_lf(&self) -> LazyFrame;
}

pub trait ToJson {
    /// Serializes the report to a generic JSON Value.
    /// Returns a `Value::Array` containing row objects.
    fn to_json(&self) -> ChapatyResult<serde_json::Value>;
}

pub trait ExportSync {
    /// Writes the report to a local file system (blocking).
    ///
    /// - Creates directories if they do not exist.
    /// - Automatically generates a file name if one is not provided in `FileConfig`.
    fn to_file_sync(&self, config: &FileConfig<'_>) -> ChapatyResult<()>;
}

#[async_trait]
pub trait Export {
    /// Streams the report to a cloud bucket using Polars' streaming engine.
    async fn to_cloud(&self, config: CloudConfig<'_>) -> ChapatyResult<()>;
}

// ================================================================================================
// Blanket Implementations
// ================================================================================================

impl<T> AsFormattedLazyFrame for T
where
    T: Report + ToSchema,
{
    fn as_formatted_lf(&self) -> LazyFrame {
        self.as_df()
            .clone()
            .lazy()
            .with_human_durations(T::to_schema())
    }
}

impl<T> ToJson for T
where
    T: Report + ToSchema,
{
    fn to_json(&self) -> ChapatyResult<serde_json::Value> {
        let rows = self
            .as_formatted_lf()
            .collect()
            .map_err(|e| ChapatyError::Data(DataError::DataFrame(e.to_string())))?
            .to_json_rows()?;
        Ok(Value::Array(rows.into_iter().map(Value::Object).collect()))
    }
}

impl<T> ExportSync for T
where
    T: Report + ReportName + ToSchema + Sync + Send,
{
    fn to_file_sync(&self, config: &FileConfig<'_>) -> ChapatyResult<()> {
        let ext: FileExtension = (&config.format).into();
        let filename = match &config.file_stem {
            Some(stem) => format!("{stem}.{ext}"),
            None => format!("{}.{ext}", self.base_name()),
        };
        let file_path = config.dir.join(&filename);

        if !config.dir.exists() {
            fs::create_dir_all(config.dir).map_err(|e| {
                IoError::FileSystem(format!(
                    "Failed to create directory {}: {}",
                    config.dir.display(),
                    e
                ))
            })?;
        }

        let uri = file_path.to_str().ok_or_else(|| {
            IoError::FileSystem(format!(
                "Path contains invalid UTF-8 characters: {}",
                file_path.display()
            ))
        })?;

        let target = SinkTarget::Path(PlPath::new(uri));
        let sink_opts = &config.sink_opts;
        let lf = self.as_formatted_lf();

        let sink_plan = match &config.format {
            ExportFormat::Csv(opts) => lf
                .sink_csv(target, opts.clone(), None, sink_opts.clone())
                .map_err(|e| DataError::DataFrame(format!("Failed to build CSV sink plan: {e}"))),
            ExportFormat::Parquet(opts) => lf
                .sink_parquet(target, opts.clone(), None, sink_opts.clone())
                .map_err(|e| {
                    DataError::DataFrame(format!("Failed to build Parquet sink plan: {e}"))
                }),
        }?;

        let _ = sink_plan.collect().map_err(|e| {
            DataError::DataFrame(format!(
                "Failed to write file to '{}': {e}",
                file_path.display()
            ))
        })?;

        Ok(())
    }
}

#[async_trait]
impl<T> Export for T
where
    T: Report + ReportName + ToSchema + Sync + Send,
{
    async fn to_cloud(&self, config: CloudConfig<'_>) -> ChapatyResult<()> {
        let lf = self.as_formatted_lf();
        let target = SinkTarget::Path(PlPath::new(config.uri));
        let cloud_opts = config.cloud_opts;
        let sink_opts = config.sink_opts;
        let format = config.format;

        // Clone URI to move into the blocking task safely
        let uri_string = config.uri.to_string();

        tokio::task::spawn_blocking(move || {
            let sink_plan = match format {
                ExportFormat::Csv(opts) => lf
                    .sink_csv(target, opts, Some(cloud_opts), sink_opts)
                    .map_err(|e| {
                        DataError::DataFrame(format!("Failed to build Cloud CSV plan: {e}"))
                    }),
                ExportFormat::Parquet(opts) => lf
                    .with_new_streaming(true)
                    .sink_parquet(target, opts, Some(cloud_opts), sink_opts)
                    .map_err(|e| {
                        DataError::DataFrame(format!("Failed to build Cloud Parquet plan: {e}"))
                    }),
            }?;

            let _ = sink_plan.collect().map_err(|e| {
                DataError::DataFrame(format!("Streaming upload failed to '{}': {e}", uri_string))
            })?;

            Ok(())
        })
        .await
        .map_err(|e| SystemError::Generic(format!("Task Join Error: {e}")))?
    }
}

// ================================================================================================
// Helpers
// ================================================================================================

#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    Serialize,
    Deserialize,
    EnumString,
    Display,
    EnumIter,
    IntoStaticStr,
)]
#[strum(serialize_all = "lowercase")]
enum FileExtension {
    Csv,
    Parquet,
}

impl From<&ExportFormat> for FileExtension {
    fn from(format: &ExportFormat) -> Self {
        match format {
            ExportFormat::Csv(_) => Self::Csv,
            ExportFormat::Parquet(_) => Self::Parquet,
        }
    }
}

impl From<ExportFormat> for FileExtension {
    fn from(format: ExportFormat) -> Self {
        (&format).into()
    }
}

/// Generates a base name dynamically based on the presence of grouping columns.
///
/// # Logic
/// 1. Scans the DataFrame column names.
/// 2. Filters for columns starting with `__` (the `GroupCol` prefix).
/// 3. Strips the prefix to get clean names (e.g., `__symbol` -> `symbol`).
/// 4. Joins them to form a prefix for the file.
///
/// # Example
/// - No groups: `cumulative_returns`
/// - Grouped by Symbol: `symbol_cumulative_returns`
/// - Grouped by Symbol & Year: `symbol_entry_year_cumulative_returns`
pub(crate) fn generate_dynamic_base_name(df: &DataFrame, base_name: &str) -> String {
    let group_keys = df
        .get_column_names()
        .iter()
        .filter_map(|name| {
            if name.starts_with("__") {
                Some(name.strip_prefix("__").unwrap_or(name))
            } else {
                None
            }
        })
        .collect::<Vec<_>>();

    if group_keys.is_empty() {
        base_name.to_string()
    } else {
        let prefix = group_keys.join("_");
        format!("{}_{}", prefix, base_name)
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use polars::{
        df,
        prelude::{LazyCsvReader, LazyFileListReader},
    };

    use crate::{data::common::RiskMetricsConfig, report::journal::Journal};

    use super::*;

    #[test]
    fn test_generate_dynamic_base_name() {
        // 1. Case: No Groups (Plain)
        let df_plain = df![
            "pnl" => &[100.0],
            "count" => &[5]
        ]
        .expect("Failed to create plain DF");

        let name_plain = generate_dynamic_base_name(&df_plain, "stats");
        assert_eq!(name_plain, "stats");

        // 2. Case: Single Group (Symbol)
        let df_symbol = df![
            "__symbol" => &["BTC"],
            "pnl" => &[100.0]
        ]
        .expect("Failed to create symbol DF");

        let name_symbol = generate_dynamic_base_name(&df_symbol, "stats");
        assert_eq!(name_symbol, "symbol_stats");

        // 3. Case: Multi Group (Symbol + Year)
        let df_multi = df![
            "__symbol" => &["BTC"],
            "__entry_year" => &[2023],
            "pnl" => &[100.0]
        ]
        .expect("Failed to create multi DF");

        let name_multi = generate_dynamic_base_name(&df_multi, "stats");
        assert_eq!(name_multi, "symbol_entry_year_stats");
    }

    #[test]
    fn test_to_json_rows() {
        let manifest_dir = env!("CARGO_MANIFEST_DIR");
        let pb = PathBuf::from(manifest_dir).join("tests/fixtures/report/input/journal.csv");
        let path = PlPath::new(
            pb.as_os_str()
                .to_str()
                .expect("failed to convert input file path to string"),
        );

        let schema = Journal::to_schema();
        let df = LazyCsvReader::new(path)
            .with_has_header(true)
            .with_schema(Some(schema.clone()))
            .with_try_parse_dates(true)
            .finish()
            .expect("failed to create LazyFrame from CSV")
            .collect()
            .expect("failed to collect DataFrame from LazyFrame");

        let journal = Journal::new(df, RiskMetricsConfig::default())
            .expect("failed to create Journal from DataFrame");

        let have = journal
            .to_json()
            .expect("failed to serialize journal to JSON");

        let expected_path =
            PathBuf::from(manifest_dir).join("tests/fixtures/report/expected/journal.json");
        let want =
            std::fs::read_to_string(&expected_path).expect("failed to read expected JSON file");
        let want_value: serde_json::Value =
            serde_json::from_str(&want).expect("failed to parse expected JSON file");

        assert_eq!(have, want_value, "JSON output does not match expected");
    }
}