File size: 11,527 Bytes
edd00ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import os
import json
import pickle
import hashlib
import httpx
from datetime import datetime, timezone
from pymongo import MongoClient
from tenacity import retry, stop_after_attempt, wait_exponential
from dotenv import load_dotenv

load_dotenv()

# ============================================================
# PIPELINE METRICS CLASS (Complete tracking system)
# ============================================================

class PipelineMetrics:
    """
    Complete metrics tracking for pipeline execution.
    Tracks timing, stages, cache hits, and saves to MongoDB.
    """
    
    def __init__(self, topic, mode):
        """Initialize metrics tracker"""
        self.topic = topic
        self.mode = mode
        self.run_id = f"{mode}_{int(datetime.now().timestamp())}"
        self.start_time = datetime.now(timezone.utc)
        self.stages = {}
        self.current_stage = None
        self.current_stage_start = None
        self.cache_hit = False
        self.cache_type = None
        self.error_occurred = False
        self.error_message = None
        
    def start_stage(self, stage_name):
        """Start tracking a stage"""
        self.current_stage = stage_name
        self.current_stage_start = datetime.now(timezone.utc)
        print(f"   πŸ“Š [METRICS] Starting: {stage_name}")
        
    def end_stage(self, stage_name, output_summary=None):
        """End tracking a stage"""
        if self.current_stage_start:
            duration = (datetime.now(timezone.utc) - self.current_stage_start).total_seconds()
            self.stages[stage_name] = {
                "duration_seconds": duration,
                "timestamp": datetime.now(timezone.utc),
                "output_summary": output_summary
            }
            print(f"   βœ“ Stage '{stage_name}' completed in {duration:.2f}s")
        
    def set_cache_hit(self, cache_type="mongodb"):
        """Record cache hit"""
        self.cache_hit = True
        self.cache_type = cache_type
        print(f"   πŸ’Ύ Cache hit: {cache_type}")
        
    def set_error(self, error_message):
        """Record error"""
        self.error_occurred = True
        self.error_message = error_message
        print(f"   ❌ Error: {error_message}")
        
    def end(self):
        """End pipeline tracking"""
        total_duration = (datetime.now(timezone.utc) - self.start_time).total_seconds()
        self.metrics = {
            "run_id": self.run_id,
            "topic": self.topic,
            "mode": self.mode,
            "started_at": self.start_time,
            "completed_at": datetime.now(timezone.utc),
            "total_duration_seconds": total_duration,
            "stages": self.stages,
            "cache_hit": self.cache_hit,
            "cache_type": self.cache_type,
            "error_occurred": self.error_occurred,
            "error_message": self.error_message
        }
        print(f"\n   πŸ“Š Pipeline Complete: {total_duration:.2f}s total")
        return self.metrics
        
    def save_metrics(self):
        """Save metrics to MongoDB"""
        try:
            mongo_uri = os.getenv("MONGO_URI")
            if not mongo_uri:
                print("   ⚠️ MONGO_URI not set - skipping metrics save")
                return False
                
            client = MongoClient(mongo_uri, serverSelectionTimeoutMS=5000)
            db = client["learnToGo"]
            
            # Collections based on mode
            if self.mode == "technical":
                metrics_col = db["pipelinemetrics"]
                stages_col = db["stageoutputs"]
            else:
                metrics_col = db["operational_pipeline_metrics"]
                stages_col = db["operational_stage_outputs"]
            
            # Save metrics
            metrics_col.insert_one(self.metrics)
            
            # Save stage details
            for stage_name, stage_data in self.stages.items():
                stage_doc = {
                    "run_id": self.run_id,
                    "topic": self.topic,
                    "mode": self.mode,
                    "stage_name": stage_name,
                    "stage_data": stage_data
                }
                stages_col.insert_one(stage_doc)
            
            print(f"   βœ“ Metrics saved to MongoDB")
            return True
            
        except Exception as e:
            print(f"   ⚠️ Could not save metrics: {e}")
            return False

# ============================================================
# MONGODB CONNECTION & COLLECTIONS
# ============================================================

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=2, max=10)
)
def get_mongo_client():
    """Get MongoDB client from environment variables"""
    mongo_uri = os.getenv("MONGO_URI")
    if not mongo_uri:
        raise ValueError("MONGO_URI not set in .env")
    return MongoClient(mongo_uri, serverSelectionTimeoutMS=5000)

def get_collections():
    """Get MongoDB collections for Technical and Operational keywords"""
    client = get_mongo_client()
    db = client["learnToGo"]
    
    technical_collection = db["Keywords"]
    operational_collection = db["OperationalKeywords"]
    
    # Create indexes
    technical_collection.create_index("aliases")
    operational_collection.create_index("aliases")
    
    return technical_collection, operational_collection, db

# ============================================================
# URL CACHING (Pickle-based - FIXED with proper dict structure)
# ============================================================

URL_CACHE_FILE = "/tmp/url_validation_cache.pkl"

