mockforge-data 0.3.118

Data generator for MockForge - faker + RAG synthetic data engine
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
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
634
635
636
637
//! Core dataset structures and basic operations
//!
//! This module provides the fundamental data structures for datasets,
//! including dataset definitions, rows, and basic operations.

use crate::{DataConfig, OutputFormat};
use crate::{Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

/// Dataset validation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetValidationResult {
    /// Whether the dataset is valid
    pub valid: bool,
    /// Validation errors
    pub errors: Vec<String>,
    /// Validation warnings
    pub warnings: Vec<String>,
    /// Total number of rows validated
    pub total_rows_validated: usize,
}

/// Dataset metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetMetadata {
    /// Dataset name
    pub name: String,
    /// Dataset description
    pub description: Option<String>,
    /// Schema name used to generate this dataset
    pub schema_name: String,
    /// Number of rows
    pub row_count: usize,
    /// Generation configuration
    pub config: DataConfig,
    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,
    /// Generation time in milliseconds
    pub generation_time_ms: u128,
    /// File format
    pub format: OutputFormat,
    /// File size in bytes
    pub file_size_bytes: Option<u64>,
    /// Additional metadata
    pub tags: HashMap<String, String>,
}

impl Default for DatasetMetadata {
    fn default() -> Self {
        Self {
            name: String::new(),
            description: None,
            schema_name: String::new(),
            row_count: 0,
            config: DataConfig::default(),
            created_at: chrono::Utc::now(),
            generation_time_ms: 0,
            format: OutputFormat::Json,
            file_size_bytes: None,
            tags: HashMap::new(),
        }
    }
}

impl DatasetMetadata {
    /// Create new dataset metadata
    pub fn new(
        name: String,
        schema_name: String,
        config: DataConfig,
        format: OutputFormat,
    ) -> Self {
        Self {
            name,
            schema_name,
            config,
            format,
            created_at: chrono::Utc::now(),
            ..Default::default()
        }
    }

    /// Update generation time
    pub fn set_generation_time(&mut self, time_ms: u128) {
        self.generation_time_ms = time_ms;
    }

    /// Set file size
    pub fn set_file_size(&mut self, size_bytes: u64) {
        self.file_size_bytes = Some(size_bytes);
    }

    /// Add tag
    pub fn add_tag(&mut self, key: String, value: String) {
        self.tags.insert(key, value);
    }

    /// Get tag value
    pub fn get_tag(&self, key: &str) -> Option<&String> {
        self.tags.get(key)
    }

    /// Remove tag
    pub fn remove_tag(&mut self, key: &str) -> Option<String> {
        self.tags.remove(key)
    }

    /// Get total size in bytes (estimated)
    pub fn estimated_size_bytes(&self) -> u64 {
        self.file_size_bytes.unwrap_or_else(|| {
            // Rough estimate: each row ~1KB
            (self.row_count * 1024) as u64
        })
    }

    /// Check if dataset is empty
    pub fn is_empty(&self) -> bool {
        self.row_count == 0
    }

    /// Get human-readable size
    pub fn human_readable_size(&self) -> String {
        let bytes = self.estimated_size_bytes();
        if bytes < 1024 {
            format!("{} B", bytes)
        } else if bytes < 1024 * 1024 {
            format!("{:.1} KB", bytes as f64 / 1024.0)
        } else if bytes < 1024 * 1024 * 1024 {
            format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))
        } else {
            format!("{:.1} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
        }
    }
}

/// Single row of data in a dataset
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetRow {
    /// Row ID
    pub id: String,
    /// Row data as key-value pairs
    pub data: HashMap<String, serde_json::Value>,
    /// Row metadata
    pub metadata: HashMap<String, String>,
    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,
}

impl DatasetRow {
    /// Create a new dataset row
    pub fn new(id: String, data: HashMap<String, serde_json::Value>) -> Self {
        Self {
            id,
            data,
            metadata: HashMap::new(),
            created_at: chrono::Utc::now(),
        }
    }

    /// Add metadata to the row
    pub fn add_metadata(&mut self, key: String, value: String) {
        self.metadata.insert(key, value);
    }

    /// Get metadata value
    pub fn get_metadata(&self, key: &str) -> Option<&String> {
        self.metadata.get(key)
    }

    /// Remove metadata
    pub fn remove_metadata(&mut self, key: &str) -> Option<String> {
        self.metadata.remove(key)
    }

    /// Get field value
    pub fn get_field(&self, field_name: &str) -> Option<&serde_json::Value> {
        self.data.get(field_name)
    }

