turbovault-export 1.4.0

Reporting and export utilities for TurboVault Server
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
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
//! # Export System
//!
//! Provides data export functionality for vault analysis in multiple formats (JSON, CSV).
//! Enables downstream processing and reporting of vault metrics and analysis.
//!
//! ## Quick Start
//!
//! ```no_run
//! use turbovault_export::{create_health_report, HealthReportExporter};
//!
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a health report
//! let report = create_health_report(
//!     "my-vault",
//!     85,        // health score
//!     100,       // total notes
//!     150,       // total links
//!     2,         // broken links
//!     5,         // orphaned notes
//! );
//!
//! // Export as JSON
//! let json = HealthReportExporter::to_json(&report)?;
//! println!("JSON:\n{}", json);
//!
//! // Export as CSV
//! let csv = HealthReportExporter::to_csv(&report)?;
//! println!("CSV:\n{}", csv);
//! # Ok(())
//! # }
//! ```
//!
//! ## Export Formats
//!
//! The system supports two formats for all exporters:
//!
//! ### JSON Export
//! - Pretty-printed for readability
//! - Full structure preserved
//! - Suitable for API responses
//! - Nested arrays and objects supported
//!
//! ### CSV Export
//! - Flat structure (all values in one row)
//! - Header row included
//! - Quoted fields for safety
//! - Suitable for spreadsheets and databases
//!
//! ## Core Exporters
//!
//! ### HealthReportExporter
//!
//! Exports vault health analysis:
//! - Health score (0-100)
//! - Connected vs orphaned notes
//! - Broken link count
//! - Connectivity and link density metrics
//! - Status and recommendations
//!
//! ### BrokenLinksExporter
//!
//! Exports broken link analysis:
//! - Source file for each broken link
//! - Target that could not be resolved
//! - Line number in source file
//! - Suggested fixes
//!
//! ### VaultStatsExporter
//!
//! Exports vault statistics:
//! - Timestamp of analysis
//! - Total files and links
//! - Orphaned file count
//! - Average links per file
//!
//! ### AnalysisReportExporter
//!
//! Exports comprehensive analysis combining:
//! - Health report
//! - Broken links data
//! - Recommendations
//! - Full analysis context
//!
//! ## Data Models
//!
//! ### Health Metrics
//!
//! ```ignore
//! #[derive(Serialize, Deserialize)]
//! pub struct HealthReport {
//!     pub timestamp: String,
//!     pub vault_name: String,
//!     pub health_score: u8,
//!     pub total_notes: usize,
//!     pub total_links: usize,
//!     pub broken_links: usize,
//!     pub orphaned_notes: usize,
//!     pub connectivity_rate: f64,
//!     pub link_density: f64,
//!     pub status: String,
//!     pub recommendations: Vec<String>,
//! }
//! ```
//!
//! ### Broken Links
//!
//! Each broken link includes:
//! - Source file path
//! - Target reference (failed to resolve)
//! - Line number
//! - Suggested alternatives
//!
//! ## Integration with Analysis
//!
//! Export data is typically generated from:
//! - `turbovault_graph` health analysis (see <https://docs.rs/turbovault-graph>)
//! - `turbovault_tools` analysis tools (see <https://docs.rs/turbovault-tools>)
//! - Vault statistics computed at runtime
//!
//! Example integration:
//! ```no_run
//! use turbovault_export::{create_health_report, HealthReportExporter};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Get health from graph analysis
//! // let health = graph.analyze_health().await?;
//!
//! // Create report from health data
//! let report = create_health_report(
//!     "vault",
//!     80,
//!     50,
//!     100,
//!     1,
//!     2,
//! );
//!
//! // Export in desired format
//! // let json = HealthReportExporter::to_json(&report)?;
//! # Ok(())
//! # }
//! ```
//!
//! ## File I/O Patterns
//!
//! Exporters return strings (JSON or CSV). To save to files:
//!
//! ```ignore
//! use std::fs;
//!
//! let report = create_health_report(...);
//! let json = HealthReportExporter::to_json(&report)?;
//! fs::write("health-report.json", json)?;
//! ```
//!
//! ## Performance Considerations
//!
//! - JSON serialization is optimized with `serde`
//! - CSV generation uses string formatting (fast)
//! - All exporters run in-memory
//! - No I/O operations within exporters
//! - Suitable for batch processing large datasets

