oxigdal-qc 0.1.4

Quality control and validation suite for OxiGDAL - Comprehensive data integrity checks for geospatial data
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
638
639
640
641
642
643
644
645
//! Quality control report generation.
//!
//! This module provides functionality to generate quality control reports
//! in various formats (HTML, JSON, etc.).

use crate::error::{QcError, QcIssue, QcResult, Severity};
use std::io::Write;

/// Quality control report.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct QualityReport {
    /// Report title.
    pub title: String,

    /// Report generation timestamp.
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Executive summary.
    pub summary: ReportSummary,

    /// Detailed sections.
    pub sections: Vec<ReportSection>,

    /// All issues collected.
    pub issues: Vec<QcIssue>,

    /// Overall quality score (0.0 - 100.0).
    pub quality_score: f64,
}

/// Executive summary of the report.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ReportSummary {
    /// Total number of checks performed.
    pub total_checks: usize,

    /// Number of passed checks.
    pub passed_checks: usize,

    /// Number of failed checks.
    pub failed_checks: usize,

    /// Total issues by severity.
    pub issues_by_severity: SeverityCounts,

    /// Overall assessment.
    pub assessment: QualityAssessment,

    /// Key findings.
    pub key_findings: Vec<String>,
}

/// Count of issues by severity.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SeverityCounts {
    /// Number of critical issues.
    pub critical: usize,

    /// Number of major issues.
    pub major: usize,

    /// Number of minor issues.
    pub minor: usize,

    /// Number of warnings.
    pub warnings: usize,

    /// Number of info messages.
    pub info: usize,
}

/// Overall quality assessment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum QualityAssessment {
    /// Excellent quality.
    Excellent,

    /// Good quality.
    Good,

    /// Fair quality.
    Fair,

    /// Poor quality.
    Poor,

    /// Unacceptable quality.
    Unacceptable,
}

/// A section of the report.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ReportSection {
    /// Section title.
    pub title: String,

    /// Section description.
    pub description: String,

    /// Section results (key-value pairs).
    pub results: Vec<(String, String)>,

    /// Section-specific issues.
    pub issues: Vec<QcIssue>,

    /// Section pass/fail status.
    pub passed: bool,
}