    /// Set field value
    pub fn set_field(&mut self, field_name: String, value: serde_json::Value) {
        self.data.insert(field_name, value);
    }

    /// Check if row contains a field
    pub fn has_field(&self, field_name: &str) -> bool {
        self.data.contains_key(field_name)
    }

    /// Get all field names
    pub fn field_names(&self) -> Vec<&String> {
        self.data.keys().collect()
    }

    /// Get row as JSON value
    pub fn to_json(&self) -> serde_json::Value {
        serde_json::json!({
            "id": self.id,
            "data": self.data,
            "metadata": self.metadata,
            "created_at": self.created_at,
        })
    }
}

/// Dataset statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetStats {
    /// Total number of rows
    pub row_count: usize,
    /// Number of columns/fields
    pub column_count: usize,
    /// Total size in bytes
    pub total_size_bytes: u64,
    /// Average row size in bytes
    pub average_row_size_bytes: f64,
    /// Smallest row size in bytes
    pub min_row_size_bytes: u64,
    /// Largest row size in bytes
    pub max_row_size_bytes: u64,
    /// Field name statistics
    pub field_stats: HashMap<String, FieldStats>,
    /// Generation timestamp
    pub generated_at: chrono::DateTime<chrono::Utc>,
}

/// Field statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldStats {
    /// Field name
    pub field_name: String,
    /// Field type
    pub field_type: String,
    /// Number of non-null values
    pub non_null_count: usize,
    /// Number of null values
    pub null_count: usize,
    /// Number of unique values
    pub unique_count: usize,
    /// Minimum value (if numeric)
    pub min_value: Option<serde_json::Value>,
    /// Maximum value (if numeric)
    pub max_value: Option<serde_json::Value>,
    /// Average value (if numeric)
    pub average_value: Option<f64>,
    /// Most common values
    pub most_common_values: Vec<(serde_json::Value, usize)>,
}

/// Dataset represents a collection of generated data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Dataset {
    /// Dataset metadata
    pub metadata: DatasetMetadata,
    /// Dataset rows
    pub rows: Vec<DatasetRow>,
    /// Dataset statistics
    pub stats: Option<DatasetStats>,
}

impl Dataset {
    /// Create a new empty dataset
    pub fn new(
        name: String,
        schema_name: String,
        config: DataConfig,
        format: OutputFormat,
    ) -> Self {
        Self {
            metadata: DatasetMetadata::new(name, schema_name, config, format),
            rows: Vec::new(),
            stats: None,
        }
    }

    /// Create a dataset with pre-existing rows
    pub fn with_rows(
        name: String,
        schema_name: String,
        config: DataConfig,
        format: OutputFormat,
        rows: Vec<DatasetRow>,
    ) -> Self {
        let mut dataset = Self::new(name, schema_name, config, format);
        dataset.rows = rows;
        dataset.metadata.row_count = dataset.rows.len();
        dataset
    }

    /// Add a row to the dataset
    pub fn add_row(&mut self, row: DatasetRow) {
        self.rows.push(row);
        self.metadata.row_count = self.rows.len();
    }

    /// Add multiple rows to the dataset
    pub fn add_rows(&mut self, rows: Vec<DatasetRow>) {
        self.rows.extend(rows);
        self.metadata.row_count = self.rows.len();
    }

    /// Get row by ID
    pub fn get_row(&self, id: &str) -> Option<&DatasetRow> {
        self.rows.iter().find(|row| row.id == id)
    }

    /// Get row by ID (mutable)
    pub fn get_row_mut(&mut self, id: &str) -> Option<&mut DatasetRow> {
        self.rows.iter_mut().find(|row| row.id == id)
    }

    /// Remove row by ID
    pub fn remove_row(&mut self, id: &str) -> Option<DatasetRow> {
        if let Some(pos) = self.rows.iter().position(|row| row.id == id) {
            let row = self.rows.remove(pos);
            self.metadata.row_count = self.rows.len();
            Some(row)
        } else {
            None
        }
    }

    /// Get rows by metadata key-value
    pub fn get_rows_by_metadata(&self, key: &str, value: &str) -> Vec<&DatasetRow> {
        self.rows
            .iter()
            .filter(|row| row.get_metadata(key).map(|v| v == value).unwrap_or(false))
            .collect()
    }

    /// Get all row IDs
    pub fn row_ids(&self) -> Vec<&String> {
        self.rows.iter().map(|row| &row.id).collect()
    }

