From 2a59651fb9303bb43ffbce6e2984a5b5a914a95d Mon Sep 17 00:00:00 2001 From: perf3ct Date: Tue, 8 Jul 2025 20:16:33 +0000 Subject: [PATCH] fix(stats): try to fix stats export, again again --- ...708000001_fix_ocr_queue_stats_function.sql | 42 ------------------- src/main.rs | 2 +- 2 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 migrations/20250708000001_fix_ocr_queue_stats_function.sql diff --git a/migrations/20250708000001_fix_ocr_queue_stats_function.sql b/migrations/20250708000001_fix_ocr_queue_stats_function.sql deleted file mode 100644 index 0370004..0000000 --- a/migrations/20250708000001_fix_ocr_queue_stats_function.sql +++ /dev/null @@ -1,42 +0,0 @@ --- Fix the get_ocr_queue_stats function to ensure it matches the expected structure --- This migration ensures the function correctly gets completed_today from documents table --- and handles the case where migration 20250620100019 may have failed silently - -CREATE OR REPLACE FUNCTION get_ocr_queue_stats() -RETURNS TABLE ( - pending_count BIGINT, - processing_count BIGINT, - failed_count BIGINT, - completed_today BIGINT, - avg_wait_time_minutes DOUBLE PRECISION, - oldest_pending_minutes DOUBLE PRECISION -) AS $$ -BEGIN - RETURN QUERY - WITH queue_stats AS ( - SELECT - COUNT(*) FILTER (WHERE status = 'pending') as pending_count, - COUNT(*) FILTER (WHERE status = 'processing') as processing_count, - COUNT(*) FILTER (WHERE status = 'failed' AND attempts >= max_attempts) as failed_count, - CAST(AVG(EXTRACT(EPOCH FROM (COALESCE(started_at, NOW()) - created_at))/60) FILTER (WHERE status IN ('processing', 'completed')) AS DOUBLE PRECISION) as avg_wait_time_minutes, - CAST(MAX(EXTRACT(EPOCH FROM (NOW() - created_at))/60) FILTER (WHERE status = 'pending') AS DOUBLE PRECISION) as oldest_pending_minutes - FROM ocr_queue - ), - document_stats AS ( - -- Count documents that completed OCR today (looking at documents table where actual completion is tracked) - SELECT COUNT(*) as completed_today - FROM documents - WHERE ocr_status = 'completed' - AND updated_at >= CURRENT_DATE - AND updated_at < CURRENT_DATE + INTERVAL '1 day' - ) - SELECT - queue_stats.pending_count, - queue_stats.processing_count, - queue_stats.failed_count, - document_stats.completed_today, - queue_stats.avg_wait_time_minutes, - queue_stats.oldest_pending_minutes - FROM queue_stats, document_stats; -END; -$$ LANGUAGE plpgsql; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 98d0e02..9071e58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -251,7 +251,7 @@ async fn main() -> anyhow::Result<()> { match function_check { Ok(Some(def)) => { // Check if it contains the correct logic from our latest migration - if def.contains("document_stats") && def.contains("documents.ocr_status = 'completed'") { + if def.contains("document_stats") && def.contains("ocr_status = 'completed'") { info!("✅ get_ocr_queue_stats function has correct logic (uses documents table for completed_today)"); } else { error!("❌ get_ocr_queue_stats function still uses old logic - migration may have failed");