impl QualityReport {
    /// Creates a new quality report.
    #[must_use]
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            timestamp: chrono::Utc::now(),
            summary: ReportSummary {
                total_checks: 0,
                passed_checks: 0,
                failed_checks: 0,
                issues_by_severity: SeverityCounts {
                    critical: 0,
                    major: 0,
                    minor: 0,
                    warnings: 0,
                    info: 0,
                },
                assessment: QualityAssessment::Excellent,
                key_findings: Vec::new(),
            },
            sections: Vec::new(),
            issues: Vec::new(),
            quality_score: 100.0,
        }
    }

    /// Adds a section to the report.
    pub fn add_section(&mut self, section: ReportSection) {
        self.summary.total_checks += 1;
        if section.passed {
            self.summary.passed_checks += 1;
        } else {
            self.summary.failed_checks += 1;
        }

        self.issues.extend(section.issues.clone());
        self.sections.push(section);
    }

    /// Adds issues to the report.
    pub fn add_issues(&mut self, issues: Vec<QcIssue>) {
        for issue in &issues {
            match issue.severity {
                Severity::Critical => self.summary.issues_by_severity.critical += 1,
                Severity::Major => self.summary.issues_by_severity.major += 1,
                Severity::Minor => self.summary.issues_by_severity.minor += 1,
                Severity::Warning => self.summary.issues_by_severity.warnings += 1,
                Severity::Info => self.summary.issues_by_severity.info += 1,
            }
        }
        self.issues.extend(issues);
    }

    /// Finalizes the report by computing scores and assessment.
    pub fn finalize(&mut self) {
        // Calculate quality score
        let total_issues = self.summary.issues_by_severity.critical
            + self.summary.issues_by_severity.major
            + self.summary.issues_by_severity.minor
            + self.summary.issues_by_severity.warnings;

        // Weighted scoring
        let critical_weight = 20.0;
        let major_weight = 10.0;
        let minor_weight = 3.0;
        let warning_weight = 1.0;

        let penalty = (self.summary.issues_by_severity.critical as f64 * critical_weight)
            + (self.summary.issues_by_severity.major as f64 * major_weight)
            + (self.summary.issues_by_severity.minor as f64 * minor_weight)
            + (self.summary.issues_by_severity.warnings as f64 * warning_weight);

        self.quality_score = (100.0 - penalty).max(0.0);

        // Determine assessment
        self.summary.assessment = if self.summary.issues_by_severity.critical > 0 {
            QualityAssessment::Unacceptable
        } else if self.quality_score >= 90.0 {
            QualityAssessment::Excellent
        } else if self.quality_score >= 75.0 {
            QualityAssessment::Good
        } else if self.quality_score >= 50.0 {
            QualityAssessment::Fair
        } else {
            QualityAssessment::Poor
        };

        // Generate key findings
        self.summary.key_findings.clear();

        if self.summary.issues_by_severity.critical > 0 {
            self.summary.key_findings.push(format!(
                "{} critical issues require immediate attention",
                self.summary.issues_by_severity.critical
            ));
        }

        if self.summary.issues_by_severity.major > 5 {
            self.summary.key_findings.push(format!(
                "{} major issues affect data quality",
                self.summary.issues_by_severity.major
            ));
        }

        if total_issues == 0 {
            self.summary
                .key_findings
                .push("No quality issues detected".to_string());
        }

        if self.summary.passed_checks == self.summary.total_checks {
            self.summary
                .key_findings
                .push("All quality checks passed".to_string());
        }
    }

    /// Generates an HTML report.
    ///
    /// # Errors
    ///
    /// Returns an error if HTML generation fails.
    pub fn generate_html(&self, path: impl AsRef<std::path::Path>) -> QcResult<()> {
        let mut file = std::fs::File::create(path).map_err(QcError::Io)?;

        writeln!(file, "<!DOCTYPE html>")?;
        writeln!(file, "<html lang=\"en\">")?;
        writeln!(file, "<head>")?;
        writeln!(file, "    <meta charset=\"UTF-8\">")?;
        writeln!(
            file,
            "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
        )?;
        writeln!(file, "    <title>{}</title>", self.title)?;
        writeln!(file, "    <style>")?;
        writeln!(file, "{}", Self::get_css())?;
        writeln!(file, "    </style>")?;
        writeln!(file, "</head>")?;
        writeln!(file, "<body>")?;
        writeln!(file, "    <div class=\"container\">")?;

        // Header
        writeln!(file, "        <header>")?;
        writeln!(file, "            <h1>{}</h1>", self.title)?;
        writeln!(
            file,
            "            <p class=\"timestamp\">Generated: {}</p>",
            self.timestamp.format("%Y-%m-%d %H:%M:%S UTC")
        )?;
        writeln!(file, "        </header>")?;

        // Executive Summary
        writeln!(file, "        <section class=\"summary\">")?;
        writeln!(file, "            <h2>Executive Summary</h2>")?;
        writeln!(
            file,
            "            <div class=\"quality-score quality-{}\">{:.1}</div>",
            self.get_score_class(),
            self.quality_score
        )?;
        writeln!(
            file,
            "            <p class=\"assessment\">Assessment: <strong>{:?}</strong></p>",
            self.summary.assessment
        )?;
        writeln!(
            file,
            "            <p>Checks: {} passed, {} failed out of {} total</p>",
            self.summary.passed_checks, self.summary.failed_checks, self.summary.total_checks
        )?;

        // Issues by severity
        writeln!(file, "            <div class=\"severity-counts\">")?;
        self.write_severity_badge(
            &mut file,
            "Critical",
            self.summary.issues_by_severity.critical,
        )?;
        self.write_severity_badge(&mut file, "Major", self.summary.issues_by_severity.major)?;
        self.write_severity_badge(&mut file, "Minor", self.summary.issues_by_severity.minor)?;
        self.write_severity_badge(
            &mut file,
            "Warning",
            self.summary.issues_by_severity.warnings,
        )?;
        writeln!(file, "            </div>")?;

        // Key findings
        if !self.summary.key_findings.is_empty() {
            writeln!(file, "            <div class=\"key-findings\">")?;
            writeln!(file, "                <h3>Key Findings</h3>")?;
            writeln!(file, "                <ul>")?;
            for finding in &self.summary.key_findings {
                writeln!(file, "                    <li>{}</li>", finding)?;
            }
            writeln!(file, "                </ul>")?;
            writeln!(file, "            </div>")?;
        }

        writeln!(file, "        </section>")?;

        // Sections
        for section in &self.sections {
            self.write_section(&mut file, section)?;
        }

        // All Issues
        if !self.issues.is_empty() {
            writeln!(file, "        <section class=\"issues\">")?;
            writeln!(
                file,
                "            <h2>All Issues ({} total)</h2>",
                self.issues.len()
            )?;
            writeln!(file, "            <table>")?;
            writeln!(file, "                <thead>")?;
            writeln!(file, "                    <tr>")?;
            writeln!(file, "                        <th>Severity</th>")?;
            writeln!(file, "                        <th>Category</th>")?;
            writeln!(file, "                        <th>Description</th>")?;
            writeln!(file, "                        <th>Location</th>")?;
            writeln!(file, "                    </tr>")?;
            writeln!(file, "                </thead>")?;
            writeln!(file, "                <tbody>")?;

            for issue in &self.issues {
                writeln!(file, "                    <tr>")?;
                writeln!(
                    file,
                    "                        <td class=\"severity-{}\">{}</td>",
                    format!("{:?}", issue.severity).to_lowercase(),
                    issue.severity
                )?;
                writeln!(file, "                        <td>{}</td>", issue.category)?;
                writeln!(file, "                        <td>")?;
                writeln!(
                    file,
                    "                            <strong>{}</strong>",
                    issue.description
                )?;
                writeln!(file, "                            <p>{}</p>", issue.message)?;
                if let Some(ref suggestion) = issue.suggestion {
                    writeln!(
                        file,
                        "                            <p class=\"suggestion\">Suggestion: {}</p>",
                        suggestion
                    )?;
                }
                writeln!(file, "                        </td>")?;
                writeln!(
                    file,
                    "                        <td>{}</td>",
                    issue.location.as_deref().unwrap_or("-")
                )?;
                writeln!(file, "                    </tr>")?;
            }

            writeln!(file, "                </tbody>")?;
            writeln!(file, "            </table>")?;
            writeln!(file, "        </section>")?;
        }

        writeln!(file, "    </div>")?;
        writeln!(file, "</body>")?;
        writeln!(file, "</html>")?;

        Ok(())
    }

    /// Generates a JSON report.
    ///
    /// # Errors
    ///
    /// Returns an error if JSON generation fails.
    pub fn generate_json(&self, path: impl AsRef<std::path::Path>) -> QcResult<()> {
        let json = serde_json::to_string_pretty(self)?;
        std::fs::write(path, json).map_err(QcError::Io)?;
        Ok(())
    }

    fn write_severity_badge<W: Write>(
        &self,
        writer: &mut W,
        label: &str,
        count: usize,
    ) -> QcResult<()> {
        writeln!(
            writer,
            "                <span class=\"badge badge-{}\">{}: {}</span>",
            label.to_lowercase(),
            label,
            count
        )?;
        Ok(())
    }

    fn write_section<W: Write>(&self, writer: &mut W, section: &ReportSection) -> QcResult<()> {
        writeln!(writer, "        <section class=\"report-section\">")?;
        writeln!(writer, "            <h2>{}</h2>", section.title)?;
        writeln!(writer, "            <p>{}</p>", section.description)?;

        if !section.results.is_empty() {
            writeln!(writer, "            <table>")?;
            for (key, value) in &section.results {
                writeln!(writer, "                <tr>")?;
                writeln!(
                    writer,
                    "                    <td><strong>{}</strong></td>",
                    key
                )?;
                writeln!(writer, "                    <td>{}</td>", value)?;
                writeln!(writer, "                </tr>")?;
            }
            writeln!(writer, "            </table>")?;
        }

        if !section.issues.is_empty() {
            writeln!(
                writer,
                "            <p class=\"issue-count\">{} issues found</p>",
                section.issues.len()
            )?;
        }

        writeln!(writer, "        </section>")?;
        Ok(())
    }

    fn get_score_class(&self) -> &str {
        if self.quality_score >= 90.0 {
            "excellent"
        } else if self.quality_score >= 75.0 {
            "good"
        } else if self.quality_score >= 50.0 {
            "fair"
        } else {
            "poor"
        }
    }

    fn get_css() -> &'static str {
        r#"
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    line-height: 1.6;
    color: #333;
    background: #f5f5f5;
    margin: 0;
    padding: 20px;
}
.container {
    max-width: 1200px;
    margin: 0 auto;
    background: white;
    padding: 30px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
header {
    border-bottom: 3px solid #007bff;
    padding-bottom: 20px;
    margin-bottom: 30px;
}
h1 {
    margin: 0;
    color: #007bff;
}
.timestamp {
    color: #666;
    margin: 10px 0 0 0;
}
.summary {
    background: #f8f9fa;
    padding: 20px;
    border-radius: 8px;
    margin-bottom: 30px;
}
.quality-score {
    font-size: 48px;
    font-weight: bold;
    text-align: center;
    margin: 20px 0;
    padding: 20px;
    border-radius: 8px;
}
.quality-excellent {
    background: #d4edda;
    color: #155724;
}
.quality-good {
    background: #d1ecf1;
    color: #0c5460;
}
.quality-fair {
    background: #fff3cd;
    color: #856404;
}
.quality-poor {
    background: #f8d7da;
    color: #721c24;
}
.assessment {
    text-align: center;
    font-size: 1.2em;
}
.severity-counts {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
    margin: 20px 0;
}
.badge {
    padding: 5px 15px;
    border-radius: 20px;
    font-weight: bold;
    color: white;
}
.badge-critical {
    background: #721c24;
}
.badge-major {
    background: #dc3545;
}
.badge-minor {
    background: #fd7e14;
}
.badge-warning {
    background: #ffc107;
    color: #333;
}
.key-findings {
    margin-top: 20px;
}
.key-findings ul {
    list-style-type: none;
    padding: 0;
}
.key-findings li {
    padding: 10px;
    margin: 5px 0;
    background: white;
    border-left: 4px solid #007bff;
}
.report-section {
    margin: 30px 0;
    padding: 20px;
    border: 1px solid #dee2e6;
    border-radius: 8px;
}
table {
    width: 100%;
    border-collapse: collapse;
    margin: 15px 0;
}
th, td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #dee2e6;
}
th {
    background: #f8f9fa;
    font-weight: bold;
}
.severity-critical {
    color: #721c24;
    font-weight: bold;
}
.severity-major {
    color: #dc3545;
    font-weight: bold;
}
.severity-minor {
    color: #fd7e14;
    font-weight: bold;
}
.severity-warning {
    color: #856404;
}
.suggestion {
    color: #0c5460;
    font-style: italic;
    margin-top: 5px;
}
.issue-count {
    color: #dc3545;
    font-weight: bold;
}
"#
    }
}

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

    #[test]
    fn test_report_creation() {
        let report = QualityReport::new("Test Report");
        assert_eq!(report.title, "Test Report");
        assert_eq!(report.sections.len(), 0);
        assert_eq!(report.issues.len(), 0);
    }

    #[test]
    fn test_report_finalize() {
        let mut report = QualityReport::new("Test Report");
        report.add_issues(vec![QcIssue::new(
            Severity::Critical,
            "test",
            "Test issue",
            "Test message",
        )]);
        report.finalize();

        assert_eq!(report.summary.issues_by_severity.critical, 1);
        assert_eq!(report.summary.assessment, QualityAssessment::Unacceptable);
        assert!(report.quality_score < 100.0);
    }

    #[test]
    fn test_json_generation() {
        let mut report = QualityReport::new("Test Report");
        report.finalize();

        let temp_dir = std::env::temp_dir();
        let json_path = temp_dir.join("test_report.json");

        let result = report.generate_json(&json_path);
        assert!(result.is_ok());
        assert!(json_path.exists());

        let cleanup = std::fs::remove_file(&json_path);
        assert!(cleanup.is_ok());
    }
}