log events are now displayed on the monitoring page to show when a host comes up/goes down

This commit is contained in:
Trevor Merritt 2025-04-25 13:36:58 -04:00
parent 039d30fe12
commit 26c3195d26
4 changed files with 312 additions and 19 deletions

204
Cargo.lock generated
View File

@ -32,6 +32,21 @@ version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
@ -118,6 +133,12 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd"
[[package]]
name = "bumpalo"
version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]]
name = "cassowary"
version = "0.3.0"
@ -148,6 +169,20 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-link",
]
[[package]]
name = "clap"
version = "4.5.37"
@ -235,6 +270,12 @@ dependencies = [
"static_assertions",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "crossterm"
version = "0.28.1"
@ -406,6 +447,30 @@ version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "iana-time-zone"
version = "0.1.63"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c919e5debc312ad217002b8048a17b7d83f80703865bbfcfebb0458b0b27d8"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"log",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "ident_case"
version = "1.0.1"
@ -482,6 +547,16 @@ dependencies = [
"syn",
]
[[package]]
name = "js-sys"
version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
@ -552,6 +627,15 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.32.2"
@ -625,9 +709,10 @@ dependencies = [
[[package]]
name = "pp"
version = "0.1.1"
version = "0.2.0-PREVIEW"
dependencies = [
"ansi_term",
"chrono",
"clap",
"color-eyre",
"crossterm",
@ -971,6 +1056,64 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [
"bumpalo",
"log",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]]
name = "winapi"
version = "0.3.9"
@ -993,6 +1136,65 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-core"
version = "0.61.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4763c1de310c86d75a878046489e2e5ba02c649d185f21c67d4cf8a56d098980"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-implement"
version = "0.60.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a47fddd13af08290e67f4acabf4b459f647552718f683a7b415d290ac744a836"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-interface"
version = "0.59.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd9211b69f8dcdfa817bfd14bf1c97c9188afa36f4750130fcdf3f400eca9fa8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "windows-link"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76840935b766e1b0a05c0066835fb9ec80071d4c09a16f6bd5f7e655e3c14c38"
[[package]]
name = "windows-result"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c64fd11a4fd95df68efcfee5f44a294fe71b8bc6a91993e2791938abcc712252"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2ba9642430ee452d5a7aa78d72907ebe8cfda358e8cb7918a2050581322f97"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-sys"
version = "0.52.0"

View File

@ -1,6 +1,6 @@
[package]
name = "pp"
version = "0.1.1"
version = "0.2.0-PREVIEW"
edition = "2024"
[dependencies]
@ -15,5 +15,7 @@ color-eyre = "0.6.3"
crossterm = "0.28.1"
ratatui = "0.29.0"
# Time Display
chrono = "0.4"
[[bin]]
name = "pp"

View File

@ -7,6 +7,7 @@ use std::str::FromStr;
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::time::{Duration, SystemTime};
use chrono::{DateTime, Local};
use crossterm::event;
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::{DefaultTerminal, Frame};
@ -22,14 +23,16 @@ use crate::tui::ratatui_ui::RatatuiUi;
pub enum RatatuiScreens {
#[default]
Monitoring,
Exiting
Exiting,
Editing
}
#[derive(Default)]
pub struct RatatuiApp {
running: bool,
state: BTreeMap<String, TargetState>,
current_screen: RatatuiScreens
pub(crate) state: BTreeMap<String, TargetState>,
current_screen: RatatuiScreens,
pub(crate) log_entries: Vec<String>
}
impl RatatuiApp {
@ -44,13 +47,17 @@ impl RatatuiApp {
match self.current_screen {
RatatuiScreens::Monitoring => {
terminal.draw(|frame| RatatuiUi::monitoring_mode(frame, &self.state)).expect("Unable to draw to screen");
terminal.draw(|frame| RatatuiUi::monitoring_mode(frame, &mut self)).expect("Unable to draw to screen");
self.handle_monitoring_crossterm_events();
}
RatatuiScreens::Exiting => {
terminal.draw(| frame | RatatuiUi::exiting_mode(frame));
self.handle_exiting_crossterm_events();
}
RatatuiScreens::Editing => {
terminal.draw(|frame| RatatuiUi::editing_mode(frame, &mut self.state));
self.handle_editing_crossterm_events();
}
}
// self.handle_crossterm_events()?;
@ -64,10 +71,12 @@ impl RatatuiApp {
// find the right TargetState
for (name, mut current_state) in local_state.clone() {
if current_state.target == new_message.target {
let last_alive_change = if new_message.success == current_state.alive {
current_state.last_alive_change
} else {
let did_change = new_message.success != current_state.alive;
let last_alive_change = if did_change {
SystemTime::now()
} else {
current_state.last_alive_change
};
let new_state = TargetState {
@ -77,7 +86,23 @@ impl RatatuiApp {
last_rtt: new_message.rtt,
last_alive_change,
};
local_state.insert(name.clone(), new_state);
local_state.insert(name.clone(), new_state.clone());
let success_message = if new_state.alive {
"Success"
} else {
"Failure"
};
let current_time: DateTime<Local> = SystemTime::now().into();
if did_change {
self.log_entries.push(
format!("{:?} {} for {}",
current_time.format("%Y-%m-%d %H:%M:%S: ").to_string(),
success_message,
new_state.name.clone())
);
}
}
}
}
@ -94,8 +119,9 @@ impl RatatuiApp {
fn render(&mut self, frame: &mut Frame) {
match self.current_screen {
RatatuiScreens::Monitoring => { RatatuiUi::monitoring_mode(frame, &self.state) }
RatatuiScreens::Monitoring => { RatatuiUi::monitoring_mode(frame, self) }
RatatuiScreens::Exiting => { RatatuiUi::exiting_mode(frame)}
RatatuiScreens::Editing => { RatatuiUi::editing_mode(frame, &mut self.state) }
}
}
@ -107,6 +133,9 @@ impl RatatuiApp {
(_, KeyCode::Esc) => {
self.current_screen = RatatuiScreens::Exiting;
}
(_, KeyCode::Char('e')) | (_, KeyCode::Char('E')) => {
self.current_screen = RatatuiScreens::Editing
}
_ => {}
}
},
@ -134,6 +163,24 @@ impl RatatuiApp {
Ok(())
}
fn handle_editing_crossterm_events(&mut self) -> Result<()> {
if event::poll(Duration::from_millis(100))? {
match event::read()? {
Event::Key(key) if key.kind == KeyEventKind::Press => {
match key.code {
KeyCode::Up => { println!("UP") },
KeyCode::Down => { println!("Down") },
KeyCode::Char('+') => { println!("ADD HOST") },
KeyCode::Char('-') => { println!("Delete Host") }
_ => {}
}
}
_ => {}
}
}
Ok(())
}
fn handle_crossterm_events(&mut self) -> Result<()> {
if event::poll(Duration::from_millis(100))? {
match event::read()? {

View File

@ -1,10 +1,12 @@
use std::collections::BTreeMap;
use std::time::SystemTime;
use color_eyre::owo_colors::OwoColorize;
use ratatui::Frame;
use ratatui::prelude::*;
use ratatui::widgets::{Block, Paragraph};
use ratatui::widgets::{Block, Borders, List, ListItem, ListState, Paragraph};
use crate::duration_to_string;
use crate::target_state::TargetState;
use crate::tui::ratatui_app::RatatuiApp;
pub struct RatatuiUi {}
@ -22,21 +24,45 @@ impl RatatuiUi {
frame.area()
);
}
pub fn monitoring_mode(frame: &mut Frame, state: &BTreeMap<String, TargetState>) {
let layout = Layout::default()
pub fn editing_mode(frame: &mut Frame, state: &mut BTreeMap<String, TargetState>) {
let title = Line::from("Editing Hosts")
.bold()
.blue()
.centered();
let body_text = "This is the body text for editing hosts";
frame.render_widget(
Paragraph::new(body_text)
.centered()
.block(Block::new()
.title(title)
.blue()),
frame.area()
);
}
pub fn monitoring_mode(frame: &mut Frame, state: &mut RatatuiApp) {
let layouts = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Fill(1),
Constraint::Length(10),
Constraint::Length(3)
])
.split(frame.area());
let title = Line::from("PP Sample App")
let body_layout = layouts[0];
let logs_layout = layouts[1];
let footer_layout = layouts[2];
let title = Line::from(format!("PP v{}", env!("CARGO_PKG_VERSION")))
.bold()
.blue()
.centered();
let mut working = vec![]; // Line::from("Empty");
for (title, current) in state {
for (title, current) in state.state.iter() {
let mut name_field = format!("{} ({})", current.name.clone(), current.target.clone());
while name_field.len() < 40 {
@ -62,19 +88,35 @@ impl RatatuiUi {
]));
}
let footer_text = "Press <ESC> to exit | Press + to Add a Host | Press - to Delete a host";
let footer_text = "Press <ESC> to exit | Press e to edit hosts";
frame.render_widget(
Paragraph::new(working)
.block(Block::bordered().title(title)),
layout[0],
body_layout
);
let mut list_items = vec![];
for entry in state.log_entries.clone().into_iter().rev() {
list_items.push(ListItem::new(entry));
}
let list = List::new(list_items)
.block(Block::default().title("Hosts").borders(Borders::ALL))
.highlight_style(Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD))
.highlight_symbol(">> ");
let mut list_state = ListState::default();
list_state.select(Some(0));
frame.render_stateful_widget(
list, logs_layout, &mut list_state);
frame.render_widget(
Paragraph::new(footer_text)
.block(Block::bordered())
.centered(),
layout[1]
footer_layout
);
}
}