datasynth-fingerprint 5.36.0

Privacy-preserving synthetic data fingerprinting for DataSynth
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
//! Business rules fingerprint models.

use std::collections::HashMap;

use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

/// Rules fingerprint containing business rules and constraints.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RulesFingerprint {
    /// Balance rules (e.g., debits = credits).
    pub balance_rules: Vec<BalanceRule>,

    /// Approval threshold rules.
    pub approval_thresholds: Vec<ApprovalThreshold>,

    /// Temporal ordering rules.
    pub temporal_rules: Vec<TemporalRule>,

    /// Value range constraints.
    pub range_constraints: Vec<RangeConstraint>,

    /// Custom business rules.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub custom_rules: Vec<CustomRule>,

    /// Rule compliance statistics.
    pub compliance_stats: HashMap<String, RuleComplianceStats>,
}

impl RulesFingerprint {
    /// Create a new empty rules fingerprint.
    pub fn new() -> Self {
        Self {
            balance_rules: Vec::new(),
            approval_thresholds: Vec::new(),
            temporal_rules: Vec::new(),
            range_constraints: Vec::new(),
            custom_rules: Vec::new(),
            compliance_stats: HashMap::new(),
        }
    }

    /// Add a balance rule.
    pub fn add_balance_rule(&mut self, rule: BalanceRule) {
        self.balance_rules.push(rule);
    }

    /// Add an approval threshold.
    pub fn add_approval_threshold(&mut self, threshold: ApprovalThreshold) {
        self.approval_thresholds.push(threshold);
    }

    /// Add compliance statistics for a rule.
    pub fn add_compliance(&mut self, rule_name: impl Into<String>, stats: RuleComplianceStats) {
        self.compliance_stats.insert(rule_name.into(), stats);
    }
}

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

/// Balance rule (e.g., sum of debits = sum of credits).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceRule {
    /// Rule name.
    pub name: String,

    /// Description of the rule.
    pub description: String,

    /// Table this rule applies to.
    pub table: String,

    /// Grouping columns (e.g., document_id for JE balance).
    pub group_by: Vec<String>,

    /// Left side of the equation (column or expression).
    pub left_side: BalanceExpression,

    /// Right side of the equation.
    pub right_side: BalanceExpression,

    /// Tolerance for matching (absolute or relative).
    pub tolerance: BalanceTolerance,

    /// Compliance rate observed in data.
    pub compliance_rate: f64,
}

impl BalanceRule {
    /// Create a new balance rule.
    pub fn new(
        name: impl Into<String>,
        table: impl Into<String>,
        left_side: BalanceExpression,
        right_side: BalanceExpression,
    ) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            table: table.into(),
            group_by: Vec::new(),
            left_side,
            right_side,
            tolerance: BalanceTolerance::Absolute(Decimal::ZERO),
            compliance_rate: 1.0,
        }
    }

    /// Add grouping columns.
    pub fn with_group_by(mut self, columns: Vec<String>) -> Self {
        self.group_by = columns;
        self
    }
}

/// Expression for balance rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BalanceExpression {
    /// Sum of a column.
    Sum { column: String },

    /// Sum with filter condition.
    SumWhere {
        column: String,
        filter: FilterCondition,
    },

    /// Count of rows.
    Count,

    /// Count with filter.
    CountWhere { filter: FilterCondition },

    /// Constant value.
    Constant { value: Decimal },

    /// Difference between two expressions.
    Difference {
        left: Box<BalanceExpression>,
        right: Box<BalanceExpression>,
    },
}

/// Filter condition for expressions.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterCondition {
    /// Column to filter on.
    pub column: String,
    /// Operator.
    pub operator: FilterOperator,
    /// Value to compare against.
    pub value: String,
}

/// Filter operators.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FilterOperator {
    Equals,
    NotEquals,
    GreaterThan,
    LessThan,
    GreaterOrEqual,
    LessOrEqual,
    In,
    NotIn,
    IsNull,
    IsNotNull,
}

/// Tolerance for balance matching.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum BalanceTolerance {
    /// Absolute tolerance (e.g., within $0.01).
    Absolute(Decimal),
    /// Relative tolerance (e.g., within 0.1%).
    Relative(f64),
}

/// Approval threshold rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApprovalThreshold {
    /// Threshold name.
    pub name: String,

    /// Description.
    pub description: String,

    /// Amount thresholds in ascending order.
    pub thresholds: Vec<ThresholdLevel>,

    /// Observed compliance rate.
    pub compliance_rate: f64,

    /// Distribution of amounts across threshold levels.
    pub level_distribution: Vec<f64>,
}

