1
use schemars::JsonSchema;
2
use serde::Serialize;
3
use serde_with::skip_serializing_none;
4

            
5
/// Represents the statistical data collected during the model rewriting process.
6
///
7
/// The `RewriterStats` struct is used to track various metrics and statistics related to the rewriting
8
/// of a model using a set of rules. These statistics can be used for performance monitoring, debugging,
9
/// and optimization purposes. The structure supports optional fields, allowing selective tracking of data
10
/// without requiring all fields to be set.
11
///
12
/// The struct uses the following features:
13
/// - `#[skip_serializing_none]`: Skips serializing fields that have a value of `None`, resulting in cleaner JSON output.
14
/// - `#[serde(rename_all = "camelCase")]`: Uses camelCase for all serialized field names, adhering to common JSON naming conventions.
15
///
16
/// # Fields
17
/// - `is_optimization_enabled`:
18
///   - Type: `Option<bool>`
19
///   - Indicates whether optimizations were enabled during the rewriting process.
20
///   - If `Some(true)`, it means optimizations were enabled.
21
///   - If `Some(false)`, optimizations were explicitly disabled.
22
///   - If `None`, the status of optimizations is unknown or not tracked.
23
///
24
/// - `rewriter_run_time`:
25
///   - Type: `Option<std::time::Duration>`
26
///   - The total runtime duration of the rewriter in the current session.
27
///   - If set, it indicates the amount of time spent on rewriting, measured as a `Duration`.
28
///   - If `None`, the runtime is either unknown or not tracked.
29
///
30
/// - `rewriter_rule_application_attempts`:
31
///   - Type: `Option<usize>`
32
///   - The number of rule application attempts made during the rewriting process.
33
///   - An attempt is counted each time a rule is evaluated, regardless of whether it was successfully applied.
34
///   - If `None`, this metric is not tracked or not applicable for the current session.
35
///
36
/// - `rewriter_rule_applications`:
37
///   - Type: `Option<usize>`
38
///   - The number of successful rule applications during the rewriting process.
39
///   - A successful application means the rule was successfully applied to transform the expression or constraint.
40
///   - If `None`, this metric is not tracked or not applicable for the current session.
41
///
42
/// # Example
43
///
44
/// let stats = RewriterStats {
45
///     is_optimization_enabled: Some(true),
46
///     rewriter_run_time: Some(std::time::Duration::new(2, 0)),
47
///     rewriter_rule_application_attempts: Some(15),
48
///     rewriter_rule_applications: Some(10),
49
/// };
50
///
51
/// // Serialize the stats to JSON
52
/// let serialized_stats = serde_json::to_string(&stats).unwrap();
53
/// println!("Serialized Stats: {}", serialized_stats);
54
///
55
///
56
/// # Usage Notes
57
/// - This struct is intended to be used in contexts where tracking the performance and behavior of rule-based
58
///   rewriting systems is necessary. It is designed to be easily serialized and deserialized to/from JSON, making it
59
///   suitable for logging, analytics, and reporting purposes.
60
///
61
/// # See Also
62
/// - [`serde_with::skip_serializing_none`]: For skipping `None` values during serialization.
63
/// - [`std::time::Duration`]: For measuring and representing time intervals.
64
#[skip_serializing_none]
65
#[derive(Default, Serialize, Clone, JsonSchema)]
66
#[serde(rename_all = "camelCase")]
67
#[allow(dead_code)]
68
pub struct RewriterStats {
69
    pub is_optimization_enabled: Option<bool>,
70
    pub rewriter_run_time: Option<std::time::Duration>,
71
    pub rewriter_rule_application_attempts: Option<usize>,
72
    pub rewriter_rule_applications: Option<usize>,
73
}
74

            
75
impl RewriterStats {
76
    pub fn new() -> Self {
77
        Self {
78
            is_optimization_enabled: None,
79
            rewriter_run_time: None,
80
            rewriter_rule_application_attempts: None,
81
            rewriter_rule_applications: None,
82
        }
83
    }
84
}