forked from NationTech/harmony
fix(cli): reduce noise & better track progress within Harmony (#91)
Introduce a way to instrument what happens within Harmony and around Harmony (e.g. in the CLI or in Composer). The goal is to provide visual feedback to the end users and inform them of the progress of their tasks (e.g. deployment) as clearly as possible. It is important to also let them know of the outcome of their tasks (what was created, where to access stuff, etc.). <img src="https://media.discordapp.net/attachments/1295353830300713062/1400289618636574741/demo.gif?ex=688c18d5&is=688ac755&hm=2c70884aacb08f7bd15cbb65a7562a174846906718aa15294bbb238e64febbce&=" /> ## Changes ### Instrumentation architecture Extensibility and ease of use is key here, while preserving type safety as much as possible. The proposed API is quite simple: ```rs // Emit an event instrumentation::instrument( HarmonyEvent::TopologyPrepared { topology: "k8s-anywhere", outcome: Outcome::success("yay") } ); // Consume events instrumentation::subscribe("Harmony CLI Logger", async |event| { match event { HarmonyEvent::TopologyPrepared { name, outcome } => todo!(), } }); ``` #### Current limitations * this API is not very extensible, but it could be easily changed to allow end users to define custom events in addition to Harmony core events * we use a tokio broadcast channel behind the scene so only in process communication can happen, but it could be easily changed to a more flexible communication mechanism as implementation details are hidden ### `harmony_composer` VS `harmony_cli` As Harmony Composer launches commands from Harmony (CLI), they both live in different processes. And because of this, we cannot easily make all the logging happens in one place (Harmony Composer) and get rid of Harmony CLI. At least not without introducing additional complexity such as communication through a server, unix socket, etc. So for the time being, it was decided to preserve both `harmony_composer` and `harmony_cli` and let them independently log their stuff and handle their own responsibilities: * `harmony_composer`: takes care only of setting up & packaging a project, delegates everything else to `harmony_cli` * `harmony_cli`: takes care of configuring & running Harmony ### Logging & prompts * [indicatif](https://github.com/console-rs/indicatif) is used to create progress bars and track progress within Harmony, Harmony CLI, and Harmony Composer * [inquire](https://github.com/mikaelmello/inquire) is preserved, but was removed from `harmony` (core) as UI concerns shouldn't go that deep * note: for now the only prompt we had was simply deleted, we'll have to find a better way to prompt stuff in the future ## Todos * [ ] Update/Create ADRs * [ ] Continue instrumentation for missing branches * [ ] Allow instrumentation to emit and subscribe to custom events Co-authored-by: Ian Letourneau <letourneau.ian@gmail.com> Reviewed-on: NationTech/harmony#91 Reviewed-by: johnride <jg@nationtech.io>
This commit is contained in:
116
harmony_cli/src/cli_logger.rs
Normal file
116
harmony_cli/src/cli_logger.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
use harmony::instrumentation::{self, HarmonyEvent};
|
||||
use indicatif::{MultiProgress, ProgressBar};
|
||||
use indicatif_log_bridge::LogWrapper;
|
||||
use std::{
|
||||
collections::{HashMap, hash_map},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::progress;
|
||||
|
||||
pub fn init() -> tokio::task::JoinHandle<()> {
|
||||
configure_logger();
|
||||
let handle = tokio::spawn(handle_events());
|
||||
|
||||
loop {
|
||||
if instrumentation::instrument(HarmonyEvent::HarmonyStarted).is_ok() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
handle
|
||||
}
|
||||
|
||||
fn configure_logger() {
|
||||
let logger =
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).build();
|
||||
let level = logger.filter();
|
||||
let multi = MultiProgress::new();
|
||||
LogWrapper::new(multi.clone(), logger).try_init().unwrap();
|
||||
log::set_max_level(level);
|
||||
}
|
||||
|
||||
async fn handle_events() {
|
||||
instrumentation::subscribe("Harmony CLI Logger", {
|
||||
let sections: Arc<Mutex<HashMap<String, MultiProgress>>> =
|
||||
Arc::new(Mutex::new(HashMap::new()));
|
||||
let progress_bars: Arc<Mutex<HashMap<String, ProgressBar>>> =
|
||||
Arc::new(Mutex::new(HashMap::new()));
|
||||
|
||||
move |event| {
|
||||
let sections_clone = Arc::clone(§ions);
|
||||
let progress_bars_clone = Arc::clone(&progress_bars);
|
||||
|
||||
async move {
|
||||
let mut sections = sections_clone.lock().unwrap();
|
||||
let mut progress_bars = progress_bars_clone.lock().unwrap();
|
||||
|
||||
match event {
|
||||
HarmonyEvent::HarmonyStarted => {}
|
||||
HarmonyEvent::PrepareTopologyStarted { topology: name } => {
|
||||
let section = progress::new_section(format!(
|
||||
"{} Preparing environment: {name}...",
|
||||
crate::theme::EMOJI_TOPOLOGY,
|
||||
));
|
||||
(*sections).insert(name, section);
|
||||
}
|
||||
HarmonyEvent::TopologyPrepared {
|
||||
topology: name,
|
||||
outcome,
|
||||
} => {
|
||||
let section = (*sections).get(&name).unwrap();
|
||||
let progress = progress::add_spinner(section, "".into());
|
||||
|
||||
match outcome.status {
|
||||
harmony::interpret::InterpretStatus::SUCCESS => {
|
||||
progress::success(section, Some(progress), outcome.message);
|
||||
}
|
||||
harmony::interpret::InterpretStatus::FAILURE => {
|
||||
progress::error(section, Some(progress), outcome.message);
|
||||
}
|
||||
harmony::interpret::InterpretStatus::RUNNING => todo!(),
|
||||
harmony::interpret::InterpretStatus::QUEUED => todo!(),
|
||||
harmony::interpret::InterpretStatus::BLOCKED => todo!(),
|
||||
harmony::interpret::InterpretStatus::NOOP => {
|
||||
progress::skip(section, Some(progress), outcome.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
HarmonyEvent::InterpretExecutionStarted {
|
||||
interpret: name,
|
||||
topology,
|
||||
message,
|
||||
} => {
|
||||
let section = (*sections).get(&topology).unwrap();
|
||||
let progress_bar = progress::add_spinner(section, message);
|
||||
|
||||
(*progress_bars).insert(name, progress_bar);
|
||||
}
|
||||
HarmonyEvent::InterpretExecutionFinished {
|
||||
topology,
|
||||
interpret: name,
|
||||
outcome,
|
||||
} => {
|
||||
let section = (*sections).get(&topology).unwrap();
|
||||
let progress_bar = (*progress_bars).get(&name).cloned();
|
||||
|
||||
let _ = section.clear();
|
||||
|
||||
match outcome {
|
||||
Ok(outcome) => {
|
||||
progress::success(section, progress_bar, outcome.message);
|
||||
}
|
||||
Err(err) => {
|
||||
progress::error(section, progress_bar, err.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
(*progress_bars).remove(&name);
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
}
|
||||
@@ -4,8 +4,13 @@ use harmony;
|
||||
use harmony::{score::Score, topology::Topology};
|
||||
use inquire::Confirm;
|
||||
|
||||
pub mod cli_logger; // FIXME: Don't make me pub
|
||||
pub mod progress;
|
||||
pub mod theme;
|
||||
|
||||
#[cfg(feature = "tui")]
|
||||
use harmony_tui;
|
||||
use log::debug;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
@@ -134,7 +139,7 @@ pub async fn init<T: Topology + Send + Sync + 'static>(
|
||||
|
||||
// Run filtered scores
|
||||
for s in scores_vec {
|
||||
println!("Running: {}", s.name());
|
||||
debug!("Running: {}", s.name());
|
||||
maestro.interpret(s).await?;
|
||||
}
|
||||
|
||||
|
||||
50
harmony_cli/src/progress.rs
Normal file
50
harmony_cli/src/progress.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use indicatif::{MultiProgress, ProgressBar};
|
||||
|
||||
pub fn new_section(title: String) -> MultiProgress {
|
||||
let multi_progress = MultiProgress::new();
|
||||
let _ = multi_progress.println(title);
|
||||
|
||||
multi_progress
|
||||
}
|
||||
|
||||
pub fn add_spinner(multi_progress: &MultiProgress, message: String) -> ProgressBar {
|
||||
let progress = multi_progress.add(ProgressBar::new_spinner());
|
||||
|
||||
progress.set_style(crate::theme::SPINNER_STYLE.clone());
|
||||
progress.set_message(message);
|
||||
progress.enable_steady_tick(Duration::from_millis(100));
|
||||
|
||||
progress
|
||||
}
|
||||
|
||||
pub fn success(multi_progress: &MultiProgress, progress: Option<ProgressBar>, message: String) {
|
||||
if let Some(progress) = progress {
|
||||
multi_progress.remove(&progress)
|
||||
}
|
||||
|
||||
let progress = multi_progress.add(ProgressBar::new_spinner());
|
||||
progress.set_style(crate::theme::SUCCESS_SPINNER_STYLE.clone());
|
||||
progress.finish_with_message(message);
|
||||
}
|
||||
|
||||
pub fn error(multi_progress: &MultiProgress, progress: Option<ProgressBar>, message: String) {
|
||||
if let Some(progress) = progress {
|
||||
multi_progress.remove(&progress)
|
||||
}
|
||||
|
||||
let progress = multi_progress.add(ProgressBar::new_spinner());
|
||||
progress.set_style(crate::theme::ERROR_SPINNER_STYLE.clone());
|
||||
progress.finish_with_message(message);
|
||||
}
|
||||
|
||||
pub fn skip(multi_progress: &MultiProgress, progress: Option<ProgressBar>, message: String) {
|
||||
if let Some(progress) = progress {
|
||||
multi_progress.remove(&progress)
|
||||
}
|
||||
|
||||
let progress = multi_progress.add(ProgressBar::new_spinner());
|
||||
progress.set_style(crate::theme::SKIP_SPINNER_STYLE.clone());
|
||||
progress.finish_with_message(message);
|
||||
}
|
||||
26
harmony_cli/src/theme.rs
Normal file
26
harmony_cli/src/theme.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use console::Emoji;
|
||||
use indicatif::ProgressStyle;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
pub static EMOJI_HARMONY: Emoji<'_, '_> = Emoji("🎼", "");
|
||||
pub static EMOJI_SUCCESS: Emoji<'_, '_> = Emoji("✅", "");
|
||||
pub static EMOJI_SKIP: Emoji<'_, '_> = Emoji("⏭️", "");
|
||||
pub static EMOJI_ERROR: Emoji<'_, '_> = Emoji("⚠️", "");
|
||||
pub static EMOJI_DEPLOY: Emoji<'_, '_> = Emoji("🚀", "");
|
||||
pub static EMOJI_TOPOLOGY: Emoji<'_, '_> = Emoji("📦", "");
|
||||
|
||||
lazy_static! {
|
||||
pub static ref SPINNER_STYLE: ProgressStyle = ProgressStyle::default_spinner()
|
||||
.template(" {spinner:.green} {msg}")
|
||||
.unwrap()
|
||||
.tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]);
|
||||
pub static ref SUCCESS_SPINNER_STYLE: ProgressStyle = SPINNER_STYLE
|
||||
.clone()
|
||||
.tick_strings(&[format!("{}", EMOJI_SUCCESS).as_str()]);
|
||||
pub static ref SKIP_SPINNER_STYLE: ProgressStyle = SPINNER_STYLE
|
||||
.clone()
|
||||
.tick_strings(&[format!("{}", EMOJI_SKIP).as_str()]);
|
||||
pub static ref ERROR_SPINNER_STYLE: ProgressStyle = SPINNER_STYLE
|
||||
.clone()
|
||||
.tick_strings(&[format!("{}", EMOJI_ERROR).as_str()]);
|
||||
}
|
||||
Reference in New Issue
Block a user