Skip to content

Commit f55bde5

Browse files
apollo_dashboard: simpler new fn for Panel
1 parent 6b0e112 commit f55bde5

File tree

14 files changed

+160
-148
lines changed

14 files changed

+160
-148
lines changed

crates/apollo_dashboard/src/dashboard.rs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,58 @@ pub(crate) struct Panel {
137137
extra: ExtraParams,
138138
}
139139

140+
// helper: unify "String" and "Vec<String>"
141+
pub(crate) trait IntoExprs {
142+
fn into_exprs(self) -> Vec<String>;
143+
}
144+
145+
impl IntoExprs for String {
146+
fn into_exprs(self) -> Vec<String> {
147+
vec![self]
148+
}
149+
}
150+
151+
impl IntoExprs for &str {
152+
fn into_exprs(self) -> Vec<String> {
153+
vec![self.to_string()]
154+
}
155+
}
156+
157+
impl IntoExprs for Vec<String> {
158+
fn into_exprs(self) -> Vec<String> {
159+
self
160+
}
161+
}
162+
163+
impl IntoExprs for Vec<&str> {
164+
fn into_exprs(self) -> Vec<String> {
165+
self.into_iter().map(|s| s.to_string()).collect::<Vec<_>>()
166+
}
167+
}
168+
140169
impl Panel {
141170
pub(crate) fn new(
142171
name: impl ToString,
143172
description: impl ToString,
144-
exprs: Vec<String>,
173+
exprs: impl IntoExprs,
145174
panel_type: PanelType,
146175
) -> Self {
147176
// A panel assigns a unique id to each of its expressions. Conventionally, we use letters
148177
// A–Z, and for simplicity, we limit the number of expressions to this range.
149178
const NUM_LETTERS: u8 = b'Z' - b'A' + 1;
150179
let name = name.to_string();
151180
let description = description.to_string();
181+
182+
let exprs = exprs.into_exprs();
183+
152184
assert!(
153185
exprs.len() <= NUM_LETTERS.into(),
154186
"Too many expressions ({} > {NUM_LETTERS}) in panel '{name}'.",
155187
exprs.len(),
156188
);
157189
Self { name, description, exprs, panel_type, extra: ExtraParams::default() }
158190
}
191+
159192
pub fn with_unit(mut self, unit: Unit) -> Self {
160193
self.extra.unit = Some(unit);
161194
self
@@ -265,7 +298,7 @@ impl Panel {
265298
(rate({metric_name_with_filter_and_reason}[{HISTOGRAM_TIME_RANGE}])))",
266299
)
267300
})
268-
.collect(),
301+
.collect::<Vec<_>>(),
269302
panel_type,
270303
)
271304
}
@@ -288,14 +321,14 @@ impl Panel {
288321

289322
let expr = format!("({numerator_expr} / ({denominator_expr}))");
290323

291-
Self::new(name, description, vec![expr], PanelType::TimeSeries).with_unit(Unit::PercentUnit)
324+
Self::new(name, description, expr, PanelType::TimeSeries).with_unit(Unit::PercentUnit)
292325
}
293326

294327
pub(crate) fn from_counter(metric: &MetricCounter, panel_type: PanelType) -> Self {
295328
Self::new(
296329
metric.get_name(),
297330
metric.get_description(),
298-
vec![metric.get_name_with_filter().to_string()],
331+
metric.get_name_with_filter().to_string(),
299332
panel_type,
300333
)
301334
}
@@ -304,7 +337,7 @@ impl Panel {
304337
Self::new(
305338
metric.get_name(),
306339
metric.get_description(),
307-
vec![metric.get_name_with_filter().to_string()],
340+
metric.get_name_with_filter().to_string(),
308341
panel_type,
309342
)
310343
}
@@ -328,7 +361,7 @@ impl Panel {
328361
metric_name_with_filter.as_ref(),
329362
)
330363
})
331-
.collect(),
364+
.collect::<Vec<_>>(),
332365
PanelType::TimeSeries,
333366
)
334367
}

