diff --git a/.github/workflows/maturin.yml b/.github/workflows/maturin.yml new file mode 100644 index 0000000..1bae4be --- /dev/null +++ b/.github/workflows/maturin.yml @@ -0,0 +1,120 @@ +# This file is autogenerated by maturin v1.4.0 +# To update, run +# +# maturin generate-ci github +# +name: CI + +on: + push: + branches: + - main + - master + tags: + - '*' + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + linux: + runs-on: ubuntu-latest + strategy: + matrix: + target: [x86_64, x86, aarch64, armv7, s390x, ppc64le] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + manylinux: auto + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + windows: + runs-on: windows-latest + strategy: + matrix: + target: [x64, x86] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + architecture: ${{ matrix.target }} + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + macos: + runs-on: macos-latest + strategy: + matrix: + target: [x86_64, aarch64] + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - name: Build wheels + uses: PyO3/maturin-action@v1 + with: + target: ${{ matrix.target }} + args: --release --out dist --find-interpreter + sccache: 'true' + - name: Upload wheels + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + sdist: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Build sdist + uses: PyO3/maturin-action@v1 + with: + command: sdist + args: --out dist + - name: Upload sdist + uses: actions/upload-artifact@v3 + with: + name: wheels + path: dist + + release: + name: Release + runs-on: ubuntu-latest + if: "startsWith(github.ref, 'refs/tags/')" + needs: [linux, windows, macos, sdist] + steps: + - uses: actions/download-artifact@v3 + with: + name: wheels + - name: Publish to PyPI + uses: PyO3/maturin-action@v1 + env: + MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }} + with: + command: upload + args: --non-interactive --skip-existing * diff --git a/Cargo.toml b/Cargo.toml index 97dde94..185fd93 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,9 +3,7 @@ resolver = "2" members = [ ".", "members/libpt-core", - "members/libpt-math", "members/libpt-log", - "members/libpt-net", "members/libpt-py", ] default-members = [".", "members/libpt-core"] @@ -33,8 +31,6 @@ thiserror = "1.0.56" libpt-core = { version = "0.3.11", path = "members/libpt-core" } libpt-bintols = { version = "0.3.11", path = "members/libpt-bintols" } libpt-log = { version = "0.3.11", path = "members/libpt-log" } -libpt-math = { version = "0.3.11", path = "members/libpt-math" } -libpt-net = { version = "0.3.11", path = "members/libpt-net" } libpt-py = { version = "0.3.11", path = "members/libpt-py" } [package] @@ -54,11 +50,9 @@ categories.workspace = true [features] default = ["log", "core"] core = [] -full = ["default", "core", "math", "log", "bintols", "net"] -math = ["dep:libpt-math", "log"] +full = ["default", "core", "log", "bintols"] log = ["dep:libpt-log"] bintols = ["dep:libpt-bintols", "log"] -net = ["dep:libpt-net", "log"] # py = ["dep:libpt-py"] [lib] @@ -73,5 +67,3 @@ crate-type = [ libpt-core = { workspace = true } libpt-bintols = { workspace = true, optional = true } libpt-log = { workspace = true, optional = true } -libpt-math = { workspace = true, optional = true } -libpt-net = { workspace = true, optional = true } diff --git a/members/libpt-math/Cargo.toml b/members/libpt-math/Cargo.toml deleted file mode 100644 index 34b56d1..0000000 --- a/members/libpt-math/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "libpt-math" -publish.workspace = true -version.workspace = true -edition.workspace = true -authors.workspace = true -license.workspace = true -description.workspace = true -readme.workspace = true -homepage.workspace = true -repository.workspace = true -keywords.workspace = true -categories.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -num = "0.4.1" -num-traits = "0.2.16" -libpt-core = { workspace = true } -libpt-log = { workspace = true } diff --git a/members/libpt-math/src/lib.rs b/members/libpt-math/src/lib.rs deleted file mode 100644 index dc00368..0000000 --- a/members/libpt-math/src/lib.rs +++ /dev/null @@ -1,6 +0,0 @@ -//! # General Mathmatics functionalities -//! -//! This crate is part of [`pt`](../libpt/index.html), but can also be used as a standalone -//! module. -//! -//! This crate is currently empty, but will contain many math functionalities in a future version. diff --git a/members/libpt-net/Cargo.toml b/members/libpt-net/Cargo.toml deleted file mode 100644 index 4a4e4bf..0000000 --- a/members/libpt-net/Cargo.toml +++ /dev/null @@ -1,24 +0,0 @@ -[package] -name = "libpt-net" -publish.workspace = true -version.workspace = true -edition.workspace = true -authors.workspace = true -license.workspace = true -description.workspace = true -readme.workspace = true -homepage.workspace = true -repository.workspace = true -keywords.workspace = true -categories.workspace = true - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -humantime = "2.1.0" -libpt-core = { workspace = true } -libpt-log = { workspace = true } -libpt-math = { workspace = true } -reqwest = { version = "0.11.20", features = ["blocking"] } -serde = { version = "1.0.188", features = ["serde_derive"] } -serde_json = "1.0.107" diff --git a/members/libpt-net/src/lib.rs b/members/libpt-net/src/lib.rs deleted file mode 100644 index 8c09c8d..0000000 --- a/members/libpt-net/src/lib.rs +++ /dev/null @@ -1,40 +0,0 @@ -//! # various networking tools -//! -//! This crate is part of [`pt`](../libpt/index.html), but can also be used as a standalone -//! module. -//! -//! The networking module contains various tools related to connections. For example, it contains a -//! tool that has the purpose to check if your connection is consistently available. - -// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// -// we want docs -#![warn(missing_docs)] -#![warn(rustdoc::missing_crate_level_docs)] -//////////////////////////////////////////////////////////////////////////////////////////////////// -// we want Debug everywhere. -#![warn(missing_debug_implementations)] -//////////////////////////////////////////////////////////////////////////////////////////////////// -// enable clippy's extra lints, the pedantic version -#![warn(clippy::pedantic)] - -// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// -/// monitor your connection -pub mod monitoring; - -// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// -/// how long to wait for a remove server to respond in ms -pub const DEFAULT_REQUEST_TIMEOUT: u64 = 2000; - -// STATICS /////////////////////////////////////////////////////////////////////////////////////// - -// MACROS //////////////////////////////////////////////////////////////////////////////////////// - -// ENUMS ///////////////////////////////////////////////////////////////////////////////////////// - -// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// - -// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// - -// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// - -// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// diff --git a/members/libpt-net/src/monitoring/mod.rs b/members/libpt-net/src/monitoring/mod.rs deleted file mode 100644 index 763d812..0000000 --- a/members/libpt-net/src/monitoring/mod.rs +++ /dev/null @@ -1,33 +0,0 @@ -//! # monitor your network -//! -//! This module offers functions to monitor your network. - -// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// -// we want docs -#![warn(missing_docs)] -#![warn(rustdoc::missing_crate_level_docs)] -//////////////////////////////////////////////////////////////////////////////////////////////////// -// we want Debug everywhere. -#![warn(missing_debug_implementations)] -//////////////////////////////////////////////////////////////////////////////////////////////////// -// enable clippy's extra lints, the pedantic version -#![warn(clippy::pedantic)] - -// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// -pub mod uptime; - -// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// - -// STATICS /////////////////////////////////////////////////////////////////////////////////////// - -// MACROS //////////////////////////////////////////////////////////////////////////////////////// - -// ENUMS ///////////////////////////////////////////////////////////////////////////////////////// - -// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// - -// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// - -// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// - -// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// diff --git a/members/libpt-net/src/monitoring/uptime.rs b/members/libpt-net/src/monitoring/uptime.rs deleted file mode 100644 index ad9259e..0000000 --- a/members/libpt-net/src/monitoring/uptime.rs +++ /dev/null @@ -1,274 +0,0 @@ -//! # monitor your network uptime -//! -//! This method offers a way to monitor your networks/hosts uptime. This is achieved by making -//! HTTPS requests to a given list of -//! -//! Warning: This module is not unit tested. - -//// ATTRIBUTES //////////////////////////////////////////////////////////////////////////////////// -// we want docs -#![warn(missing_docs)] -#![warn(rustdoc::missing_crate_level_docs)] -//////////////////////////////////////////////////////////////////////////////////////////////////// -// we want Debug everywhere. -#![warn(missing_debug_implementations)] -//////////////////////////////////////////////////////////////////////////////////////////////////// -// enable clippy's extra lints, the pedantic version -#![warn(clippy::pedantic)] - -use std::{fmt, time::Duration}; - -// IMPORTS /////////////////////////////////////////////////////////////////////////////////////// -use libpt_log::{debug, error, info, trace, warn}; - -use reqwest; - -use humantime::{format_duration, format_rfc3339}; -use std::time::SystemTime; - -use serde::{Deserialize, Serialize}; -use serde_json; - -use libpt_core::printing::divider; - -// TYPES ///////////////////////////////////////////////////////////////////////////////////////// - -// CONSTANTS ///////////////////////////////////////////////////////////////////////////////////// -/// urls used for checking by default -pub const DEFAULT_CHECK_URLS: &[&str] = &["https://www.cscherr.de", "https://www.cloudflare.com"]; - -// STATICS /////////////////////////////////////////////////////////////////////////////////////// - -// MACROS //////////////////////////////////////////////////////////////////////////////////////// - -// ENUMS ///////////////////////////////////////////////////////////////////////////////////////// - -// STRUCTS /////////////////////////////////////////////////////////////////////////////////////// -/// ## Describes an uptime status -/// -/// [`UptimeStatus`] describes the result of an uptime check. -#[derive(Serialize, Deserialize)] -pub struct Status { - /// true if the [`UptimeStatus`] is considered successful - pub success: bool, - /// the percentage of reachable urls out of the total urls - 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. - 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, - /// timeout length for requests (in ms) - pub timeout: u64, -} - -// IMPLEMENTATION //////////////////////////////////////////////////////////////////////////////// -/// Main implementation -impl Status { - /// ## create a new `UptimeStatus` and perform it's check - pub fn new(success_ratio_target: u8, urls: Vec, timeout: u64) -> Self { - assert!(success_ratio_target <= 100); - let mut status = Status { - success: false, - success_ratio: 0, - success_ratio_target, - reachable: 0, - urls, - timeout, - }; - status.urls.dedup(); - - status.check(); - - return status; - } - - /// ## check for success with the given urls - /// - /// 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(self.timeout)) - .build() - .expect("could not build a client for https requests"); - let response = client.get(url.clone()).send(); - if response.is_ok() { - self.reachable += 1 - } - }); - self.calc_success(); - } - - /// ## calculate the success based on the `reachable` and `total` - /// - /// Calculates the ratio of [`reachable`](UptimeStatus::reachable) / - /// (length of [urls](UptimeStatus::urls)). - /// - /// Calculates a [`success_ratio`](UptimeStatus::success_ratio) (as [u8]) from that, - /// by multiplying with 100, then flooring. - /// - /// If the [`success_ratio`](UptimeStatus::success_ratio) is greater than or equal to the - /// [`success_ratio_target`](UptimeStatus::success_ratio_target), the [`UptimeStatus`] will be - /// considered a success. - /// - /// 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()`](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 { - self.success = true; - self.success_ratio = 0; - return; - } - let ratio: f32 = (self.reachable as f32) / (self.urls.len() as f32) * 100f32; - trace!("calculated success_ratio: {}", ratio); - self.success_ratio = ratio.floor() as u8; - self.success = self.success_ratio >= self.success_ratio_target; - trace!("calculated success as: {}", self.success) - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -impl fmt::Debug for Status { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut urls: Vec<&str> = Vec::new(); - for url in &self.urls { - urls.push(url.as_str()); - } - write!(f, "{}", serde_json::to_string(self).unwrap()) - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -impl fmt::Display for Status { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut urls: Vec<&str> = Vec::new(); - for url in &self.urls { - urls.push(url.as_str()); - } - write!(f, "{}", serde_json::to_string_pretty(self).unwrap()) - } -} - -//// PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////// -/// ## Uptime monitor -/// -/// 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, - timeout: u64, -) { - if urls.len() == 0 { - error!("No URLs provided. There is nothing to monitor."); - return; - } - - let interval = std::time::Duration::from_millis(interval); - let mut last_downtime: Option = None; - let mut last_uptime: Option = None; - let mut status = Status::new(success_ratio_target, urls, timeout); - // we assume that the last status was up, so the binary shows the first status if its a - // failure. - let mut last_was_up: bool = true; - let mut last_ratio: u8 = status.success_ratio; - loop { - trace!( - ?status, - ?last_was_up, - "loop iteration for continuous uptime monitor" - ); - if !status.success { - if last_was_up { - trace!("displaying status"); - display_uptime_status("fail", last_uptime, last_downtime, &status) - } - last_downtime = Some(SystemTime::now()); - last_was_up = false; - } else if status.success_ratio < 100 { - if status.success_ratio != last_ratio { - let msg = format!( - "uptime check: not all urls are reachable ({}%)", - status.success_ratio - ); - display_uptime_status(&msg, last_uptime, last_downtime, &status) - } - last_uptime = Some(SystemTime::now()); - last_was_up = true; - } else { - if !last_was_up { - display_uptime_status("success", last_uptime, last_downtime, &status) - } - last_uptime = Some(SystemTime::now()); - last_was_up = true; - } - - last_ratio = status.success_ratio; - std::thread::sleep(interval); - status.check(); - } -} - -// PRIVATE FUNCTIONS ///////////////////////////////////////////////////////////////////////////// -/// Displays the current status for the [continuous uptime monitor](continuous_uptime_monitor) -fn display_uptime_status( - msg: &str, - last_uptime: Option, - last_downtime: Option, - status: &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!( - "since downtime: {}", - match_format_duration_since(last_downtime) - ); - info!( - "since uptime: {}", - match_format_duration_since(last_uptime) - ); - debug!("\n{}", status); - info!("{}", divider()); -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Returns "None" if the given [Option] is [None](Option::None). Otherwise, returns the time stamp -/// formatted according to rfc3999. -fn match_format_time(time: Option) -> String { - match time { - Some(time) => format_rfc3339(time).to_string(), - None => String::from("None"), - } -} - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// Returns "None" if the given [Option] is [None](Option::None). Otherwise, returns duration since -/// that time in a human readable format. -fn match_format_duration_since(time: Option) -> String { - match time { - Some(time) => format_duration( - SystemTime::now() - .duration_since(time) - .expect("could not calculate elapsed time"), - ) - .to_string(), - None => String::from("None"), - } -} diff --git a/members/libpt-py/Cargo.toml b/members/libpt-py/Cargo.toml index 1e97a36..e2ae9b2 100644 --- a/members/libpt-py/Cargo.toml +++ b/members/libpt-py/Cargo.toml @@ -18,8 +18,6 @@ anyhow.workspace = true [features] default = ["log", "core", "full"] core = [] -full = ["default", "core", "math", "log", "bintols", "net"] -math = ["libpt/math", "log"] +full = ["default", "core", "log", "bintols"] log = ["libpt/log"] bintols = ["libpt/bintols", "log"] -net = ["libpt/net", "log"] diff --git a/src/lib.rs b/src/lib.rs index e4f1ae3..7809945 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,9 +12,5 @@ pub use libpt_bintols as bintols; pub use libpt_core as core; #[cfg(feature = "log")] pub use libpt_log as log; -#[cfg(feature = "math")] -pub use libpt_math as math; -#[cfg(feature = "net")] -pub use libpt_net as net; #[cfg(feature = "py")] pub use libpt_py as py;