mockforge-reporting 0.3.136

Report generation and visualization for MockForge
Documentation
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
//! CSV export for reports and metrics

use crate::comparison::ComparisonReport;
use crate::pdf::{ExecutionReport, ReportMetrics};
use crate::Result;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::Write;

/// CSV export configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CsvExportConfig {
    pub delimiter: char,
    pub include_headers: bool,
    pub quote_strings: bool,
}

impl Default for CsvExportConfig {
    fn default() -> Self {
        Self {
            delimiter: ',',
            include_headers: true,
            quote_strings: true,
        }
    }
}

/// CSV exporter
pub struct CsvExporter {
    config: CsvExportConfig,
}

impl CsvExporter {
    /// Create a new CSV exporter
    pub fn new(config: CsvExportConfig) -> Self {
        Self { config }
    }

    /// Export execution report to CSV
    pub fn export_execution_report(
        &self,
        report: &ExecutionReport,
        output_path: &str,
    ) -> Result<()> {
        let mut file = File::create(output_path)?;

        // Write header
        if self.config.include_headers {
            writeln!(
                file,
                "orchestration_name,start_time,end_time,duration_seconds,status,total_steps,completed_steps,failed_steps,total_requests,successful_requests,failed_requests,error_rate,avg_latency_ms,p95_latency_ms,p99_latency_ms"
            )?;
        }

        // Write data row
        writeln!(
            file,
            "{},{},{},{},{},{},{},{},{},{},{},{:.4},{:.2},{:.2},{:.2}",
            self.quote_if_needed(&report.orchestration_name),
            report.start_time.to_rfc3339(),
            report.end_time.to_rfc3339(),
            report.duration_seconds,
            self.quote_if_needed(&report.status),
            report.total_steps,
            report.completed_steps,
            report.failed_steps,
            report.metrics.total_requests,
            report.metrics.successful_requests,
            report.metrics.failed_requests,
            report.metrics.error_rate,
            report.metrics.avg_latency_ms,
            report.metrics.p95_latency_ms,
            report.metrics.p99_latency_ms,
        )?;

        Ok(())
    }

    /// Export multiple execution reports to CSV
    pub fn export_execution_reports(
        &self,
        reports: &[ExecutionReport],
        output_path: &str,
    ) -> Result<()> {
        let mut file = File::create(output_path)?;

        // Write header
        if self.config.include_headers {
            writeln!(
                file,
                "orchestration_name,start_time,end_time,duration_seconds,status,total_steps,completed_steps,failed_steps,total_requests,successful_requests,failed_requests,error_rate,avg_latency_ms,p95_latency_ms,p99_latency_ms"
            )?;
        }

        // Write data rows
        for report in reports {
            writeln!(
                file,
                "{},{},{},{},{},{},{},{},{},{},{},{:.4},{:.2},{:.2},{:.2}",
                self.quote_if_needed(&report.orchestration_name),
                report.start_time.to_rfc3339(),
                report.end_time.to_rfc3339(),
                report.duration_seconds,
                self.quote_if_needed(&report.status),
                report.total_steps,
                report.completed_steps,
                report.failed_steps,
                report.metrics.total_requests,
                report.metrics.successful_requests,
                report.metrics.failed_requests,
                report.metrics.error_rate,
                report.metrics.avg_latency_ms,
                report.metrics.p95_latency_ms,
                report.metrics.p99_latency_ms,
            )?;
        }

        Ok(())
    }

    /// Export comparison report to CSV
    pub fn export_comparison_report(
        &self,
        report: &ComparisonReport,
        output_path: &str,
    ) -> Result<()> {
        let mut file = File::create(output_path)?;

        // Write header
        if self.config.include_headers {
            writeln!(
                file,
                "metric_name,baseline_value,comparison_value,absolute_difference,percentage_difference,direction,significance"
            )?;
        }

        // Write metric differences
        for diff in &report.metric_differences {
            writeln!(
                file,
                "{},{:.4},{:.4},{:.4},{:.2},{:?},{:?}",
                self.quote_if_needed(&diff.metric_name),
                diff.baseline_value,
                diff.comparison_value,
                diff.absolute_difference,
                diff.percentage_difference,
                diff.direction,
                diff.significance,
            )?;
        }

        Ok(())
    }

    /// Export metrics time series to CSV
    pub fn export_metrics_time_series(
        &self,
        metrics: &[(i64, ReportMetrics)],
        output_path: &str,
    ) -> Result<()> {
        let mut file = File::create(output_path)?;

        // Write header
        if self.config.include_headers {
            writeln!(
                file,
                "timestamp,total_requests,successful_requests,failed_requests,error_rate,avg_latency_ms,p95_latency_ms,p99_latency_ms"
            )?;
        }

        // Write time series data
        for (timestamp, metric) in metrics {
            writeln!(
                file,
                "{},{},{},{},{:.4},{:.2},{:.2},{:.2}",
                timestamp,
                metric.total_requests,
                metric.successful_requests,
                metric.failed_requests,
                metric.error_rate,
                metric.avg_latency_ms,
                metric.p95_latency_ms,
                metric.p99_latency_ms,
            )?;
        }

        Ok(())
    }

    /// Export regressions to CSV
    pub fn export_regressions(&self, report: &ComparisonReport, output_path: &str) -> Result<()> {
        let mut file = File::create(output_path)?;

        // Write header
        if self.config.include_headers {
            writeln!(
                file,
                "metric_name,baseline_value,regressed_value,impact_percentage,severity,description"
            )?;
        }

        // Write regressions
        for regression in &report.regressions {
            writeln!(
                file,
                "{},{:.4},{:.4},{:.2},{},{}",
                self.quote_if_needed(&regression.metric_name),
                regression.baseline_value,
                regression.regressed_value,
                regression.impact_percentage,
                self.quote_if_needed(&regression.severity),
                self.quote_if_needed(&regression.description),
            )?;
        }

        Ok(())
    }

