|
1 |
| -use std::{ |
2 |
| - sync::Mutex, |
3 |
| - time::{Duration, Instant}, |
4 |
| -}; |
5 |
| - |
6 |
| -use gasket::{metrics::Reading, runtime::Tether}; |
7 |
| -use lazy_static::lazy_static; |
8 |
| -use tracing::{debug, error, warn}; |
9 |
| - |
10 |
| -#[derive(clap::ValueEnum, Clone, Default)] |
11 |
| -pub enum Mode { |
12 |
| - /// shows progress as a plain sequence of logs |
13 |
| - #[default] |
14 |
| - Plain, |
15 |
| - /// shows aggregated progress and metrics |
16 |
| - Tui, |
17 |
| -} |
| 1 | +use gasket::{daemon::Daemon, metrics::Reading, runtime::Tether}; |
| 2 | +use std::{sync::Arc, time::Duration}; |
18 | 3 |
|
19 | 4 | struct TuiConsole {
|
20 | 5 | chainsync_progress: indicatif::ProgressBar,
|
@@ -116,77 +101,15 @@ impl TuiConsole {
|
116 | 101 | }
|
117 | 102 | }
|
118 | 103 |
|
119 |
| -struct PlainConsole { |
120 |
| - last_report: Mutex<Instant>, |
121 |
| -} |
122 |
| - |
123 |
| -impl PlainConsole { |
124 |
| - fn new() -> Self { |
125 |
| - Self { |
126 |
| - last_report: Mutex::new(Instant::now()), |
127 |
| - } |
| 104 | +pub async fn render(daemon: Arc<Daemon>, tui_enabled: bool) { |
| 105 | + if !tui_enabled { |
| 106 | + return; |
128 | 107 | }
|
129 | 108 |
|
130 |
| - fn refresh<'a>(&self, tethers: impl Iterator<Item = &'a Tether>) { |
131 |
| - let mut last_report = self.last_report.lock().unwrap(); |
132 |
| - |
133 |
| - if last_report.elapsed() <= Duration::from_secs(10) { |
134 |
| - return; |
135 |
| - } |
136 |
| - |
137 |
| - for tether in tethers { |
138 |
| - match tether.check_state() { |
139 |
| - gasket::runtime::TetherState::Dropped => { |
140 |
| - error!("[{}] stage tether has been dropped", tether.name()); |
141 |
| - } |
142 |
| - gasket::runtime::TetherState::Blocked(_) => { |
143 |
| - warn!( |
144 |
| - "[{}] stage tehter is blocked or not reporting state", |
145 |
| - tether.name() |
146 |
| - ); |
147 |
| - } |
148 |
| - gasket::runtime::TetherState::Alive(state) => { |
149 |
| - debug!("[{}] stage is alive with state: {:?}", tether.name(), state); |
150 |
| - match tether.read_metrics() { |
151 |
| - Ok(readings) => { |
152 |
| - for (key, value) in readings { |
153 |
| - debug!("[{}] metric `{}` = {:?}", tether.name(), key, value); |
154 |
| - } |
155 |
| - } |
156 |
| - Err(err) => { |
157 |
| - error!("[{}] error reading metrics: {}", tether.name(), err) |
158 |
| - } |
159 |
| - } |
160 |
| - } |
161 |
| - } |
162 |
| - } |
163 |
| - |
164 |
| - *last_report = Instant::now(); |
165 |
| - } |
166 |
| -} |
167 |
| - |
168 |
| -lazy_static! { |
169 |
| - static ref TUI_CONSOLE: TuiConsole = TuiConsole::new(); |
170 |
| -} |
171 |
| - |
172 |
| -lazy_static! { |
173 |
| - static ref PLAIN_CONSOLE: PlainConsole = PlainConsole::new(); |
174 |
| -} |
175 |
| - |
176 |
| -pub fn initialize(mode: &Option<Mode>) { |
177 |
| - if !matches!(mode, Some(Mode::Tui)) { |
178 |
| - tracing::subscriber::set_global_default( |
179 |
| - tracing_subscriber::FmtSubscriber::builder() |
180 |
| - .with_max_level(tracing::Level::DEBUG) |
181 |
| - .finish(), |
182 |
| - ) |
183 |
| - .unwrap(); |
184 |
| - } |
185 |
| -} |
| 109 | + let tui = TuiConsole::new(); |
186 | 110 |
|
187 |
| -pub fn refresh<'a>(mode: &Option<Mode>, tethers: impl Iterator<Item = &'a Tether>) { |
188 |
| - match mode { |
189 |
| - Some(Mode::Tui) => TUI_CONSOLE.refresh(tethers), |
190 |
| - _ => PLAIN_CONSOLE.refresh(tethers), |
| 111 | + loop { |
| 112 | + tui.refresh(daemon.tethers()); |
| 113 | + tokio::time::sleep(Duration::from_secs(1)).await; |
191 | 114 | }
|
192 | 115 | }
|
0 commit comments