athena_rs 3.23.0

Hyper performant polyglot Database driver
Documentation
//! Health-classification helpers for `/debug/schema`.
//!
//! This module owns the policy that converts expected-vs-observed mismatch
//! counters into one overall diagnostics health label while threshold policy is
//! delegated to `debug_health_threshold_policy`.

use super::debug_health_threshold_policy::is_unhealthy_by_required_missing_thresholds;

/// Derives overall health label from missing-table/column and relation-type counts.
pub(super) fn derive_health(
    required_missing_table_count: usize,
    optional_missing_table_count: usize,
    required_missing_column_count: usize,
    optional_missing_column_count: usize,
    relation_type_mismatch_count: usize,
) -> &'static str {
    if required_missing_table_count == 0
        && required_missing_column_count == 0
        && relation_type_mismatch_count == 0
    {
        if optional_missing_table_count == 0 && optional_missing_column_count == 0 {
            return "healthy";
        }
        return "warning";
    }

    if is_unhealthy_by_required_missing_thresholds(
        required_missing_table_count,
        required_missing_column_count,
    ) {
        return "unhealthy";
    }

    "degraded"
}

#[cfg(test)]
mod tests {
    use super::derive_health;

    #[test]
    /// Marks health as unhealthy when required missing-column count crosses threshold.
    fn derive_health_unhealthy_when_required_missing_columns_cross_threshold() {
        assert_eq!(derive_health(0, 0, 10, 0, 0), "unhealthy");
    }

    #[test]
    /// Marks health as warning when only optional expected resources are missing.
    fn derive_health_warning_when_only_optional_items_are_missing() {
        assert_eq!(derive_health(0, 1, 0, 2, 0), "warning");
    }
}