Skip to content

Commit

Permalink
check_http: Simplify UpperLevels
Browse files Browse the repository at this point in the history
This is a preparation step for using the UpperLevels as displayable
information

* Reduce the usage of UpperLevels to primitive types that implement
  Display
* Remove Ord as trait bound, as it's not implemented on f64, and it's
  not needed anyways

CMK-14257

Change-Id: I3799ec67797ba9e87a0c694a552650609893a8b8
  • Loading branch information
andiumbreit committed Nov 15, 2023
1 parent 9d31787 commit 8f22f71
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 57 deletions.
74 changes: 25 additions & 49 deletions packages/check-http/src/checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ use crate::http::ProcessedResponse;
// So we're modelling exactly this.

pub struct UpperLevels<T> {
warn: T,
crit: Option<T>,
pub warn: T,
pub crit: Option<T>,
}

impl<T> UpperLevels<T>
where
T: Ord + PartialOrd,
T: PartialOrd,
{
pub fn warn(warn: T) -> Self {
Self { warn, crit: None }
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct Bounds<T> {

impl<T> Bounds<T>
where
T: Ord + PartialOrd,
T: PartialOrd,
{
pub fn lower(lower: T) -> Self {
Self { lower, upper: None }
Expand Down Expand Up @@ -178,8 +178,8 @@ pub enum CheckResult {
pub struct CheckParameters {
pub onredirect: OnRedirect,
pub page_size: Option<Bounds<usize>>,
pub response_time_levels: Option<UpperLevels<Duration>>,
pub document_age_levels: Option<UpperLevels<Duration>>,
pub response_time_levels: Option<UpperLevels<f64>>,
pub document_age_levels: Option<UpperLevels<u64>>,
}

pub fn collect_response_checks(
Expand Down Expand Up @@ -248,10 +248,10 @@ fn check_page_size(page_size: usize, page_size_limits: Option<Bounds<usize>>) ->

fn check_response_time(
response_time: Duration,
response_time_levels: Option<UpperLevels<Duration>>,
response_time_levels: Option<UpperLevels<f64>>,
) -> Option<CheckItem> {
let state = response_time_levels
.and_then(|levels| levels.evaluate(&response_time))
.and_then(|levels| levels.evaluate(&response_time.as_secs_f64()))
.unwrap_or(State::Ok);

Some(CheckItem {
Expand All @@ -266,7 +266,7 @@ fn check_response_time(

fn check_document_age(
headers: &HeaderMap,
document_age_levels: Option<UpperLevels<Duration>>,
document_age_levels: Option<UpperLevels<u64>>,
) -> Option<CheckItem> {
let document_age_levels = document_age_levels?;

Expand Down Expand Up @@ -295,7 +295,7 @@ fn check_document_age(
return cr_document_age_error;
};

let state = document_age_levels.evaluate(&age)?;
let state = document_age_levels.evaluate(&age.as_secs())?;

//TODO(au): Specify "too old" in Output
Some(CheckItem {
Expand Down Expand Up @@ -375,73 +375,49 @@ mod test_check_response_time {
#[test]
fn test_warn_within_bounds() {
assert_eq!(
check_response_time(
Duration::new(5, 0),
Some(UpperLevels::warn(Duration::new(6, 0)))
)
.unwrap()
.state,
check_response_time(Duration::new(5, 0), Some(UpperLevels::warn(6.)))
.unwrap()
.state,
State::Ok
);
}

#[test]
fn test_warn_is_warn() {
assert_eq!(
check_response_time(
Duration::new(5, 0),
Some(UpperLevels::warn(Duration::new(4, 0)))
)
.unwrap()
.state,
check_response_time(Duration::new(5, 0), Some(UpperLevels::warn(4.)))
.unwrap()
.state,
State::Warn
);
}

#[test]
fn test_warncrit_within_bounds() {
assert_eq!(
check_response_time(
Duration::new(5, 0),
Some(UpperLevels::warn_crit(
Duration::new(6, 0),
Duration::new(7, 0)
))
)
.unwrap()
.state,
check_response_time(Duration::new(5, 0), Some(UpperLevels::warn_crit(6., 7.)))
.unwrap()
.state,
State::Ok
);
}

#[test]
fn test_warncrit_is_warn() {
assert_eq!(
check_response_time(
Duration::new(5, 0),
Some(UpperLevels::warn_crit(
Duration::new(4, 0),
Duration::new(6, 0)
))
)
.unwrap()
.state,
check_response_time(Duration::new(5, 0), Some(UpperLevels::warn_crit(4., 6.)))
.unwrap()
.state,
State::Warn
);
}

#[test]
fn test_warncrit_is_crit() {
assert_eq!(
check_response_time(
Duration::new(5, 0),
Some(UpperLevels::warn_crit(
Duration::new(2, 0),
Duration::new(3, 0)
))
)
.unwrap()
.state,
check_response_time(Duration::new(5, 0), Some(UpperLevels::warn_crit(2., 3.)))
.unwrap()
.state,
State::Crit
);
}
Expand Down
11 changes: 3 additions & 8 deletions packages/check-http/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use check_http::output::Output;
use check_http::runner::collect_checks;
use clap::Parser;
use cli::Cli;
use std::time::Duration;

mod cli;
mod pwstore;
Expand Down Expand Up @@ -76,14 +75,10 @@ fn make_configs(
(x, Some(y)) => Bounds::lower_upper(x, y),
}),
response_time_levels: args.response_time_levels.map(|val| match val {
(x, None) => UpperLevels::warn(Duration::from_secs_f64(x)),
(x, Some(y)) => {
UpperLevels::warn_crit(Duration::from_secs_f64(x), Duration::from_secs_f64(y))
}
(x, None) => UpperLevels::warn(x),
(x, Some(y)) => UpperLevels::warn_crit(x, y),
}),
document_age_levels: args
.document_age_levels
.map(|val| UpperLevels::warn(Duration::from_secs(val))),
document_age_levels: args.document_age_levels.map(UpperLevels::warn),
},
)
}

0 comments on commit 8f22f71

Please sign in to comment.