|
| 1 | +use anyhow::Result; |
| 2 | +use spin::RwLock; |
| 3 | +use std::collections::{BTreeMap, HashMap, HashSet}; |
| 4 | +use std::sync::Arc; |
| 5 | +use std::{io, vec}; |
| 6 | + |
| 7 | +use clap::Parser; |
| 8 | +use futures::Stream; |
| 9 | +use futures::StreamExt; |
| 10 | +use ratatui::prelude::*; |
| 11 | +use ratatui::widgets::{Block, Gauge}; |
| 12 | +use ratatui::{ |
| 13 | + crossterm::event::{self, KeyCode, KeyEventKind}, |
| 14 | + widgets::{Axis, Borders, Chart, Dataset, GraphType, StatefulWidget, Widget}, |
| 15 | + DefaultTerminal, |
| 16 | +}; |
| 17 | +use std::pin::Pin; |
| 18 | +use std::time::{Duration, Instant}; |
| 19 | +use tonic::transport::{Channel, Endpoint, Uri}; |
| 20 | + |
| 21 | +mod mock; |
| 22 | +mod model; |
| 23 | +mod real; |
| 24 | +mod ui; |
| 25 | + |
| 26 | +mod proto { |
| 27 | + tonic::include_proto!("sorock"); |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Parser)] |
| 31 | +enum Sub { |
| 32 | + #[clap(about = "Start monitoring a cluster by connecting to a node.")] |
| 33 | + Monitor { addr: Uri, shard_id: u32 }, |
| 34 | + #[clap(about = "Embedded test. 0 -> Static data, 1 -> Mock servers")] |
| 35 | + TestMonitor { number: u8 }, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Parser)] |
| 39 | +struct Args { |
| 40 | + #[clap(subcommand)] |
| 41 | + sub: Sub, |
| 42 | +} |
| 43 | + |
| 44 | +#[tokio::main] |
| 45 | +async fn main() -> Result<()> { |
| 46 | + let args = Args::parse(); |
| 47 | + |
| 48 | + let model = match args.sub { |
| 49 | + Sub::Monitor { addr, shard_id } => { |
| 50 | + let node = real::connect_real_node(addr, shard_id); |
| 51 | + model::Model::new(node).await |
| 52 | + } |
| 53 | + Sub::TestMonitor { number: 0 } => model::Model::test(), |
| 54 | + Sub::TestMonitor { number: 1 } => { |
| 55 | + let mock = mock::connect_mock_node(); |
| 56 | + model::Model::new(mock).await |
| 57 | + } |
| 58 | + _ => unreachable!(), |
| 59 | + }; |
| 60 | + |
| 61 | + let mut terminal = ratatui::init(); |
| 62 | + let app_result = App::new(model).run(&mut terminal)?; |
| 63 | + terminal.clear()?; |
| 64 | + ratatui::restore(); |
| 65 | + |
| 66 | + Ok(app_result) |
| 67 | +} |
| 68 | + |
| 69 | +struct App { |
| 70 | + model: model::Model, |
| 71 | +} |
| 72 | +impl App { |
| 73 | + pub fn new(model: model::Model) -> Self { |
| 74 | + Self { model } |
| 75 | + } |
| 76 | + |
| 77 | + fn run(self, terminal: &mut DefaultTerminal) -> io::Result<()> { |
| 78 | + let mut app_state = AppState::default(); |
| 79 | + loop { |
| 80 | + terminal.draw(|frame| { |
| 81 | + frame.render_stateful_widget(&self, frame.area(), &mut app_state); |
| 82 | + })?; |
| 83 | + |
| 84 | + if !event::poll(Duration::from_millis(100))? { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + if let event::Event::Key(key) = event::read()? { |
| 89 | + if key.kind == KeyEventKind::Press { |
| 90 | + match key.code { |
| 91 | + KeyCode::Char('q') => return Ok(()), |
| 92 | + KeyCode::Up | KeyCode::Char('k') => app_state.list_state.previous(), |
| 93 | + KeyCode::Down | KeyCode::Char('j') => app_state.list_state.next(), |
| 94 | + _ => {} |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[derive(Default)] |
| 103 | +struct AppState { |
| 104 | + list_state: tui_widget_list::ListState, |
| 105 | +} |
| 106 | +impl StatefulWidget for &App { |
| 107 | + type State = AppState; |
| 108 | + fn render( |
| 109 | + self, |
| 110 | + area: ratatui::prelude::Rect, |
| 111 | + buf: &mut ratatui::prelude::Buffer, |
| 112 | + state: &mut Self::State, |
| 113 | + ) where |
| 114 | + Self: Sized, |
| 115 | + { |
| 116 | + let chunks = Layout::default() |
| 117 | + .direction(Direction::Vertical) |
| 118 | + .margin(1) |
| 119 | + .constraints([Constraint::Length(15), Constraint::Fill(1)].as_ref()) |
| 120 | + .split(area); |
| 121 | + |
| 122 | + let progress_chart = { |
| 123 | + let end = Instant::now(); |
| 124 | + let start = end - Duration::from_secs(120); |
| 125 | + let data = self.model.progress_log.read().get_range(start, end); |
| 126 | + ui::progress_chart::ProgressChart::new(data, start, end) |
| 127 | + }; |
| 128 | + Widget::render(progress_chart, chunks[0], buf); |
| 129 | + |
| 130 | + let nodes_list = { |
| 131 | + let mut nodes = vec![]; |
| 132 | + let reader = &self.model.nodes.read(); |
| 133 | + |
| 134 | + let min_index = reader |
| 135 | + .nodes |
| 136 | + .values() |
| 137 | + .map(|node_state| node_state.log_state.head_index) |
| 138 | + .min() |
| 139 | + .unwrap_or(0); |
| 140 | + let max_index = reader |
| 141 | + .nodes |
| 142 | + .values() |
| 143 | + .map(|node_state| node_state.log_state.last_index) |
| 144 | + .max() |
| 145 | + .unwrap_or(0); |
| 146 | + |
| 147 | + for (uri, node_state) in &reader.nodes { |
| 148 | + let log_state = &node_state.log_state; |
| 149 | + nodes.push(ui::node_list::Node { |
| 150 | + name: uri.to_string(), |
| 151 | + head_index: log_state.head_index, |
| 152 | + snapshot_index: log_state.snapshot_index, |
| 153 | + app_index: log_state.app_index, |
| 154 | + commit_index: log_state.commit_index, |
| 155 | + last_index: log_state.last_index, |
| 156 | + min_max: ui::node_list::IndexRange { |
| 157 | + min_index, |
| 158 | + max_index, |
| 159 | + }, |
| 160 | + }); |
| 161 | + } |
| 162 | + nodes.sort_by_key(|node| node.name.clone()); |
| 163 | + ui::node_list::NodeList::new(nodes) |
| 164 | + }; |
| 165 | + StatefulWidget::render(nodes_list, chunks[1], buf, &mut state.list_state); |
| 166 | + } |
| 167 | +} |
0 commit comments