def load_url_cache():
    """Load URL validation cache from pickle file"""
    try:
        if os.path.exists(URL_CACHE_FILE):
            with open(URL_CACHE_FILE, 'rb') as f:
                cache = pickle.load(f)
            print(f"βœ“ Loaded URL cache with {len(cache)} entries")
            return cache
    except Exception as e:
        print(f"⚠️ Could not load URL cache: {e}")
    return {}

def save_url_cache(cache):
    """Save URL validation cache to pickle file"""
    try:
        with open(URL_CACHE_FILE, 'wb') as f:
            pickle.dump(cache, f)
        print(f"βœ“ Saved URL cache with {len(cache)} entries")
        return True
    except Exception as e:
        print(f"⚠️ Could not save URL cache: {e}")
        return False

def get_url_hash(url):
    """Generate MD5 hash for URL as cache key"""
    return hashlib.md5(url.encode()).hexdigest()

@retry(
    stop=stop_after_attempt(2),
    wait=wait_exponential(multiplier=1, min=2, max=5)
)
def validate_url_cached(url, timeout=5):
    """Check if URL is valid with cache check - FIXED to return dict"""
    url_hash = get_url_hash(url)
    
    # Load cache
    url_cache = load_url_cache()
    
    # Check cache
    if url_hash in url_cache:
        print(f"   πŸ’Ύ URL cache hit: {url[:50]}...")
        return url_cache[url_hash]['valid']  # ← Returns boolean from dict
    
    # Validate URL
    try:
        response = httpx.head(url, timeout=timeout, follow_redirects=True)
        is_valid = response.status_code in [200, 301, 302, 303, 307, 308]
    except:
        try:
            response = httpx.get(url, timeout=timeout, follow_redirects=True)
            is_valid = response.status_code == 200
        except:
            is_valid = False
    
    # Save to cache as DICT with valid, checked_at, url
    url_cache[url_hash] = {
        'valid': is_valid,
        'checked_at': datetime.now(timezone.utc).isoformat(),
        'url': url
    }
    save_url_cache(url_cache)
    
    print(f"   βœ“ URL validated: {url[:50]}... = {is_valid}")
    return is_valid

# ============================================================
# CACHE OPERATIONS
# ============================================================

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=2, max=10)
)
def check_cache(topic, collection):
    """
    Check MongoDB cache using normalized keyword - NO LLM call!
    Includes retry logic for connection failures.
    """
    try:
        normalized = topic.lower().strip()
        print(f"πŸ” Checking cache for: {normalized}")
        
        cached = collection.find_one({"aliases": normalized})
        
        if cached:
            print(f"βœ… CACHE HIT! Found topic: {cached['topic']}")
            return cached['content'], True
        else:
            print(f"❌ CACHE MISS - Will run full pipeline")
            return None, False
            
    except Exception as e:
        print(f"❌ Cache lookup error: {e}")
        raise

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=2, max=10)
)
def save_to_cache(topic, content, collection):
    """
    Save generated slides to MongoDB.
    Includes retry logic for connection failures.
    """
    try:
        aliases = content.get('aliases', [topic.lower().strip()])
        
        document = {
            "topic": content.get('topic', topic),
            "aliases": aliases,
            "createdAt": datetime.now(timezone.utc),
            "content": content
        }
        
        result = collection.insert_one(document)
        print(f"βœ… Saved to MongoDB - Document ID: {result.inserted_id}")
        return result.inserted_id
        
    except Exception as e:
        print(f"❌ Cache save error: {e}")
        raise

# ============================================================
# URL VALIDATION & SELECTION
# ============================================================

def validate_and_select_urls(corrected_json):
    """
    Validate ALL URLs and select best ones.
    Uses cached validation to avoid repeated HTTP requests.
    """
    urls = corrected_json.get("urls", [])
    print(f"Validating {len(urls)} URLs with caching...")
    
    valid_urls = []
    validation_results = []
    
    for url_obj in urls:
        url = url_obj.get("url")
        if url:
            is_valid = validate_url_cached(url)
            
            validation_results.append({
                "url": url,
                "title": url_obj.get("title"),
                "valid": is_valid
            })
            
            if is_valid:
                valid_urls.append(url_obj)
    
    # Keep only best 5 URLs
    valid_urls = valid_urls[:5]
    
    print(f"βœ“ Kept {len(valid_urls)} valid URLs")
    
    corrected_json["urls"] = valid_urls
    return corrected_json, validation_results

# ============================================================
# INPUT VALIDATION (50 char limit for both technical and operational)
# ============================================================

@retry(
    stop=stop_after_attempt(3),
    wait=wait_exponential(multiplier=1, min=1, max=3)
)
def validate_and_sanitize_topic(topic):
    """
    Validate and sanitize user input before pipeline.
    Prevents errors and invalid topics.
    FIXED: Both technical and operational now have 50 char limit
    """
    if not topic or not topic.strip():
        raise ValueError("❌ Topic cannot be empty.")
    
    topic = topic.strip()
    
    if len(topic) < 1:
        raise ValueError("❌ Topic must be at least 1 character long.")
    if len(topic) > 50:
        raise ValueError("❌ Topic cannot exceed 50 characters.")
    
    print(f"βœ… Input validated: '{topic}'")
    return topic

print("βœ“ All utility functions ready with metrics, URL caching, and retry logic")