impl ApprovalThreshold {
    /// Create a new approval threshold.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            thresholds: Vec::new(),
            compliance_rate: 1.0,
            level_distribution: Vec::new(),
        }
    }

    /// Add a threshold level.
    pub fn add_level(&mut self, level: ThresholdLevel) {
        self.thresholds.push(level);
        // Keep sorted by amount
        self.thresholds.sort_by_key(|a| a.amount);
    }
}

/// A single threshold level.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThresholdLevel {
    /// Threshold amount.
    pub amount: Decimal,
    /// Required approval level.
    pub approval_level: String,
    /// Proportion of transactions at this level.
    pub proportion: f64,
}

/// Temporal ordering rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalRule {
    /// Rule name.
    pub name: String,

    /// Description.
    pub description: String,

    /// First event/date column.
    pub before_column: String,

    /// Second event/date column (must be >= before_column).
    pub after_column: String,

    /// Table(s) involved.
    pub tables: Vec<String>,

    /// Join condition if multiple tables.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub join_condition: Option<String>,

    /// Observed compliance rate.
    pub compliance_rate: f64,

    /// Typical gap statistics.
    pub gap_stats: Option<GapStatistics>,
}

impl TemporalRule {
    /// Create a new temporal rule.
    pub fn new(
        name: impl Into<String>,
        before_column: impl Into<String>,
        after_column: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            description: String::new(),
            before_column: before_column.into(),
            after_column: after_column.into(),
            tables: Vec::new(),
            join_condition: None,
            compliance_rate: 1.0,
            gap_stats: None,
        }
    }
}

/// Statistics about time gaps.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GapStatistics {
    /// Minimum gap in days.
    pub min_days: f64,
    /// Maximum gap in days.
    pub max_days: f64,
    /// Mean gap in days.
    pub mean_days: f64,
    /// Median gap in days.
    pub median_days: f64,
    /// Standard deviation of gap.
    pub std_dev_days: f64,
}

/// Value range constraint.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RangeConstraint {
    /// Constraint name.
    pub name: String,

    /// Table name.
    pub table: String,

    /// Column name.
    pub column: String,

    /// Minimum value (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_value: Option<f64>,

    /// Maximum value (optional).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_value: Option<f64>,

    /// Compliance rate.
    pub compliance_rate: f64,
}

impl RangeConstraint {
    /// Create a new range constraint.
    pub fn new(
        name: impl Into<String>,
        table: impl Into<String>,
        column: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            table: table.into(),
            column: column.into(),
            min_value: None,
            max_value: None,
            compliance_rate: 1.0,
        }
    }

    /// Set minimum value.
    pub fn with_min(mut self, min: f64) -> Self {
        self.min_value = Some(min);
        self
    }

    /// Set maximum value.
    pub fn with_max(mut self, max: f64) -> Self {
        self.max_value = Some(max);
        self
    }
}

/// Custom business rule.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CustomRule {
    /// Rule name.
    pub name: String,

    /// Description.
    pub description: String,

    /// Rule category.
    pub category: String,

    /// Tables involved.
    pub tables: Vec<String>,

    /// Columns involved.
    pub columns: Vec<String>,

    /// Rule expression (human-readable).
    pub expression: String,

    /// Compliance rate.
    pub compliance_rate: f64,

    /// Additional parameters.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub parameters: HashMap<String, String>,
}

/// Rule compliance statistics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuleComplianceStats {
    /// Total rows checked.
    pub total_checked: u64,

    /// Rows that passed the rule.
    pub passed: u64,

    /// Rows that failed the rule.
    pub failed: u64,

    /// Compliance rate (passed / total).
    pub compliance_rate: f64,

    /// Common failure patterns.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub failure_patterns: Vec<FailurePattern>,
}

impl RuleComplianceStats {
    /// Create from counts.
    pub fn from_counts(total: u64, passed: u64) -> Self {
        let failed = total.saturating_sub(passed);
        let compliance_rate = if total > 0 {
            passed as f64 / total as f64
        } else {
            1.0
        };

        Self {
            total_checked: total,
            passed,
            failed,
            compliance_rate,
            failure_patterns: Vec::new(),
        }
    }
}

/// Pattern of rule failures.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailurePattern {
    /// Pattern description.
    pub description: String,
    /// Count of this pattern.
    pub count: u64,
    /// Proportion of failures.
    pub proportion: f64,
}