From 42d7965f3e4abfb9573e179a9c96698658224d06 Mon Sep 17 00:00:00 2001 From: Jeremy Andrews Date: Thu, 17 Aug 2023 15:54:28 +0200 Subject: [PATCH 1/2] don't panic when graph has no data --- CHANGELOG.md | 1 + src/graph.rs | 162 ++++++++++++++++++++++++++------------------------- 2 files changed, 84 insertions(+), 79 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b347e95..700abbe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#552](https://github.com/tag1consulting/goose/pull/552) add `scenario_index`, `scenario_name`, `transaction_index` and `transaction_name` to the request log - [#553](https://github.com/tag1consulting/goose/pull/553) remove `serde_cbor` dependency no longer required due to [#529] - [#554](https://github.com/tag1consulting/goose/pull/554) update `flume`, `itertools`, `strum`, `strum_macros`, `tokio-tungstenite`, and `tungestenite` dependencies to latest versions + - [#555](https://github.com/tag1consulting/goose/pull/555) don't panic when report has no data ## 0.17.0 December 9, 2022 - [#529](https://github.com/tag1consulting/goose/pull/529) **API change** temporaryily removed Gaggle support `gaggle` feature) to allow upgrading Tokio and other dependencies. diff --git a/src/graph.rs b/src/graph.rs index bb479600..b0443063 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -357,79 +357,82 @@ impl<'a, T: Clone + TimeSeriesValue, U: Serialize + Copy + PartialEq + Par } let mut total_values: TimeSeries = TimeSeries::new(); - let (legend, main_label, main_values, other_values) = if self.data.len() > 1 { - // If we are dealing with a metric with granular data we need to calculate totals. - for (_, single_data) in self.data.iter() { - total_values.add_time_series(single_data); - } + if self.data.is_empty() { + "".to_string() + } else { + let (legend, main_label, main_values, other_values) = if self.data.len() > 1 { + // If we are dealing with a metric with granular data we need to calculate totals. + for (_, single_data) in self.data.iter() { + total_values.add_time_series(single_data); + } - // We will have multiple lines. We need to prepare the legend section on the graph - // and create data series for all of them. - let mut legend = vec!["Total"]; - - let mut other_values = String::new(); - if self.granular_data { - // In order for this to sort correctly we need to flip label and time series since tuples - // are sorted lexicographically so we want time series to be the first element of the tuple. - for (sub_data, label) in self - .data - .iter() - .map(|(label, sub_data)| (sub_data, label)) - .sorted() - .rev() - { - legend.push(label); - - let formatted_line = format!( - r#"{{ - name: '{label}', - type: 'line', - symbol: 'none', - sampling: 'lttb', - data: {values}, - }}, - "#, - label = label, - values = json!(self.add_timestamp_to_html_graph_data( - &sub_data.get_graph_data(), - test_started_time - )) - ); - other_values += formatted_line.as_str(); + // We will have multiple lines. We need to prepare the legend section on the graph + // and create data series for all of them. + let mut legend = vec!["Total"]; + + let mut other_values = String::new(); + if self.granular_data { + // In order for this to sort correctly we need to flip label and time series since tuples + // are sorted lexicographically so we want time series to be the first element of the tuple. + for (sub_data, label) in self + .data + .iter() + .map(|(label, sub_data)| (sub_data, label)) + .sorted() + .rev() + { + legend.push(label); + + let formatted_line = format!( + r#"{{ + name: '{label}', + type: 'line', + symbol: 'none', + sampling: 'lttb', + data: {values}, + }}, + "#, + label = label, + values = json!(self.add_timestamp_to_html_graph_data( + &sub_data.get_graph_data(), + test_started_time + )) + ); + other_values += formatted_line.as_str(); + } + ( + format!( + r#"legend: {{ + type: '{legend_type}', + width: '75%', + data: {data}, + }},"#, + legend_type = if self.data.len() > 4 { + "scroll" + } else { + "plain" + }, + data = json!(legend) + ), + "Total", + &total_values, + other_values, + ) + } else { + ("".to_string(), "Total", &total_values, "".to_string()) } + } else { + // If there is only one data series in the metric we simply display it. ( - format!( - r#"legend: {{ - type: '{legend_type}', - width: '75%', - data: {data}, - }},"#, - legend_type = if self.data.len() > 4 { - "scroll" - } else { - "plain" - }, - data = json!(legend) - ), - "Total", - &total_values, - other_values, + "".to_string(), + self.data.keys().next().unwrap().as_str(), + self.data.values().next().unwrap(), + "".to_string(), ) - } else { - ("".to_string(), "Total", &total_values, "".to_string()) - } - } else { - // If there is only one data series in the metric we simply display it. - ( - "".to_string(), - self.data.keys().next().unwrap().as_str(), - self.data.values().next().unwrap(), - "".to_string(), - ) - }; + }; - format!( - r#"
+ format!( + r#"
"#, - html_id = self.html_id, - main_values = json!(self.add_timestamp_to_html_graph_data( - &main_values.get_graph_data(), - test_started_time - )), - y_axis_label = self.y_axis_label, - main_label = main_label, - legend = legend, - other_values = other_values, - steps = steps, - ) + html_id = self.html_id, + main_values = json!(self.add_timestamp_to_html_graph_data( + &main_values.get_graph_data(), + test_started_time + )), + y_axis_label = self.y_axis_label, + main_label = main_label, + legend = legend, + other_values = other_values, + steps = steps, + ) + } } /// Adds timestamps to the graph data series to ensure correct time display on x axis. From 9480849ccd3d7750f67c3572c0dac01eaa973ea2 Mon Sep 17 00:00:00 2001 From: Jeremy Andrews Date: Thu, 17 Aug 2023 17:04:50 +0200 Subject: [PATCH 2/2] fix spacing issues in tests --- src/graph.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index b0443063..f0baa635 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -385,12 +385,12 @@ impl<'a, T: Clone + TimeSeriesValue, U: Serialize + Copy + PartialEq + Par let formatted_line = format!( r#"{{ - name: '{label}', - type: 'line', - symbol: 'none', - sampling: 'lttb', - data: {values}, - }}, + name: '{label}', + type: 'line', + symbol: 'none', + sampling: 'lttb', + data: {values}, + }}, "#, label = label, values = json!(self.add_timestamp_to_html_graph_data( @@ -403,10 +403,10 @@ impl<'a, T: Clone + TimeSeriesValue, U: Serialize + Copy + PartialEq + Par ( format!( r#"legend: {{ - type: '{legend_type}', - width: '75%', - data: {data}, - }},"#, + type: '{legend_type}', + width: '75%', + data: {data}, + }},"#, legend_type = if self.data.len() > 4 { "scroll" } else {