use chrono::Utc;
use serde::{Deserialize, Serialize};
use turbovault_core::prelude::*;
use turbovault_core::to_json_string;

/// Export format options
#[derive(Debug, Clone, Copy)]
pub enum ExportFormat {
    /// JSON format (pretty-printed)
    Json,
    /// CSV format (flattened)
    Csv,
}

/// Health report for export
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthReport {
    pub timestamp: String,
    pub vault_name: String,
    pub health_score: u8,
    pub total_notes: usize,
    pub total_links: usize,
    pub broken_links: usize,
    pub orphaned_notes: usize,
    pub connectivity_rate: f64,
    pub link_density: f64,
    pub status: String,
    pub recommendations: Vec<String>,
}

/// Broken link record for export
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrokenLinkRecord {
    pub source_file: String,
    pub target: String,
    pub line: usize,
    pub suggestions: Vec<String>,
}

/// Vault statistics for export
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VaultStatsRecord {
    pub timestamp: String,
    pub vault_name: String,
    pub total_files: usize,
    pub total_links: usize,
    pub orphaned_files: usize,
    pub average_links_per_file: f64,
    /// Total word count across all notes (plain text, excludes markdown syntax)
    #[serde(default)]
    pub total_words: usize,
    /// Total character count across all notes (plain text)
    #[serde(default)]
    pub total_readable_chars: usize,
    /// Average words per note
    #[serde(default)]
    pub avg_words_per_note: f64,
}

/// Full analysis report combining multiple metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisReport {
    pub timestamp: String,
    pub vault_name: String,
    pub health: HealthReport,
    pub broken_links_count: usize,
    pub orphaned_notes_count: usize,
    pub recommendations: Vec<String>,
}

/// Escape a value for safe CSV output.
///
/// Always wraps in double-quotes and escapes internal double-quotes by doubling
/// them (RFC 4180). Prefixes formula-triggering characters (`=`, `+`, `-`, `@`)
/// with a single-quote to prevent DDE/formula injection in spreadsheets.
fn csv_escape(value: &str) -> String {
    let safe_value = if value.starts_with('=')
        || value.starts_with('+')
        || value.starts_with('-')
        || value.starts_with('@')
    {
        format!("'{}", value)
    } else {
        value.to_string()
    };
    format!("\"{}\"", safe_value.replace('"', "\"\""))
}

/// Health report exporter
pub struct HealthReportExporter;

impl HealthReportExporter {
    /// Export health report as JSON
    pub fn to_json(report: &HealthReport) -> Result<String> {
        to_json_string(report, "health report")
    }

    /// Export health report as CSV (single row)
    pub fn to_csv(report: &HealthReport) -> Result<String> {
        let csv = format!(
            "timestamp,vault_name,health_score,total_notes,total_links,broken_links,orphaned_notes,connectivity_rate,link_density,status\n\
             {},{},{},{},{},{},{},{:.3},{:.3},{}",
            csv_escape(&report.timestamp),
            csv_escape(&report.vault_name),
            report.health_score,
            report.total_notes,
            report.total_links,
            report.broken_links,
            report.orphaned_notes,
            report.connectivity_rate,
            report.link_density,
            csv_escape(&report.status)
        );

        Ok(csv)
    }
}

/// Broken links exporter
pub struct BrokenLinksExporter;

impl BrokenLinksExporter {
    /// Export broken links as JSON
    pub fn to_json(links: &[BrokenLinkRecord]) -> Result<String> {
        to_json_string(links, "broken links")
    }