    /// Export improvements to CSV
    pub fn export_improvements(&self, report: &ComparisonReport, output_path: &str) -> Result<()> {
        let mut file = File::create(output_path)?;

        // Write header
        if self.config.include_headers {
            writeln!(
                file,
                "metric_name,baseline_value,improved_value,improvement_percentage,description"
            )?;
        }

        // Write improvements
        for improvement in &report.improvements {
            writeln!(
                file,
                "{},{:.4},{:.4},{:.2},{}",
                self.quote_if_needed(&improvement.metric_name),
                improvement.baseline_value,
                improvement.improved_value,
                improvement.improvement_percentage,
                self.quote_if_needed(&improvement.description),
            )?;
        }

        Ok(())
    }

    /// Quote string if configuration requires it
    fn quote_if_needed(&self, s: &str) -> String {
        if self.config.quote_strings {
            format!("\"{}\"", s.replace('"', "\"\""))
        } else {
            s.to_string()
        }
    }
}

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

/// CSV batch exporter for multiple reports
pub struct CsvBatchExporter {
    exporter: CsvExporter,
}

impl CsvBatchExporter {
    /// Create a new batch exporter
    pub fn new(config: CsvExportConfig) -> Self {
        Self {
            exporter: CsvExporter::new(config),
        }
    }

    /// Export all reports to a directory
    pub fn export_all(
        &self,
        execution_reports: &[ExecutionReport],
        comparison_report: Option<&ComparisonReport>,
        output_dir: &str,
    ) -> Result<()> {
        // Create output directory if it doesn't exist
        std::fs::create_dir_all(output_dir)?;

        // Export execution reports
        let exec_path = format!("{}/execution_reports.csv", output_dir);
        self.exporter.export_execution_reports(execution_reports, &exec_path)?;

        // Export comparison report if available
        if let Some(comparison) = comparison_report {
            let comp_path = format!("{}/comparison.csv", output_dir);
            self.exporter.export_comparison_report(comparison, &comp_path)?;

            let reg_path = format!("{}/regressions.csv", output_dir);
            self.exporter.export_regressions(comparison, &reg_path)?;

            let imp_path = format!("{}/improvements.csv", output_dir);
            self.exporter.export_improvements(comparison, &imp_path)?;
        }

        Ok(())
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use tempfile::tempdir;

    #[test]
    fn test_csv_export_execution_report() {
        let config = CsvExportConfig::default();
        let exporter = CsvExporter::new(config);

        let report = ExecutionReport {
            orchestration_name: "test-orch".to_string(),
            start_time: Utc::now(),
            end_time: Utc::now(),
            duration_seconds: 120,
            status: "Completed".to_string(),
            total_steps: 5,
            completed_steps: 5,
            failed_steps: 0,
            metrics: ReportMetrics {
                total_requests: 1000,
                successful_requests: 980,
                failed_requests: 20,
                avg_latency_ms: 125.5,
                p95_latency_ms: 250.0,
                p99_latency_ms: 350.0,
                error_rate: 0.02,
            },
            failures: vec![],
            recommendations: vec![],
        };

        let temp_dir = tempdir().unwrap();
        let output_path = temp_dir.path().join("report.csv");

        let result = exporter.export_execution_report(&report, output_path.to_str().unwrap());
        assert!(result.is_ok());
        assert!(output_path.exists());

        // Verify content
        let content = std::fs::read_to_string(output_path).unwrap();
        assert!(content.contains("orchestration_name"));
        assert!(content.contains("test-orch"));
    }

    #[test]
    fn test_csv_export_multiple_reports() {
        let config = CsvExportConfig::default();
        let exporter = CsvExporter::new(config);

        let reports = vec![
            ExecutionReport {
                orchestration_name: "test-1".to_string(),
                start_time: Utc::now(),
                end_time: Utc::now(),
                duration_seconds: 100,
                status: "Completed".to_string(),
                total_steps: 3,
                completed_steps: 3,
                failed_steps: 0,
                metrics: ReportMetrics {
                    total_requests: 500,
                    successful_requests: 490,
                    failed_requests: 10,
                    avg_latency_ms: 100.0,
                    p95_latency_ms: 200.0,
                    p99_latency_ms: 300.0,
                    error_rate: 0.02,
                },
                failures: vec![],
                recommendations: vec![],
            },
            ExecutionReport {
                orchestration_name: "test-2".to_string(),
                start_time: Utc::now(),
                end_time: Utc::now(),
                duration_seconds: 150,
                status: "Completed".to_string(),
                total_steps: 4,
                completed_steps: 4,
                failed_steps: 0,
                metrics: ReportMetrics {
                    total_requests: 750,
                    successful_requests: 740,
                    failed_requests: 10,
                    avg_latency_ms: 110.0,
                    p95_latency_ms: 220.0,
                    p99_latency_ms: 320.0,
                    error_rate: 0.013,
                },
                failures: vec![],
                recommendations: vec![],
            },
        ];

        let temp_dir = tempdir().unwrap();
        let output_path = temp_dir.path().join("reports.csv");

        let result = exporter.export_execution_reports(&reports, output_path.to_str().unwrap());
        assert!(result.is_ok());

        let content = std::fs::read_to_string(output_path).unwrap();
        let lines: Vec<&str> = content.lines().collect();
        assert_eq!(lines.len(), 3); // Header + 2 data rows
    }
}