Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 526: Fix blank graph bits #581

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ impl<'a, T: Clone + TimeSeriesValue<T, U>, U: Serialize + Copy + PartialEq + Par
/// it belongs to.
fn add_timestamp_to_html_graph_data(
&self,
data: &[U],
data: &[Option<U>],
started: DateTime<Utc>,
) -> Vec<(String, U)> {
data.iter()
Expand All @@ -573,6 +573,7 @@ impl<'a, T: Clone + TimeSeriesValue<T, U>, U: Serialize + Copy + PartialEq + Par
*value,
)
})
.filter_map(|(time, data_option)| data_option.map(|data| (time, data)))
.collect::<Vec<_>>()
}
}
Expand Down Expand Up @@ -672,7 +673,7 @@ impl<T: Clone + TimeSeriesValue<T, U>, U> TimeSeries<T, U> {

/// Gets time series suitable for usage in HTML graphs (generally each value
/// becomes some kind of a scalar).
fn get_graph_data(&self) -> Vec<U> {
fn get_graph_data(&self) -> Vec<Option<U>> {
self.data
.iter()
.map(|value| value.get_graph_value())
Expand All @@ -696,7 +697,7 @@ impl<T: Clone + TimeSeriesValue<T, U>, U> TimeSeries<T, U> {

/// Gets time series total value (sum of all values).
fn total(&self) -> U {
self.total.get_graph_value()
self.total.get_total_value()
}
}

Expand All @@ -711,7 +712,8 @@ pub trait TimeSeriesValue<T, U> {
/// Merges (adds) another TimeSeriesValue.
fn merge(&mut self, other: &T);
/// Gets representation of the value suitable for HTML graphs (generally a scalar).
fn get_graph_value(&self) -> U;
fn get_graph_value(&self) -> Option<U>;
fn get_total_value(&self) -> U;
}

impl TimeSeriesValue<usize, usize> for usize {
Expand All @@ -727,7 +729,13 @@ impl TimeSeriesValue<usize, usize> for usize {
fn merge(&mut self, other: &usize) {
*self += *other;
}
fn get_graph_value(&self) -> usize {
fn get_graph_value(&self) -> Option<usize> {
match *self == 0 {
true => None,
false => Some(*self),
}
}
fn get_total_value(&self) -> usize {
*self
}
}
Expand All @@ -745,7 +753,13 @@ impl TimeSeriesValue<u32, u32> for u32 {
fn merge(&mut self, other: &u32) {
*self += *other;
}
fn get_graph_value(&self) -> u32 {
fn get_graph_value(&self) -> Option<u32> {
match *self == 0 {
true => None,
false => Some(*self),
}
}
fn get_total_value(&self) -> u32 {
*self
}
}
Expand All @@ -770,7 +784,13 @@ impl TimeSeriesValue<MovingAverage, f32> for MovingAverage {
};
self.count = total_count;
}
fn get_graph_value(&self) -> f32 {
fn get_graph_value(&self) -> Option<f32> {
match self.average == 0f32 {
true => None,
false => Some(self.average),
}
}
fn get_total_value(&self) -> f32 {
self.average
}
}
Expand Down Expand Up @@ -1542,7 +1562,7 @@ mod test {

#[test]
fn test_add_timestamp_to_html_graph_data() {
let data = vec![123, 234, 345, 456, 567];
let data = vec![Some(123), Some(234), Some(345), Some(456), Some(567)];
let graph: Graph<usize, usize> = Graph::new("html_id", "Label", true, HashMap::new());

assert_eq!(
Expand Down
Loading