#!/usr/bin/env python3 """ Health check and diagnostic script for CVE Fact Checker. Run this to diagnose common issues and verify system health. """ import os import sys import tempfile import requests import time from pathlib import Path def check_environment(): """Check environment variables and directories.""" print("🔍 Environment Check") print("=" * 50) # Check Python version print(f"Python version: {sys.version}") # Check key environment variables env_vars = [ 'PORT', 'VECTOR_PERSIST_DIR', 'SENTENCE_TRANSFORMERS_HOME', 'FIREBASE_API_KEY', 'OPENROUTER_API_KEY', 'AUTO_INGEST', 'LANGUAGE_FILTER' ] for var in env_vars: value = os.environ.get(var, 'Not set') if 'API_KEY' in var and value != 'Not set': value = f"{value[:8]}..." if len(value) > 8 else "***" print(f" {var}: {value}") print() def check_directories(): """Check directory permissions and setup.""" print("📁 Directory Permissions Check") print("=" * 50) dirs_to_check = [ ('/tmp', 'System temp directory'), ('/tmp/vector_db', 'Vector DB storage'), ('/tmp/sentence_transformers', 'Model cache'), ('/data', 'Persistent data (if available)'), ('.', 'Current directory'), ] for dir_path, description in dirs_to_check: try: path = Path(dir_path) # Check if exists exists = path.exists() if not exists and dir_path != '/data': # /data might not exist path.mkdir(parents=True, exist_ok=True) exists = True # Test write permission if exists writable = False if exists: try: test_file = path / "health_check_test" test_file.write_text("test") test_file.unlink() writable = True except: pass status = "✅" if (exists and writable) else "❌" print(f" {status} {dir_path} - {description}") if exists and not writable: print(f" ⚠️ Exists but not writable") elif not exists: print(f" ⚠️ Does not exist") except Exception as e: print(f" ❌ {dir_path} - Error: {e}") print() def check_packages(): """Check if required packages are importable.""" print("📦 Package Import Check") print("=" * 50) packages = [ ('flask', 'Flask web framework'), ('requests', 'HTTP requests'), ('langchain', 'LangChain framework'), ('chromadb', 'Vector database'), ('sentence_transformers', 'Embedding models'), ('firebase_admin', 'Firebase SDK'), ] for package, description in packages: try: __import__(package) print(f" ✅ {package} - {description}") except ImportError as e: print(f" ❌ {package} - {description} (Error: {e})") except Exception as e: print(f" ⚠️ {package} - {description} (Warning: {e})") print() def check_app_health(): """Check if the Flask app can be imported and basic functionality.""" print("🏥 Application Health Check") print("=" * 50) try: # Test basic imports from cve_factchecker.config import load_openrouter_config print(" ✅ Config module imports") from cve_factchecker.embeddings import build_embeddings print(" ✅ Embeddings module imports") from cve_factchecker.models import NewsArticle print(" ✅ Models module imports") # Test embeddings embeddings = build_embeddings() test_embedding = embeddings.embed_query("test") if len(test_embedding) > 0: print(" ✅ Embeddings functional") else: print(" ❌ Embeddings not working") # Test Flask app import from cve_factchecker.app import app print(" ✅ Flask app imports") # Test basic app functionality with app.test_client() as client: response = client.get('/health') if response.status_code == 200: print(" ✅ Health endpoint responds") else: print(f" ❌ Health endpoint error: {response.status_code}") except Exception as e: print(f" ❌ Application error: {e}") import traceback traceback.print_exc() print() def check_firebase_connectivity(): """Check Firebase connectivity.""" print("🔥 Firebase Connectivity Check") print("=" * 50) try: from cve_factchecker.firebase_loader import FirebaseNewsLoader loader = FirebaseNewsLoader() print(f" ✅ Firebase loader initialized") print(f" 📍 Project ID: {loader.project_id}") # Test with very small limit to avoid rate limiting, filter for English print(" 🔍 Testing connectivity with 1 English article limit...") articles = loader.fetch_articles(limit=1, language="English") if articles: print(f" ✅ Successfully fetched {len(articles)} article(s)") print(f" 📄 Sample title: {articles[0].title[:50]}...") else: print(" ⚠️ No articles fetched (might be rate limited or empty)") except Exception as e: print(f" ❌ Firebase error: {e}") print() def main(): """Run all health checks.""" print("🏥 CVE Fact Checker Health Check") print("=" * 60) print(f"Timestamp: {time.strftime('%Y-%m-%d %H:%M:%S')}") print() check_environment() check_directories() check_packages() check_app_health() check_firebase_connectivity() print("🏁 Health check complete!") print("=" * 60) if __name__ == "__main__": main()