crates/apollo_dashboard/src/dashboard_test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn test_ratio_time_series() {
9191

9292
#[test]
9393
fn test_extra_params() {
94-
let panel_with_extra_params = Panel::new("x", "x", vec!["y".to_string()], PanelType::Stat)
94+
let panel_with_extra_params = Panel::new("x", "x", "y".to_string(), PanelType::Stat)
9595
.with_unit(Unit::Bytes)
9696
.show_percent_change()
9797
.with_log_query("Query")
@@ -118,7 +118,7 @@ fn test_extra_params() {
118118
);
119119
assert_eq!(panel_with_extra_params.extra.legends, Some(vec!["a".to_string()]));
120120

121-
let panel_without_extra_params = Panel::new("x", "x", vec!["y".to_string()], PanelType::Stat);
121+
let panel_without_extra_params = Panel::new("x", "x", "y".to_string(), PanelType::Stat);
122122
assert!(panel_without_extra_params.extra.unit.is_none());
123123
assert!(panel_without_extra_params.extra.show_percent_change.is_none());
124124
assert!(panel_without_extra_params.extra.log_query.is_none());
@@ -129,7 +129,7 @@ fn test_extra_params() {
129129
#[test]
130130
#[should_panic]
131131
fn thresholds_first_step_must_be_none() {
132-
let _ = Panel::new("x", "x", vec!["y".into()], PanelType::Stat).with_absolute_thresholds(vec![
132+
let _ = Panel::new("x", "x", "y", PanelType::Stat).with_absolute_thresholds(vec![
133133
("green", Some(0.0)), // illegal: first step must be None
134134
("red", Some(80.0)),
135135
]);
@@ -138,7 +138,7 @@ fn thresholds_first_step_must_be_none() {
138138
#[test]
139139
#[should_panic]
140140
fn thresholds_must_be_strictly_increasing() {
141-
let _ = Panel::new("x", "x", vec!["y".into()], PanelType::Stat).with_absolute_thresholds(vec![
141+
let _ = Panel::new("x", "x", "y", PanelType::Stat).with_absolute_thresholds(vec![
142142
("green", None),
143143
("red", Some(90.0)),
144144
("yellow", Some(80.0)), // illegal: not increasing
@@ -148,5 +148,5 @@ fn thresholds_must_be_strictly_increasing() {
148148
#[test]
149149
#[should_panic]
150150
fn amount_of_legends_must_match_amount_of_exprs() {
151-
let _ = Panel::new("x", "x", vec!["y".into()], PanelType::Stat).with_legends(vec!["a", "b"]);
151+
let _ = Panel::new("x", "x", "y", PanelType::Stat).with_legends(vec!["a", "b"]);
152152
}

crates/apollo_dashboard/src/panels/batcher.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn get_panel_validator_wasted_txs() -> Panel {
1919
Panel::new(
2020
"Proposal Validation: Wasted TXs",
2121
"Number of txs executed by the validator but excluded from the block (10m window)",
22-
vec![format!("increase({}[10m])", VALIDATOR_WASTED_TXS.get_name_with_filter())],
22+
format!("increase({}[10m])", VALIDATOR_WASTED_TXS.get_name_with_filter()),
2323
PanelType::TimeSeries,
2424
)
2525
.with_log_query("Finished building block as validator. Started executing")
@@ -29,7 +29,7 @@ fn get_panel_proposer_deferred_txs() -> Panel {
2929
Panel::new(
3030
"Proposal Build: Deferred TXs",
3131
"Number of txs started execution by the proposer but excluded from the block (10m window)",
32-
vec![format!("increase({}[10m])", PROPOSER_DEFERRED_TXS.get_name_with_filter())],
32+
format!("increase({}[10m])", PROPOSER_DEFERRED_TXS.get_name_with_filter()),
3333
PanelType::TimeSeries,
3434
)
3535
.with_log_query("Finished building block as proposer. Started executing")
@@ -39,7 +39,7 @@ fn get_panel_storage_height() -> Panel {
3939
Panel::new(
4040
"Storage Height",
4141
"The height of the batcher's storage",
42-
vec![STORAGE_HEIGHT.get_name_with_filter().to_string()],
42+
STORAGE_HEIGHT.get_name_with_filter().to_string(),
4343
PanelType::Stat,
4444
)
4545
.with_log_query("Committing block at height")
@@ -75,7 +75,7 @@ pub(crate) fn get_panel_batched_transactions_rate() -> Panel {
7575
Panel::new(
7676
"Batched Transactions Rate (TPS)",
7777
"The rate of transactions batched by the Batcher (1m window)",
78-
vec![format!("rate({}[1m])", BATCHED_TRANSACTIONS.get_name_with_filter())],
78+
format!("rate({}[1m])", BATCHED_TRANSACTIONS.get_name_with_filter()),
7979
PanelType::TimeSeries,
8080
)
8181
.with_log_query("BATCHER_FIN_VALIDATOR")
@@ -85,11 +85,11 @@ fn get_panel_block_close_reasons() -> Panel {
8585
Panel::new(
8686
"Block Close Reasons",
8787
"Number of blocks closed by reason (10m window)",
88-
vec![format!(
88+
format!(
8989
"sum by ({}) (increase({}[10m]))",
9090
LABEL_NAME_BLOCK_CLOSE_REASON,
9191
BLOCK_CLOSE_REASON.get_name_with_filter()
92-
)],
92+
),
9393
PanelType::Stat,
9494
)
9595
.with_log_query("\"Block builder deadline reached.\" OR \"Block is full.\"")
@@ -99,7 +99,7 @@ fn get_panel_num_batches_in_proposal() -> Panel {
9999
Panel::new(
100100
"Number of Chunks in Proposal",
101101
"The number of transaction batches received in a valid proposal",
102-
vec![CONSENSUS_NUM_BATCHES_IN_PROPOSAL.get_name_with_filter().to_string()],
102+
CONSENSUS_NUM_BATCHES_IN_PROPOSAL.get_name_with_filter().to_string(),
103103
PanelType::TimeSeries,
104104
)
105105
}
@@ -108,7 +108,7 @@ fn get_panel_num_txs_in_proposal() -> Panel {
108108
Panel::new(
109109
"Number of Transactions in Proposal",
110110
"The total number of individual transactions in a valid proposal received",
111-
vec![CONSENSUS_NUM_TXS_IN_PROPOSAL.get_name_with_filter().to_string()],
111+
CONSENSUS_NUM_TXS_IN_PROPOSAL.get_name_with_filter().to_string(),
112112
PanelType::TimeSeries,
113113
)
114114
.with_log_query("BATCHER_FIN_PROPOSER")

crates/apollo_dashboard/src/panels/blockifier.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ fn get_panel_sierra_gas_in_last_block() -> Panel {
6666
Panel::new(
6767
"Average Sierra Gas Usage in Block",
6868
"The average sierra gas usage in block (10m window)",
69-
vec![format!(
69+
format!(
7070
"avg_over_time({}[10m])/{}",
7171
SIERRA_GAS_IN_LAST_BLOCK.get_name_with_filter(),
7272
DENOMINATOR_DIVISOR_FOR_READABILITY
73-
)],
73+
),
7474
PanelType::TimeSeries,
7575
)
7676
}
@@ -79,11 +79,11 @@ fn get_panel_proving_gas_in_last_block() -> Panel {
7979
Panel::new(
8080
"Average Proving Gas Usage in Block",
8181
"The average proving gas usage in block (10m window)",
82-
vec![format!(
82+
format!(
8383
"avg_over_time({}[10m])/{}",
8484
PROVING_GAS_IN_LAST_BLOCK.get_name_with_filter(),
8585
DENOMINATOR_DIVISOR_FOR_READABILITY
86-
)],
86+
),
8787
PanelType::TimeSeries,
8888
)
8989
}

0 commit comments

Comments
 (0)