    /// Export broken links as CSV
    pub fn to_csv(links: &[BrokenLinkRecord]) -> Result<String> {
        let mut csv = String::from("source_file,target,line,suggestions\n");

        for link in links {
            let suggestions = link.suggestions.join("|");
            csv.push_str(&format!(
                "{},{},{},{}\n",
                csv_escape(&link.source_file),
                csv_escape(&link.target),
                link.line,
                csv_escape(&suggestions)
            ));
        }

        Ok(csv)
    }
}

/// Vault statistics exporter
pub struct VaultStatsExporter;

impl VaultStatsExporter {
    /// Export vault stats as JSON
    pub fn to_json(stats: &VaultStatsRecord) -> Result<String> {
        to_json_string(stats, "vault stats")
    }

    /// Export vault stats as CSV
    pub fn to_csv(stats: &VaultStatsRecord) -> Result<String> {
        let csv = format!(
            "timestamp,vault_name,total_files,total_links,orphaned_files,average_links_per_file,total_words,total_readable_chars,avg_words_per_note\n\
             {},{},{},{},{},{:.3},{},{},{:.1}",
            csv_escape(&stats.timestamp),
            csv_escape(&stats.vault_name),
            stats.total_files,
            stats.total_links,
            stats.orphaned_files,
            stats.average_links_per_file,
            stats.total_words,
            stats.total_readable_chars,
            stats.avg_words_per_note
        );

        Ok(csv)
    }
}

/// Analysis report exporter
pub struct AnalysisReportExporter;

impl AnalysisReportExporter {
    /// Export analysis report as JSON
    pub fn to_json(report: &AnalysisReport) -> Result<String> {
        to_json_string(report, "analysis report")
    }

    /// Export analysis report as CSV (health metrics only, flattened)
    pub fn to_csv(report: &AnalysisReport) -> Result<String> {
        let csv = format!(
            "timestamp,vault_name,health_score,total_notes,total_links,broken_links,orphaned_notes,broken_links_count,recommendations\n\
             {},{},{},{},{},{},{},{},{}",
            csv_escape(&report.timestamp),
            csv_escape(&report.vault_name),
            report.health.health_score,
            report.health.total_notes,
            report.health.total_links,
            report.health.broken_links,
            report.health.orphaned_notes,
            report.broken_links_count,
            csv_escape(&report.recommendations.join("|"))
        );

        Ok(csv)
    }
}

