use serde::{Deserialize, Serialize};
const GIB: u64 = 1024 * 1024 * 1024;
const LOW_BYTES: u64 = 2 * GIB;
const RECOVER_BYTES: u64 = 5 * GIB / 2;
const LOW_RATIO_DIVISOR: u128 = 100; const RECOVER_RATIO_DIVISOR: u128 = 80;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DiskStatus {
pub path: String,
pub available_bytes: u64,
pub total_bytes: u64,
pub low: bool,
pub checked_at_ms: u64,
}
pub fn is_low(available_bytes: u64, total_bytes: u64, was_low: bool) -> bool {
let (limit, divisor) = if was_low {
(RECOVER_BYTES, RECOVER_RATIO_DIVISOR)
} else {
(LOW_BYTES, LOW_RATIO_DIVISOR)
};
available_bytes < limit || u128::from(available_bytes) * divisor < u128::from(total_bytes)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn enters_below_either_limit_but_not_at_it() {
assert!(is_low(2 * GIB - 1, 100 * GIB, false));
assert!(!is_low(2 * GIB, 100 * GIB, false));
assert!(is_low(4 * GIB - 1, 400 * GIB, false));
assert!(!is_low(4 * GIB, 400 * GIB, false));
assert!(is_low(4 * GIB, 400 * GIB + 1, false));
}
#[test]
fn stays_low_until_both_recovery_limits_are_met() {
let near_byte_limit = 5 * GIB / 2 - 1;
assert!(!is_low(near_byte_limit, 100 * GIB, false));
assert!(is_low(near_byte_limit, 100 * GIB, true));
assert!(!is_low(near_byte_limit + 1, 100 * GIB, true));
let near_ratio_limit = 5 * GIB - 1;
assert!(!is_low(near_ratio_limit, 400 * GIB, false));
assert!(is_low(near_ratio_limit, 400 * GIB, true));
assert!(!is_low(near_ratio_limit + 1, 400 * GIB, true));
assert!(is_low(near_ratio_limit + 1, 400 * GIB + 1, true));
}
#[test]
fn ratio_comparison_handles_large_volumes() {
assert!(!is_low(u64::MAX / 2, u64::MAX, true));
}
}