    /// Check if dataset is empty
    pub fn is_empty(&self) -> bool {
        self.rows.is_empty()
    }

    /// Get dataset size
    pub fn size(&self) -> usize {
        self.rows.len()
    }

    /// Get field names from the first row (if available)
    pub fn field_names(&self) -> Vec<&String> {
        if let Some(first_row) = self.rows.first() {
            first_row.field_names()
        } else {
            Vec::new()
        }
    }

    /// Calculate dataset statistics
    pub fn calculate_stats(&mut self) -> Result<()> {
        if self.rows.is_empty() {
            self.stats = Some(DatasetStats {
                row_count: 0,
                column_count: 0,
                total_size_bytes: 0,
                average_row_size_bytes: 0.0,
                min_row_size_bytes: 0,
                max_row_size_bytes: 0,
                field_stats: HashMap::new(),
                generated_at: chrono::Utc::now(),
            });
            return Ok(());
        }

        let mut total_size = 0u64;
        let mut row_sizes = Vec::new();

        // Temporary structure for collecting field statistics
        #[derive(Default)]
        struct TempFieldStats {
            field_type: Option<String>,
            non_null_count: usize,
            null_count: usize,
            unique_values: HashSet<serde_json::Value>,
            numeric_values: Vec<f64>,
            frequency: HashMap<serde_json::Value, usize>,
        }

        let mut temp_field_stats: HashMap<String, TempFieldStats> = HashMap::new();

        // Get field names from first row
        let field_names = self.field_names();
        for field_name in &field_names {
            temp_field_stats.insert(field_name.to_string(), TempFieldStats::default());
        }

        // Process each row
        for row in &self.rows {
            let row_json = row.to_json();
            let row_size = serde_json::to_string(&row_json)
                .map_err(|e| Error::generic(format!("Failed to serialize row: {}", e)))?
                .len() as u64;

            total_size += row_size;
            row_sizes.push(row_size);

            // Update field statistics
            for (field_name, field_value) in &row.data {
                if let Some(temp_stats) = temp_field_stats.get_mut(field_name) {
                    match field_value {
                        serde_json::Value::Null => temp_stats.null_count += 1,
                        _ => {
                            temp_stats.non_null_count += 1;

                            // Type detection
                            let value_type = match field_value {
                                serde_json::Value::Bool(_) => "boolean",
                                serde_json::Value::Number(_) => "number",
                                serde_json::Value::String(_) => "string",
                                serde_json::Value::Array(_) => "array",
                                serde_json::Value::Object(_) => "object",
                                serde_json::Value::Null => unreachable!(),
                            };

                            if temp_stats.field_type.is_none() {
                                temp_stats.field_type = Some(value_type.to_string());
                            } else if temp_stats.field_type.as_ref()
                                != Some(&value_type.to_string())
                            {
                                temp_stats.field_type = Some("mixed".to_string());
                            }

                            // Collect unique values
                            temp_stats.unique_values.insert(field_value.clone());

                            // Collect numeric values for min/max/avg
                            if let serde_json::Value::Number(num) = field_value {
                                if let Some(f) = num.as_f64() {
                                    temp_stats.numeric_values.push(f);
                                }
                            }

                            // Update frequency
                            *temp_stats.frequency.entry(field_value.clone()).or_insert(0) += 1;
                        }
                    }
                }
            }
        }

        // Convert temporary stats to final FieldStats
        let mut field_stats: HashMap<String, FieldStats> = HashMap::new();
        for (field_name, temp_stats) in temp_field_stats {
            let field_type = temp_stats.field_type.unwrap_or_else(|| "unknown".to_string());

            let (min_value, max_value, average_value) = if field_type == "number"
                && !temp_stats.numeric_values.is_empty()
            {
                let min = temp_stats.numeric_values.iter().fold(f64::INFINITY, |a, &b| a.min(b));
                let max =
                    temp_stats.numeric_values.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));
                let sum: f64 = temp_stats.numeric_values.iter().sum();
                let avg = sum / temp_stats.numeric_values.len() as f64;
                (
                    Some(serde_json::Value::Number(
                        serde_json::Number::from_f64(min).unwrap_or(serde_json::Number::from(0)),
                    )),
                    Some(serde_json::Value::Number(
                        serde_json::Number::from_f64(max).unwrap_or(serde_json::Number::from(0)),
                    )),
                    Some(avg),
                )
            } else {
                (None, None, None)
            };

