athena_rs 3.22.1

Hyper performant polyglot Database driver
Documentation
//! Health-reason assembly for `/debug/schema`.
//!
//! This module derives structured health reasons from summary metrics so the
//! debug payload can explain why a health label was assigned, while staged
//! reason collection is delegated to
//! `debug_health_reasons_stage_input_assembly`,
//! `debug_health_reasons_stage_output_assembly` and final staged handoff is
//! delegated to the stable facade in
//! `debug_health_reasons_from_stage_output_assembly`, which delegates to
//! `debug_health_reasons_stage_output_handoff_assembly`.

use super::debug_health_reasons_from_stage_output_assembly::build_logging_schema_debug_health_reasons_from_stage_output_assembly;
use super::debug_health_reasons_stage_input_assembly::build_debug_health_reasons_stage_input_assembly;
use super::debug_health_reasons_stage_output_assembly::build_debug_health_reasons_stage_output_assembly;
use super::debug_summary_contracts::LoggingSchemaDebugHealthReason;
use super::debug_summary_metrics::LoggingSchemaDebugSummaryMetrics;

/// Builds structured health reasons from summary metrics.
pub(super) fn build_logging_schema_debug_health_reasons(
    metrics: &LoggingSchemaDebugSummaryMetrics,
) -> Vec<LoggingSchemaDebugHealthReason> {
    let stage_input = build_debug_health_reasons_stage_input_assembly(metrics);
    let stage = build_debug_health_reasons_stage_output_assembly(stage_input);
    build_logging_schema_debug_health_reasons_from_stage_output_assembly(stage)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::api::schema::debug_health_stage_test_fixtures::build_health_summary_metrics_for_tests;

    #[test]
    /// Emits one info reason when no missing resources or mismatches exist.
    fn health_reasons_include_info_reason_when_all_resources_are_present() {
        let reasons = build_logging_schema_debug_health_reasons(
            &build_health_summary_metrics_for_tests(0, 0, 0, 0, 0),
        );
        assert_eq!(reasons.len(), 1);
        assert_eq!(reasons[0].code, "all_expected_resources_present");
        assert_eq!(reasons[0].severity, "info");
    }

    #[test]
    /// Emits deterministic error/warning reasons for nonzero metric groups.
    fn health_reasons_emit_error_and_warning_reason_groups() {
        let reasons = build_logging_schema_debug_health_reasons(
            &build_health_summary_metrics_for_tests(1, 2, 1, 3, 4),
        );
        assert_eq!(reasons.len(), 5);
        assert_eq!(reasons[0].code, "missing_required_tables");
        assert_eq!(reasons[0].severity, "error");
        assert_eq!(reasons[1].code, "missing_required_columns");
        assert_eq!(reasons[2].code, "relation_type_mismatches");
        assert_eq!(reasons[3].code, "missing_optional_tables");
        assert_eq!(reasons[3].severity, "warning");
        assert_eq!(reasons[4].code, "missing_optional_columns");
    }
}