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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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
"""
Smart Real-time Event Processing Engine for Email Triage Environment

Advanced event streaming providing:
- Complex event processing (CEP)
- Event sourcing and replay
- Real-time stream analytics
- Event correlation and pattern detection
"""

from typing import Any, Dict, List, Optional, Callable, Set
from datetime import datetime, timedelta
from collections import deque, defaultdict
from enum import Enum
import threading
import json
import time
import uuid
import asyncio


class EventType(str, Enum):
    """Event types in the system"""
    EMAIL_RECEIVED = "email_received"
    EMAIL_CATEGORIZED = "email_categorized"
    EMAIL_PRIORITIZED = "email_prioritized"
    EMAIL_REPLIED = "email_replied"
    EMAIL_FORWARDED = "email_forwarded"
    EMAIL_ARCHIVED = "email_archived"
    EMAIL_FLAGGED = "email_flagged"
    SPAM_DETECTED = "spam_detected"
    VIP_EMAIL = "vip_email"
    SLA_VIOLATION = "sla_violation"
    SYSTEM_ALERT = "system_alert"
    USER_ACTION = "user_action"
    MODEL_PREDICTION = "model_prediction"
    PERFORMANCE_METRIC = "performance_metric"


class EventSeverity(str, Enum):
    """Event severity levels"""
    DEBUG = "debug"
    INFO = "info"
    WARNING = "warning"
    ERROR = "error"
    CRITICAL = "critical"


class Event:
    """Individual event in the system"""
    
    def __init__(
        self,
        event_type: EventType,
        payload: Dict[str, Any],
        severity: EventSeverity = EventSeverity.INFO,
        source: str = "system",
        correlation_id: Optional[str] = None
    ):
        self.id = str(uuid.uuid4())
        self.event_type = event_type
        self.payload = payload
        self.severity = severity
        self.source = source
        self.correlation_id = correlation_id or self.id
        self.timestamp = datetime.now()
        self.processed = False
        self.retry_count = 0
    
    def to_dict(self) -> Dict[str, Any]:
        """Convert event to dictionary"""
        return {
            "id": self.id,
            "type": self.event_type,
            "payload": self.payload,
            "severity": self.severity,
            "source": self.source,
            "correlation_id": self.correlation_id,
            "timestamp": self.timestamp.isoformat(),
            "processed": self.processed,
            "retry_count": self.retry_count
        }


class EventPattern:
    """Complex event pattern definition"""
    
    def __init__(
        self,
        name: str,
        pattern_fn: Callable[[List[Event]], bool],
        window_seconds: int = 300,
        min_events: int = 2,
        max_events: int = 100
    ):
        self.name = name
        self.pattern_fn = pattern_fn
        self.window_seconds = window_seconds
        self.min_events = min_events
        self.max_events = max_events
        self.matches = 0
        self.last_match = None


