Skip to content

Commit 0c5b1bc

Browse files
committed
units_symbol
1 parent 85bc826 commit 0c5b1bc

File tree

4 files changed

+233
-147
lines changed

4 files changed

+233
-147
lines changed

lib/bencher_comment/src/lib.rs

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl ReportComment {
261261
html.push_str("<tbody>");
262262

263263
for alert in &self.json_report.alerts {
264-
let (factor, units) = {
264+
let (factor, units, units_symbol) = {
265265
let mut min = alert.metric.value;
266266
if let Some(lower_limit) = alert.boundary.lower_limit {
267267
min = min.min(lower_limit);
@@ -270,7 +270,11 @@ impl ReportComment {
270270
min = min.min(upper_limit);
271271
}
272272
let units = Units::new(min.into(), alert.threshold.measure.units.clone());
273-
(units.scale_factor(), units.scale_units())
273+
(
274+
units.scale_factor(),
275+
units.scale_units(),
276+
units.scale_units_symbol(),
277+
)
274278
};
275279

276280
html.push_str("<tr>");
@@ -293,7 +297,7 @@ impl ReportComment {
293297
alert.metric.value,
294298
alert.boundary.baseline,
295299
factor,
296-
&units,
300+
&units_symbol,
297301
true,
298302
);
299303
if self.has_lower_boundary_alert() {
@@ -302,7 +306,7 @@ impl ReportComment {
302306
alert.metric.value,
303307
alert.boundary.lower_limit,
304308
factor,
305-
&units,
309+
&units_symbol,
306310
alert.limit == BoundaryLimit::Lower,
307311
);
308312
}
@@ -312,7 +316,7 @@ impl ReportComment {
312316
alert.metric.value,
313317
alert.boundary.upper_limit,
314318
factor,
315-
&units,
319+
&units_symbol,
316320
alert.limit == BoundaryLimit::Upper,
317321
);
318322
}
@@ -329,14 +333,14 @@ impl ReportComment {
329333
));
330334
html.push_str("<br />");
331335
html.push_str(&format!(
332-
"🚨 <a href=\"{url}\">alert ({status})</a>",
333-
url = self.resource_url(Resource::Alert(alert.uuid)),
334-
status = alert_status(alert),
336+
"🚷 <a href=\"{url}\">threshold</a>",
337+
url = self.resource_url(Resource::Threshold(alert.threshold.uuid)),
335338
));
336339
html.push_str("<br />");
337340
html.push_str(&format!(
338-
"🚷 <a href=\"{url}\">threshold</a>",
339-
url = self.resource_url(Resource::Threshold(alert.threshold.uuid)),
341+
"🚨 <a href=\"{url}\">alert ({status})</a>",
342+
url = self.resource_url(Resource::Alert(alert.uuid)),
343+
status = alert_status(alert),
340344
));
341345
html.push_str("</td>");
342346
}
@@ -441,9 +445,9 @@ impl ReportComment {
441445
name = result.benchmark.name,
442446
));
443447
for (measure, boundary_limits) in mbl {
444-
let (factor, units) = {
448+
let (factor, units_symbol) = {
445449
let units = Units::new(boundary_limits.min.into(), measure.units.clone());
446-
(units.scale_factor(), units.scale_units())
450+
(units.scale_factor(), units.scale_units_symbol())
447451
};
448452

449453
let report_measure = result
@@ -469,7 +473,7 @@ impl ReportComment {
469473
report_measure.metric.value,
470474
report_measure.boundary.and_then(|b| b.baseline),
471475
factor,
472-
&units,
476+
&units_symbol,
473477
alert.is_some(),
474478
);
475479
} else {
@@ -482,7 +486,7 @@ impl ReportComment {
482486
report_measure.metric.value,
483487
report_measure.boundary.and_then(|b| b.lower_limit),
484488
factor,
485-
&units,
489+
&units_symbol,
486490
alert.is_some_and(|a| a.limit == BoundaryLimit::Lower),
487491
);
488492
} else {
@@ -496,7 +500,7 @@ impl ReportComment {
496500
report_measure.metric.value,
497501
report_measure.boundary.and_then(|b| b.upper_limit),
498502
factor,
499-
&units,
503+
&units_symbol,
500504
alert.is_some_and(|a| a.limit == BoundaryLimit::Upper),
501505
);
502506
} else {
@@ -526,14 +530,6 @@ impl ReportComment {
526530
Some(boundary_limits)
527531
)
528532
));
529-
if let Some(alert) = alert {
530-
html.push_str("<br />");
531-
html.push_str(&format!(
532-
"🚨 <a href=\"{url}\">view alert ({status})</a>",
533-
url = self.resource_url(Resource::Alert(alert.uuid)),
534-
status = alert_status(alert),
535-
));
536-
}
537533
if let Some(threshold) = &report_measure.threshold {
538534
html.push_str("<br />");
539535
html.push_str(&format!(
@@ -544,6 +540,14 @@ impl ReportComment {
544540
html.push_str("<br />");
545541
html.push_str("⚠️ NO THRESHOLD");
546542
}
543+
if let Some(alert) = alert {
544+
html.push_str("<br />");
545+
html.push_str(&format!(
546+
"🚨 <a href=\"{url}\">view alert ({status})</a>",
547+
url = self.resource_url(Resource::Alert(alert.uuid)),
548+
status = alert_status(alert),
549+
));
550+
}
547551
html.push_str("</td>");
548552
}
549553

@@ -802,19 +806,18 @@ fn value_cell(
802806
value: OrderedFloat<f64>,
803807
baseline: Option<OrderedFloat<f64>>,
804808
factor: OrderedFloat<f64>,
805-
units: &str,
809+
units_symbol: &str,
806810
bold: bool,
807811
) {
808812
fn value_cell_inner(
809813
value: OrderedFloat<f64>,
810814
baseline: Option<OrderedFloat<f64>>,
811815
factor: OrderedFloat<f64>,
812-
units: &str,
816+
units_symbol: &str,
813817
) -> String {
814818
let mut cell = Units::format_float((value / factor).into());
815-
let short_units = short_units(units);
816-
if let Some(short_units) = &short_units {
817-
cell.push_str(short_units);
819+
if !units_symbol.is_empty() {
820+
cell.push_str(&format!(" {units_symbol}"));
818821
}
819822

820823
if let Some(baseline) = baseline {
@@ -832,8 +835,8 @@ fn value_cell(
832835
cell.push_str(&format!("({plus}{percent}%)"));
833836
cell.push_str("</summary>");
834837
cell.push_str(&format!("Baseline: {baseline}"));
835-
if let Some(short_units) = &short_units {
836-
cell.push_str(short_units);
838+
if !units_symbol.is_empty() {
839+
cell.push_str(&format!(" {units_symbol}"));
837840
}
838841
cell.push_str("</details>");
839842
}
@@ -845,10 +848,10 @@ fn value_cell(
845848
if bold {
846849
html.push_str(&format!(
847850
"<b>{}</b>",
848-
value_cell_inner(value, baseline, factor, units)
851+
value_cell_inner(value, baseline, factor, units_symbol)
849852
));
850853
} else {
851-
html.push_str(&value_cell_inner(value, baseline, factor, units));
854+
html.push_str(&value_cell_inner(value, baseline, factor, units_symbol));
852855
}
853856
html.push_str("</td>");
854857
}
@@ -858,7 +861,7 @@ fn lower_limit_cell(
858861
value: OrderedFloat<f64>,
859862
lower_limit: Option<OrderedFloat<f64>>,
860863
factor: OrderedFloat<f64>,
861-
units: &str,
864+
units_symbol: &str,
862865
bold: bool,
863866
) {
864867
let Some(limit) = lower_limit else {
@@ -872,15 +875,15 @@ fn lower_limit_cell(
872875
0.0.into()
873876
};
874877

875-
limit_cell(html, limit, percent, factor, units, bold);
878+
limit_cell(html, limit, percent, factor, units_symbol, bold);
876879
}
877880

878881
fn upper_limit_cell(
879882
html: &mut String,
880883
value: OrderedFloat<f64>,
881884
upper_limit: Option<OrderedFloat<f64>>,
882885
factor: OrderedFloat<f64>,
883-
units: &str,
886+
units_symbol: &str,
884887
bold: bool,
885888
) {
886889
let Some(limit) = upper_limit else {
@@ -894,26 +897,26 @@ fn upper_limit_cell(
894897
0.0.into()
895898
};
896899

897-
limit_cell(html, limit, percent, factor, units, bold);
900+
limit_cell(html, limit, percent, factor, units_symbol, bold);
898901
}
899902

900903
fn limit_cell(
901904
html: &mut String,
902905
limit: OrderedFloat<f64>,
903906
percent: OrderedFloat<f64>,
904907
factor: OrderedFloat<f64>,
905-
units: &str,
908+
units_symbol: &str,
906909
bold: bool,
907910
) {
908911
fn limit_cell_inner(
909912
limit: OrderedFloat<f64>,
910913
percent: OrderedFloat<f64>,
911914
factor: OrderedFloat<f64>,
912-
units: &str,
915+
units_symbol: &str,
913916
) -> String {
914917
let mut cell = Units::format_float((limit / factor).into());
915-
if let Some(short_units) = short_units(units) {
916-
cell.push_str(&short_units);
918+
if !units_symbol.is_empty() {
919+
cell.push_str(&format!(" {units_symbol}"));
917920
}
918921
let percent = Units::format_float(percent.into());
919922
cell.push_str(&format!("<br />({percent}%)"));
@@ -927,21 +930,14 @@ fn limit_cell(
927930
// where the row cells are vertically aligned to each other.
928931
html.push_str(&format!(
929932
"<b>{}<br /><br /></b>",
930-
limit_cell_inner(limit, percent, factor, units)
933+
limit_cell_inner(limit, percent, factor, units_symbol)
931934
));
932935
} else {
933-
html.push_str(&limit_cell_inner(limit, percent, factor, units));
936+
html.push_str(&limit_cell_inner(limit, percent, factor, units_symbol));
934937
}
935938
html.push_str("</td>");
936939
}
937940

938-
fn short_units(units: &str) -> Option<String> {
939-
units
940-
.split_once('(')
941-
.and_then(|(_, delimited)| delimited.split_once(')'))
942-
.map(|(units, _)| format!(" {units}"))
943-
}
944-
945941
#[derive(Clone, Copy)]
946942
pub struct BoundaryLimits {
947943
min: OrderedFloat<f64>,

0 commit comments

Comments
 (0)