From 9fb82b5caa0627b1cea519b59f42e7761086697f Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 9 Jul 2023 18:08:18 +0200 Subject: [PATCH] doc fixes --- src/bin/main/mod.rs | 4 --- src/common/printing.rs | 3 +- src/networking/mod.rs | 2 ++ src/networking/monitoring/uptime.rs | 48 +++++++++++++++-------------- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/bin/main/mod.rs b/src/bin/main/mod.rs index dfbed19..6cfc299 100644 --- a/src/bin/main/mod.rs +++ b/src/bin/main/mod.rs @@ -12,7 +12,6 @@ #![warn(clippy::pedantic)] //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// -use libpt::logger; use libpt::networking::monitoring::uptime; // we want the log macros in any case @@ -43,9 +42,6 @@ use args::*; //// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// /// ## Main function of the `pt` binary fn main() { - #[cfg(debug_assertions)] - std::env::set_var(logger::LOGGER_ENV_KEY, "trace"); - let cli = Cli::parse(); if cli.log_meta { // set up our logger to use the given verbosity diff --git a/src/common/printing.rs b/src/common/printing.rs index 56d8163..588f3f9 100644 --- a/src/common/printing.rs +++ b/src/common/printing.rs @@ -11,7 +11,8 @@ //// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// // reimport our macros to this module, so the user does not get confused when importing the macros -pub use crate::get_stdout_for; +pub use crate::divider; +pub use crate::println_divider; //// TYPES ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/networking/mod.rs b/src/networking/mod.rs index 84f6ec5..cae88ee 100644 --- a/src/networking/mod.rs +++ b/src/networking/mod.rs @@ -21,6 +21,8 @@ pub mod monitoring; //// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// +/// how long to wait for a remove server to respond in ms +pub const REQUEST_TIMEOUT: u64 = 2000; //// STATICS /////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/networking/monitoring/uptime.rs b/src/networking/monitoring/uptime.rs index ca723f6..9003c1a 100644 --- a/src/networking/monitoring/uptime.rs +++ b/src/networking/monitoring/uptime.rs @@ -47,16 +47,16 @@ pub const DEFAULT_CHECK_URLS: &'static [&'static str] = /// [`UptimeStatus`] describes the result of an uptime check. pub struct UptimeStatus { /// true if the [`UptimeStatus`] is considered successful - success: bool, + pub success: bool, /// the percentage of reachable urls out of the total urls - success_ratio: u8, + pub success_ratio: u8, /// the percentage of reachable urls out of the total urls that need to be reachable in order /// for this [`UptimeStatus`] to be considered a success. - success_ratio_target: u8, - /// the number of reachable [`urls`] - reachable: usize, - /// which urls to check in [`check()`] - urls: Vec, + pub success_ratio_target: u8, + /// the number of reachable [`urls`](UptimeStatus::urls) + pub reachable: usize, + /// which urls to check in [`check()`](UptimeStatus::check) + pub urls: Vec, } //// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// @@ -87,12 +87,15 @@ impl UptimeStatus { /// ## check for success with the given urls /// - /// Makes the actual https requests and updates the success fields. + /// Makes the actual https requests and updates fields accordingly. + /// + /// Note: Blocking execution for all requests, timeout is set to + /// [REQUEST_TIMEOUT](crate::networking::REQUEST_TIMEOUT). pub fn check(&mut self) { self.reachable = 0; self.urls.iter().for_each(|url| { let client = reqwest::blocking::Client::builder() - .timeout(Duration::from_millis(2000)) + .timeout(Duration::from_millis(crate::networking::REQUEST_TIMEOUT)) .build() .expect("could not build a client for https requests"); let response = client.get(url.clone()).send(); @@ -106,7 +109,7 @@ impl UptimeStatus { /// ## calculate the success based on the `reachable` and `total` /// /// Calculates the ratio of [`reachable`](UptimeStatus::reachable) / - /// [`total`](UptimeStatus::total). + /// (length of [urls](UptimeStatus::urls)). /// /// Calculates a [`success_ratio`](UptimeStatus::success_ratio) (as [u8]) from that, /// by multiplying with 100, then flooring. @@ -118,7 +121,7 @@ impl UptimeStatus { /// In the special case that no URLs to check for have been provided, the check will be /// considered a success, but the [`success_ratio`](UptimeStatus::success_ratio) will be `0`. /// - /// Note: does not check for networking, use [`check()`] for that. + /// Note: does not check for networking, use [`check()`](UptimeStatus::check) for that. pub fn calc_success(&mut self) { // if no urls need to be checked, success without checking if self.urls.len() == 0 { @@ -159,12 +162,12 @@ impl fmt::Display for UptimeStatus { } write!( f, - "{{ + " success: {}, success_ratio: {}%, success_ratio_target: {}%, reachable: {}, - urls: {:?}\n}}", + urls: {:?}\n", self.success, self.success_ratio, self.success_ratio_target, self.reachable, url_strs ) } @@ -173,7 +176,11 @@ impl fmt::Display for UptimeStatus { //// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// /// ## Uptime monitor /// -/// This function continuously monitors the uptime of your host/network +/// This function continuously monitors the uptime of your host/network. +/// +/// On change of status, an update will be logged at [INFO Level](log::Level::Info), containing +/// information on your current status, including timestamps of the last up/down time and durations +/// since. pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec, interval: u64) { if urls.len() == 0 { error!("No URLs provided. There is nothing to monitor."); @@ -218,6 +225,7 @@ pub fn continuous_uptime_monitor(success_ratio_target: u8, urls: Vec, in } //// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// +/// Displays the current status for the [continuous uptime monitor](continuous_uptime_monitor) fn display_uptime_status( msg: &str, last_uptime: Option, @@ -227,14 +235,8 @@ fn display_uptime_status( // I know it's weird that this has two spaces too much, but somehow just the tabs is missing // two spaces. info!("uptime check: {}", msg); - info!( - "last uptime: {}", - match_format_time(last_uptime) - ); - info!( - "last downtime: {}", - match_format_time(last_downtime) - ); + info!("last uptime: {}", match_format_time(last_uptime)); + info!("last downtime: {}", match_format_time(last_downtime)); info!( "since downtime: {}", match_format_duration_since(last_downtime) @@ -243,7 +245,7 @@ fn display_uptime_status( "since uptime: {}", match_format_duration_since(last_uptime) ); - debug!("\n{}", status); + debug!("{}", status); info!("{}", divider!()); }