Skip to main content

conjure_cp_core/stats/
rewriter_stats.rs

1use schemars::JsonSchema;
2use serde::Serialize;
3use 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 optimisation 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_optimisation_enabled`:
18///   - Type: `Option<bool>`
19///   - Indicates whether optimisations were enabled during the rewriting process.
20///   - If `Some(true)`, it means optimisations were enabled.
21///   - If `Some(false)`, optimisations were explicitly disabled.
22///   - If `None`, the status of optimisations 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/// - `rewriter_value_letting_rewrites`:
43///   - Type: `Option<usize>`
44///   - The number of value-letting bodies rewritten by the rewriter.
45///   - If `None`, this metric is not tracked or not applicable for the current session.
46///
47/// # Example
48///
49/// let stats = RewriterStats {
50///     is_optimisation_enabled: Some(true),
51///     rewriter_run_time: Some(std::time::Duration::new(2, 0)),
52///     rewriter_rule_application_attempts: Some(15),
53///     rewriter_rule_applications: Some(10),
54/// };
55///
56/// // Serialize the stats to JSON
57/// let serialized_stats = serde_json::to_string(&stats).unwrap();
58/// println!("Serialized Stats: {}", serialized_stats);
59///
60///
61/// # Usage Notes
62/// - This struct is intended to be used in contexts where tracking the performance and behavior of rule-based
63///   rewriting systems is necessary. It is designed to be easily serialized and deserialized to/from JSON, making it
64///   suitable for logging, analytics, and reporting purposes.
65///
66/// # See Also
67/// - [`serde_with::skip_serializing_none`]: For skipping `None` values during serialization.
68/// - [`std::time::Duration`]: For measuring and representing time intervals.
69#[skip_serializing_none]
70#[derive(Default, Serialize, Clone, JsonSchema)]
71#[serde(rename_all = "camelCase")]
72#[allow(dead_code)]
73pub struct RewriterStats {
74    pub is_optimisation_enabled: Option<bool>,
75    pub rewriter_run_time: Option<std::time::Duration>,
76    pub rewriter_rule_application_attempts: Option<usize>,
77    pub rewriter_rule_applications: Option<usize>,
78    pub rewriter_value_letting_rewrites: Option<usize>,
79}
80
81impl RewriterStats {
82    pub fn new() -> Self {
83        Self {
84            is_optimisation_enabled: None,
85            rewriter_run_time: None,
86            rewriter_rule_application_attempts: None,
87            rewriter_rule_applications: None,
88            rewriter_value_letting_rewrites: Some(0),
89        }
90    }
91}