CVE-FactChecker / health_check.py
NLPGenius's picture
Fix permission errors, rate limiting, and add English language filtering
e06a21d
#!/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()