Handle NaNs in diagnostics (#12633)
# Objective Fixes #12628. ## Solution Added several check for NaN values in `add_measurement`.
This commit is contained in:
parent
2f6d8663d0
commit
ba0f033e8f
@ -130,7 +130,9 @@ pub struct Diagnostic {
|
|||||||
impl Diagnostic {
|
impl Diagnostic {
|
||||||
/// Add a new value as a [`DiagnosticMeasurement`].
|
/// Add a new value as a [`DiagnosticMeasurement`].
|
||||||
pub fn add_measurement(&mut self, measurement: DiagnosticMeasurement) {
|
pub fn add_measurement(&mut self, measurement: DiagnosticMeasurement) {
|
||||||
if let Some(previous) = self.measurement() {
|
if measurement.value.is_nan() {
|
||||||
|
// Skip calculating the moving average.
|
||||||
|
} else if let Some(previous) = self.measurement() {
|
||||||
let delta = (measurement.time - previous.time).as_secs_f64();
|
let delta = (measurement.time - previous.time).as_secs_f64();
|
||||||
let alpha = (delta / self.ema_smoothing_factor).clamp(0.0, 1.0);
|
let alpha = (delta / self.ema_smoothing_factor).clamp(0.0, 1.0);
|
||||||
self.ema += alpha * (measurement.value - self.ema);
|
self.ema += alpha * (measurement.value - self.ema);
|
||||||
@ -139,17 +141,25 @@ impl Diagnostic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if self.max_history_length > 1 {
|
if self.max_history_length > 1 {
|
||||||
if self.history.len() == self.max_history_length {
|
if self.history.len() >= self.max_history_length {
|
||||||
if let Some(removed_diagnostic) = self.history.pop_front() {
|
if let Some(removed_diagnostic) = self.history.pop_front() {
|
||||||
|
if !removed_diagnostic.value.is_nan() {
|
||||||
self.sum -= removed_diagnostic.value;
|
self.sum -= removed_diagnostic.value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if measurement.value.is_finite() {
|
||||||
self.sum += measurement.value;
|
self.sum += measurement.value;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
self.history.clear();
|
self.history.clear();
|
||||||
|
if measurement.value.is_nan() {
|
||||||
|
self.sum = 0.0;
|
||||||
|
} else {
|
||||||
self.sum = measurement.value;
|
self.sum = measurement.value;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.history.push_back(measurement);
|
self.history.push_back(measurement);
|
||||||
}
|
}
|
||||||
@ -172,8 +182,13 @@ impl Diagnostic {
|
|||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn with_max_history_length(mut self, max_history_length: usize) -> Self {
|
pub fn with_max_history_length(mut self, max_history_length: usize) -> Self {
|
||||||
self.max_history_length = max_history_length;
|
self.max_history_length = max_history_length;
|
||||||
self.history.reserve(self.max_history_length);
|
|
||||||
self.history.shrink_to(self.max_history_length);
|
// reserve/reserve_exact reserve space for n *additional* elements.
|
||||||
|
let expected_capacity = self
|
||||||
|
.max_history_length
|
||||||
|
.saturating_sub(self.history.capacity());
|
||||||
|
self.history.reserve_exact(expected_capacity);
|
||||||
|
self.history.shrink_to(expected_capacity);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user