class EventProcessor:
    """Real-time event processing engine"""
    
    def __init__(self):
        self._lock = threading.RLock()
        self.event_store = deque(maxlen=50000)
        self.event_handlers: Dict[EventType, List[Callable]] = defaultdict(list)
        self.event_patterns: List[EventPattern] = []
        self.subscribers: Dict[str, Callable] = {}
        self.metrics = {
            "events_processed": 0,
            "events_per_second": 0.0,
            "pattern_matches": 0,
            "processing_errors": 0
        }
        self.recent_events = deque(maxlen=1000)
        self.event_counts: Dict[EventType, int] = defaultdict(int)
        self.processing_times = deque(maxlen=1000)
        
        # Register default patterns
        self._register_default_patterns()
    
    def _register_default_patterns(self):
        """Register default event patterns"""
        
        # Spam wave detection
        def spam_wave_pattern(events: List[Event]) -> bool:
            spam_events = [e for e in events if e.event_type == EventType.SPAM_DETECTED]
            return len(spam_events) >= 5
        
        self.register_pattern(
            "spam_wave",
            spam_wave_pattern,
            window_seconds=60,
            min_events=5
        )
        
        # VIP email rush
        def vip_rush_pattern(events: List[Event]) -> bool:
            vip_events = [e for e in events if e.event_type == EventType.VIP_EMAIL]
            return len(vip_events) >= 3
        
        self.register_pattern(
            "vip_rush",
            vip_rush_pattern,
            window_seconds=300,
            min_events=3
        )
        
        # SLA violation cascade
        def sla_cascade_pattern(events: List[Event]) -> bool:
            sla_events = [e for e in events if e.event_type == EventType.SLA_VIOLATION]
            return len(sla_events) >= 2
        
        self.register_pattern(
            "sla_cascade",
            sla_cascade_pattern,
            window_seconds=180,
            min_events=2
        )
        
        # System overload
        def system_overload_pattern(events: List[Event]) -> bool:
            alert_events = [e for e in events if e.event_type == EventType.SYSTEM_ALERT]
            error_events = [e for e in events if e.severity == EventSeverity.ERROR]
            return len(alert_events) >= 3 or len(error_events) >= 10
        
        self.register_pattern(
            "system_overload", 
            system_overload_pattern,
            window_seconds=120,
            min_events=3
        )
    
    def register_handler(self, event_type: EventType, handler: Callable[[Event], None]):
        """Register an event handler"""
        with self._lock:
            self.event_handlers[event_type].append(handler)
    
    def register_pattern(
        self,
        name: str,
        pattern_fn: Callable[[List[Event]], bool],
        window_seconds: int = 300,
        min_events: int = 2
    ):
        """Register an event pattern"""
        with self._lock:
            pattern = EventPattern(name, pattern_fn, window_seconds, min_events)
            self.event_patterns.append(pattern)
    
    def subscribe(self, subscriber_id: str, callback: Callable[[Event], None]):
        """Subscribe to all events"""
        with self._lock:
            self.subscribers[subscriber_id] = callback
    
    def unsubscribe(self, subscriber_id: str):
        """Unsubscribe from events"""
        with self._lock:
            if subscriber_id in self.subscribers:
                del self.subscribers[subscriber_id]
    
    def emit(
        self,
        event_type: EventType,
        payload: Dict[str, Any],
        severity: EventSeverity = EventSeverity.INFO,
        source: str = "system",
        correlation_id: Optional[str] = None
    ) -> Event:
        """Emit a new event"""
        start_time = time.time()
        
        try:
            event = Event(event_type, payload, severity, source, correlation_id)
            
            with self._lock:
                # Store event
                self.event_store.append(event)
                self.recent_events.append(event)
                self.event_counts[event_type] += 1
                self.metrics["events_processed"] += 1
                
                # Process handlers
                for handler in self.event_handlers[event_type]:
                    try:
                        handler(event)
                    except Exception as e:
                        self.metrics["processing_errors"] += 1
                        print(f"Event handler error: {e}")
                
                # Notify subscribers
                for callback in self.subscribers.values():
                    try:
                        callback(event)
                    except Exception as e:
                        self.metrics["processing_errors"] += 1
                        print(f"Subscriber error: {e}")
                
                # Check patterns
                self._check_patterns()
            
            # Record processing time
            processing_time = (time.time() - start_time) * 1000
            self.processing_times.append(processing_time)
            
            event.processed = True
            return event
            
        except Exception as e:
            self.metrics["processing_errors"] += 1
            raise
    
    def _check_patterns(self):
        """Check for event patterns"""
        current_time = datetime.now()
        
        for pattern in self.event_patterns:
            # Get events in time window
            window_start = current_time - timedelta(seconds=pattern.window_seconds)
            window_events = [
                e for e in self.recent_events 
                if e.timestamp >= window_start
            ]
            
            if len(window_events) >= pattern.min_events:
                try:
                    if pattern.pattern_fn(window_events):
                        pattern.matches += 1
                        pattern.last_match = current_time
                        self.metrics["pattern_matches"] += 1
                        
                        # Emit pattern match event
                        self.emit(
                            EventType.SYSTEM_ALERT,
                            {
                                "pattern": pattern.name,
                                "matches": pattern.matches,
                                "events_in_window": len(window_events)
                            },
                            EventSeverity.WARNING,
                            "event_processor"
                        )
                except Exception as e:
                    print(f"Pattern check error for {pattern.name}: {e}")
    
    def get_events(
        self,
        event_type: Optional[EventType] = None,
        severity: Optional[EventSeverity] = None,
        source: Optional[str] = None,
        since: Optional[datetime] = None,
        limit: int = 100
    ) -> List[Dict[str, Any]]:
        """Query events with filters"""
        with self._lock:
            events = list(self.recent_events)
            
            # Apply filters
            if event_type:
                events = [e for e in events if e.event_type == event_type]
            if severity:
                events = [e for e in events if e.severity == severity]
            if source:
                events = [e for e in events if e.source == source]
            if since:
                events = [e for e in events if e.timestamp >= since]
            
            # Sort by timestamp and limit
            events = sorted(events, key=lambda e: e.timestamp, reverse=True)[:limit]
            return [e.to_dict() for e in events]
    
    def get_event_stream(self, filters: Optional[Dict] = None) -> List[Dict[str, Any]]:
        """Get real-time event stream"""
        return self.get_events(limit=50)
    
    def replay_events(
        self,
        from_time: datetime,
        to_time: datetime,
        event_types: Optional[List[EventType]] = None
    ) -> List[Dict[str, Any]]:
        """Replay events from a time range"""
        with self._lock:
            events = [
                e for e in self.event_store
                if from_time <= e.timestamp <= to_time
            ]
            
            if event_types:
                events = [e for e in events if e.event_type in event_types]
            
            return [e.to_dict() for e in sorted(events, key=lambda e: e.timestamp)]
    
    def get_pattern_stats(self) -> List[Dict[str, Any]]:
        """Get pattern matching statistics"""
        with self._lock:
            return [
                {
                    "name": p.name,
                    "matches": p.matches,
                    "last_match": p.last_match.isoformat() if p.last_match else None,
                    "window_seconds": p.window_seconds,
                    "min_events": p.min_events
                }
                for p in self.event_patterns
            ]
    
    def get_stats(self) -> Dict[str, Any]:
        """Get processing statistics"""
        with self._lock:
            # Calculate events per second
            recent_events_count = len([
                e for e in self.recent_events
                if e.timestamp > datetime.now() - timedelta(minutes=1)
            ])
            events_per_second = recent_events_count / 60.0
            
            # Calculate average processing time
            avg_processing_time = (
                sum(self.processing_times) / len(self.processing_times)
                if self.processing_times else 0
            )
            
            return {
                **self.metrics,
                "events_per_second": round(events_per_second, 2),
                "avg_processing_time_ms": round(avg_processing_time, 2),
                "event_types": len(self.event_counts),
                "active_patterns": len(self.event_patterns),
                "subscribers": len(self.subscribers),
                "event_distribution": dict(self.event_counts)
            }
    
    def get_analytics(self) -> Dict[str, Any]:
        """Get comprehensive analytics"""
        stats = self.get_stats()
        patterns = self.get_pattern_stats()
        
        return {
            "status": "active",
            "events_processed": stats["events_processed"],
            "events_per_second": stats["events_per_second"],
            "pattern_matches": stats["pattern_matches"],
            "processing_errors": stats["processing_errors"],
            "features": [
                "complex_event_processing",
                "event_sourcing", 
                "stream_analytics",
                "pattern_detection",
                "real_time_processing",
                "event_replay",
                "subscriber_notifications"
            ],
            "patterns": patterns,
            "statistics": stats
        }


# Global instance
_event_processor: Optional[EventProcessor] = None
_processor_lock = threading.Lock()


def get_event_processor() -> EventProcessor:
    """Get or create event processor instance"""
    global _event_processor
    with _processor_lock:
        if _event_processor is None:
            _event_processor = EventProcessor()
        return _event_processor