File size: 6,141 Bytes
e06a21d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/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()