|
| 1 | +# Analytics & Monitoring |
| 2 | + |
| 3 | +Billy Bullshit now tracks usage and performance metrics using Cloudflare Analytics Engine. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The analytics system tracks three main categories of metrics: |
| 8 | + |
| 9 | +### 1. Usage Metrics |
| 10 | +- **API call volume** by endpoint (review, chat, roast, analyze, debate) |
| 11 | +- **Language distribution** in code reviews (JavaScript, Python, Go, etc.) |
| 12 | +- **BS score distribution** (1-10 rating of code quality) |
| 13 | +- **Code smell frequency** (CRITICAL, MAJOR, BS, WTAF categories) |
| 14 | + |
| 15 | +### 2. Performance Metrics |
| 16 | +- **Response times** per endpoint (milliseconds) |
| 17 | +- **AI model usage** (Workers AI, Anthropic Claude, OpenAI) |
| 18 | +- **Error rates** by error type |
| 19 | +- **Fallback usage** tracking when primary models fail |
| 20 | + |
| 21 | +### 3. Quality Metrics |
| 22 | +- **User feedback** (thumbs up/down via `/feedback` endpoint) |
| 23 | +- **Review success rate** (successful vs. failed reviews) |
| 24 | + |
| 25 | +## Architecture |
| 26 | + |
| 27 | +### AnalyticsService (`src/analytics.ts`) |
| 28 | + |
| 29 | +The `AnalyticsService` class handles all analytics tracking: |
| 30 | + |
| 31 | +```typescript |
| 32 | +const analytics = new AnalyticsService(env.ANALYTICS); |
| 33 | + |
| 34 | +// Track a code review |
| 35 | +await analytics.trackReview({ |
| 36 | + language: 'javascript', |
| 37 | + bsScore: 7, |
| 38 | + responseTime: 1234, |
| 39 | + aiModel: 'workers-ai', |
| 40 | + success: true, |
| 41 | + codeSmells: ['critical', 'major'] |
| 42 | +}); |
| 43 | + |
| 44 | +// Track user feedback |
| 45 | +await analytics.trackFeedback('/review', 'up', sessionId); |
| 46 | + |
| 47 | +// Track an error |
| 48 | +await analytics.trackError('/review', 'timeout', 1500); |
| 49 | +``` |
| 50 | + |
| 51 | +### Data Points |
| 52 | + |
| 53 | +Each analytics event includes: |
| 54 | +- **Blobs**: endpoint, language, AI model, error type (text data) |
| 55 | +- **Doubles**: BS score, response time (numeric data) |
| 56 | +- **Indexes**: endpoint (for efficient querying) |
| 57 | + |
| 58 | +## Endpoints |
| 59 | + |
| 60 | +### POST `/feedback` |
| 61 | + |
| 62 | +Submit user feedback on Billy's responses. |
| 63 | + |
| 64 | +**Request:** |
| 65 | +```json |
| 66 | +{ |
| 67 | + "endpoint": "/review", |
| 68 | + "feedback": "up", |
| 69 | + "sessionId": "billy_1234567890" |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +**Response:** |
| 74 | +```json |
| 75 | +{ |
| 76 | + "message": "Feedback recorded. Thanks for your honesty.", |
| 77 | + "feedback": "up", |
| 78 | + "billy_says": "👍 Glad you appreciate brutal honesty." |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### GET `/analytics` |
| 83 | + |
| 84 | +View information about tracked metrics. |
| 85 | + |
| 86 | +**Response:** |
| 87 | +```json |
| 88 | +{ |
| 89 | + "message": "Analytics are being tracked via Cloudflare Analytics Engine", |
| 90 | + "metrics_tracked": { |
| 91 | + "usage": [...], |
| 92 | + "performance": [...], |
| 93 | + "quality": [...] |
| 94 | + }, |
| 95 | + "query_instructions": { |
| 96 | + "note": "Use Cloudflare GraphQL API to query Analytics Engine dataset", |
| 97 | + "dataset": "ANALYTICS binding", |
| 98 | + "documentation": "https://developers.cloudflare.com/analytics/analytics-engine/" |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +## Querying Analytics Data |
| 104 | + |
| 105 | +Analytics data is stored in Cloudflare Analytics Engine and can be queried using the GraphQL API. |
| 106 | + |
| 107 | +### Example GraphQL Query |
| 108 | + |
| 109 | +```graphql |
| 110 | +query { |
| 111 | + viewer { |
| 112 | + accounts(filter: {accountTag: "YOUR_ACCOUNT_ID"}) { |
| 113 | + analyticsEngineDatasets(filter: {datasetTag: "ANALYTICS"}) { |
| 114 | + data( |
| 115 | + filter: { |
| 116 | + timestamp_geq: "2024-01-01T00:00:00Z" |
| 117 | + timestamp_lt: "2024-12-31T23:59:59Z" |
| 118 | + } |
| 119 | + orderBy: [timestamp_DESC] |
| 120 | + limit: 1000 |
| 121 | + ) { |
| 122 | + blob1 # endpoint |
| 123 | + blob2 # language |
| 124 | + blob3 # AI model |
| 125 | + blob4 # error type |
| 126 | + double1 # BS score |
| 127 | + double2 # response time |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +### Using wrangler |
| 136 | + |
| 137 | +You can also query analytics using the Wrangler CLI: |
| 138 | + |
| 139 | +```bash |
| 140 | +# Query recent data |
| 141 | +wrangler analytics-engine query ANALYTICS \ |
| 142 | + --start 2024-01-01T00:00:00Z \ |
| 143 | + --end 2024-12-31T23:59:59Z |
| 144 | + |
| 145 | +# Export to CSV |
| 146 | +wrangler analytics-engine query ANALYTICS --format csv > analytics.csv |
| 147 | +``` |
| 148 | + |
| 149 | +## Configuration |
| 150 | + |
| 151 | +### wrangler.toml |
| 152 | + |
| 153 | +The Analytics Engine binding is configured in `wrangler.toml`: |
| 154 | + |
| 155 | +```toml |
| 156 | +[[analytics_engine_datasets]] |
| 157 | +binding = "ANALYTICS" |
| 158 | +``` |
| 159 | + |
| 160 | +### Environment Variables |
| 161 | + |
| 162 | +No additional environment variables are required. The analytics service gracefully handles missing bindings. |
| 163 | + |
| 164 | +## Deployment |
| 165 | + |
| 166 | +### Prerequisites |
| 167 | + |
| 168 | +1. Ensure your Cloudflare account has Analytics Engine enabled |
| 169 | +2. The ANALYTICS binding is automatically created during deployment |
| 170 | + |
| 171 | +### Deploy |
| 172 | + |
| 173 | +```bash |
| 174 | +# Deploy to production |
| 175 | +npm run deploy:production |
| 176 | +``` |
| 177 | + |
| 178 | +## Data Privacy |
| 179 | + |
| 180 | +- No sensitive data (API keys, secrets, user credentials) is logged |
| 181 | +- Session IDs are optional and only tracked if provided |
| 182 | +- All data is retained according to Cloudflare's Analytics Engine retention policy (typically 31 days) |
| 183 | + |
| 184 | +## Monitoring Dashboard |
| 185 | + |
| 186 | +While Billy provides basic analytics info via the `/analytics` endpoint, you can build custom dashboards using: |
| 187 | + |
| 188 | +1. **Cloudflare Dashboard**: View basic metrics in the Workers Analytics section |
| 189 | +2. **GraphQL API**: Build custom queries for detailed analysis |
| 190 | +3. **External Tools**: Export data to tools like Grafana, Datadog, or custom dashboards |
| 191 | + |
| 192 | +## Metrics Examples |
| 193 | + |
| 194 | +### Average Response Time by Endpoint |
| 195 | + |
| 196 | +Query the `double2` (response time) field grouped by `blob1` (endpoint). |
| 197 | + |
| 198 | +### BS Score Distribution |
| 199 | + |
| 200 | +Query `double1` (BS score) to see how much BS Billy is finding in code: |
| 201 | +- 1-2: Clean code (rare!) |
| 202 | +- 3-4: Minor issues |
| 203 | +- 5-6: Moderate BS |
| 204 | +- 7-8: Significant problems |
| 205 | +- 9-10: Complete disaster |
| 206 | + |
| 207 | +### Most Common Languages Reviewed |
| 208 | + |
| 209 | +Query `blob2` (language) to see which programming languages are being reviewed most often. |
| 210 | + |
| 211 | +### Error Rate Analysis |
| 212 | + |
| 213 | +Query `blob4` (error type) where not 'none' to track error patterns and troubleshoot issues. |
| 214 | + |
| 215 | +## Future Enhancements |
| 216 | + |
| 217 | +Potential improvements to the analytics system: |
| 218 | + |
| 219 | +- Real-time dashboard UI |
| 220 | +- Automated alerts for high error rates |
| 221 | +- Trend analysis and anomaly detection |
| 222 | +- Code smell tracking over time |
| 223 | +- A/B testing different AI models |
| 224 | +- User journey tracking across sessions |
| 225 | + |
| 226 | +--- |
| 227 | + |
| 228 | +**Billy says:** "📊 Every bit of your BS is being tracked. Can't hide from the data." |
0 commit comments