Skip to content

Commit f72dc74

Browse files
committed
Add metric selection
1 parent 201096a commit f72dc74

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

collector/src/compare/mod.rs

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ use ratatui::{
88
prelude::*,
99
widgets::Block,
1010
};
11-
use std::fmt::Display;
1211
use std::future::Future;
1312
use std::pin::Pin;
1413
use std::sync::Arc;
15-
use tabled::settings::object::{Column, Columns};
16-
use tabled::settings::Modify;
1714
use tabled::Tabled;
1815

1916
static ALL_METRICS: &[Metric] = &[
2017
Metric::InstructionsUser,
21-
Metric::Cycles,
18+
Metric::CyclesUser,
2219
Metric::WallTime,
2320
Metric::MaxRSS,
2421
Metric::LinkedArtifactSize,
@@ -31,7 +28,7 @@ static ALL_METRICS: &[Metric] = &[
3128
Metric::CpuClock,
3229
Metric::CpuClockUser,
3330
Metric::CrateMetadataSize,
34-
Metric::CyclesUser,
31+
Metric::Cycles,
3532
Metric::DepGraphSize,
3633
Metric::DocByteSize,
3734
Metric::DwoFileSize,
@@ -301,7 +298,7 @@ impl CompareScreen {
301298
base: Commit,
302299
modified: Commit,
303300
) -> anyhow::Result<Self> {
304-
let pstats = load_data(
301+
let data = load_data(
305302
metric,
306303
&db_state.index,
307304
db_state.db.as_mut(),
@@ -314,10 +311,22 @@ impl CompareScreen {
314311
modified,
315312
db_state,
316313
metric,
317-
data: pstats,
314+
data,
318315
table_state: TableState::default(),
319316
})
320317
}
318+
319+
async fn reload_data(&mut self) -> anyhow::Result<()> {
320+
self.data = load_data(
321+
self.metric,
322+
&self.db_state.index,
323+
self.db_state.db.as_mut(),
324+
&self.base,
325+
&self.modified,
326+
)
327+
.await?;
328+
Ok(())
329+
}
321330
}
322331

323332
impl Screen for CompareScreen {
@@ -330,15 +339,20 @@ impl Screen for CompareScreen {
330339
[
331340
// +2 because of borders
332341
Constraint::Min((summary_table.lines().count() + 2) as u16),
342+
Constraint::Length(2),
333343
Constraint::Percentage(100),
334344
]
335345
.as_ref(),
336346
)
337347
.split(frame.area());
348+
338349
frame.render_widget(
339350
Paragraph::new(Text::raw(summary_table)).block(Block::bordered().title("Summary")),
340351
layout[0],
341352
);
353+
354+
render_metric(frame, self.metric, layout[1]);
355+
342356
let header = Row::new(vec![
343357
Line::from("Benchmark"),
344358
Line::from("Profile"),
@@ -398,7 +412,7 @@ impl Screen for CompareScreen {
398412
.row_highlight_style(Style::new().bold());
399413

400414
let table_layout =
401-
Layout::new(Direction::Horizontal, [Constraint::Max(120)]).split(layout[1]);
415+
Layout::new(Direction::Horizontal, [Constraint::Max(120)]).split(layout[2]);
402416
frame.render_stateful_widget(table, table_layout[0], &mut self.table_state);
403417
}
404418

@@ -410,6 +424,14 @@ impl Screen for CompareScreen {
410424
match key {
411425
KeyCode::Down => self.table_state.select_next(),
412426
KeyCode::Up => self.table_state.select_previous(),
427+
KeyCode::Char('a') => {
428+
self.metric = select_metric(self.metric, -1);
429+
self.reload_data().await?;
430+
}
431+
KeyCode::Char('s') => {
432+
self.metric = select_metric(self.metric, 1);
433+
self.reload_data().await?;
434+
}
413435
_ => {}
414436
}
415437

@@ -418,6 +440,23 @@ impl Screen for CompareScreen {
418440
}
419441
}
420442

443+
fn select_metric(current: Metric, direction: isize) -> Metric {
444+
let index = ALL_METRICS.iter().position(|m| *m == current).unwrap_or(0) as isize;
445+
let index = ((index + direction) + ALL_METRICS.len() as isize) % ALL_METRICS.len() as isize;
446+
ALL_METRICS[index as usize]
447+
}
448+
449+
fn render_metric(frame: &mut Frame, metric: Metric, area: Rect) {
450+
frame.render_widget(
451+
Line::from(vec![
452+
"Metric: ".into(),
453+
metric.as_str().bold(),
454+
" (switch: A/S)".into(),
455+
]),
456+
area,
457+
)
458+
}
459+
421460
async fn load_data(
422461
metric: Metric,
423462
index: &Index,

0 commit comments

Comments
 (0)