findEthics commited on
Commit
9ebdf42
Β·
1 Parent(s): e3a1eb8

Add analytics documentation and implementation plan

Browse files

- Add analytics-approach.md: technical approach and database design
- Add analytics-tasks.md: phased implementation roadmap with tasks
- Planning for MongoDB-based session and message analytics
- 4-phase implementation plan for learning project

Files changed (2) hide show
  1. analytics-approach.md +130 -0
  2. analytics-tasks.md +182 -0
analytics-approach.md ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Analytics Approach for Atlas Chat App
2
+
3
+ ## Overview
4
+ This document outlines a simple analytics implementation for the Atlas chat application, designed for learning purposes while maintaining core functionality and insights.
5
+
6
+ ## Database Choice
7
+
8
+ ### Primary Option: MongoDB
9
+ - **Rationale**: Document-based storage perfect for analytics data
10
+ - **Setup**: MongoDB Atlas free tier (512MB storage, perfect for learning)
11
+ - **Driver**: `motor` for async Python operations
12
+ - **Benefits**:
13
+ - Flexible schema for evolving analytics needs
14
+ - Built-in aggregation pipeline for queries
15
+ - JSON-like documents match Python dictionaries
16
+
17
+ ### Alternative Option: JSON Files
18
+ - **Use Case**: Ultra-simple setup without external dependencies
19
+ - **Storage**: Local JSON files with rotation
20
+ - **Benefits**: No database setup required, easy to inspect data
21
+ - **Limitations**: Not suitable for production, limited query capabilities
22
+
23
+ ## Data Schema Design
24
+
25
+ ### 1. Chat Sessions
26
+ ```json
27
+ {
28
+ "_id": "session_uuid",
29
+ "start_time": "2024-01-01T10:00:00Z",
30
+ "end_time": "2024-01-01T10:15:00Z",
31
+ "message_count": 8,
32
+ "search_used": true,
33
+ "user_agent": "local_app.py/1.0"
34
+ }
35
+ ```
36
+
37
+ ### 2. Chat Messages
38
+ ```json
39
+ {
40
+ "_id": "message_uuid",
41
+ "session_id": "session_uuid",
42
+ "timestamp": "2024-01-01T10:05:00Z",
43
+ "prompt_length": 45,
44
+ "response_length": 320,
45
+ "used_search": false,
46
+ "response_time_ms": 2500,
47
+ "max_tokens": 500,
48
+ "temperature": 0.7,
49
+ "success": true
50
+ }
51
+ ```
52
+
53
+ ### 3. Search Analytics (Optional)
54
+ ```json
55
+ {
56
+ "_id": "search_uuid",
57
+ "message_id": "message_uuid",
58
+ "timestamp": "2024-01-01T10:05:00Z",
59
+ "search_query": "python machine learning",
60
+ "results_count": 7,
61
+ "search_time_ms": 1200,
62
+ "engines_used": ["brave", "duckduckgo"]
63
+ }
64
+ ```
65
+
66
+ ## Privacy Considerations
67
+
68
+ ### For Learning Project
69
+ - Store minimal user data
70
+ - No IP address logging
71
+ - No personal information storage
72
+ - Focus on usage patterns, not user identity
73
+
74
+ ### Data Retention
75
+ - Keep data for 30 days maximum
76
+ - Automatic cleanup of old records
77
+ - Optional: Allow users to opt-out of analytics
78
+
79
+ ## Implementation Architecture
80
+
81
+ ### 1. Analytics Module Structure
82
+ ```
83
+ analytics/
84
+ β”œβ”€β”€ __init__.py
85
+ β”œβ”€β”€ database.py # Database connection and operations
86
+ β”œβ”€β”€ collectors.py # Data collection functions
87
+ β”œβ”€β”€ models.py # Data models/schemas
88
+ └── dashboard.py # Analytics endpoints
89
+ ```
90
+
91
+ ### 2. Integration Points
92
+ - **Middleware**: Automatic session and message tracking
93
+ - **Decorators**: Performance timing
94
+ - **Endpoints**: Manual analytics triggers
95
+
96
+ ### 3. Analytics Endpoints
97
+ - `GET /analytics/stats` - Basic usage statistics
98
+ - `GET /analytics/dashboard` - Simple HTML dashboard
99
+ - `GET /analytics/export` - Data export for analysis
100
+
101
+ ## Key Metrics to Track
102
+
103
+ ### Usage Metrics
104
+ - Messages per day/week
105
+ - Average session length
106
+ - Search usage percentage
107
+ - Peak usage hours
108
+
109
+ ### Performance Metrics
110
+ - Average response time
111
+ - Search performance
112
+ - Error rates
113
+ - System availability
114
+
115
+ ### Content Metrics
116
+ - Popular query types
117
+ - Message length distributions
118
+ - Search vs. direct query ratios
119
+
120
+ ## Learning Objectives
121
+
122
+ This implementation teaches:
123
+ 1. **Database Integration**: Async NoSQL operations
124
+ 2. **Data Modeling**: Schema design for analytics
125
+ 3. **Performance Monitoring**: Timing and metrics collection
126
+ 4. **Web Analytics**: Basic dashboard creation
127
+ 5. **Privacy**: Responsible data collection practices
128
+
129
+ ## Next Steps
130
+ See `analytics-tasks.md` for the phased implementation plan.
analytics-tasks.md ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Analytics Implementation Tasks
2
+
3
+ ## Project Overview
4
+ Implement basic analytics and user session tracking for the Atlas chat application in four phases.
5
+
6
+ ---
7
+
8
+ ## Phase 1: Foundation Setup
9
+ **Goal**: Set up database connection and basic infrastructure
10
+
11
+ ### Tasks
12
+ - [ ] **1.1** Add MongoDB dependencies to requirements.txt
13
+ - Add `motor` (async MongoDB driver)
14
+ - Add `python-dotenv` (already exists)
15
+
16
+ - [ ] **1.2** Set up MongoDB Atlas free account
17
+ - Create cluster and database
18
+ - Get connection string
19
+ - Add to `.env` file
20
+
21
+ - [ ] **1.3** Create analytics module structure
22
+ - Create `analytics/` directory
23
+ - Create `analytics/__init__.py`
24
+ - Create `analytics/database.py` with connection logic
25
+
26
+ - [ ] **1.4** Test database connection
27
+ - Simple connection test function
28
+ - Add to startup event in app.py
29
+
30
+ **Deliverables**: Working MongoDB connection, basic module structure
31
+
32
+ ---
33
+
34
+ ## Phase 2: Data Collection
35
+ **Goal**: Implement session tracking and message analytics
36
+
37
+ ### Tasks
38
+ - [ ] **2.1** Create data models
39
+ - Create `analytics/models.py`
40
+ - Define Session and Message data classes
41
+ - Add validation and helper methods
42
+
43
+ - [ ] **2.2** Implement session management
44
+ - Create `analytics/collectors.py`
45
+ - Session creation and tracking functions
46
+ - Session ID generation and management
47
+
48
+ - [ ] **2.3** Add message tracking
49
+ - Message analytics collection function
50
+ - Performance timing decorators
51
+ - Integration with chat endpoint
52
+
53
+ - [ ] **2.4** Update app.py for analytics
54
+ - Import analytics functions
55
+ - Add analytics calls to chat endpoint
56
+ - Add session middleware
57
+
58
+ **Deliverables**: Automatic data collection on all chat interactions
59
+
60
+ ---
61
+
62
+ ## Phase 3: Analytics Endpoints
63
+ **Goal**: Create endpoints to view collected analytics data
64
+
65
+ ### Tasks
66
+ - [ ] **3.1** Create analytics dashboard module
67
+ - Create `analytics/dashboard.py`
68
+ - Basic statistics calculation functions
69
+ - Data aggregation utilities
70
+
71
+ - [ ] **3.2** Add basic stats endpoint
72
+ - `GET /analytics/stats` endpoint
73
+ - Return JSON with key metrics:
74
+ - Total messages today/week
75
+ - Average response time
76
+ - Search usage percentage
77
+ - Active sessions
78
+
79
+ - [ ] **3.3** Add simple HTML dashboard
80
+ - `GET /analytics/dashboard` endpoint
81
+ - Basic HTML template with charts
82
+ - Real-time statistics display
83
+
84
+ - [ ] **3.4** Add data export endpoint
85
+ - `GET /analytics/export` endpoint
86
+ - CSV/JSON export functionality
87
+ - Date range filtering
88
+
89
+ **Deliverables**: Functional analytics dashboard and API endpoints
90
+
91
+ ---
92
+
93
+ ## Phase 4: Enhancement & Polish
94
+ **Goal**: Add advanced features and polish the implementation
95
+
96
+ ### Tasks
97
+ - [ ] **4.1** Add search analytics
98
+ - Track search-specific metrics
99
+ - Search performance analysis
100
+ - Popular search terms (anonymized)
101
+
102
+ - [ ] **4.2** Implement data retention
103
+ - Automatic cleanup of old data
104
+ - Configurable retention periods
105
+ - Database optimization
106
+
107
+ - [ ] **4.3** Add error tracking
108
+ - Error analytics collection
109
+ - Error rate monitoring
110
+ - System health metrics
111
+
112
+ - [ ] **4.4** Performance optimization
113
+ - Database indexing
114
+ - Async optimization
115
+ - Memory usage monitoring
116
+
117
+ - [ ] **4.5** Documentation and testing
118
+ - Update README with analytics features
119
+ - Add basic tests for analytics functions
120
+ - API documentation updates
121
+
122
+ **Deliverables**: Production-ready analytics system with monitoring
123
+
124
+ ---
125
+
126
+ ## Optional Enhancements
127
+ *These can be added after core implementation*
128
+
129
+ ### Advanced Features
130
+ - [ ] **Real-time WebSocket dashboard**
131
+ - [ ] **Email/Slack alerts for errors**
132
+ - [ ] **Advanced data visualization**
133
+ - [ ] **User behavior analysis**
134
+ - [ ] **A/B testing framework**
135
+
136
+ ### Integration Features
137
+ - [ ] **Grafana dashboard integration**
138
+ - [ ] **Prometheus metrics export**
139
+ - [ ] **Log aggregation (ELK stack)**
140
+ - [ ] **API rate limiting based on usage**
141
+
142
+ ---
143
+
144
+ ## Success Criteria
145
+
146
+ ### Phase 1 Success
147
+ - βœ… MongoDB connection established
148
+ - βœ… Basic module structure created
149
+ - βœ… Connection test passing
150
+
151
+ ### Phase 2 Success
152
+ - βœ… All chat messages tracked in database
153
+ - βœ… Session management working
154
+ - βœ… Performance metrics collected
155
+
156
+ ### Phase 3 Success
157
+ - βœ… Analytics dashboard accessible
158
+ - βœ… Key metrics displayed correctly
159
+ - βœ… Data export functionality working
160
+
161
+ ### Phase 4 Success
162
+ - βœ… Search analytics implemented
163
+ - βœ… Data retention policies active
164
+ - βœ… System monitoring in place
165
+
166
+ ---
167
+
168
+ ## Timeline Estimate
169
+ - **Phase 1**: 2-3 hours
170
+ - **Phase 2**: 4-5 hours
171
+ - **Phase 3**: 3-4 hours
172
+ - **Phase 4**: 3-4 hours
173
+
174
+ **Total**: ~12-16 hours for complete implementation
175
+
176
+ ---
177
+
178
+ ## Dependencies
179
+ - MongoDB Atlas account (free tier)
180
+ - Python packages: `motor`, `python-dotenv`
181
+ - Basic HTML/CSS knowledge for dashboard
182
+ - Understanding of async Python programming