File size: 6,421 Bytes
f83b968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any, Optional
import json
import time
from datetime import datetime
from utils.log_manager import LogManager

class AnalyticsLogger:
    """Handles logging of analytics events and metrics"""
    
    def __init__(self):
        self.log_manager = LogManager()
        self.logger = self.log_manager.get_analytics_logger("events")
        self.metrics_logger = self.log_manager.get_analytics_logger("metrics")
        
    def log_user_interaction(self, 
                           user_id: str,
                           interaction_type: str,
                           agent_type: str,
                           duration: float,
                           success: bool,
                           details: Optional[Dict] = None):
        """Log user interaction events"""
        event = {
            "event_type": "user_interaction",
            "user_id": user_id,
            "interaction_type": interaction_type,
            "agent_type": agent_type,
            "duration": duration,
            "success": success,
            "timestamp": datetime.now().isoformat(),
            "details": details or {}
        }
        
        self.logger.info(f"User Interaction: {json.dumps(event, indent=2)}")
        
    def log_agent_performance(self,
                            agent_type: str,
                            operation: str,
                            response_time: float,
                            success: bool,
                            error: Optional[str] = None):
        """Log agent performance metrics"""
        metric = {
            "metric_type": "agent_performance",
            "agent_type": agent_type,
            "operation": operation,
            "response_time": response_time,
            "success": success,
            "error": error,
            "timestamp": datetime.now().isoformat()
        }
        
        self.metrics_logger.info(f"Agent Performance: {json.dumps(metric, indent=2)}")
        
    def log_system_health(self,
                         cpu_usage: float,
                         memory_usage: float,
                         active_users: int,
                         active_sessions: int):
        """Log system health metrics"""
        metric = {
            "metric_type": "system_health",
            "cpu_usage": cpu_usage,
            "memory_usage": memory_usage,
            "active_users": active_users,
            "active_sessions": active_sessions,
            "timestamp": datetime.now().isoformat()
        }
        
        self.metrics_logger.info(f"System Health: {json.dumps(metric, indent=2)}")
        
    def log_error(self,
                  error_type: str,
                  error_message: str,
                  severity: str,
                  context: Optional[Dict] = None):
        """Log error events"""
        event = {
            "event_type": "error",
            "error_type": error_type,
            "error_message": error_message,
            "severity": severity,
            "context": context or {},
            "timestamp": datetime.now().isoformat()
        }
        
        self.logger.error(f"Error Event: {json.dumps(event, indent=2)}")
        
    def log_security_event(self,
                          event_type: str,
                          user_id: str,
                          success: bool,
                          details: Optional[Dict] = None):
        """Log security-related events"""
        event = {
            "event_type": "security",
            "security_event_type": event_type,
            "user_id": user_id,
            "success": success,
            "details": details or {},
            "timestamp": datetime.now().isoformat()
        }
        
        self.logger.info(f"Security Event: {json.dumps(event, indent=2)}")
        
    def log_model_performance(self,
                            model_name: str,
                            operation: str,
                            input_tokens: int,
                            output_tokens: int,
                            response_time: float,
                            success: bool):
        """Log AI model performance metrics"""
        metric = {
            "metric_type": "model_performance",
            "model_name": model_name,
            "operation": operation,
            "input_tokens": input_tokens,
            "output_tokens": output_tokens,
            "response_time": response_time,
            "success": success,
            "timestamp": datetime.now().isoformat()
        }
        
        self.metrics_logger.info(f"Model Performance: {json.dumps(metric, indent=2)}")
        
    def log_user_feedback(self,
                         user_id: str,
                         interaction_id: str,
                         rating: int,
                         feedback_text: Optional[str] = None):
        """Log user feedback"""
        event = {
            "event_type": "user_feedback",
            "user_id": user_id,
            "interaction_id": interaction_id,
            "rating": rating,
            "feedback_text": feedback_text,
            "timestamp": datetime.now().isoformat()
        }
        
        self.logger.info(f"User Feedback: {json.dumps(event, indent=2)}")
        
    def log_session_metrics(self,
                          session_id: str,
                          user_id: str,
                          session_type: str,
                          start_time: str,
                          end_time: str,
                          metrics: Dict[str, Any]):
        """Log session-specific metrics"""
        session_data = {
            "metric_type": "session_metrics",
            "session_id": session_id,
            "user_id": user_id,
            "session_type": session_type,
            "start_time": start_time,
            "end_time": end_time,
            "duration": self._calculate_duration(start_time, end_time),
            "metrics": metrics,
            "timestamp": datetime.now().isoformat()
        }
        
        self.metrics_logger.info(f"Session Metrics: {json.dumps(session_data, indent=2)}")
        
    def _calculate_duration(self, start_time: str, end_time: str) -> float:
        """Calculate duration between two ISO format timestamps"""
        start = datetime.fromisoformat(start_time)
        end = datetime.fromisoformat(end_time)
        return (end - start).total_seconds()