|
| 1 | +use console_api as proto; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::time::Duration; |
| 4 | +use tui::{ |
| 5 | + layout, |
| 6 | + style::{self, Style}, |
| 7 | + widgets::{Block, Cell, Row, Table, TableState}, |
| 8 | +}; |
| 9 | + |
| 10 | +#[derive(Default, Debug)] |
| 11 | +pub(crate) struct State { |
| 12 | + tasks: HashMap<u64, Task>, |
| 13 | + table_state: TableState, |
| 14 | +} |
| 15 | + |
| 16 | +#[derive(Default, Debug)] |
| 17 | +struct Task { |
| 18 | + id_hex: String, |
| 19 | + fields: String, |
| 20 | + kind: &'static str, |
| 21 | + stats: Stats, |
| 22 | +} |
| 23 | + |
| 24 | +#[derive(Default, Debug)] |
| 25 | +struct Stats { |
| 26 | + polls: u64, |
| 27 | + busy: Duration, |
| 28 | + idle: Duration, |
| 29 | + total: Duration, |
| 30 | +} |
| 31 | +impl State { |
| 32 | + pub(crate) fn len(&self) -> usize { |
| 33 | + self.tasks.len() |
| 34 | + } |
| 35 | + pub(crate) fn update(&mut self, update: proto::tasks::TaskUpdate) { |
| 36 | + let new_tasks = update.new_tasks.into_iter().filter_map(|task| { |
| 37 | + if task.id.is_none() { |
| 38 | + tracing::warn!(?task, "skipping task with no id"); |
| 39 | + } |
| 40 | + let kind = match task.kind() { |
| 41 | + proto::tasks::task::Kind::Spawn => "T", |
| 42 | + proto::tasks::task::Kind::Blocking => "B", |
| 43 | + }; |
| 44 | + |
| 45 | + let id = task.id?.id; |
| 46 | + let task = Task { |
| 47 | + id_hex: format!("{:x}", id), |
| 48 | + fields: task.string_fields, |
| 49 | + kind, |
| 50 | + stats: Default::default(), |
| 51 | + }; |
| 52 | + Some((id, task)) |
| 53 | + }); |
| 54 | + self.tasks.extend(new_tasks); |
| 55 | + |
| 56 | + for (id, stats) in update.stats_update { |
| 57 | + if let Some(task) = self.tasks.get_mut(&id) { |
| 58 | + task.stats = stats.into(); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + for proto::SpanId { id } in update.completed { |
| 63 | + if self.tasks.remove(&id).is_none() { |
| 64 | + tracing::warn!(?id, "tried to complete a task that didn't exist"); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + pub(crate) fn render<B: tui::backend::Backend>( |
| 70 | + &mut self, |
| 71 | + frame: &mut tui::terminal::Frame<B>, |
| 72 | + area: layout::Rect, |
| 73 | + ) { |
| 74 | + const HEADER: &[&str] = &["TID", "KIND", "TOTAL", "BUSY", "IDLE", "POLLS", "FIELDS"]; |
| 75 | + const DUR_LEN: usize = 10; |
| 76 | + // This data is only updated every second, so it doesn't make a ton of |
| 77 | + // sense to have a lot of precision in timestamps (and this makes sure |
| 78 | + // there's room for the unit!) |
| 79 | + const DUR_PRECISION: usize = 4; |
| 80 | + const POLLS_LEN: usize = 5; |
| 81 | + let rows = self.tasks.values().map(|task| { |
| 82 | + let row = Row::new(vec![ |
| 83 | + Cell::from(task.id_hex.as_str()), |
| 84 | + // TODO(eliza): is there a way to write a `fmt::Debug` impl |
| 85 | + // directly to tui without doing an allocation? |
| 86 | + Cell::from(task.kind), |
| 87 | + Cell::from(format!( |
| 88 | + "{:>width$.prec$?}", |
| 89 | + task.stats.total, |
| 90 | + width = DUR_LEN, |
| 91 | + prec = DUR_PRECISION, |
| 92 | + )), |
| 93 | + Cell::from(format!( |
| 94 | + "{:>width$.prec$?}", |
| 95 | + task.stats.busy, |
| 96 | + width = DUR_LEN, |
| 97 | + prec = DUR_PRECISION, |
| 98 | + )), |
| 99 | + Cell::from(format!( |
| 100 | + "{:>width$.prec$?}", |
| 101 | + task.stats.idle, |
| 102 | + width = DUR_LEN, |
| 103 | + prec = DUR_PRECISION, |
| 104 | + )), |
| 105 | + Cell::from(format!("{:>width$}", task.stats.polls, width = POLLS_LEN)), |
| 106 | + Cell::from(task.fields.as_str()), |
| 107 | + ]); |
| 108 | + row |
| 109 | + }); |
| 110 | + let t = Table::new(rows) |
| 111 | + .header( |
| 112 | + Row::new(HEADER.iter().map(|&v| Cell::from(v))) |
| 113 | + .height(1) |
| 114 | + .style(Style::default().add_modifier(style::Modifier::REVERSED)), |
| 115 | + ) |
| 116 | + .block(Block::default()) |
| 117 | + .widths(&[ |
| 118 | + layout::Constraint::Min(20), |
| 119 | + layout::Constraint::Length(4), |
| 120 | + layout::Constraint::Min(DUR_LEN as u16), |
| 121 | + layout::Constraint::Min(DUR_LEN as u16), |
| 122 | + layout::Constraint::Min(DUR_LEN as u16), |
| 123 | + layout::Constraint::Min(POLLS_LEN as u16), |
| 124 | + layout::Constraint::Min(10), |
| 125 | + ]); |
| 126 | + |
| 127 | + frame.render_widget(t, area) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +impl From<proto::tasks::Stats> for Stats { |
| 132 | + fn from(pb: proto::tasks::Stats) -> Self { |
| 133 | + fn pb_duration(dur: prost_types::Duration) -> Duration { |
| 134 | + use std::convert::TryFrom; |
| 135 | + |
| 136 | + let secs = |
| 137 | + u64::try_from(dur.seconds).expect("a task should not have a negative duration!"); |
| 138 | + let nanos = |
| 139 | + u64::try_from(dur.nanos).expect("a task should not have a negative duration!"); |
| 140 | + Duration::from_secs(secs) + Duration::from_nanos(nanos) |
| 141 | + } |
| 142 | + |
| 143 | + let total = pb.total_time.map(pb_duration).unwrap_or_default(); |
| 144 | + let busy = pb.busy_time.map(pb_duration).unwrap_or_default(); |
| 145 | + let idle = total - busy; |
| 146 | + Self { |
| 147 | + total, |
| 148 | + idle, |
| 149 | + busy, |
| 150 | + polls: pb.polls, |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments