domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
Documentation
//! Statistics printing and database updates.

use log::info;
use std::sync::Arc;
use strum::IntoEnumIterator;

use crate::error_handling::{ErrorType, InfoType, ProcessingStats, WarningType};
use crate::utils::TimingStats;

/// Prints timing statistics if enabled.
///
/// Optionally accepts flags to indicate whether `GeoIP` and WHOIS are enabled,
/// which will be displayed in the output when these features are disabled.
pub fn print_timing_statistics(
    timing_stats: &Arc<TimingStats>,
    geoip_enabled: Option<bool>,
    whois_enabled: Option<bool>,
) {
    timing_stats.log_summary(geoip_enabled, whois_enabled);
}

/// Prints error, warning, and info statistics to the log.
///
/// This function is used internally and in tests.
pub fn print_error_statistics(error_stats: &ProcessingStats) {
    let total_errors = error_stats.total_errors();
    let total_warnings = error_stats.total_warnings();
    let total_info = error_stats.total_info();

    if total_errors > 0 {
        info!("Error Counts ({total_errors} total):");
        for error_type in ErrorType::iter() {
            let count = error_stats.get_error_count(error_type);
            if count > 0 {
                info!("   {}: {}", error_type.as_str(), count);
            }
        }
    }

    if total_warnings > 0 {
        info!("Warning Counts ({total_warnings} total):");
        for warning_type in WarningType::iter() {
            let count = error_stats.get_warning_count(warning_type);
            if count > 0 {
                info!("   {}: {}", warning_type.as_str(), count);
            }
        }
    }

    if total_info > 0 {
        info!("Info Counts ({total_info} total):");
        for info_type in InfoType::iter() {
            let count = error_stats.get_info_count(info_type);
            if count > 0 {
                info!("   {}: {}", info_type.as_str(), count);
            }
        }
    }
}

/// Logs `url_partial_failures` fact-table counts. These do not increment `failed_urls`.
pub fn print_partial_failure_counts(total: i64, satellite_insert_errors: i64) {
    if total > 0 {
        info!(
            "Partial failures ({total} url_partial_failures rows, {satellite_insert_errors} satellite insert errors). These do not increment failed_urls."
        );
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::error_handling::ProcessingStats;
    use crate::storage::{update_run_stats, RunStats};
    use anyhow::{Context, Result};
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[allow(clippy::too_many_arguments)]
    async fn print_and_save_final_statistics(
        pool: &sqlx::SqlitePool,
        run_id: &str,
        total_urls_attempted: &Arc<AtomicUsize>,
        successful_urls: &Arc<AtomicUsize>,
        failed_urls: &Arc<AtomicUsize>,
        skipped_urls: &Arc<AtomicUsize>,
        error_stats: &Arc<ProcessingStats>,
        elapsed_seconds: f64,
    ) -> Result<()> {
        #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
        let total_urls = total_urls_attempted.load(Ordering::SeqCst) as i32;
        let skipped = skipped_urls.load(Ordering::SeqCst);
        #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
        let successful_urls = successful_urls.load(Ordering::SeqCst) as i32;
        #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
        let failed_urls_count = failed_urls.load(Ordering::SeqCst) as i32;
        #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
        let skipped_urls_count = skipped as i32;

        info!(
            "Run statistics: total={total_urls}, successful={successful_urls}, failed={failed_urls_count}, skipped={skipped_urls_count}"
        );

        let stats = RunStats {
            run_id,
            total_urls,
            successful_urls,
            failed_urls: failed_urls_count,
            skipped_urls: skipped_urls_count,
            elapsed_seconds,
        };
        update_run_stats(pool, &stats)
            .await
            .context("Failed to update run statistics")?;

        print_error_statistics(error_stats);

        info!(
            "Processed {} URL{} ({} succeeded, {} failed, {} skipped) in {:.1}s",
            total_urls,
            if total_urls == 1 { "" } else { "s" },
            successful_urls,
            failed_urls_count,
            skipped_urls_count,
            elapsed_seconds
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_print_and_save_final_statistics() {
        // Create in-memory database for testing
        let pool = sqlx::SqlitePool::connect("sqlite::memory:")
            .await
            .expect("Failed to create in-memory database");

        // Create tables (matching actual schema)
        sqlx::query(
            "CREATE TABLE runs (
                run_id TEXT PRIMARY KEY,
                version TEXT,
                start_time_ms INTEGER NOT NULL,
                end_time_ms INTEGER,
                total_urls INTEGER,
                successful_urls INTEGER,
                failed_urls INTEGER,
                skipped_urls INTEGER,
                elapsed_seconds REAL,
                fingerprints_source TEXT,
                fingerprints_version TEXT,
                geoip_version TEXT
            )",
        )
        .execute(&pool)
        .await
        .expect("Failed to create runs table");

        let run_id = "test-run-123";
        let total_urls = Arc::new(AtomicUsize::new(100));
        let successful_urls = Arc::new(AtomicUsize::new(85));
        let failed_urls = Arc::new(AtomicUsize::new(15));
        let skipped_urls = Arc::new(AtomicUsize::new(0));
        let error_stats = Arc::new(ProcessingStats::new());

        // Insert initial run record using insert_run_metadata
        use crate::storage::{insert_run_metadata, RunMetadata};
        let meta = RunMetadata {
            run_id,
            start_time_ms: chrono::Utc::now().timestamp_millis(),
            version: "0.1.4",
            fingerprints_source: None,
            fingerprints_version: None,
            geoip_version: None,
        };
        insert_run_metadata(&pool, &meta)
            .await
            .expect("Failed to insert run");

        // Call function
        let result = print_and_save_final_statistics(
            &pool,
            run_id,
            &total_urls,
            &successful_urls,
            &failed_urls,
            &skipped_urls,
            &error_stats,
            10.5, // elapsed_seconds for test
        )
        .await;

        assert!(result.is_ok(), "Function should succeed");

        // Verify statistics were updated
        let row = sqlx::query_as::<_, (i32, i32, i32, i32)>(
            "SELECT total_urls, successful_urls, failed_urls, skipped_urls FROM runs WHERE run_id = ?",
        )
        .bind(run_id)
        .fetch_one(&pool)
        .await
        .expect("Failed to fetch run");

        assert_eq!(row.0, 100, "Total URLs should be 100");
        assert_eq!(row.1, 85, "Successful URLs should be 85");
        assert_eq!(row.2, 15, "Failed URLs should be 15 (100 - 85)");
        assert_eq!(row.3, 0, "Skipped URLs should be 0");
    }

    #[tokio::test]
    async fn test_print_and_save_final_statistics_zero_urls() {
        let pool = sqlx::SqlitePool::connect("sqlite::memory:")
            .await
            .expect("Failed to create in-memory database");

        sqlx::query(
            "CREATE TABLE runs (
                run_id TEXT PRIMARY KEY,
                version TEXT,
                start_time_ms INTEGER NOT NULL,
                end_time_ms INTEGER,
                total_urls INTEGER,
                successful_urls INTEGER,
                failed_urls INTEGER,
                skipped_urls INTEGER,
                elapsed_seconds REAL,
                fingerprints_source TEXT,
                fingerprints_version TEXT,
                geoip_version TEXT
            )",
        )
        .execute(&pool)
        .await
        .expect("Failed to create runs table");

        let run_id = "test-run-zero";
        let total_urls = Arc::new(AtomicUsize::new(0));
        let successful_urls = Arc::new(AtomicUsize::new(0));
        let failed_urls = Arc::new(AtomicUsize::new(0));
        let skipped_urls = Arc::new(AtomicUsize::new(0));
        let error_stats = Arc::new(ProcessingStats::new());

        // Insert initial run record using insert_run_metadata
        use crate::storage::{insert_run_metadata, RunMetadata};
        let meta = RunMetadata {
            run_id,
            start_time_ms: chrono::Utc::now().timestamp_millis(),
            version: "0.1.4",
            fingerprints_source: None,
            fingerprints_version: None,
            geoip_version: None,
        };
        insert_run_metadata(&pool, &meta)
            .await
            .expect("Failed to insert run");

        let result = print_and_save_final_statistics(
            &pool,
            run_id,
            &total_urls,
            &successful_urls,
            &failed_urls,
            &skipped_urls,
            &error_stats,
            0.0, // elapsed_seconds for test (zero URLs)
        )
        .await;

        assert!(result.is_ok(), "Function should succeed with zero URLs");
    }
}