/// Create a health report with recommendations
pub fn create_health_report(
    vault_name: &str,
    health_score: u8,
    total_notes: usize,
    total_links: usize,
    broken_links: usize,
    orphaned_notes: usize,
) -> HealthReport {
    let connectivity_rate = if total_notes > 0 {
        (total_notes - orphaned_notes) as f64 / total_notes as f64
    } else {
        0.0
    };

    let link_density = if total_notes > 1 {
        total_links as f64 / ((total_notes as f64) * (total_notes as f64 - 1.0))
    } else {
        0.0
    };

    let status = if health_score >= 80 {
        "Healthy".to_string()
    } else if health_score >= 60 {
        "Fair".to_string()
    } else if health_score >= 40 {
        "Needs Attention".to_string()
    } else {
        "Critical".to_string()
    };

    let mut recommendations = Vec::new();

    if broken_links > 0 {
        recommendations.push(format!(
            "Found {} broken links. Consider fixing or updating them.",
            broken_links
        ));
    }

    if orphaned_notes as f64 / total_notes as f64 > 0.1 {
        recommendations
            .push("Over 10% of notes are orphaned. Link them to improve connectivity.".to_string());
    }

    if link_density < 0.05 {
        recommendations.push(
            "Low link density. Consider adding more cross-references between notes.".to_string(),
        );
    }

    HealthReport {
        timestamp: Utc::now().to_rfc3339(),
        vault_name: vault_name.to_string(),
        health_score,
        total_notes,
        total_links,
        broken_links,
        orphaned_notes,
        connectivity_rate,
        link_density,
        status,
        recommendations,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_health_report_creation() {
        let report = create_health_report("test", 85, 100, 150, 2, 5);
        assert_eq!(report.vault_name, "test");
        assert_eq!(report.health_score, 85);
        assert_eq!(report.status, "Healthy");
    }

    #[test]
    fn test_health_report_json_export() {
        let report = create_health_report("test", 85, 100, 150, 2, 5);
        let json = HealthReportExporter::to_json(&report).unwrap();
        assert!(json.contains("test"));
        assert!(json.contains("85"));
    }

    #[test]
    fn test_health_report_csv_export() {
        let report = create_health_report("test", 85, 100, 150, 2, 5);
        let csv = HealthReportExporter::to_csv(&report).unwrap();
        assert!(csv.contains("test"));
        assert!(csv.contains("85"));
    }

    #[test]
    fn test_broken_links_export() {
        let links = vec![BrokenLinkRecord {
            source_file: "file.md".to_string(),
            target: "missing.md".to_string(),
            line: 5,
            suggestions: vec!["existing.md".to_string()],
        }];

        let json = BrokenLinksExporter::to_json(&links).unwrap();
        assert!(json.contains("file.md"));
        assert!(json.contains("missing.md"));

        let csv = BrokenLinksExporter::to_csv(&links).unwrap();
        assert!(csv.contains("file.md"));
    }

    #[test]
    fn test_vault_stats_export() {
        let stats = VaultStatsRecord {
            timestamp: "2025-01-01T00:00:00Z".to_string(),
            vault_name: "test".to_string(),
            total_files: 100,
            total_links: 150,
            orphaned_files: 5,
            average_links_per_file: 1.5,
            total_words: 25000,
            total_readable_chars: 150000,
            avg_words_per_note: 250.0,
        };

        let json = VaultStatsExporter::to_json(&stats).unwrap();
        assert!(json.contains("100"));
        assert!(json.contains("25000"));

        let csv = VaultStatsExporter::to_csv(&stats).unwrap();
        assert!(csv.contains("100"));
        assert!(csv.contains("25000"));
    }

    // =========================================================================
    // csv_escape tests
    // =========================================================================

    #[test]
    fn test_csv_escape_plain_text() {
        assert_eq!(csv_escape("hello world"), "\"hello world\"");
    }

    #[test]
    fn test_csv_escape_embedded_quotes() {
        assert_eq!(csv_escape(r#"say "hi""#), r#""say ""hi""""#);
    }

    #[test]
    fn test_csv_escape_formula_equals() {
        assert_eq!(csv_escape("=SUM(A1:A2)"), "\"'=SUM(A1:A2)\"");
    }

    #[test]
    fn test_csv_escape_formula_plus() {
        assert_eq!(csv_escape("+1234"), "\"'+1234\"");
    }

    #[test]
    fn test_csv_escape_formula_minus() {
        assert_eq!(csv_escape("-1234"), "\"'-1234\"");
    }

    #[test]
    fn test_csv_escape_formula_at() {
        assert_eq!(csv_escape("@SUM"), "\"'@SUM\"");
    }

    #[test]
    fn test_csv_escape_empty_string() {
        assert_eq!(csv_escape(""), "\"\"");
    }

    #[test]
    fn test_csv_escape_with_newlines() {
        // Newlines embedded inside quoted fields are valid per RFC 4180
        assert_eq!(csv_escape("line1\nline2"), "\"line1\nline2\"");
    }

    #[test]
    fn test_csv_escape_with_commas() {
        assert_eq!(csv_escape("a,b,c"), "\"a,b,c\"");
    }

    #[test]
    fn test_csv_escape_combined_injection() {
        // Formula prefix AND embedded double-quotes
        // Input:  =cmd|'/C calc'!A1
        // After formula-prefix:  '=cmd|'/C calc'!A1
        // After quote-doubling:  no double-quotes present, so unchanged
        // Wrapped:               "'=cmd|'/C calc'!A1"
        assert_eq!(csv_escape("=cmd|'/C calc'!A1"), "\"'=cmd|'/C calc'!A1\"");
    }
}