Skip to main content

euv_performance/observability/
impl.rs

1use super::*;
2
3/// Inherent implementation of [`ObservabilityCoverage`].
4impl ObservabilityCoverage {
5    /// Returns the optional diagnostic reason explaining missing/unsupported coverage.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Coverage item reference.
10    ///
11    /// # Returns
12    ///
13    /// - `Option<&str>` - Reason text when present.
14    #[must_use]
15    pub fn reason_value(&self) -> Option<&str> {
16        let Self { reason, .. } = self;
17        reason.as_deref()
18    }
19}
20
21/// Inherent implementation of [`ObservabilityMetric`].
22impl ObservabilityMetric {
23    /// Creates a measured common scalar metric conforming to `schemas/report-v1.schema.json`.
24    ///
25    /// # Arguments
26    ///
27    /// - `&str` - Canonical catalog metric ID.
28    /// - `&str` - Coverage scope name.
29    /// - `ObservabilityUnit` - Canonical metric unit.
30    /// - `&str` - Sampling window label.
31    /// - `ObservabilityStatistic` - Summary statistic tag.
32    /// - `f64` - Measured scalar value.
33    ///
34    /// # Returns
35    ///
36    /// - `Self` - Measured common metric entry.
37    #[must_use]
38    pub fn measured_common(
39        id: &str,
40        scope: &str,
41        unit: ObservabilityUnit,
42        window: &str,
43        statistic: ObservabilityStatistic,
44        value: f64,
45    ) -> Self {
46        Self {
47            id: id.to_string(),
48            scope: scope.to_string(),
49            unit,
50            status: ObservabilityMetricStatus::Measured,
51            window: window.to_string(),
52            statistic,
53            value: Some(value),
54            baseline: None,
55            after_drain: None,
56            slope_per_cycle: None,
57            samples: None,
58            source: Some(OBS_SOURCE_EUV_PERF.to_string()),
59            clock: Some(OBS_CLOCK_MONOTONIC.to_string()),
60            sample_rate_hz: None,
61            reason: None,
62            evidence_refs: Vec::new(),
63            threshold: None,
64        }
65    }
66
67    /// Creates a measured leak/lifecycle metric carrying explicit `baseline`, `after_drain`, and `slope_per_cycle`.
68    ///
69    /// # Arguments
70    ///
71    /// - `&str` - Canonical catalog leak metric ID.
72    /// - `&str` - Coverage scope name.
73    /// - `ObservabilityUnit` - Canonical metric unit.
74    /// - `&str` - Sampling window label.
75    /// - `f64` - Post-warmup baseline value.
76    /// - `f64` - Post-drain value.
77    /// - `f64` - Linear slope per lifecycle cycle.
78    ///
79    /// # Returns
80    ///
81    /// - `Self` - Measured leak metric entry.
82    #[must_use]
83    pub fn measured_leak(
84        id: &str,
85        scope: &str,
86        unit: ObservabilityUnit,
87        window: &str,
88        baseline: f64,
89        after_drain: f64,
90        slope_per_cycle: f64,
91    ) -> Self {
92        Self {
93            id: id.to_string(),
94            scope: scope.to_string(),
95            unit,
96            status: ObservabilityMetricStatus::Measured,
97            window: window.to_string(),
98            statistic: ObservabilityStatistic::LiveCount,
99            value: None,
100            baseline: Some(baseline),
101            after_drain: Some(after_drain),
102            slope_per_cycle: Some(slope_per_cycle),
103            samples: None,
104            source: Some(OBS_SOURCE_REACTIVE_ARENA.to_string()),
105            clock: Some(OBS_CLOCK_MONOTONIC.to_string()),
106            sample_rate_hz: None,
107            reason: None,
108            evidence_refs: Vec::new(),
109            threshold: None,
110        }
111    }
112
113    /// Attaches a sample count to this metric entry.
114    ///
115    /// # Arguments
116    ///
117    /// - `Self` - Metric builder.
118    /// - `u64` - Sample count collected during the window.
119    ///
120    /// # Returns
121    ///
122    /// - `Self` - Updated metric entry.
123    #[must_use]
124    pub fn with_samples(mut self, samples: u64) -> Self {
125        let Self { samples: slot, .. } = &mut self;
126        *slot = Some(samples);
127        self
128    }
129
130    /// Attaches a budget threshold to this metric entry.
131    ///
132    /// # Arguments
133    ///
134    /// - `Self` - Metric builder.
135    /// - `ObservabilityThreshold` - Budget threshold contract.
136    ///
137    /// # Returns
138    ///
139    /// - `Self` - Updated metric entry.
140    #[must_use]
141    pub fn with_threshold(mut self, threshold: ObservabilityThreshold) -> Self {
142        let Self {
143            threshold: slot, ..
144        } = &mut self;
145        *slot = Some(threshold);
146        self
147    }
148
149    /// Attaches linked evidence identifiers to this metric entry.
150    ///
151    /// # Arguments
152    ///
153    /// - `Self` - Metric builder.
154    /// - `Vec<String>` - Evidence reference IDs.
155    ///
156    /// # Returns
157    ///
158    /// - `Self` - Updated metric entry.
159    #[must_use]
160    pub fn with_evidence_refs(mut self, refs: Vec<String>) -> Self {
161        let Self { evidence_refs, .. } = &mut self;
162        *evidence_refs = refs;
163        self
164    }
165
166    /// Returns the measured scalar value when present.
167    ///
168    /// # Arguments
169    ///
170    /// - `&Self` - Metric reference.
171    ///
172    /// # Returns
173    ///
174    /// - `Option<f64>` - Scalar value.
175    #[must_use]
176    pub fn scalar_value(&self) -> Option<f64> {
177        let Self { value, .. } = *self;
178        value
179    }
180
181    /// Returns the baseline leak measurement when present.
182    ///
183    /// # Arguments
184    ///
185    /// - `&Self` - Metric reference.
186    ///
187    /// # Returns
188    ///
189    /// - `Option<f64>` - Baseline value.
190    #[must_use]
191    pub fn baseline_value(&self) -> Option<f64> {
192        let Self { baseline, .. } = *self;
193        baseline
194    }
195
196    /// Returns the post-drain leak measurement when present.
197    ///
198    /// # Arguments
199    ///
200    /// - `&Self` - Metric reference.
201    ///
202    /// # Returns
203    ///
204    /// - `Option<f64>` - Post-drain value.
205    #[must_use]
206    pub fn after_drain_value(&self) -> Option<f64> {
207        let Self { after_drain, .. } = *self;
208        after_drain
209    }
210
211    /// Returns the per-cycle leak slope when present.
212    ///
213    /// # Arguments
214    ///
215    /// - `&Self` - Metric reference.
216    ///
217    /// # Returns
218    ///
219    /// - `Option<f64>` - Slope per cycle.
220    #[must_use]
221    pub fn slope_per_cycle_value(&self) -> Option<f64> {
222        let Self {
223            slope_per_cycle, ..
224        } = *self;
225        slope_per_cycle
226    }
227
228    /// Returns `true` if any numeric sample field (`value`, `baseline`, `after_drain`, `slope_per_cycle`) is populated.
229    ///
230    /// # Arguments
231    ///
232    /// - `&Self` - Metric reference.
233    ///
234    /// # Returns
235    ///
236    /// - `bool` - Whether numeric measurements are present.
237    #[must_use]
238    pub fn has_any_numeric_value(&self) -> bool {
239        let Self {
240            value,
241            baseline,
242            after_drain,
243            slope_per_cycle,
244            ..
245        } = *self;
246        value.is_some() || baseline.is_some() || after_drain.is_some() || slope_per_cycle.is_some()
247    }
248}