            // Get most common values (top 5)
            let mut most_common: Vec<(serde_json::Value, usize)> =
                temp_stats.frequency.into_iter().collect();
            most_common.sort_by(|a, b| b.1.cmp(&a.1));
            most_common.truncate(5);

            field_stats.insert(
                field_name.clone(),
                FieldStats {
                    field_name,
                    field_type,
                    non_null_count: temp_stats.non_null_count,
                    null_count: temp_stats.null_count,
                    unique_count: temp_stats.unique_values.len(),
                    min_value,
                    max_value,
                    average_value,
                    most_common_values: most_common,
                },
            );
        }

        let row_count = self.rows.len();
        let average_row_size = if row_count > 0 {
            total_size as f64 / row_count as f64
        } else {
            0.0
        };

        let min_row_size = row_sizes.iter().min().unwrap_or(&0);
        let max_row_size = row_sizes.iter().max().unwrap_or(&0);

        self.stats = Some(DatasetStats {
            row_count,
            column_count: field_names.len(),
            total_size_bytes: total_size,
            average_row_size_bytes: average_row_size,
            min_row_size_bytes: *min_row_size,
            max_row_size_bytes: *max_row_size,
            field_stats,
            generated_at: chrono::Utc::now(),
        });

        Ok(())
    }

    /// Validate dataset integrity
    pub fn validate(&self) -> DatasetValidationResult {
        let mut errors = Vec::new();
        let mut warnings = Vec::new();

        // Check metadata
        if self.metadata.name.is_empty() {
            errors.push("Dataset name cannot be empty".to_string());
        }

        if self.metadata.schema_name.is_empty() {
            errors.push("Schema name cannot be empty".to_string());
        }

        // Check rows
        for (index, row) in self.rows.iter().enumerate() {
            if row.id.is_empty() {
                errors.push(format!("Row {} has empty ID", index));
            }

            if row.data.is_empty() {
                warnings.push(format!("Row {} has no data", index));
            }
        }

        DatasetValidationResult {
            valid: errors.is_empty(),
            errors,
            warnings,
            total_rows_validated: self.rows.len(),
        }
    }

    /// Export dataset to JSON
    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string_pretty(self)
            .map_err(|e| Error::generic(format!("Failed to serialize dataset: {}", e)))
    }

    /// Export dataset rows to JSON array
    pub fn rows_to_json(&self) -> Result<String> {
        let rows_json: Vec<_> = self.rows.iter().map(|row| row.to_json()).collect();
        serde_json::to_string_pretty(&rows_json)
            .map_err(|e| Error::generic(format!("Failed to serialize dataset rows: {}", e)))
    }

    /// Get dataset summary
    pub fn summary(&self) -> String {
        format!(
            "Dataset '{}' - {} rows, {} columns, {}",
            self.metadata.name,
            self.rows.len(),
            self.field_names().len(),
            self.metadata.human_readable_size()
        )
    }
}

impl Default for Dataset {
    fn default() -> Self {
        Self::new(
            "Untitled Dataset".to_string(),
            "Unknown Schema".to_string(),
            DataConfig::default(),
            OutputFormat::Json,
        )
    }
}

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

    #[test]
    fn test_dataset_new() {
        let dataset = Dataset::new(
            "TestDataset".to_string(),
            "TestSchema".to_string(),
            DataConfig::default(),
            OutputFormat::Json,
        );

        assert_eq!(dataset.metadata.name, "TestDataset");
        assert_eq!(dataset.metadata.schema_name, "TestSchema");
        assert_eq!(dataset.rows.len(), 0);
    }

    #[test]
    fn test_dataset_default() {
        let dataset = Dataset::default();

        assert_eq!(dataset.metadata.name, "Untitled Dataset");
        assert_eq!(dataset.metadata.schema_name, "Unknown Schema");
    }

    #[test]
    fn test_dataset_row_new() {
        let mut data = HashMap::new();
        data.insert("name".to_string(), serde_json::json!("test"));

        let row = DatasetRow::new("1".to_string(), data.clone());

        assert_eq!(row.id, "1");
        assert_eq!(row.data.len(), 1);
        assert!(row.metadata.is_empty());
    }

    #[test]
    fn test_dataset_row_metadata() {
        let mut data = HashMap::new();
        data.insert("name".to_string(), serde_json::json!("test"));

        let mut row = DatasetRow::new("1".to_string(), data);
        row.metadata.insert("source".to_string(), "test".to_string());

        assert_eq!(row.metadata.len(), 1);
    }
}